From e8e58fd04c6a1d3bcab71b488293e8ecd49a4dc2 Mon Sep 17 00:00:00 2001 From: Dan Burkert Date: Sat, 3 Mar 2018 10:33:48 -0800 Subject: [PATCH] Add test for Environment::stats --- src/environment.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/environment.rs b/src/environment.rs index 6321f38..db31f7a 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -330,7 +330,10 @@ impl EnvironmentBuilder { #[cfg(test)] mod test { + extern crate byteorder; + use tempdir::TempDir; + use self::byteorder::{ByteOrder, LittleEndian}; use flags::*; @@ -422,4 +425,39 @@ mod test { assert!(env.sync(true).is_err()); } } + + #[test] + fn test_stat() { + let dir = TempDir::new("test").unwrap(); + let env = Environment::new().open(dir.path()).unwrap(); + + // Stats should be empty initially. + let stat = env.stat().unwrap(); + assert_eq!(stat.page_size(), 4096); + assert_eq!(stat.depth(), 0); + assert_eq!(stat.branch_pages(), 0); + assert_eq!(stat.leaf_pages(), 0); + assert_eq!(stat.overflow_pages(), 0); + assert_eq!(stat.entries(), 0); + + let db = env.open_db(None).unwrap(); + + // Write a few small values. + for i in 0..64 { + let mut value = [0u8; 8]; + LittleEndian::write_u64(&mut value, i); + let mut tx = env.begin_rw_txn().expect("begin_rw_txn"); + tx.put(db, &value, &value, WriteFlags::default()).expect("tx.put"); + tx.commit().expect("tx.commit") + } + + // Stats should now reflect inserted values. + let stat = env.stat().unwrap(); + assert_eq!(stat.page_size(), 4096); + assert_eq!(stat.depth(), 1); + assert_eq!(stat.branch_pages(), 0); + assert_eq!(stat.leaf_pages(), 1); + assert_eq!(stat.overflow_pages(), 0); + assert_eq!(stat.entries(), 64); + } } diff --git a/src/lib.rs b/src/lib.rs index 18524b9..ef58f79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,7 +118,7 @@ mod test_utils { &HEIGHT_KEY, &value, WriteFlags::empty()).expect("tx.put"); - tx.commit().expect("ts.commit") + tx.commit().expect("tx.commit") } } }