Skip to content

Commit

Permalink
test: compare to Btreemap instead of hardcoding expected results
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Dec 23, 2023
1 parent 7b460ba commit eb1b858
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
6 changes: 4 additions & 2 deletions mast/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
27 changes: 7 additions & 20 deletions mast/src/operations/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ mod test {
});

test_operations(
&expected.clone().map(|e| (e, Operation::Insert)),
&expected,
&expected.map(|e| (e, Operation::Insert)),
Some("78fd7507ef338f1a5816ffd702394999680a9694a85f4b8af77795d9fdd5854d"),
)
}
Expand All @@ -252,8 +251,7 @@ mod test {
});

test_operations(
&expected.clone().map(|e| (e, Operation::Insert)),
&expected,
&expected.map(|e| (e, Operation::Insert)),
Some("02af3de6ed6368c5abc16f231a17d1140e7bfec483c8d0aa63af4ef744d29bc3"),
);
}
Expand All @@ -273,8 +271,7 @@ mod test {
});

test_operations(
&expected.clone().map(|e| (e, Operation::Insert)),
&expected,
&expected.map(|e| (e, Operation::Insert)),
Some("02af3de6ed6368c5abc16f231a17d1140e7bfec483c8d0aa63af4ef744d29bc3"),
)
}
Expand All @@ -289,8 +286,7 @@ mod test {
});

test_operations(
&expected.clone().map(|e| (e, Operation::Insert)),
&expected,
&expected.map(|e| (e, Operation::Insert)),
Some("0957cc9b87c11cef6d88a95328cfd9043a3d6a99e9ba35ee5c9c47e53fb6d42b"),
)
}
Expand All @@ -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"),
)
}
Expand All @@ -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"),
)
}
Expand All @@ -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"),
)
}
Expand Down
28 changes: 23 additions & 5 deletions mast/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::assert_eq;
use std::collections::BTreeMap;

use crate::node::Node;
use crate::treap::HashTreap;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit eb1b858

Please sign in to comment.