From ce8f8f40d7e7a7fa8a59518257ef48fde9b7c807 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 27 May 2017 11:33:18 +0800 Subject: [PATCH] move MemoryDB's tests into tests module --- util/src/memorydb.rs | 85 +++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/util/src/memorydb.rs b/util/src/memorydb.rs index 1feca9cba..ab62b2c57 100644 --- a/util/src/memorydb.rs +++ b/util/src/memorydb.rs @@ -250,46 +250,51 @@ impl HashDB for MemoryDB { } } -#[test] -fn memorydb_remove_and_purge() { - let hello_bytes = b"Hello world!"; - let hello_key = hello_bytes.sha3(); +#[cfg(test)] +mod tests { + use super::*; - let mut m = MemoryDB::new(); - m.remove(&hello_key); - assert_eq!(m.raw(&hello_key).unwrap().1, -1); - m.purge(); - assert_eq!(m.raw(&hello_key).unwrap().1, -1); - m.insert(hello_bytes); - assert_eq!(m.raw(&hello_key).unwrap().1, 0); - m.purge(); - assert_eq!(m.raw(&hello_key), None); + #[test] + fn memorydb_remove_and_purge() { + let hello_bytes = b"Hello world!"; + let hello_key = hello_bytes.sha3(); - let mut m = MemoryDB::new(); - assert!(m.remove_and_purge(&hello_key).is_none()); - assert_eq!(m.raw(&hello_key).unwrap().1, -1); - m.insert(hello_bytes); - m.insert(hello_bytes); - assert_eq!(m.raw(&hello_key).unwrap().1, 1); - assert_eq!(&*m.remove_and_purge(&hello_key).unwrap(), hello_bytes); - assert_eq!(m.raw(&hello_key), None); - assert!(m.remove_and_purge(&hello_key).is_none()); -} - -#[test] -fn consolidate() { - let mut main = MemoryDB::new(); - let mut other = MemoryDB::new(); - let remove_key = other.insert(b"doggo"); - main.remove(&remove_key); - - let insert_key = other.insert(b"arf"); - main.emplace(insert_key, DBValue::from_slice(b"arf")); - - main.consolidate(other); - - let overlay = main.drain(); - - assert_eq!(overlay.get(&remove_key).unwrap(), &(DBValue::from_slice(b"doggo"), 0)); - assert_eq!(overlay.get(&insert_key).unwrap(), &(DBValue::from_slice(b"arf"), 2)); + let mut m = MemoryDB::new(); + m.remove(&hello_key); + assert_eq!(m.raw(&hello_key).unwrap().1, -1); + m.purge(); + assert_eq!(m.raw(&hello_key).unwrap().1, -1); + m.insert(hello_bytes); + assert_eq!(m.raw(&hello_key).unwrap().1, 0); + m.purge(); + assert_eq!(m.raw(&hello_key), None); + + let mut m = MemoryDB::new(); + assert!(m.remove_and_purge(&hello_key).is_none()); + assert_eq!(m.raw(&hello_key).unwrap().1, -1); + m.insert(hello_bytes); + m.insert(hello_bytes); + assert_eq!(m.raw(&hello_key).unwrap().1, 1); + assert_eq!(&*m.remove_and_purge(&hello_key).unwrap(), hello_bytes); + assert_eq!(m.raw(&hello_key), None); + assert!(m.remove_and_purge(&hello_key).is_none()); + } + + #[test] + fn consolidate() { + let mut main = MemoryDB::new(); + let mut other = MemoryDB::new(); + let remove_key = other.insert(b"doggo"); + main.remove(&remove_key); + + let insert_key = other.insert(b"arf"); + main.emplace(insert_key, DBValue::from_slice(b"arf")); + + main.consolidate(other); + + let overlay = main.drain(); + + assert_eq!(overlay.get(&remove_key).unwrap(), &(DBValue::from_slice(b"doggo"), 0)); + assert_eq!(overlay.get(&insert_key).unwrap(), &(DBValue::from_slice(b"arf"), 2)); + } }