From 73d75af5c3fdc86ab550ef15b812c88f3c7848ab Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Sun, 5 Mar 2017 17:57:16 +0000 Subject: [PATCH] Added tests for DBRawIterator --- test/test.rs | 1 + test/test_raw_iterator.rs | 192 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 test/test_raw_iterator.rs diff --git a/test/test.rs b/test/test.rs index 295d4b0..c1a6f0e 100644 --- a/test/test.rs +++ b/test/test.rs @@ -16,6 +16,7 @@ extern crate rocksdb; mod test_iterator; +mod test_raw_iterator; mod test_multithreaded; mod test_column_family; mod test_rocksdb_options; diff --git a/test/test_raw_iterator.rs b/test/test_raw_iterator.rs new file mode 100644 index 0000000..9bb27bc --- /dev/null +++ b/test/test_raw_iterator.rs @@ -0,0 +1,192 @@ +// Copyright 2014 Tyler Neely +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +use rocksdb::DB; + + +fn setup_test_db(name: &str) -> DB { + use std::fs::remove_dir_all; + + let path = "_rust_rocksdb_rawiteratortest_".to_string() + name; + + match remove_dir_all(&path) { + Ok(_) => {} + Err(_) => {} // Don't care if tis fails + } + + DB::open_default(path).unwrap() +} + + +#[test] +pub fn test_forwards_iteration() { + let db = setup_test_db("forwards_iteration"); + db.put(b"k1", b"v1").unwrap(); + db.put(b"k2", b"v2").unwrap(); + db.put(b"k3", b"v3").unwrap(); + db.put(b"k4", b"v4").unwrap(); + + let mut iter = db.raw_iterator(); + iter.seek_to_first(); + + // Shouldn't be valid yet + assert_eq!(iter.valid(), false); + assert_eq!(iter.key(), None); + assert_eq!(iter.value(), None); + + let valid = iter.next(); + + assert_eq!(valid, true); + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k1".to_vec())); + assert_eq!(iter.value(), Some(b"v1".to_vec())); + + let valid = iter.next(); + + assert_eq!(valid, true); + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k2".to_vec())); + assert_eq!(iter.value(), Some(b"v2".to_vec())); + + iter.next(); // k3 + iter.next(); // k4 + + let valid = iter.next(); + + // Should be invalid again + assert_eq!(valid, false); + assert_eq!(iter.valid(), false); + assert_eq!(iter.key(), None); + assert_eq!(iter.value(), None); +} + + +#[test] +pub fn test_seek_last() { + let db = setup_test_db("backwards_iteration"); + db.put(b"k1", b"v1").unwrap(); + db.put(b"k2", b"v2").unwrap(); + db.put(b"k3", b"v3").unwrap(); + db.put(b"k4", b"v4").unwrap(); + + let mut iter = db.raw_iterator(); + iter.seek_to_last(); + + // Shouldn't be valid yet + assert_eq!(iter.valid(), false); + assert_eq!(iter.key(), None); + assert_eq!(iter.value(), None); + + let valid = iter.prev(); + + assert_eq!(valid, true); + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k4".to_vec())); + assert_eq!(iter.value(), Some(b"v4".to_vec())); + + let valid = iter.prev(); + + assert_eq!(valid, true); + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k3".to_vec())); + assert_eq!(iter.value(), Some(b"v3".to_vec())); + + iter.prev(); // k2 + iter.prev(); // k1 + + let valid = iter.prev(); + + // Should be invalid again + assert_eq!(valid, false); + assert_eq!(iter.valid(), false); + assert_eq!(iter.key(), None); + assert_eq!(iter.value(), None); +} + + +#[test] +pub fn test_seek() { + let db = setup_test_db("seek"); + db.put(b"k1", b"v1").unwrap(); + db.put(b"k2", b"v2").unwrap(); + db.put(b"k3", b"v3").unwrap(); + db.put(b"k4", b"v4").unwrap(); + + let mut iter = db.raw_iterator(); + iter.seek(b"k2"); + + // Shouldn't be valid yet + assert_eq!(iter.valid(), false); + assert_eq!(iter.key(), None); + assert_eq!(iter.value(), None); + + let valid = iter.next(); + + // Should now be valid + assert_eq!(valid, true); + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k2".to_vec())); + assert_eq!(iter.value(), Some(b"v2".to_vec())); +} + + +#[test] +pub fn test_seek_then_prev() { + let db = setup_test_db("seek_then_prev"); + db.put(b"k1", b"v1").unwrap(); + db.put(b"k2", b"v2").unwrap(); + db.put(b"k3", b"v3").unwrap(); + db.put(b"k4", b"v4").unwrap(); + + let mut iter = db.raw_iterator(); + iter.seek(b"k2"); + + // Shouldn't be valid yet + assert_eq!(iter.valid(), false); + assert_eq!(iter.key(), None); + assert_eq!(iter.value(), None); + + let valid = iter.prev(); + + // Should now be valid + assert_eq!(valid, true); + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k2".to_vec())); + assert_eq!(iter.value(), Some(b"v2".to_vec())); +} + + +#[test] +pub fn test_seek_to_nonexistant() { + let db = setup_test_db("seek_to_nonexistant"); + db.put(b"k1", b"v1").unwrap(); + db.put(b"k3", b"v3").unwrap(); + db.put(b"k4", b"v4").unwrap(); + + let mut iter = db.raw_iterator(); + iter.seek(b"k2"); + + // Shouldn't be valid yet + assert_eq!(iter.valid(), false); + assert_eq!(iter.key(), None); + assert_eq!(iter.value(), None); + + let valid = iter.next(); + + assert_eq!(valid, true); + assert_eq!(iter.valid(), true); + assert_eq!(iter.key(), Some(b"k3".to_vec())); + assert_eq!(iter.value(), Some(b"v3".to_vec())); +}