From 0f3b74b20eee105bb0666edcd1e64dd5bf5a0f25 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 26 Apr 2019 18:10:06 +1000 Subject: [PATCH] Update `TestRandom` to vary list length --- eth2/types/src/test_utils/macros.rs | 17 +++++++++++++++-- eth2/types/src/test_utils/test_random.rs | 12 +++++++----- eth2/types/src/tree_hash_vector.rs | 6 +++++- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/eth2/types/src/test_utils/macros.rs b/eth2/types/src/test_utils/macros.rs index 984f2962f..193d01b4f 100644 --- a/eth2/types/src/test_utils/macros.rs +++ b/eth2/types/src/test_utils/macros.rs @@ -47,12 +47,25 @@ macro_rules! cached_tree_hash_tests { // Test the original hash let original = $type::random_for_test(&mut rng); let mut hasher = cached_tree_hash::CachedTreeHasher::new(&original).unwrap(); - assert_eq!(hasher.tree_hash_root().unwrap(), original.tree_hash_root()); + assert_eq!( + hasher.tree_hash_root().unwrap(), + original.tree_hash_root(), + "Original hash failed." + ); // Test the updated hash let modified = $type::random_for_test(&mut rng); hasher.update(&modified).unwrap(); - assert_eq!(hasher.tree_hash_root().unwrap(), modified.tree_hash_root()); + dbg!(&hasher.cache.chunk_modified); + dbg!(hasher.cache.chunk_modified.len()); + dbg!(hasher.cache.chunk_index); + dbg!(hasher.cache.schemas.len()); + dbg!(hasher.cache.schema_index); + assert_eq!( + hasher.tree_hash_root().unwrap(), + modified.tree_hash_root(), + "Modification hash failed" + ); } }; } diff --git a/eth2/types/src/test_utils/test_random.rs b/eth2/types/src/test_utils/test_random.rs index cb7abe3a4..2d4269b08 100644 --- a/eth2/types/src/test_utils/test_random.rs +++ b/eth2/types/src/test_utils/test_random.rs @@ -44,11 +44,13 @@ where U: TestRandom, { fn random_for_test(rng: &mut T) -> Self { - vec![ - ::random_for_test(rng), - ::random_for_test(rng), - ::random_for_test(rng), - ] + let mut output = vec![]; + + for _ in 0..(usize::random_for_test(rng) % 4) { + output.push(::random_for_test(rng)); + } + + output } } diff --git a/eth2/types/src/tree_hash_vector.rs b/eth2/types/src/tree_hash_vector.rs index 6279c4512..bfbb42e57 100644 --- a/eth2/types/src/tree_hash_vector.rs +++ b/eth2/types/src/tree_hash_vector.rs @@ -105,7 +105,11 @@ where U: TestRandom, { fn random_for_test(rng: &mut T) -> Self { - Vec::random_for_test(rng).into() + TreeHashVector::from(vec![ + U::random_for_test(rng), + U::random_for_test(rng), + U::random_for_test(rng), + ]) } }