From 779f74268e1ae99e794e082944c6146be6d7aaa9 Mon Sep 17 00:00:00 2001 From: Bodil Stokke Date: Mon, 11 Mar 2019 17:01:14 +0000 Subject: [PATCH] Comprehensive tests validate the reality of the size table at each step. --- src/nodes/rrb.rs | 33 +++++++++++++++++++++++++++++++++ src/tests/vector.rs | 2 ++ src/vector/mod.rs | 7 +++++++ 3 files changed, 42 insertions(+) diff --git a/src/nodes/rrb.rs b/src/nodes/rrb.rs index 34d56db..49d81f2 100644 --- a/src/nodes/rrb.rs +++ b/src/nodes/rrb.rs @@ -952,6 +952,39 @@ impl Node { } } + 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(&self, f: &mut W, indent: usize, level: usize) -> Result<(), fmt::Error> // where // W: fmt::Write, diff --git a/src/tests/vector.rs b/src/tests/vector.rs index 901e42a..405985e 100644 --- a/src/tests/vector.rs +++ b/src/tests/vector.rs @@ -123,6 +123,7 @@ proptest! { fn comprehensive(actions: Actions) { let mut vec = Vector::new(); let mut nat = Vec::new(); + vec.assert_invariants(); for action in actions.0 { match action { Action::PushFront(value) => { @@ -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); } diff --git a/src/vector/mod.rs b/src/vector/mod.rs index c115d08..b150ae5 100644 --- a/src/vector/mod.rs +++ b/src/vector/mod.rs @@ -1424,6 +1424,13 @@ impl Vector { 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