reftable: add test for length of disambiguating prefix

The ID => ref map is trimming object IDs to a disambiguating prefix. Check that we are computing their length correctly. Signed-off-by: Han-Wen Nienhuys <hanwen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Han-Wen Nienhuys committed Feb 21, 2022 at 18:46 UTC 3c443a02a9f98227fa5c22935d88bcf68cfeaeec
1 file changed +38
reftable/readwrite_test.c
+38
@@ -703,6 +703,43 @@ static void test_write_object_id_min_length(void)
703 strbuf_release(&buf);
704 }
705
706 +static void test_write_object_id_length(void)
707 +{
708 + struct reftable_write_options opts = {
709 + .block_size = 75,
710 + };
711 + struct strbuf buf = STRBUF_INIT;
712 + struct reftable_writer *w =
713 + reftable_new_writer(&strbuf_add_void, &buf, &opts);
714 + uint8_t hash[GIT_SHA1_RAWSZ] = {42};
715 + struct reftable_ref_record ref = {
716 + .update_index = 1,
717 + .value_type = REFTABLE_REF_VAL1,
718 + .value.val1 = hash,
719 + };
720 + int err;
721 + int i;
722 +
723 + reftable_writer_set_limits(w, 1, 1);
724 +
725 + /* Write the same hash in many refs. If there is only 1 hash, the
726 + * disambiguating prefix is length 0 */
727 + for (i = 0; i < 256; i++) {
728 + char name[256];
729 + snprintf(name, sizeof(name), "ref%05d", i);
730 + ref.refname = name;
731 + ref.value.val1[15] = i;
732 + err = reftable_writer_add_ref(w, &ref);
733 + EXPECT_ERR(err);
734 + }
735 +
736 + err = reftable_writer_close(w);
737 + EXPECT_ERR(err);
738 + EXPECT(writer_stats(w)->object_id_len == 16);
739 + reftable_writer_free(w);
740 + strbuf_release(&buf);
741 +}
742 +
743 static void test_write_empty_key(void)
744 {
745 struct reftable_write_options opts = { 0 };
@@ -808,6 +845,7 @@ int readwrite_test_main(int argc, const char *argv[])
845 RUN_TEST(test_write_empty_key);
846 RUN_TEST(test_write_empty_table);
847 RUN_TEST(test_log_overflow);
848 + RUN_TEST(test_write_object_id_length);
849 RUN_TEST(test_write_object_id_min_length);
850 return 0;
851 }