From de735c10ae78ac53cc10bfb0621e2a9e37c47403 Mon Sep 17 00:00:00 2001 From: al8n Date: Thu, 31 Oct 2024 17:39:47 +0800 Subject: [PATCH] more unit tests --- src/memtable.rs | 7 +- src/swmr/tests/get.rs | 5 +- src/swmr/tests/iters.rs | 91 +++++++++++++++++++++++- src/swmr/tests/multiple_version_get.rs | 16 ++--- src/swmr/tests/multiple_version_iters.rs | 18 +++-- 5 files changed, 116 insertions(+), 21 deletions(-) diff --git a/src/memtable.rs b/src/memtable.rs index 3f5da27..5aece1b 100644 --- a/src/memtable.rs +++ b/src/memtable.rs @@ -162,11 +162,8 @@ where for<'a> Self::Item<'a>: MultipleVersionMemtableEntry<'a>, { /// The item returned by the iterator or query methods. - type MultipleVersionItem<'a>: MultipleVersionMemtableEntry< - 'a, - Key = Self::Key, - Value = Self::Value, - > + type MultipleVersionItem<'a>: MultipleVersionMemtableEntry<'a, Key = Self::Key, Value = Self::Value> + + Clone where KeyPointer: 'a, Self: 'a; diff --git a/src/swmr/tests/get.rs b/src/swmr/tests/get.rs index ed2374c..ef412ae 100644 --- a/src/swmr/tests/get.rs +++ b/src/swmr/tests/get.rs @@ -140,7 +140,10 @@ where let v = format!("My name is {}", p.name); unsafe { wal - .insert(MaybeStructured::from_slice(pvec.as_ref()), &v) + .insert_with_key_builder( + KeyBuilder::once(p.encoded_len(), |buf| p.encode_to_buffer(buf)), + &v, + ) .unwrap(); } (p, v) diff --git a/src/swmr/tests/iters.rs b/src/swmr/tests/iters.rs index dd0756a..c33bd19 100644 --- a/src/swmr/tests/iters.rs +++ b/src/swmr/tests/iters.rs @@ -41,6 +41,30 @@ where assert_eq!(&pwal.1, pvec.value()); } + let mut iter = wal.keys(); + + for (pwal, pvec) in people.iter().zip(iter.by_ref()) { + assert!(pwal.0.equivalent(pvec.key())); + } + + let mut rev_iter = wal.keys().rev(); + + for (pwal, pvec) in people.iter().rev().zip(rev_iter.by_ref()) { + assert!(pwal.0.equivalent(pvec.key())); + } + + let mut iter = wal.values(); + + for (pwal, pvec) in people.iter().zip(iter.by_ref()) { + assert_eq!(&pwal.1, pvec.value()); + } + + let mut rev_iter = wal.values().rev(); + + for (pwal, pvec) in people.iter().rev().zip(rev_iter.by_ref()) { + assert_eq!(&pwal.1, pvec.value()); + } + let wal = wal.reader(); let mut iter = wal.iter(); @@ -219,6 +243,32 @@ where assert!(iter.next().is_none()); + let mut iter = wal.range_keys::(&mid..); + for (pwal, pvec) in people.range(&mid..).zip(iter.by_ref()) { + assert!(pwal.0.equivalent(pvec.clone().key())); + } + + assert!(iter.next().is_none()); + + let mut rev_iter = wal.range_keys::(&mid..).rev(); + + for (pwal, pvec) in people.range(&mid..).rev().zip(rev_iter.by_ref()) { + assert!(pwal.0.equivalent(pvec.key())); + } + + let mut iter = wal.range_values::(&mid..); + for (pwal, pvec) in people.range(&mid..).zip(iter.by_ref()) { + assert_eq!(&pwal.1, pvec.clone().value()); + } + + assert!(iter.next().is_none()); + + let mut rev_iter = wal.range_values::(&mid..).rev(); + + for (pwal, pvec) in people.range(&mid..).rev().zip(rev_iter.by_ref()) { + assert_eq!(&pwal.1, pvec.value()); + } + let wal = wal.reader(); let mut iter = wal.range::(&mid..); @@ -238,7 +288,7 @@ where fn entry_iter(wal: &mut OrderWal) where M: Memtable + 'static, - for<'a> M::Item<'a>: MemtableEntry<'a>, + for<'a> M::Item<'a>: MemtableEntry<'a> + std::fmt::Debug, M::Error: std::fmt::Debug, { for i in 0..100u32 { @@ -246,6 +296,7 @@ where } let mut curr = wal.first(); + println!("{:?}", curr); let mut cursor = 0; while let Some(mut ent) = curr { assert_eq!(ent.key(), &cursor); @@ -264,6 +315,44 @@ where assert_eq!(ent.value(), &cursor); curr = ent.prev(); } + + let mut curr = wal.keys().next(); + println!("{:?}", curr); + let mut cursor = 0; + while let Some(mut ent) = curr { + assert_eq!(ent.key(), &cursor); + cursor += 1; + curr = ent.next(); + } + + let curr = wal.keys().next_back(); + + let mut curr = curr.clone(); + let mut cursor = 100; + while let Some(mut ent) = curr { + cursor -= 1; + assert_eq!(ent.key(), &cursor); + curr = ent.prev(); + } + + let mut curr = wal.values().next(); + println!("{:?}", curr); + let mut cursor = 0; + while let Some(mut ent) = curr { + assert_eq!(ent.value(), &cursor); + cursor += 1; + curr = ent.next(); + } + + let curr = wal.values().next_back(); + + let mut curr = curr.clone(); + let mut cursor = 100; + while let Some(mut ent) = curr { + cursor -= 1; + assert_eq!(ent.value(), &cursor); + curr = ent.prev(); + } } expand_unit_tests!("linked": OrderWalAlternativeTable [TableOptions::Linked]: Table<_, _> { diff --git a/src/swmr/tests/multiple_version_get.rs b/src/swmr/tests/multiple_version_get.rs index 0447efc..819f7d7 100644 --- a/src/swmr/tests/multiple_version_get.rs +++ b/src/swmr/tests/multiple_version_get.rs @@ -482,7 +482,7 @@ where .insert_with_value_builder( 0, &p, - ValueBuilder::new(v.len(), |buf: &mut VacantBuffer<'_>| { + ValueBuilder::once(v.len(), |buf: &mut VacantBuffer<'_>| { buf.put_slice(v.as_bytes()).map(|_| v.len()) }), ) @@ -497,7 +497,6 @@ where } } -#[allow(clippy::needless_borrows_for_generic_args)] fn insert_with_key_builder(wal: &mut OrderWal) where M: MultipleVersionMemtable + 'static, @@ -507,13 +506,14 @@ where let people = (0..100) .map(|_| { let p = Person::random(); - let pvec = p.to_vec(); let v = format!("My name is {}", p.name); - unsafe { - wal - .insert(0, MaybeStructured::from_slice(pvec.as_ref()), &v) - .unwrap(); - } + wal + .insert_with_key_builder( + 0, + KeyBuilder::once(p.encoded_len(), |buf| p.encode_to_buffer(buf)), + &v, + ) + .unwrap(); (p, v) }) .collect::>(); diff --git a/src/swmr/tests/multiple_version_iters.rs b/src/swmr/tests/multiple_version_iters.rs index 7d7f6df..b1a0209 100644 --- a/src/swmr/tests/multiple_version_iters.rs +++ b/src/swmr/tests/multiple_version_iters.rs @@ -195,8 +195,8 @@ where wal.insert(0, &make_int_key(i), &make_value(i)).unwrap(); } - let mut ent = wal.first(0); - + let mut ent = wal.first(0).clone(); + std::println!("{ent:?}"); let mut i = 0; while let Some(ref mut entry) = ent { assert_eq!(entry.key(), make_int_key(i).as_str()); @@ -206,7 +206,8 @@ where } assert_eq!(i, N); - let mut ent = wal.keys(0).next(); + let mut ent = wal.keys(0).next().clone(); + std::println!("{ent:?}"); let mut i = 0; while let Some(ref mut entry) = ent { @@ -216,7 +217,8 @@ where } assert_eq!(i, N); - let mut ent = wal.values(0).next(); + let mut ent = wal.values(0).next().clone(); + std::println!("{ent:?}"); let mut i = 0; while let Some(ref mut entry) = ent { @@ -243,7 +245,7 @@ fn iter_all_versions_next_by_versioned_entry( wal.remove(1, &k).unwrap(); } - let mut ent = wal.first(0); + let mut ent = wal.first(0).clone(); let mut i = 0; while let Some(ref mut entry) = ent { assert_eq!(entry.key(), make_int_key(i).as_str()); @@ -253,7 +255,7 @@ fn iter_all_versions_next_by_versioned_entry( } assert_eq!(i, N); - let mut ent = wal.first_versioned(1); + let mut ent = wal.first_versioned(1).clone(); let mut i = 0; while let Some(ref mut entry) = ent { if i % 2 == 1 { @@ -490,6 +492,7 @@ where for ent in it { assert_eq!(ent.key(), make_int_key(i).as_str()); assert_eq!(ent.value(), make_value(i).as_str()); + assert_eq!(ent.version(), 0); i -= 1; } @@ -501,6 +504,7 @@ where for ent in it { assert_eq!(ent.key(), make_int_key(i).as_str()); assert_eq!(ent.value().unwrap(), make_value(i).as_str()); + assert_eq!(ent.version(), 0); i -= 1; } @@ -510,6 +514,7 @@ where let mut iter = wal.range_keys(0, lower.as_str()..).rev(); for ent in &mut iter { assert_eq!(ent.key(), make_int_key(i).as_str()); + assert_eq!(ent.version(), 0); i -= 1; } assert_eq!(i, 49); @@ -518,6 +523,7 @@ where let mut iter = wal.range_values(0, lower.as_str()..).rev(); for ent in &mut iter { assert_eq!(ent.value(), make_value(i).as_str()); + assert_eq!(ent.version(), 0); i -= 1; } assert_eq!(i, 49);