diff --git a/test/test.rs b/test/test.rs new file mode 100644 index 0000000..d10d9ae --- /dev/null +++ b/test/test.rs @@ -0,0 +1,3 @@ +extern crate rocksdb; + +mod test_iterator; diff --git a/test/test_iterator.rs b/test/test_iterator.rs new file mode 100644 index 0000000..0985bfd --- /dev/null +++ b/test/test_iterator.rs @@ -0,0 +1,110 @@ +use rocksdb::{Options, RocksDB, Writable, Direction}; +use std; + + +#[test] +pub fn test_iterator() { + let path = "_rust_rocksdb_iteratortest"; + { + let k1:Box<[u8]> = b"k1".to_vec().into_boxed_slice(); + let k2:Box<[u8]> = b"k2".to_vec().into_boxed_slice(); + let k3:Box<[u8]> = b"k3".to_vec().into_boxed_slice(); + let k4:Box<[u8]> = b"k4".to_vec().into_boxed_slice(); + let v1:Box<[u8]> = b"v1111".to_vec().into_boxed_slice(); + let v2:Box<[u8]> = b"v2222".to_vec().into_boxed_slice(); + let v3:Box<[u8]> = b"v3333".to_vec().into_boxed_slice(); + let v4:Box<[u8]> = b"v4444".to_vec().into_boxed_slice(); + let mut db = RocksDB::open_default(path).unwrap(); + let p = db.put(&*k1, &*v1); + assert!(p.is_ok()); + let p = db.put(&*k2, &*v2); + assert!(p.is_ok()); + let p = db.put(&*k3, &*v3); + assert!(p.is_ok()); + let mut view1 = db.iterator(); + let expected = vec![(k1, v1), (k2, v2), (k3, v3)]; + { + let mut iterator1 = view1.from_start(); + assert_eq!(iterator1.collect::>(), expected); + } + // Test that it's reusable a few times + { + let mut iterator1 = view1.from_start(); + assert_eq!(iterator1.collect::>(), expected); + } + { + let mut iterator1 = view1.from_start(); + assert_eq!(iterator1.collect::>(), expected); + } + { + let mut iterator1 = view1.from_start(); + assert_eq!(iterator1.collect::>(), expected); + } + // Test it in reverse a few times + { + let mut iterator1 = view1.from_end(); + let mut tmp_vec = iterator1.collect::>(); + tmp_vec.reverse(); + assert_eq!(tmp_vec, expected); + } + { + let mut iterator1 = view1.from_end(); + let mut tmp_vec = iterator1.collect::>(); + tmp_vec.reverse(); + assert_eq!(tmp_vec, expected); + } + { + let mut iterator1 = view1.from_end(); + let mut tmp_vec = iterator1.collect::>(); + tmp_vec.reverse(); + assert_eq!(tmp_vec, expected); + } + { + let mut iterator1 = view1.from_end(); + let mut tmp_vec = iterator1.collect::>(); + tmp_vec.reverse(); + assert_eq!(tmp_vec, expected); + } + { + let mut iterator1 = view1.from_end(); + let mut tmp_vec = iterator1.collect::>(); + tmp_vec.reverse(); + assert_eq!(tmp_vec, expected); + } + // Try it forward again + { + let mut iterator1 = view1.from_start(); + assert_eq!(iterator1.collect::>(), expected); + } + { + let mut iterator1 = view1.from_start(); + assert_eq!(iterator1.collect::>(), expected); + } + + let mut view2 = db.iterator(); + let p = db.put(k4, v4); + assert!(p.is_ok()); + let mut view3 = db.iterator(); + let expected2 = vec![(k1, v1), (k2, v2), (k3, v3), (k4, v4)]; + { + let mut iterator1 = view1.from_start(); + assert_eq!(iterator1.collect::>(), expected2); + } +//TODO continue implementing tests! + println!("See the output of the third iter"); + for (k,v) in view3.from_start() { + //println!("Hello {}: {}", std::str::from_utf8(k).unwrap(), std::str::from_utf8(v).unwrap()); + } + println!("now the 3rd iter from k2 fwd"); + for (k,v) in view3.from(b"k2", Direction::forward) { + //println!("Hello {}: {}", std::str::from_utf8(k).unwrap(), std::str::from_utf8(v).unwrap()); + } + println!("now the 3rd iter from k2 and back"); + for (k,v) in view3.from(b"k2", Direction::reverse) { + //println!("Hello {}: {}", std::str::from_utf8(k).unwrap(), std::str::from_utf8(v).unwrap()); + } + } + let opts = Options::new(); + assert!(RocksDB::destroy(&opts, path).is_ok()); +} +