test: compare to Btreemap instead of hardcoding expected results

This commit is contained in:
nazeh
2023-12-23 09:37:35 +03:00
parent 7b460ba3bd
commit eb1b858ebb
3 changed files with 34 additions and 27 deletions

View File

@@ -4,8 +4,10 @@ use redb::{ReadableTable, Table};
use crate::{Hash, Hasher, HASH_LEN};
// TODO: Are we creating too many hashers?
// TODO: are we calculating the rank and hash too often?
// TODO: room for improvement (pending actual benchmarks to justify):
// - cache encoding
// - cache hashing
// TODO: remove unwrap
#[derive(Debug, Clone, PartialEq)]

View File

@@ -233,8 +233,7 @@ mod test {
});
test_operations(
&expected.clone().map(|e| (e, Operation::Insert)),
&expected,
&expected.map(|e| (e, Operation::Insert)),
Some("78fd7507ef338f1a5816ffd702394999680a9694a85f4b8af77795d9fdd5854d"),
)
}
@@ -252,8 +251,7 @@ mod test {
});
test_operations(
&expected.clone().map(|e| (e, Operation::Insert)),
&expected,
&expected.map(|e| (e, Operation::Insert)),
Some("02af3de6ed6368c5abc16f231a17d1140e7bfec483c8d0aa63af4ef744d29bc3"),
);
}
@@ -273,8 +271,7 @@ mod test {
});
test_operations(
&expected.clone().map(|e| (e, Operation::Insert)),
&expected,
&expected.map(|e| (e, Operation::Insert)),
Some("02af3de6ed6368c5abc16f231a17d1140e7bfec483c8d0aa63af4ef744d29bc3"),
)
}
@@ -289,8 +286,7 @@ mod test {
});
test_operations(
&expected.clone().map(|e| (e, Operation::Insert)),
&expected,
&expected.map(|e| (e, Operation::Insert)),
Some("0957cc9b87c11cef6d88a95328cfd9043a3d6a99e9ba35ee5c9c47e53fb6d42b"),
)
}
@@ -310,8 +306,7 @@ mod test {
});
test_operations(
&entries.clone().map(|e| (e, Operation::Insert)),
&entries[1..],
&entries.map(|e| (e, Operation::Insert)),
Some("4538b4de5e58f9be9d54541e69fab8c94c31553a1dec579227ef9b572d1c1dff"),
)
}
@@ -331,12 +326,8 @@ mod test {
}
});
let mut expected = entries.to_vec();
expected.sort_by(|a, b| a.key.cmp(&b.key));
test_operations(
&entries.clone().map(|e| (e, Operation::Insert)),
&expected[1..],
&entries.map(|e| (e, Operation::Insert)),
Some("c9f7aaefb18ec8569322b9621fc64f430a7389a790e0bf69ec0ad02879d6ce54"),
)
}
@@ -356,12 +347,8 @@ mod test {
}
});
let mut expected = entries.to_vec();
expected.remove(1);
test_operations(
&entries.clone().map(|e| (e, Operation::Insert)),
&expected,
&entries.map(|e| (e, Operation::Insert)),
Some("02e26311f2b55bf6d4a7163399f99e17c975891a05af2f1e09bc969f8bf0f95d"),
)
}

View File

@@ -1,4 +1,5 @@
use std::assert_eq;
use std::collections::BTreeMap;
use crate::node::Node;
use crate::treap::HashTreap;
@@ -27,7 +28,7 @@ impl std::fmt::Debug for Entry {
}
}
pub fn test_operations(input: &[(Entry, Operation)], expected: &[Entry], root_hash: Option<&str>) {
pub fn test_operations(input: &[(Entry, Operation)], root_hash: Option<&str>) {
let inmemory = InMemoryBackend::new();
let db = Database::builder()
.create_with_backend(inmemory)
@@ -62,12 +63,29 @@ pub fn test_operations(input: &[(Entry, Operation)], expected: &[Entry], root_ha
})
.collect::<Vec<_>>();
let mut sorted = expected.to_vec();
sorted.sort_by(|a, b| a.key.cmp(&b.key));
verify_ranks(&treap);
assert_eq!(collected, sorted, "{}", format!("Entries do not match"));
let mut btree = BTreeMap::new();
for (entry, operation) in input {
match operation {
Operation::Insert => {
btree.insert(&entry.key, &entry.value);
}
Operation::Delete => {
btree.remove(&entry.key);
}
}
}
let expected = btree
.iter()
.map(|(key, value)| Entry {
key: key.to_vec(),
value: value.to_vec(),
})
.collect::<Vec<_>>();
assert_eq!(collected, expected, "{}", format!("Entries do not match"));
if root_hash.is_some() {
assert_root(&treap, root_hash.unwrap());