t: move tests from reftable/record_test.c to the new unit test

common_prefix_size(), get_be24() and put_be24() are functions defined in reftable/basics.{c, h}. Move the tests for these functions from reftable/record_test.c to the newly ported test. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Chandra Pratap committed May 29, 2024 at 22:29 UTC afe5b9e7ec6dcc92751cae7f58a37457e3da96ac
2 files changed +33 -44
reftable/record_test.c
-37
@@ -64,31 +64,6 @@ static void test_varint_roundtrip(void)
64 }
65 }
66
67 -static void test_common_prefix(void)
68 -{
69 - struct {
70 - const char *a, *b;
71 - int want;
72 - } cases[] = {
73 - { "abc", "ab", 2 },
74 - { "", "abc", 0 },
75 - { "abc", "abd", 2 },
76 - { "abc", "pqr", 0 },
77 - };
78 -
79 - int i = 0;
80 - for (i = 0; i < ARRAY_SIZE(cases); i++) {
81 - struct strbuf a = STRBUF_INIT;
82 - struct strbuf b = STRBUF_INIT;
83 - strbuf_addstr(&a, cases[i].a);
84 - strbuf_addstr(&b, cases[i].b);
85 - EXPECT(common_prefix_size(&a, &b) == cases[i].want);
86 -
87 - strbuf_release(&a);
88 - strbuf_release(&b);
89 - }
90 -}
91 -
67 static void set_hash(uint8_t *h, int j)
68 {
69 int i = 0;
@@ -258,16 +233,6 @@ static void test_reftable_log_record_roundtrip(void)
233 strbuf_release(&scratch);
234 }
235
261 -static void test_u24_roundtrip(void)
262 -{
263 - uint32_t in = 0x112233;
264 - uint8_t dest[3];
265 - uint32_t out;
266 - put_be24(dest, in);
267 - out = get_be24(dest);
268 - EXPECT(in == out);
269 -}
270 -
236 static void test_key_roundtrip(void)
237 {
238 uint8_t buffer[1024] = { 0 };
@@ -411,9 +376,7 @@ int record_test_main(int argc, const char *argv[])
376 RUN_TEST(test_reftable_ref_record_roundtrip);
377 RUN_TEST(test_varint_roundtrip);
378 RUN_TEST(test_key_roundtrip);
414 - RUN_TEST(test_common_prefix);
379 RUN_TEST(test_reftable_obj_record_roundtrip);
380 RUN_TEST(test_reftable_index_record_roundtrip);
417 - RUN_TEST(test_u24_roundtrip);
381 return 0;
382 }
t/unit-tests/t-reftable-basics.c
+33 -7
@@ -99,13 +99,38 @@ static void test_parse_names_drop_empty(void)
99
100 static void test_common_prefix(void)
101 {
102 - struct strbuf s1 = STRBUF_INIT;
103 - struct strbuf s2 = STRBUF_INIT;
104 - strbuf_addstr(&s1, "abcdef");
105 - strbuf_addstr(&s2, "abc");
106 - check_int(common_prefix_size(&s1, &s2), ==, 3);
107 - strbuf_release(&s1);
108 - strbuf_release(&s2);
102 + struct strbuf a = STRBUF_INIT;
103 + struct strbuf b = STRBUF_INIT;
104 + struct {
105 + const char *a, *b;
106 + int want;
107 + } cases[] = {
108 + {"abcdef", "abc", 3},
109 + { "abc", "ab", 2 },
110 + { "", "abc", 0 },
111 + { "abc", "abd", 2 },
112 + { "abc", "pqr", 0 },
113 + };
114 +
115 + for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
116 + strbuf_addstr(&a, cases[i].a);
117 + strbuf_addstr(&b, cases[i].b);
118 + check_int(common_prefix_size(&a, &b), ==, cases[i].want);
119 + strbuf_reset(&a);
120 + strbuf_reset(&b);
121 + }
122 + strbuf_release(&a);
123 + strbuf_release(&b);
124 +}
125 +
126 +static void test_u24_roundtrip(void)
127 +{
128 + uint32_t in = 0x112233;
129 + uint8_t dest[3];
130 + uint32_t out;
131 + put_be24(dest, in);
132 + out = get_be24(dest);
133 + check_int(in, ==, out);
134 }
135
136 int cmd_main(int argc, const char *argv[])
@@ -116,6 +141,7 @@ int cmd_main(int argc, const char *argv[])
141 TEST(test_binsearch(), "binary search with binsearch works");
142 TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
143 TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
144 + TEST(test_u24_roundtrip(), "put_be24 and get_be24 work");
145
146 return test_done();
147 }