t: move reftable/record_test.c to the unit testing framework

reftable/record_test.c exercises the functions defined in reftable/record.{c, h}. Migrate reftable/record_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework, and renaming the tests to fit unit-tests' naming scheme. While at it, change the type of index variable 'i' to 'size_t' from 'int'. This is because 'i' is used in comparison against 'ARRAY_SIZE(x)' which is of type 'size_t'. Also, use set_hash() which is defined locally in the test file instead of set_test_hash() which is defined by reftable/test_framework.{c, h}. This is fine to do as both these functions are similarly implemented, and reftable/test_framework.{c, h} is not #included in the ported test. Get rid of reftable_record_print() from the tests as well, because it clutters the test framework's output and we have no way of verifying the output. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com> Acked-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Chandra Pratap committed Jul 2, 2024 at 12:52 UTC ba9661b457effa34b6f6188e10546d00e4a49d54
3 files changed +61 -73
Makefile
+1 -1
@@ -1338,6 +1338,7 @@ UNIT_TEST_PROGRAMS += t-hash
1338 UNIT_TEST_PROGRAMS += t-mem-pool
1339 UNIT_TEST_PROGRAMS += t-prio-queue
1340 UNIT_TEST_PROGRAMS += t-reftable-basics
1341 +UNIT_TEST_PROGRAMS += t-reftable-record
1342 UNIT_TEST_PROGRAMS += t-strbuf
1343 UNIT_TEST_PROGRAMS += t-strcmp-offset
1344 UNIT_TEST_PROGRAMS += t-strvec
@@ -2678,7 +2679,6 @@ REFTABLE_TEST_OBJS += reftable/block_test.o
2679 REFTABLE_TEST_OBJS += reftable/dump.o
2680 REFTABLE_TEST_OBJS += reftable/merged_test.o
2681 REFTABLE_TEST_OBJS += reftable/pq_test.o
2681 -REFTABLE_TEST_OBJS += reftable/record_test.o
2682 REFTABLE_TEST_OBJS += reftable/readwrite_test.o
2683 REFTABLE_TEST_OBJS += reftable/stack_test.o
2684 REFTABLE_TEST_OBJS += reftable/test_framework.o
t/helper/test-reftable.c
-1
@@ -5,7 +5,6 @@
5 int cmd__reftable(int argc, const char **argv)
6 {
7 /* test from simple to complex. */
8 - record_test_main(argc, argv);
8 block_test_main(argc, argv);
9 tree_test_main(argc, argv);
10 pq_test_main(argc, argv);
t/unit-tests/t-reftable-record.c renamed
+60 -71
@@ -6,15 +6,11 @@
6 https://developers.google.com/open-source/licenses/bsd
7 */
8
9 -#include "record.h"
9 +#include "test-lib.h"
10 +#include "reftable/constants.h"
11 +#include "reftable/record.h"
12
11 -#include "system.h"
12 -#include "basics.h"
13 -#include "constants.h"
14 -#include "test_framework.h"
15 -#include "reftable-tests.h"
16 -
17 -static void test_copy(struct reftable_record *rec)
13 +static void t_copy(struct reftable_record *rec)
14 {
15 struct reftable_record copy;
16 uint8_t typ;
@@ -24,15 +20,12 @@ static void test_copy(struct reftable_record *rec)
20 reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
21 /* do it twice to catch memory leaks */
22 reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
27 - EXPECT(reftable_record_equal(rec, &copy, GIT_SHA1_RAWSZ));
28 -
29 - puts("testing print coverage:\n");
30 - reftable_record_print(&copy, GIT_SHA1_RAWSZ);
23 + check(reftable_record_equal(rec, &copy, GIT_SHA1_RAWSZ));
24
25 reftable_record_release(&copy);
26 }
27
35 -static void test_varint_roundtrip(void)
28 +static void t_varint_roundtrip(void)
29 {
30 uint64_t inputs[] = { 0,
31 1,
@@ -43,8 +36,8 @@ static void test_varint_roundtrip(void)
36 4096,
37 ((uint64_t)1 << 63),
38 ((uint64_t)1 << 63) + ((uint64_t)1 << 63) - 1 };
46 - int i = 0;
47 - for (i = 0; i < ARRAY_SIZE(inputs); i++) {
39 +
40 + for (size_t i = 0; i < ARRAY_SIZE(inputs); i++) {
41 uint8_t dest[10];
42
43 struct string_view out = {
@@ -55,29 +48,26 @@ static void test_varint_roundtrip(void)
48 int n = put_var_int(&out, in);
49 uint64_t got = 0;
50
58 - EXPECT(n > 0);
51 + check_int(n, >, 0);
52 out.len = n;
53 n = get_var_int(&got, &out);
61 - EXPECT(n > 0);
54 + check_int(n, >, 0);
55
63 - EXPECT(got == in);
56 + check_int(got, ==, in);
57 }
58 }
59
60 static void set_hash(uint8_t *h, int j)
61 {
69 - int i = 0;
70 - for (i = 0; i < hash_size(GIT_SHA1_FORMAT_ID); i++) {
62 + for (int i = 0; i < hash_size(GIT_SHA1_FORMAT_ID); i++)
63 h[i] = (j >> i) & 0xff;
72 - }
64 }
65
75 -static void test_reftable_ref_record_roundtrip(void)
66 +static void t_reftable_ref_record_roundtrip(void)
67 {
68 struct strbuf scratch = STRBUF_INIT;
78 - int i = 0;
69
80 - for (i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) {
70 + for (int i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) {
71 struct reftable_record in = {
72 .type = BLOCK_TYPE_REF,
73 };
@@ -107,19 +97,19 @@ static void test_reftable_ref_record_roundtrip(void)
97 }
98 in.u.ref.refname = xstrdup("refs/heads/master");
99
110 - test_copy(&in);
100 + t_copy(&in);
101
112 - EXPECT(reftable_record_val_type(&in) == i);
102 + check_int(reftable_record_val_type(&in), ==, i);
103
104 reftable_record_key(&in, &key);
105 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
116 - EXPECT(n > 0);
106 + check_int(n, >, 0);
107
108 /* decode into a non-zero reftable_record to test for leaks. */
109 m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ, &scratch);
120 - EXPECT(n == m);
110 + check_int(n, ==, m);
111
122 - EXPECT(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
112 + check(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
113 GIT_SHA1_RAWSZ));
114 reftable_record_release(&in);
115
@@ -130,7 +120,7 @@ static void test_reftable_ref_record_roundtrip(void)
120 strbuf_release(&scratch);
121 }
122
133 -static void test_reftable_log_record_equal(void)
123 +static void t_reftable_log_record_equal(void)
124 {
125 struct reftable_log_record in[2] = {
126 {
@@ -143,16 +133,15 @@ static void test_reftable_log_record_equal(void)
133 }
134 };
135
146 - EXPECT(!reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
136 + check(!reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
137 in[1].update_index = in[0].update_index;
148 - EXPECT(reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
138 + check(reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
139 reftable_log_record_release(&in[0]);
140 reftable_log_record_release(&in[1]);
141 }
142
153 -static void test_reftable_log_record_roundtrip(void)
143 +static void t_reftable_log_record_roundtrip(void)
144 {
155 - int i;
145 struct reftable_log_record in[] = {
146 {
147 .refname = xstrdup("refs/heads/master"),
@@ -180,12 +169,12 @@ static void test_reftable_log_record_roundtrip(void)
169 }
170 };
171 struct strbuf scratch = STRBUF_INIT;
172 + set_hash(in[0].value.update.new_hash, 1);
173 + set_hash(in[0].value.update.old_hash, 2);
174 + set_hash(in[2].value.update.new_hash, 3);
175 + set_hash(in[2].value.update.old_hash, 4);
176
184 - set_test_hash(in[0].value.update.new_hash, 1);
185 - set_test_hash(in[0].value.update.old_hash, 2);
186 - set_test_hash(in[2].value.update.new_hash, 3);
187 - set_test_hash(in[2].value.update.old_hash, 4);
188 - for (i = 0; i < ARRAY_SIZE(in); i++) {
177 + for (size_t i = 0; i < ARRAY_SIZE(in); i++) {
178 struct reftable_record rec = { .type = BLOCK_TYPE_LOG };
179 struct strbuf key = STRBUF_INIT;
180 uint8_t buffer[1024] = { 0 };
@@ -212,18 +201,18 @@ static void test_reftable_log_record_roundtrip(void)
201
202 rec.u.log = in[i];
203
215 - test_copy(&rec);
204 + t_copy(&rec);
205
206 reftable_record_key(&rec, &key);
207
208 n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
220 - EXPECT(n >= 0);
209 + check_int(n, >=, 0);
210 valtype = reftable_record_val_type(&rec);
211 m = reftable_record_decode(&out, key, valtype, dest,
212 GIT_SHA1_RAWSZ, &scratch);
224 - EXPECT(n == m);
213 + check_int(n, ==, m);
214
226 - EXPECT(reftable_log_record_equal(&in[i], &out.u.log,
215 + check(reftable_log_record_equal(&in[i], &out.u.log,
216 GIT_SHA1_RAWSZ));
217 reftable_log_record_release(&in[i]);
218 strbuf_release(&key);
@@ -233,7 +222,7 @@ static void test_reftable_log_record_roundtrip(void)
222 strbuf_release(&scratch);
223 }
224
236 -static void test_key_roundtrip(void)
225 +static void t_key_roundtrip(void)
226 {
227 uint8_t buffer[1024] = { 0 };
228 struct string_view dest = {
@@ -252,21 +241,21 @@ static void test_key_roundtrip(void)
241 strbuf_addstr(&key, "refs/tags/bla");
242 extra = 6;
243 n = reftable_encode_key(&restart, dest, last_key, key, extra);
255 - EXPECT(!restart);
256 - EXPECT(n > 0);
244 + check(!restart);
245 + check_int(n, >, 0);
246
247 strbuf_addstr(&roundtrip, "refs/heads/master");
248 m = reftable_decode_key(&roundtrip, &rt_extra, dest);
260 - EXPECT(n == m);
261 - EXPECT(0 == strbuf_cmp(&key, &roundtrip));
262 - EXPECT(rt_extra == extra);
249 + check_int(n, ==, m);
250 + check(!strbuf_cmp(&key, &roundtrip));
251 + check_int(rt_extra, ==, extra);
252
253 strbuf_release(&last_key);
254 strbuf_release(&key);
255 strbuf_release(&roundtrip);
256 }
257
269 -static void test_reftable_obj_record_roundtrip(void)
258 +static void t_reftable_obj_record_roundtrip(void)
259 {
260 uint8_t testHash1[GIT_SHA1_RAWSZ] = { 1, 2, 3, 4, 0 };
261 uint64_t till9[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
@@ -289,9 +278,8 @@ static void test_reftable_obj_record_roundtrip(void)
278 },
279 };
280 struct strbuf scratch = STRBUF_INIT;
292 - int i = 0;
281
294 - for (i = 0; i < ARRAY_SIZE(recs); i++) {
282 + for (size_t i = 0; i < ARRAY_SIZE(recs); i++) {
283 uint8_t buffer[1024] = { 0 };
284 struct string_view dest = {
285 .buf = buffer,
@@ -308,16 +296,16 @@ static void test_reftable_obj_record_roundtrip(void)
296 int n, m;
297 uint8_t extra;
298
311 - test_copy(&in);
299 + t_copy(&in);
300 reftable_record_key(&in, &key);
301 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
314 - EXPECT(n > 0);
302 + check_int(n, >, 0);
303 extra = reftable_record_val_type(&in);
304 m = reftable_record_decode(&out, key, extra, dest,
305 GIT_SHA1_RAWSZ, &scratch);
318 - EXPECT(n == m);
306 + check_int(n, ==, m);
307
320 - EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
308 + check(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
309 strbuf_release(&key);
310 reftable_record_release(&out);
311 }
@@ -325,7 +313,7 @@ static void test_reftable_obj_record_roundtrip(void)
313 strbuf_release(&scratch);
314 }
315
328 -static void test_reftable_index_record_roundtrip(void)
316 +static void t_reftable_index_record_roundtrip(void)
317 {
318 struct reftable_record in = {
319 .type = BLOCK_TYPE_INDEX,
@@ -350,18 +338,18 @@ static void test_reftable_index_record_roundtrip(void)
338
339 strbuf_addstr(&in.u.idx.last_key, "refs/heads/master");
340 reftable_record_key(&in, &key);
353 - test_copy(&in);
341 + t_copy(&in);
342
355 - EXPECT(0 == strbuf_cmp(&key, &in.u.idx.last_key));
343 + check(!strbuf_cmp(&key, &in.u.idx.last_key));
344 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
357 - EXPECT(n > 0);
345 + check_int(n, >, 0);
346
347 extra = reftable_record_val_type(&in);
348 m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ,
349 &scratch);
362 - EXPECT(m == n);
350 + check_int(m, ==, n);
351
364 - EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
352 + check(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
353
354 reftable_record_release(&out);
355 strbuf_release(&key);
@@ -369,14 +357,15 @@ static void test_reftable_index_record_roundtrip(void)
357 strbuf_release(&in.u.idx.last_key);
358 }
359
372 -int record_test_main(int argc, const char *argv[])
360 +int cmd_main(int argc, const char *argv[])
361 {
374 - RUN_TEST(test_reftable_log_record_equal);
375 - RUN_TEST(test_reftable_log_record_roundtrip);
376 - RUN_TEST(test_reftable_ref_record_roundtrip);
377 - RUN_TEST(test_varint_roundtrip);
378 - RUN_TEST(test_key_roundtrip);
379 - RUN_TEST(test_reftable_obj_record_roundtrip);
380 - RUN_TEST(test_reftable_index_record_roundtrip);
381 - return 0;
362 + TEST(t_reftable_log_record_equal(), "reftable_log_record_equal works");
363 + TEST(t_reftable_log_record_roundtrip(), "record operations work on log record");
364 + TEST(t_reftable_ref_record_roundtrip(), "record operations work on ref record");
365 + TEST(t_varint_roundtrip(), "put_var_int and get_var_int work");
366 + TEST(t_key_roundtrip(), "reftable_encode_key and reftable_decode_key work");
367 + TEST(t_reftable_obj_record_roundtrip(), "record operations work on obj record");
368 + TEST(t_reftable_index_record_roundtrip(), "record operations work on index record");
369 +
370 + return test_done();
371 }