Skip to content

Commit

Permalink
test: add 'range' operation to btree comprehensive test (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
ielashi authored Oct 2, 2023
1 parent c880b83 commit d422c2f
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/btreemap/proptests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ enum Operation {
Iter { from: usize, len: usize },
Get(usize),
Remove(usize),
Range { from: usize, len: usize },
}

// A custom strategy that gives unequal weights to the different operations.
// Note that `Insert` has a higher weight than `Remove` so that, on average, BTreeMaps
// are growing in size the more operations are executed.
fn operation_strategy() -> impl Strategy<Value = Operation> {
prop_oneof![
3 => (any::<Vec<u8>>(), any::<Vec<u8>>())
30 => (any::<Vec<u8>>(), any::<Vec<u8>>())
.prop_map(|(key, value)| Operation::Insert { key, value }),
1 => (any::<usize>(), any::<usize>())
5 => (any::<usize>(), any::<usize>())
.prop_map(|(from, len)| Operation::Iter { from, len }),
2 => (any::<usize>()).prop_map(Operation::Get),
1 => (any::<usize>()).prop_map(Operation::Remove),
50 => (any::<usize>()).prop_map(Operation::Get),
15 => (any::<usize>()).prop_map(Operation::Remove),
5 => (any::<usize>(), any::<usize>())
.prop_map(|(from, len)| Operation::Range { from, len }),
]
}

Expand Down Expand Up @@ -196,5 +199,37 @@ fn execute_operation<M: Memory>(
assert_eq!(btree.remove(&k), Some(v));
}
}
Operation::Range { from, len } => {
assert_eq!(std_btree.len(), btree.len() as usize);
if std_btree.is_empty() {
return;
}

eprintln!("Range({}, {})", from, len);
let from = from % std_btree.len();
let end = std::cmp::min(std_btree.len() - 1, from + len);

// Create a range for the stable btree from the keys at indexes `from` and `end`.
let range_start = btree.iter().skip(from).take(1).next().unwrap().0.clone();
let range_end = btree.iter().skip(end).take(1).next().unwrap().0.clone();
let stable_range = btree.range(range_start..range_end);

// Create a range for the std btree from the keys at indexes `from` and `end`.
let range_start = std_btree
.iter()
.skip(from)
.take(1)
.next()
.unwrap()
.0
.clone();
let range_end = std_btree.iter().skip(end).take(1).next().unwrap().0.clone();
let std_range = std_btree.range(range_start..range_end);

for ((k1, v1), (k2, v2)) in std_range.zip(stable_range) {
assert_eq!(k1, &k2);
assert_eq!(v1, &v2);
}
}
};
}

0 comments on commit d422c2f

Please sign in to comment.