@@ -649,6 +649,44 @@ static bool test__cs_add__dupe_keep_last_with_multiple_entries(void) {
649649 return true;
650650}
651651
652+ static bool check_order (struct clip_store * cs , uint64_t * order , size_t len ) {
653+ _drop_ (cs_unref ) struct ref_guard guard = cs_ref (cs );
654+ struct cs_snip * snip = NULL ;
655+ for (size_t i = 0 ; i < len ; ++ i ) {
656+ bool iter_ret = cs_snip_iter (& guard , CS_ITER_NEWEST_FIRST , & snip );
657+ t_assert (iter_ret == true);
658+ t_assert (snip -> hash == order [i ]);
659+ }
660+ return 0 ;
661+ }
662+
663+ /* After calling cs_make_newest make sure the entry is at the newest slot while
664+ * other entries remain in order. */
665+ static bool test__cs_make_newest (void ) {
666+ _drop_ (teardown_test ) struct clip_store cs = setup_test ();
667+
668+ uint64_t hash_a , hash_b , hash_c ;
669+ int ret = cs_add (& cs , "A" , & hash_a , CS_DUPE_KEEP_ALL );
670+ t_assert (ret == 0 );
671+ ret = cs_add (& cs , "B" , & hash_b , CS_DUPE_KEEP_ALL );
672+ t_assert (ret == 0 );
673+ ret = cs_add (& cs , "C" , & hash_c , CS_DUPE_KEEP_ALL );
674+ t_assert (ret == 0 );
675+ t_assert (cs .header -> nr_snips == 3 );
676+
677+ uint64_t order_before [3 ] = { hash_c , hash_b , hash_a };
678+ ret = check_order (& cs , order_before , 3 );
679+ t_assert (ret == 0 );
680+ /* Now the order should change to ["A", "C", "B"] */
681+ ret = cs_make_newest (& cs , hash_a );
682+ t_assert (ret == 0 );
683+ uint64_t order_after [3 ] = { hash_a , hash_c , hash_b };
684+ ret = check_order (& cs , order_after , 3 );
685+ t_assert (ret == 0 );
686+
687+ return true;
688+ }
689+
652690int main (void ) {
653691 t_run (test__cs_init );
654692 t_run (test__cs_init__bad_size );
@@ -676,6 +714,7 @@ int main(void) {
676714 t_run (test__cs_add__dupe_keep_all );
677715 t_run (test__cs_add__dupe_keep_last );
678716 t_run (test__cs_add__dupe_keep_last_with_multiple_entries );
717+ t_run (test__cs_make_newest );
679718
680719 return 0 ;
681720}
0 commit comments