<commit_before>#[feature(globs,phase)];
#[phase(syntax, link)] extern crate log;

extern crate leveldb;
extern crate serialize;

pub mod utils {
  use leveldb::database::Database;
  use leveldb::options::{Options};

  pub fn open_database(name: ~str, create_if_missing: bool) -> Database {
    let mut opts = Options::new();
    opts.create_if_missing(create_if_missing);
    match Database::open(name, opts) {
      Ok(db) => { db },
      Err(_) => { fail!("failed to open database") }
    }
  }
}

#[cfg(test)]
mod binary_tests {
  use super::utils::{open_database};
  use leveldb::database::Database;
  use leveldb::database::binary::Interface;
  use leveldb::iterator::Iterable;
  use leveldb::options::{Options,ReadOptions,WriteOptions};

  fn db_put_simple(database: &mut Database, key: &[u8], val: &[u8]) {
    let write_opts = WriteOptions::new();
    match database.put(write_opts, key, val) {
      Ok(_) => { () },
      Err(_) => { fail!("failed to write to database") }
    }
  }

  #[test]
  fn test_create_options() {
    Options::new();
  }

  #[test]
  fn test_open_database() {
    let mut opts = Options::new();
    opts.create_if_missing(true);
    let res = Database::open(~"testdbs/create_if_missing", opts);
    assert!(res.is_ok());
  }

  #[test]
  fn test_open_non_existant_database_without_create() {
    let mut opts = Options::new();
    opts.create_if_missing(false);
    let res = Database::open(~"testdbs/missing", opts);
    assert!(res.is_err());
  }

  #[test]
  fn test_write_to_database() {
    let mut database = open_database(~"testdbs/put_simple", true);
    let write_opts = WriteOptions::new();
    let result = database.put(write_opts,
                              &[1],
                              &[1]);
    assert!(result.is_ok());
  }

  #[test]
  fn test_delete_from_database() {
    let mut database = open_database(~"testdbs/delete_simple", true);
    db_put_simple(&mut database, &[1], &[1]);

    let write2 = WriteOptions::new();
    let res2 = database.delete(write2,
                               &[1]);
    assert!(res2.is_ok());
  }

  #[test]
  fn test_get_from_empty_database() {
    let mut database = open_database(~"testdbs/get_simple", true);
    let read_opts = ReadOptions::new();
    let res = database.get(read_opts, [1,2,3]);
    match res {
      Ok(data) => { assert!(data.is_none()) },
      Err(_) => { fail!("failed reading data") }
    }
  }

  #[test]
  fn test_get_from_filled_database() {
    let mut database = open_database(~"testdbs/get_filled", true);
    db_put_simple(&mut database, &[1], &[1]);

    let read_opts = ReadOptions::new();
    let res = database.get(read_opts,
                            &[1]);
    match res {
      Ok(data) => {
        assert!(data.is_some())
        let data = data.unwrap();
        assert_eq!(data, ~[1]);
      },
      Err(_) => { fail!("failed reading data") }
    }
  }

  #[test]
  fn test_iterator() {
    let mut database = open_database(~"testdbs/iter", true);
    db_put_simple(&mut database, &[1], &[1]);
    db_put_simple(&mut database, &[2], &[2]);

    let read_opts = ReadOptions::new();
    let mut iter = database.iter(read_opts);
    assert!(iter.valid());
    assert!(iter.next().is_some());
    assert!(iter.next().is_some());
    assert!(iter.next().is_none());
  }
}

#[cfg(test)]
mod json_tests {
  use super::utils::{open_database};
  use leveldb::database::json::Interface;
  use leveldb::options::{ReadOptions,WriteOptions};
  use serialize::{Encodable,Decodable};
  use serialize::json;

  #[deriving(Encodable,Decodable)]
  struct ToEncode {
    test: ~str,
  }

  #[test]
  fn test_write_to_database() {
    let mut database = open_database(~"testdbs/json_put", true);
    let write_opts = WriteOptions::new();
    let key = ToEncode { test: ~"string" };
    let val = ToEncode { test: ~"string2" };
    let result = database.put(write_opts,
                              &key,
                              &val);
    assert!(result.is_ok());
  }

  #[test]
  fn test_read_from_database() {
    let mut database = open_database(~"testdbs/json_read", true);
    let write_opts = WriteOptions::new();
    let key = ToEncode { test: ~"string" };
    let val = ToEncode { test: ~"string2" };
    let result = database.put(write_opts,
                              &key,
                              &val);
    assert!(result.is_ok());
    let read_opts = ReadOptions::new();
    let read = database.get(read_opts,
                            &key);
    match read {
      Ok(data) => {
        assert!(data.is_some())
        let data = data.unwrap();
        assert!(data.is_object());
        let mut decoder = json::Decoder::new(data);
        let decoded_object: ToEncode = Decodable::decode(&mut decoder);
        assert_eq!(decoded_object.test, ~"string2" );
      },
      Err(_) => { fail!("failed reading data") }
    }
  }

}<commit_msg>Fix tests for newest rust<commit_after>#![feature(globs,phase)]
#![phase(syntax, link)] extern crate log;

extern crate leveldb;
extern crate serialize;

pub mod utils {
  use leveldb::database::Database;
  use leveldb::options::{Options};

  pub fn open_database(name: ~str, create_if_missing: bool) -> Database {
    let mut opts = Options::new();
    opts.create_if_missing(create_if_missing);
    match Database::open(name, opts) {
      Ok(db) => { db },
      Err(_) => { fail!("failed to open database") }
    }
  }
}

#[cfg(test)]
mod binary_tests {
  use super::utils::{open_database};
  use leveldb::database::Database;
  use leveldb::database::binary::Interface;
  use leveldb::iterator::Iterable;
  use leveldb::options::{Options,ReadOptions,WriteOptions};

  fn db_put_simple(database: &mut Database, key: &[u8], val: &[u8]) {
    let write_opts = WriteOptions::new();
    match database.put(write_opts, key, val) {
      Ok(_) => { () },
      Err(_) => { fail!("failed to write to database") }
    }
  }

  #[test]
  fn test_create_options() {
    Options::new();
  }

  #[test]
  fn test_open_database() {
    let mut opts = Options::new();
    opts.create_if_missing(true);
    let res = Database::open(~"testdbs/create_if_missing", opts);
    assert!(res.is_ok());
  }

  #[test]
  fn test_open_non_existant_database_without_create() {
    let mut opts = Options::new();
    opts.create_if_missing(false);
    let res = Database::open(~"testdbs/missing", opts);
    assert!(res.is_err());
  }

  #[test]
  fn test_write_to_database() {
    let mut database = open_database(~"testdbs/put_simple", true);
    let write_opts = WriteOptions::new();
    let result = database.put(write_opts,
                              &[1],
                              &[1]);
    assert!(result.is_ok());
  }

  #[test]
  fn test_delete_from_database() {
    let mut database = open_database(~"testdbs/delete_simple", true);
    db_put_simple(&mut database, &[1], &[1]);

    let write2 = WriteOptions::new();
    let res2 = database.delete(write2,
                               &[1]);
    assert!(res2.is_ok());
  }

  #[test]
  fn test_get_from_empty_database() {
    let mut database = open_database(~"testdbs/get_simple", true);
    let read_opts = ReadOptions::new();
    let res = database.get(read_opts, [1,2,3]);
    match res {
      Ok(data) => { assert!(data.is_none()) },
      Err(_) => { fail!("failed reading data") }
    }
  }

  #[test]
  fn test_get_from_filled_database() {
    let mut database = open_database(~"testdbs/get_filled", true);
    db_put_simple(&mut database, &[1], &[1]);

    let read_opts = ReadOptions::new();
    let res = database.get(read_opts,
                            &[1]);
    match res {
      Ok(data) => {
        assert!(data.is_some())
        let data = data.unwrap();
        assert_eq!(data, ~[1]);
      },
      Err(_) => { fail!("failed reading data") }
    }
  }

  #[test]
  fn test_iterator() {
    let mut database = open_database(~"testdbs/iter", true);
    db_put_simple(&mut database, &[1], &[1]);
    db_put_simple(&mut database, &[2], &[2]);

    let read_opts = ReadOptions::new();
    let mut iter = database.iter(read_opts);
    assert!(iter.valid());
    assert!(iter.next().is_some());
    assert!(iter.next().is_some());
    assert!(iter.next().is_none());
  }
}

#[cfg(test)]
mod json_tests {
  use super::utils::{open_database};
  use leveldb::database::json::Interface;
  use leveldb::options::{ReadOptions,WriteOptions};
  use serialize::{Encodable,Decodable};
  use serialize::json;

  #[deriving(Encodable,Decodable)]
  struct ToEncode {
    test: ~str,
  }

  #[test]
  fn test_write_to_database() {
    let mut database = open_database(~"testdbs/json_put", true);
    let write_opts = WriteOptions::new();
    let key = ToEncode { test: ~"string" };
    let val = ToEncode { test: ~"string2" };
    let result = database.put(write_opts,
                              &key,
                              &val);
    assert!(result.is_ok());
  }

  #[test]
  fn test_read_from_database() {
    let mut database = open_database(~"testdbs/json_read", true);
    let write_opts = WriteOptions::new();
    let key = ToEncode { test: ~"string" };
    let val = ToEncode { test: ~"string2" };
    let result = database.put(write_opts,
                              &key,
                              &val);
    assert!(result.is_ok());
    let read_opts = ReadOptions::new();
    let read = database.get(read_opts,
                            &key);
    match read {
      Ok(data) => {
        assert!(data.is_some())
        let data = data.unwrap();
        assert!(data.is_object());
        let mut decoder = json::Decoder::new(data);
        let decoded_object: Result<ToEncode,json::Error> = Decodable::decode(&mut decoder);
        assert!(decoded_object.is_ok())
        assert_eq!(decoded_object.unwrap().test, ~"string2" );
      },
      Err(_) => { fail!("failed reading data") }
    }
  }

}
{'fraction_non_alphanumeric': 0.1638663967611336, 'fraction_numerical': 0.005060728744939271, 'mean_word_length': 2.8779434850863423, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4170746', 'n_tokens_mistral': 3280, 'n_tokens_neox': 3290, 'n_words': 659}