Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Oct 31, 2024
1 parent 289f3ad commit de735c1
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 21 deletions.
7 changes: 2 additions & 5 deletions src/memtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self::Key>: 'a,
Self: 'a;
Expand Down
5 changes: 4 additions & 1 deletion src/swmr/tests/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
91 changes: 90 additions & 1 deletion src/swmr/tests/iters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -219,6 +243,32 @@ where

assert!(iter.next().is_none());

let mut iter = wal.range_keys::<Person, _>(&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::<Person, _>(&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::<Person, _>(&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::<Person, _>(&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::<Person, _>(&mid..);

Expand All @@ -238,14 +288,15 @@ where
fn entry_iter<M>(wal: &mut OrderWal<u32, u32, M>)
where
M: Memtable<Key = u32, Value = u32> + '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 {
wal.insert(&i, &i).unwrap();
}

let mut curr = wal.first();
println!("{:?}", curr);
let mut cursor = 0;
while let Some(mut ent) = curr {
assert_eq!(ent.key(), &cursor);
Expand All @@ -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<u32, u32> [TableOptions::Linked]: Table<_, _> {
Expand Down
16 changes: 8 additions & 8 deletions src/swmr/tests/multiple_version_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}),
)
Expand All @@ -497,7 +497,6 @@ where
}
}

#[allow(clippy::needless_borrows_for_generic_args)]
fn insert_with_key_builder<M>(wal: &mut OrderWal<Person, String, M>)
where
M: MultipleVersionMemtable<Key = Person, Value = String> + 'static,
Expand All @@ -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::<Vec<_>>();
Expand Down
18 changes: 12 additions & 6 deletions src/swmr/tests/multiple_version_iters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -243,7 +245,7 @@ fn iter_all_versions_next_by_versioned_entry<M>(
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());
Expand All @@ -253,7 +255,7 @@ fn iter_all_versions_next_by_versioned_entry<M>(
}
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 {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit de735c1

Please sign in to comment.