Update TestRandom to vary list length

This commit is contained in:
Paul Hauner 2019-04-26 18:10:06 +10:00
parent a425beb42a
commit 0f3b74b20e
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
3 changed files with 27 additions and 8 deletions

View File

@ -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"
);
}
};
}

View File

@ -44,11 +44,13 @@ where
U: TestRandom<T>,
{
fn random_for_test(rng: &mut T) -> Self {
vec![
<U>::random_for_test(rng),
<U>::random_for_test(rng),
<U>::random_for_test(rng),
]
let mut output = vec![];
for _ in 0..(usize::random_for_test(rng) % 4) {
output.push(<U>::random_for_test(rng));
}
output
}
}

View File

@ -105,7 +105,11 @@ where
U: TestRandom<T>,
{
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),
])
}
}