@@ -500,3 +500,82 @@ fn node_deletion_resulted_in_a_branch_collapse(
500500 // collapse.
501501 branch_collapse_occurred. then ( || mpt_trie:: utils:: IntoTrieKey :: into_key ( new_path. iter ( ) ) )
502502}
503+
504+ #[ cfg( test) ]
505+ mod tests {
506+ use std:: array;
507+
508+ use itertools:: Itertools as _;
509+ use quickcheck:: Arbitrary ;
510+
511+ use super :: * ;
512+
513+ quickcheck:: quickcheck! {
514+ fn quickcheck(
515+ kvs: Vec <( TrieKey , Vec <u8 >) >,
516+ mask_kvs: Vec <( TrieKey , Vec <u8 >) >,
517+ khs: Vec <( TrieKey , ArbitraryHash ) >
518+ ) -> ( ) {
519+ do_quickcheck( kvs, mask_kvs, khs)
520+ }
521+ }
522+
523+ fn do_quickcheck (
524+ kvs : Vec < ( TrieKey , Vec < u8 > ) > ,
525+ mask_kvs : Vec < ( TrieKey , Vec < u8 > ) > ,
526+ khs : Vec < ( TrieKey , ArbitraryHash ) > ,
527+ ) {
528+ let mut mpt = HashedPartialTrie :: default ( ) ;
529+ let mask = mask_kvs
530+ . iter ( )
531+ . map ( |( k, _) | k. into_nibbles ( ) )
532+ . collect :: < Vec < _ > > ( ) ;
533+ for ( k, v) in kvs. into_iter ( ) . chain ( mask_kvs) {
534+ let _ = mpt. insert ( k. into_nibbles ( ) , v) ;
535+ }
536+ for ( k, ArbitraryHash ( h) ) in khs {
537+ let _ = mpt. insert ( k. into_nibbles ( ) , h) ;
538+ }
539+ let root = mpt. hash ( ) ;
540+ if let Ok ( sub) = mpt_trie:: trie_subsets:: create_trie_subset ( & mpt, mask) {
541+ assert_eq ! ( sub. hash( ) , root)
542+ }
543+ }
544+
545+ impl Arbitrary for TrieKey {
546+ fn arbitrary ( g : & mut quickcheck:: Gen ) -> Self {
547+ Self ( Arbitrary :: arbitrary ( g) )
548+ }
549+
550+ fn shrink ( & self ) -> Box < dyn Iterator < Item = Self > > {
551+ let Self ( comps) = * self ;
552+ Box :: new ( comps. shrink ( ) . map ( Self ) )
553+ }
554+ }
555+
556+ #[ derive( Debug , Clone , Copy ) ]
557+ struct ArbitraryHash ( H256 ) ;
558+ impl ArbitraryHash {
559+ pub fn new ( ( a, b, c, d) : ( u64 , u64 , u64 , u64 ) ) -> Self {
560+ let mut iter = [ a, b, c, d] . into_iter ( ) . flat_map ( u64:: to_ne_bytes) ;
561+ let h = H256 ( array:: from_fn ( |_| iter. next ( ) . unwrap ( ) ) ) ;
562+ assert_eq ! ( iter. count( ) , 0 ) ;
563+ Self ( h)
564+ }
565+ }
566+ impl Arbitrary for ArbitraryHash {
567+ fn arbitrary ( g : & mut quickcheck:: Gen ) -> Self {
568+ Self :: new ( Arbitrary :: arbitrary ( g) )
569+ }
570+
571+ fn shrink ( & self ) -> Box < dyn Iterator < Item = Self > > {
572+ let Self ( H256 ( bytes) ) = self ;
573+ let ( a, b, c, d) = bytes
574+ . chunks_exact ( 8 )
575+ . map ( |it| u64:: from_ne_bytes ( it. try_into ( ) . unwrap ( ) ) )
576+ . collect_tuple ( )
577+ . unwrap ( ) ;
578+ Box :: new ( ( a, b, c, d) . shrink ( ) . map ( Self :: new) )
579+ }
580+ }
581+ }
0 commit comments