@@ -577,6 +577,77 @@ fn checkpoint_query() {
577577 }
578578}
579579
580+ #[ test]
581+ fn checkpoint_insert ( ) {
582+ struct TestCase < ' a > {
583+ /// The name of the test.
584+ name : & ' a str ,
585+ /// The original checkpoint chain to call [`CheckPoint::insert`] on.
586+ chain : & ' a [ ( u32 , BlockHash ) ] ,
587+ /// The `block_id` to insert.
588+ to_insert : ( u32 , BlockHash ) ,
589+ /// The expected final checkpoint chain after calling [`CheckPoint::insert`].
590+ exp_final_chain : & ' a [ ( u32 , BlockHash ) ] ,
591+ }
592+
593+ let test_cases = [
594+ TestCase {
595+ name : "insert_above_tip" ,
596+ chain : & [ ( 1 , h ! ( "a" ) ) , ( 2 , h ! ( "b" ) ) ] ,
597+ to_insert : ( 4 , h ! ( "d" ) ) ,
598+ exp_final_chain : & [ ( 1 , h ! ( "a" ) ) , ( 2 , h ! ( "b" ) ) , ( 4 , h ! ( "d" ) ) ] ,
599+ } ,
600+ TestCase {
601+ name : "insert_already_exists_expect_no_change" ,
602+ chain : & [ ( 1 , h ! ( "a" ) ) , ( 2 , h ! ( "b" ) ) , ( 3 , h ! ( "c" ) ) ] ,
603+ to_insert : ( 2 , h ! ( "b" ) ) ,
604+ exp_final_chain : & [ ( 1 , h ! ( "a" ) ) , ( 2 , h ! ( "b" ) ) , ( 3 , h ! ( "c" ) ) ] ,
605+ } ,
606+ TestCase {
607+ name : "insert_in_middle" ,
608+ chain : & [ ( 2 , h ! ( "b" ) ) , ( 4 , h ! ( "d" ) ) , ( 5 , h ! ( "e" ) ) ] ,
609+ to_insert : ( 3 , h ! ( "c" ) ) ,
610+ exp_final_chain : & [ ( 2 , h ! ( "b" ) ) , ( 3 , h ! ( "c" ) ) , ( 4 , h ! ( "d" ) ) , ( 5 , h ! ( "e" ) ) ] ,
611+ } ,
612+ TestCase {
613+ name : "replace_one" ,
614+ chain : & [ ( 3 , h ! ( "c" ) ) , ( 4 , h ! ( "d" ) ) , ( 5 , h ! ( "e" ) ) ] ,
615+ to_insert : ( 5 , h ! ( "E" ) ) ,
616+ exp_final_chain : & [ ( 3 , h ! ( "c" ) ) , ( 4 , h ! ( "d" ) ) , ( 5 , h ! ( "E" ) ) ] ,
617+ } ,
618+ TestCase {
619+ name : "insert_conflict_should_evict" ,
620+ chain : & [ ( 3 , h ! ( "c" ) ) , ( 4 , h ! ( "d" ) ) , ( 5 , h ! ( "e" ) ) , ( 6 , h ! ( "f" ) ) ] ,
621+ to_insert : ( 4 , h ! ( "D" ) ) ,
622+ exp_final_chain : & [ ( 3 , h ! ( "c" ) ) , ( 4 , h ! ( "D" ) ) ] ,
623+ } ,
624+ ] ;
625+
626+ fn genesis_block ( ) -> impl Iterator < Item = BlockId > {
627+ core:: iter:: once ( ( 0 , h ! ( "_" ) ) ) . map ( BlockId :: from)
628+ }
629+
630+ for ( i, t) in test_cases. into_iter ( ) . enumerate ( ) {
631+ println ! ( "Running [{}] '{}'" , i, t. name) ;
632+
633+ let chain = CheckPoint :: from_block_ids (
634+ genesis_block ( ) . chain ( t. chain . iter ( ) . copied ( ) . map ( BlockId :: from) ) ,
635+ )
636+ . expect ( "test formed incorrectly, must construct checkpoint chain" ) ;
637+
638+ let exp_final_chain = CheckPoint :: from_block_ids (
639+ genesis_block ( ) . chain ( t. exp_final_chain . iter ( ) . copied ( ) . map ( BlockId :: from) ) ,
640+ )
641+ . expect ( "test formed incorrectly, must construct checkpoint chain" ) ;
642+
643+ assert_eq ! (
644+ chain. insert( t. to_insert. into( ) ) ,
645+ exp_final_chain,
646+ "unexpected final chain"
647+ ) ;
648+ }
649+ }
650+
580651#[ test]
581652fn local_chain_apply_header_connected_to ( ) {
582653 fn header_from_prev_blockhash ( prev_blockhash : BlockHash ) -> Header {
0 commit comments