From 827e1c62d9e517ae3a45413b68bcde5d356e65bd Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 25 Apr 2019 12:00:39 +1000 Subject: [PATCH] Add extra tests, all passing --- .../src/cached_tree_hash/impls/vec.rs | 2 +- .../src/cached_tree_hash/tree_hash_cache.rs | 1 + eth2/utils/tree_hash/tests/tests.rs | 68 +++++++++++++++---- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/eth2/utils/tree_hash/src/cached_tree_hash/impls/vec.rs b/eth2/utils/tree_hash/src/cached_tree_hash/impls/vec.rs index 6a0770681f..c92077e941 100644 --- a/eth2/utils/tree_hash/src/cached_tree_hash/impls/vec.rs +++ b/eth2/utils/tree_hash/src/cached_tree_hash/impls/vec.rs @@ -5,7 +5,7 @@ where T: CachedTreeHash + TreeHash, { fn new_tree_hash_cache(&self, depth: usize) -> Result { - 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() { TreeHashType::Basic => TreeHashCache::from_bytes( diff --git a/eth2/utils/tree_hash/src/cached_tree_hash/tree_hash_cache.rs b/eth2/utils/tree_hash/src/cached_tree_hash/tree_hash_cache.rs index 336d280285..169edb4d1f 100644 --- a/eth2/utils/tree_hash/src/cached_tree_hash/tree_hash_cache.rs +++ b/eth2/utils/tree_hash/src/cached_tree_hash/tree_hash_cache.rs @@ -183,6 +183,7 @@ impl TreeHashCache { .iter() .skip(overlay_index) .position(|o| o.depth <= depth) + .and_then(|i| Some(i + overlay_index)) .unwrap_or_else(|| self.overlays.len()); self.overlays.splice(overlay_index..end, vec![]); diff --git a/eth2/utils/tree_hash/tests/tests.rs b/eth2/utils/tree_hash/tests/tests.rs index 6f339b8f2d..e3d8701bd7 100644 --- a/eth2/utils/tree_hash/tests/tests.rs +++ b/eth2/utils/tree_hash/tests/tests.rs @@ -231,8 +231,7 @@ pub struct StructWithVecOfStructs { pub c: Vec, } -#[test] -fn test_struct_with_vec_of_structs() { +fn get_struct_with_vec_of_structs() -> Vec { let inner_a = Inner { a: 12, b: 13, @@ -285,21 +284,62 @@ fn test_struct_with_vec_of_structs() { ..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, + 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![ - a.clone(), - b.clone(), - c.clone(), - d.clone(), - e.clone(), - f.clone(), + StructWithVecOfStructWithVecOfStructs { + a: structs[..].to_vec(), + b: 99, + }, + StructWithVecOfStructWithVecOfStructs { a: vec![], b: 99 }, + 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()); - test_routine(b, variants.clone()); - test_routine(c, variants.clone()); - test_routine(d, variants.clone()); - test_routine(e, variants.clone()); - test_routine(f, variants); + for v in &variants { + test_routine(v.clone(), variants.clone()); + } } #[derive(Clone, Debug, TreeHash, CachedTreeHash)]