Skip to content

Commit

Permalink
Comprehensive tests validate the reality of the size table at each step.
Browse files Browse the repository at this point in the history
  • Loading branch information
bodil committed Mar 11, 2019
1 parent 7dca278 commit 779f742
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/nodes/rrb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,39 @@ impl<A: Clone> Node<A> {
}
}

pub fn assert_invariants(&self) -> usize {
// Verifies that the size table matches reality.
match self.children {
Entry::Empty => 0,
Entry::Values(ref values) => {
// An empty value node is pointless and should never occur.
assert_ne!(0, values.len());
values.len()
}
Entry::Nodes(ref size, ref children) => {
// A parent node with no children should never occur.
assert_ne!(0, children.len());
let mut lengths = Vec::new();
for child in &**children {
lengths.push(child.assert_invariants());
}
match size {
Size::Size(size) => {
let total: usize = lengths.iter().sum();
assert_eq!(*size, total);
}
Size::Table(ref table) => {
for (index, current) in table.iter().enumerate() {
let expected: usize = lengths.iter().take(index + 1).sum();
assert_eq!(expected, *current);
}
}
}
lengths.iter().sum()
}
}
}

// pub fn print<W>(&self, f: &mut W, indent: usize, level: usize) -> Result<(), fmt::Error>
// where
// W: fmt::Write,
Expand Down
2 changes: 2 additions & 0 deletions src/tests/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ proptest! {
fn comprehensive(actions: Actions<u8>) {
let mut vec = Vector::new();
let mut nat = Vec::new();
vec.assert_invariants();
for action in actions.0 {
match action {
Action::PushFront(value) => {
Expand Down Expand Up @@ -210,6 +211,7 @@ proptest! {
nat = nat_right;
}
}
vec.assert_invariants();
assert_eq!(nat.len(),vec.len());
assert_eq!(Vector::from_iter(nat.iter().cloned()), vec);
}
Expand Down
7 changes: 7 additions & 0 deletions src/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,13 @@ impl<A: Clone> Vector<A> {
sort::quicksort(&mut self.focus_mut(), 0, len - 1, &cmp);
}
}

#[allow(dead_code)]
pub(crate) fn assert_invariants(&self) {
if let Vector::Full(ref tree) = self {
tree.middle.assert_invariants();
}
}
}

// Implementation details
Expand Down

0 comments on commit 779f742

Please sign in to comment.