Add extra tests, all passing

This commit is contained in:
Paul Hauner 2019-04-25 12:00:39 +10:00
parent 58b69e9ba6
commit 827e1c62d9
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
3 changed files with 56 additions and 15 deletions

View File

@ -5,7 +5,7 @@ where
T: CachedTreeHash<T> + TreeHash, T: CachedTreeHash<T> + TreeHash,
{ {
fn new_tree_hash_cache(&self, depth: usize) -> Result<TreeHashCache, Error> { fn new_tree_hash_cache(&self, depth: usize) -> Result<TreeHashCache, Error> {
let mut overlay = self.tree_hash_cache_overlay(0, depth)?; let overlay = self.tree_hash_cache_overlay(0, depth)?;
let mut cache = match T::tree_hash_type() { let mut cache = match T::tree_hash_type() {
TreeHashType::Basic => TreeHashCache::from_bytes( TreeHashType::Basic => TreeHashCache::from_bytes(

View File

@ -183,6 +183,7 @@ impl TreeHashCache {
.iter() .iter()
.skip(overlay_index) .skip(overlay_index)
.position(|o| o.depth <= depth) .position(|o| o.depth <= depth)
.and_then(|i| Some(i + overlay_index))
.unwrap_or_else(|| self.overlays.len()); .unwrap_or_else(|| self.overlays.len());
self.overlays.splice(overlay_index..end, vec![]); self.overlays.splice(overlay_index..end, vec![]);

View File

@ -231,8 +231,7 @@ pub struct StructWithVecOfStructs {
pub c: Vec<Inner>, pub c: Vec<Inner>,
} }
#[test] fn get_struct_with_vec_of_structs() -> Vec<StructWithVecOfStructs> {
fn test_struct_with_vec_of_structs() {
let inner_a = Inner { let inner_a = Inner {
a: 12, a: 12,
b: 13, b: 13,
@ -285,21 +284,62 @@ fn test_struct_with_vec_of_structs() {
..a.clone() ..a.clone()
}; };
vec![a, b, c, d, e, f]
}
#[test]
fn test_struct_with_vec_of_structs() {
let variants = get_struct_with_vec_of_structs();
test_routine(variants[0].clone(), variants.clone());
test_routine(variants[1].clone(), variants.clone());
test_routine(variants[2].clone(), variants.clone());
test_routine(variants[3].clone(), variants.clone());
test_routine(variants[4].clone(), variants.clone());
test_routine(variants[5].clone(), variants.clone());
}
#[derive(Clone, Debug, TreeHash, CachedTreeHash)]
pub struct StructWithVecOfStructWithVecOfStructs {
pub a: Vec<StructWithVecOfStructs>,
pub b: u64,
}
#[test]
fn test_struct_with_vec_of_struct_with_vec_of_structs() {
let structs = get_struct_with_vec_of_structs();
let variants = vec![ let variants = vec![
a.clone(), StructWithVecOfStructWithVecOfStructs {
b.clone(), a: structs[..].to_vec(),
c.clone(), b: 99,
d.clone(), },
e.clone(), StructWithVecOfStructWithVecOfStructs { a: vec![], b: 99 },
f.clone(), StructWithVecOfStructWithVecOfStructs {
a: structs[0..2].to_vec(),
b: 99,
},
StructWithVecOfStructWithVecOfStructs {
a: structs[0..2].to_vec(),
b: 100,
},
StructWithVecOfStructWithVecOfStructs {
a: structs[0..1].to_vec(),
b: 100,
},
StructWithVecOfStructWithVecOfStructs {
a: structs[0..4].to_vec(),
b: 100,
},
StructWithVecOfStructWithVecOfStructs {
a: structs[0..5].to_vec(),
b: 8,
},
]; ];
test_routine(a, variants.clone()); for v in &variants {
test_routine(b, variants.clone()); test_routine(v.clone(), variants.clone());
test_routine(c, variants.clone()); }
test_routine(d, variants.clone());
test_routine(e, variants.clone());
test_routine(f, variants);
} }
#[derive(Clone, Debug, TreeHash, CachedTreeHash)] #[derive(Clone, Debug, TreeHash, CachedTreeHash)]