Skip to content

Commit

Permalink
fix: first bug with proptest
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Dec 23, 2023
1 parent 39ac5c1 commit 2f3f6bf
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 6 deletions.
118 changes: 118 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ redb = "1.4.0"
varu64 = "0.7.0"

[dev-dependencies]
proptest = "1.4.0"
tempfile = "3.8.1"
7 changes: 7 additions & 0 deletions mast/proptest-regressions/operations/insert.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 7912e0f85e7a1a9892a9c85944f3392f6e1fe1a33d2ea00b3809400d75166743 # shrinks to random_entries = [([135], [150]), ([25], [0]), ([129], [0]), ([135], [150])]
36 changes: 35 additions & 1 deletion mast/src/operations/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ pub fn insert(
if let Some(mut existing) = path.existing {
if existing.value() == value {
// There is really nothing to update. Skip traversing upwards.
return path.upper_path.pop().map(|(n, _)| n).unwrap_or(existing);

return path
.upper_path
.first()
.map(|(n, _)| n.clone())
.unwrap_or(existing);
}

// Decrement the old version.
Expand Down Expand Up @@ -222,6 +227,35 @@ fn binary_search_path(
#[cfg(test)]
mod test {
use crate::test::{test_operations, Entry};
use proptest::prelude::*;

proptest! {
#[test]
/// Test that upserting an entry with the same key in different tree shapes results in the
/// expected structure
fn test_upsert(random_entries in prop::collection::vec(
(prop::collection::vec(any::<u8>(), 1), prop::collection::vec(any::<u8>(), 1)),
1..10,
)) {
let operations = random_entries.into_iter().map(|(key, value)| {
Entry::insert(&key, &value)
}).collect::<Vec<_>>();

test_operations(&operations, None);
}

#[test]
fn test_general_insertiong(random_entries in prop::collection::vec(
(prop::collection::vec(any::<u8>(), 32), prop::collection::vec(any::<u8>(), 32)),
1..50,
)) {
let operations = random_entries.into_iter().map(|(key, value)| {
Entry::insert(&key, &value)
}).collect::<Vec<_>>();

test_operations(&operations, None);
}
}

#[test]
fn insert_single_entry() {
Expand Down
10 changes: 5 additions & 5 deletions mast/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ fn into_mermaid_graph(treap: &HashTreap) -> String {
}

fn build_graph_string(treap: &HashTreap, node: &Node, graph: &mut String) {
let key = bytes_to_string(node.key());
let key = format_key(node.key());
let node_label = format!("{}(({}))", node.hash(), key);

// graph.push_str(&format!("## START node {}\n", node_label));
if let Some(child) = treap.get_node(node.left()) {
let key = bytes_to_string(child.key());
let key = format_key(child.key());
let child_label = format!("{}(({}))", child.hash(), key);

graph.push_str(&format!(" {} --l--> {};\n", node_label, child_label));
Expand All @@ -178,7 +178,7 @@ fn build_graph_string(treap: &HashTreap, node: &Node, graph: &mut String) {
}

if let Some(child) = treap.get_node(node.right()) {
let key = bytes_to_string(child.key());
let key = format_key(child.key());
let child_label = format!("{}(({}))", child.hash(), key);

graph.push_str(&format!(" {} --r--> {};\n", node_label, child_label));
Expand All @@ -189,6 +189,6 @@ fn build_graph_string(treap: &HashTreap, node: &Node, graph: &mut String) {
}
}

fn bytes_to_string(byte: &[u8]) -> String {
String::from_utf8(byte.to_vec()).expect("Invalid utf8 key in test with mermaig graph")
fn format_key(bytes: &[u8]) -> String {
format!("\"{:?}\"", bytes)
}

0 comments on commit 2f3f6bf

Please sign in to comment.