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

reftable/readwrite_test.c exercises the functions defined in reftable/reader.{c,h} and reftable/writer.{c,h}. Migrate reftable/readwrite_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 align with unit-tests' naming conventions. Since some tests in reftable/readwrite_test.c use the functions set_test_hash(), noop_flush() and strbuf_add_void() defined in reftable/test_framework.{c,h} but these files are not #included in the ported unit test, copy these functions in the new test file. While at it, ensure structs are 0-initialized with '= { 0 }' instead of '= { NULL }'. 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 Aug 13, 2024 at 20:04 UTC 5b539a53613355a22f5c525eebdd9117fdaedb90
4 files changed +210 -212
Makefile
+1 -1
@@ -1341,6 +1341,7 @@ UNIT_TEST_PROGRAMS += t-oidtree
1341 UNIT_TEST_PROGRAMS += t-prio-queue
1342 UNIT_TEST_PROGRAMS += t-reftable-basics
1343 UNIT_TEST_PROGRAMS += t-reftable-merged
1344 +UNIT_TEST_PROGRAMS += t-reftable-readwrite
1345 UNIT_TEST_PROGRAMS += t-reftable-record
1346 UNIT_TEST_PROGRAMS += t-strbuf
1347 UNIT_TEST_PROGRAMS += t-strcmp-offset
@@ -2682,7 +2683,6 @@ REFTABLE_OBJS += reftable/writer.o
2683 REFTABLE_TEST_OBJS += reftable/block_test.o
2684 REFTABLE_TEST_OBJS += reftable/dump.o
2685 REFTABLE_TEST_OBJS += reftable/pq_test.o
2685 -REFTABLE_TEST_OBJS += reftable/readwrite_test.o
2686 REFTABLE_TEST_OBJS += reftable/stack_test.o
2687 REFTABLE_TEST_OBJS += reftable/test_framework.o
2688 REFTABLE_TEST_OBJS += reftable/tree_test.o
reftable/reftable-tests.h
-1
@@ -13,7 +13,6 @@ int basics_test_main(int argc, const char **argv);
13 int block_test_main(int argc, const char **argv);
14 int pq_test_main(int argc, const char **argv);
15 int record_test_main(int argc, const char **argv);
16 -int readwrite_test_main(int argc, const char **argv);
16 int stack_test_main(int argc, const char **argv);
17 int tree_test_main(int argc, const char **argv);
18 int reftable_dump_main(int argc, char *const *argv);
t/helper/test-reftable.c
-1
@@ -8,7 +8,6 @@ int cmd__reftable(int argc, const char **argv)
8 block_test_main(argc, argv);
9 tree_test_main(argc, argv);
10 pq_test_main(argc, argv);
11 - readwrite_test_main(argc, argv);
11 stack_test_main(argc, argv);
12 return 0;
13 }
t/unit-tests/t-reftable-readwrite.c renamed
+209 -209
@@ -6,37 +6,48 @@ license that can be found in the LICENSE file or at
6 https://developers.google.com/open-source/licenses/bsd
7 */
8
9 -#include "system.h"
10 -
11 -#include "basics.h"
12 -#include "block.h"
13 -#include "blocksource.h"
14 -#include "reader.h"
15 -#include "record.h"
16 -#include "test_framework.h"
17 -#include "reftable-tests.h"
18 -#include "reftable-writer.h"
9 +#include "test-lib.h"
10 +#include "reftable/blocksource.h"
11 +#include "reftable/reader.h"
12 +#include "reftable/reftable-error.h"
13 +#include "reftable/reftable-writer.h"
14
15 static const int update_index = 5;
16
22 -static void test_buffer(void)
17 +static void set_test_hash(uint8_t *p, int i)
18 +{
19 + memset(p, (uint8_t)i, hash_size(GIT_SHA1_FORMAT_ID));
20 +}
21 +
22 +static ssize_t strbuf_add_void(void *b, const void *data, size_t sz)
23 +{
24 + strbuf_add(b, data, sz);
25 + return sz;
26 +}
27 +
28 +static int noop_flush(void *arg)
29 +{
30 + return 0;
31 +}
32 +
33 +static void t_buffer(void)
34 {
35 struct strbuf buf = STRBUF_INIT;
25 - struct reftable_block_source source = { NULL };
26 - struct reftable_block out = { NULL };
36 + struct reftable_block_source source = { 0 };
37 + struct reftable_block out = { 0 };
38 int n;
39 uint8_t in[] = "hello";
40 strbuf_add(&buf, in, sizeof(in));
41 block_source_from_strbuf(&source, &buf);
31 - EXPECT(block_source_size(&source) == 6);
42 + check_int(block_source_size(&source), ==, 6);
43 n = block_source_read_block(&source, &out, 0, sizeof(in));
33 - EXPECT(n == sizeof(in));
34 - EXPECT(!memcmp(in, out.data, n));
44 + check_int(n, ==, sizeof(in));
45 + check(!memcmp(in, out.data, n));
46 reftable_block_done(&out);
47
48 n = block_source_read_block(&source, &out, 1, 2);
38 - EXPECT(n == 2);
39 - EXPECT(!memcmp(out.data, "el", 2));
49 + check_int(n, ==, 2);
50 + check(!memcmp(out.data, "el", 2));
51
52 reftable_block_done(&out);
53 block_source_close(&source);
@@ -52,9 +63,9 @@ static void write_table(char ***names, struct strbuf *buf, int N,
63 };
64 struct reftable_writer *w =
65 reftable_new_writer(&strbuf_add_void, &noop_flush, buf, &opts);
55 - struct reftable_ref_record ref = { NULL };
66 + struct reftable_ref_record ref = { 0 };
67 int i = 0, n;
57 - struct reftable_log_record log = { NULL };
68 + struct reftable_log_record log = { 0 };
69 const struct reftable_stats *stats = NULL;
70
71 REFTABLE_CALLOC_ARRAY(*names, N + 1);
@@ -73,7 +84,7 @@ static void write_table(char ***names, struct strbuf *buf, int N,
84 (*names)[i] = xstrdup(name);
85
86 n = reftable_writer_add_ref(w, &ref);
76 - EXPECT(n == 0);
87 + check_int(n, ==, 0);
88 }
89
90 for (i = 0; i < N; i++) {
@@ -89,27 +100,25 @@ static void write_table(char ***names, struct strbuf *buf, int N,
100 log.value.update.message = (char *) "message";
101
102 n = reftable_writer_add_log(w, &log);
92 - EXPECT(n == 0);
103 + check_int(n, ==, 0);
104 }
105
106 n = reftable_writer_close(w);
96 - EXPECT(n == 0);
107 + check_int(n, ==, 0);
108
109 stats = reftable_writer_stats(w);
110 for (i = 0; i < stats->ref_stats.blocks; i++) {
111 int off = i * opts.block_size;
101 - if (off == 0) {
102 - off = header_size(
103 - (hash_id == GIT_SHA256_FORMAT_ID) ? 2 : 1);
104 - }
105 - EXPECT(buf->buf[off] == 'r');
112 + if (!off)
113 + off = header_size((hash_id == GIT_SHA256_FORMAT_ID) ? 2 : 1);
114 + check_char(buf->buf[off], ==, 'r');
115 }
116
108 - EXPECT(stats->log_stats.blocks > 0);
117 + check_int(stats->log_stats.blocks, >, 0);
118 reftable_writer_free(w);
119 }
120
112 -static void test_log_buffer_size(void)
121 +static void t_log_buffer_size(void)
122 {
123 struct strbuf buf = STRBUF_INIT;
124 struct reftable_write_options opts = {
@@ -140,14 +149,14 @@ static void test_log_buffer_size(void)
149 }
150 reftable_writer_set_limits(w, update_index, update_index);
151 err = reftable_writer_add_log(w, &log);
143 - EXPECT_ERR(err);
152 + check(!err);
153 err = reftable_writer_close(w);
145 - EXPECT_ERR(err);
154 + check(!err);
155 reftable_writer_free(w);
156 strbuf_release(&buf);
157 }
158
150 -static void test_log_overflow(void)
159 +static void t_log_overflow(void)
160 {
161 struct strbuf buf = STRBUF_INIT;
162 char msg[256] = { 0 };
@@ -177,12 +186,12 @@ static void test_log_overflow(void)
186 memset(msg, 'x', sizeof(msg) - 1);
187 reftable_writer_set_limits(w, update_index, update_index);
188 err = reftable_writer_add_log(w, &log);
180 - EXPECT(err == REFTABLE_ENTRY_TOO_BIG_ERROR);
189 + check_int(err, ==, REFTABLE_ENTRY_TOO_BIG_ERROR);
190 reftable_writer_free(w);
191 strbuf_release(&buf);
192 }
193
185 -static void test_log_write_read(void)
194 +static void t_log_write_read(void)
195 {
196 int N = 2;
197 char **names = reftable_calloc(N + 1, sizeof(*names));
@@ -190,13 +199,13 @@ static void test_log_write_read(void)
199 struct reftable_write_options opts = {
200 .block_size = 256,
201 };
193 - struct reftable_ref_record ref = { NULL };
202 + struct reftable_ref_record ref = { 0 };
203 int i = 0;
195 - struct reftable_log_record log = { NULL };
204 + struct reftable_log_record log = { 0 };
205 int n;
197 - struct reftable_iterator it = { NULL };
198 - struct reftable_reader rd = { NULL };
199 - struct reftable_block_source source = { NULL };
206 + struct reftable_iterator it = { 0 };
207 + struct reftable_reader rd = { 0 };
208 + struct reftable_block_source source = { 0 };
209 struct strbuf buf = STRBUF_INIT;
210 struct reftable_writer *w =
211 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
@@ -204,17 +213,17 @@ static void test_log_write_read(void)
213 reftable_writer_set_limits(w, 0, N);
214 for (i = 0; i < N; i++) {
215 char name[256];
207 - struct reftable_ref_record ref = { NULL };
216 + struct reftable_ref_record ref = { 0 };
217 snprintf(name, sizeof(name), "b%02d%0*d", i, 130, 7);
218 names[i] = xstrdup(name);
219 ref.refname = name;
220 ref.update_index = i;
221
222 err = reftable_writer_add_ref(w, &ref);
214 - EXPECT_ERR(err);
223 + check(!err);
224 }
225 for (i = 0; i < N; i++) {
217 - struct reftable_log_record log = { NULL };
226 + struct reftable_log_record log = { 0 };
227
228 log.refname = names[i];
229 log.update_index = i;
@@ -223,33 +232,33 @@ static void test_log_write_read(void)
232 set_test_hash(log.value.update.new_hash, i + 1);
233
234 err = reftable_writer_add_log(w, &log);
226 - EXPECT_ERR(err);
235 + check(!err);
236 }
237
238 n = reftable_writer_close(w);
230 - EXPECT(n == 0);
239 + check_int(n, ==, 0);
240
241 stats = reftable_writer_stats(w);
233 - EXPECT(stats->log_stats.blocks > 0);
242 + check_int(stats->log_stats.blocks, >, 0);
243 reftable_writer_free(w);
244 w = NULL;
245
246 block_source_from_strbuf(&source, &buf);
247
248 err = init_reader(&rd, &source, "file.log");
240 - EXPECT_ERR(err);
249 + check(!err);
250
251 reftable_reader_init_ref_iterator(&rd, &it);
252
253 err = reftable_iterator_seek_ref(&it, names[N - 1]);
245 - EXPECT_ERR(err);
254 + check(!err);
255
256 err = reftable_iterator_next_ref(&it, &ref);
248 - EXPECT_ERR(err);
257 + check(!err);
258
259 /* end of iteration. */
260 err = reftable_iterator_next_ref(&it, &ref);
252 - EXPECT(0 < err);
261 + check_int(err, >, 0);
262
263 reftable_iterator_destroy(&it);
264 reftable_ref_record_release(&ref);
@@ -257,23 +266,21 @@ static void test_log_write_read(void)
266 reftable_reader_init_log_iterator(&rd, &it);
267
268 err = reftable_iterator_seek_log(&it, "");
260 - EXPECT_ERR(err);
269 + check(!err);
270
271 i = 0;
272 while (1) {
273 int err = reftable_iterator_next_log(&it, &log);
265 - if (err > 0) {
274 + if (err > 0)
275 break;
267 - }
268 -
269 - EXPECT_ERR(err);
270 - EXPECT_STREQ(names[i], log.refname);
271 - EXPECT(i == log.update_index);
276 + check(!err);
277 + check_str(names[i], log.refname);
278 + check_int(i, ==, log.update_index);
279 i++;
280 reftable_log_record_release(&log);
281 }
282
276 - EXPECT(i == N);
283 + check_int(i, ==, N);
284 reftable_iterator_destroy(&it);
285
286 /* cleanup. */
@@ -282,7 +289,7 @@ static void test_log_write_read(void)
289 reader_close(&rd);
290 }
291
285 -static void test_log_zlib_corruption(void)
292 +static void t_log_zlib_corruption(void)
293 {
294 struct reftable_write_options opts = {
295 .block_size = 256,
@@ -316,13 +323,13 @@ static void test_log_zlib_corruption(void)
323 reftable_writer_set_limits(w, 1, 1);
324
325 err = reftable_writer_add_log(w, &log);
319 - EXPECT_ERR(err);
326 + check(!err);
327
328 n = reftable_writer_close(w);
322 - EXPECT(n == 0);
329 + check_int(n, ==, 0);
330
331 stats = reftable_writer_stats(w);
325 - EXPECT(stats->log_stats.blocks > 0);
332 + check_int(stats->log_stats.blocks, >, 0);
333 reftable_writer_free(w);
334 w = NULL;
335
@@ -332,11 +339,11 @@ static void test_log_zlib_corruption(void)
339 block_source_from_strbuf(&source, &buf);
340
341 err = init_reader(&rd, &source, "file.log");
335 - EXPECT_ERR(err);
342 + check(!err);
343
344 reftable_reader_init_log_iterator(&rd, &it);
345 err = reftable_iterator_seek_log(&it, "refname");
339 - EXPECT(err == REFTABLE_ZLIB_ERROR);
346 + check_int(err, ==, REFTABLE_ZLIB_ERROR);
347
348 reftable_iterator_destroy(&it);
349
@@ -345,14 +352,14 @@ static void test_log_zlib_corruption(void)
352 reader_close(&rd);
353 }
354
348 -static void test_table_read_write_sequential(void)
355 +static void t_table_read_write_sequential(void)
356 {
357 char **names;
358 struct strbuf buf = STRBUF_INIT;
359 int N = 50;
353 - struct reftable_iterator it = { NULL };
354 - struct reftable_block_source source = { NULL };
355 - struct reftable_reader rd = { NULL };
360 + struct reftable_iterator it = { 0 };
361 + struct reftable_block_source source = { 0 };
362 + struct reftable_reader rd = { 0 };
363 int err = 0;
364 int j = 0;
365
@@ -361,26 +368,25 @@ static void test_table_read_write_sequential(void)
368 block_source_from_strbuf(&source, &buf);
369
370 err = init_reader(&rd, &source, "file.ref");
364 - EXPECT_ERR(err);
371 + check(!err);
372
373 reftable_reader_init_ref_iterator(&rd, &it);
374 err = reftable_iterator_seek_ref(&it, "");
368 - EXPECT_ERR(err);
375 + check(!err);
376
377 while (1) {
371 - struct reftable_ref_record ref = { NULL };
378 + struct reftable_ref_record ref = { 0 };
379 int r = reftable_iterator_next_ref(&it, &ref);
373 - EXPECT(r >= 0);
374 - if (r > 0) {
380 + check_int(r, >=, 0);
381 + if (r > 0)
382 break;
376 - }
377 - EXPECT(0 == strcmp(names[j], ref.refname));
378 - EXPECT(update_index == ref.update_index);
383 + check_str(names[j], ref.refname);
384 + check_int(update_index, ==, ref.update_index);
385
386 j++;
387 reftable_ref_record_release(&ref);
388 }
383 - EXPECT(j == N);
389 + check_int(j, ==, N);
390 reftable_iterator_destroy(&it);
391 strbuf_release(&buf);
392 free_names(names);
@@ -388,90 +394,88 @@ static void test_table_read_write_sequential(void)
394 reader_close(&rd);
395 }
396
391 -static void test_table_write_small_table(void)
397 +static void t_table_write_small_table(void)
398 {
399 char **names;
400 struct strbuf buf = STRBUF_INIT;
401 int N = 1;
402 write_table(&names, &buf, N, 4096, GIT_SHA1_FORMAT_ID);
397 - EXPECT(buf.len < 200);
403 + check_int(buf.len, <, 200);
404 strbuf_release(&buf);
405 free_names(names);
406 }
407
402 -static void test_table_read_api(void)
408 +static void t_table_read_api(void)
409 {
410 char **names;
411 struct strbuf buf = STRBUF_INIT;
412 int N = 50;
407 - struct reftable_reader rd = { NULL };
408 - struct reftable_block_source source = { NULL };
413 + struct reftable_reader rd = { 0 };
414 + struct reftable_block_source source = { 0 };
415 int err;
416 int i;
411 - struct reftable_log_record log = { NULL };
412 - struct reftable_iterator it = { NULL };
417 + struct reftable_log_record log = { 0 };
418 + struct reftable_iterator it = { 0 };
419
420 write_table(&names, &buf, N, 256, GIT_SHA1_FORMAT_ID);
421
422 block_source_from_strbuf(&source, &buf);
423
424 err = init_reader(&rd, &source, "file.ref");
419 - EXPECT_ERR(err);
425 + check(!err);
426
427 reftable_reader_init_ref_iterator(&rd, &it);
428 err = reftable_iterator_seek_ref(&it, names[0]);
423 - EXPECT_ERR(err);
429 + check(!err);
430
431 err = reftable_iterator_next_log(&it, &log);
426 - EXPECT(err == REFTABLE_API_ERROR);
432 + check_int(err, ==, REFTABLE_API_ERROR);
433
434 strbuf_release(&buf);
429 - for (i = 0; i < N; i++) {
435 + for (i = 0; i < N; i++)
436 reftable_free(names[i]);
431 - }
437 reftable_iterator_destroy(&it);
438 reftable_free(names);
439 reader_close(&rd);
440 strbuf_release(&buf);
441 }
442
438 -static void test_table_read_write_seek(int index, int hash_id)
443 +static void t_table_read_write_seek(int index, int hash_id)
444 {
445 char **names;
446 struct strbuf buf = STRBUF_INIT;
447 int N = 50;
443 - struct reftable_reader rd = { NULL };
444 - struct reftable_block_source source = { NULL };
448 + struct reftable_reader rd = { 0 };
449 + struct reftable_block_source source = { 0 };
450 int err;
451 int i = 0;
452
448 - struct reftable_iterator it = { NULL };
453 + struct reftable_iterator it = { 0 };
454 struct strbuf pastLast = STRBUF_INIT;
450 - struct reftable_ref_record ref = { NULL };
455 + struct reftable_ref_record ref = { 0 };
456
457 write_table(&names, &buf, N, 256, hash_id);
458
459 block_source_from_strbuf(&source, &buf);
460
461 err = init_reader(&rd, &source, "file.ref");
457 - EXPECT_ERR(err);
458 - EXPECT(hash_id == reftable_reader_hash_id(&rd));
462 + check(!err);
463 + check_int(hash_id, ==, reftable_reader_hash_id(&rd));
464
460 - if (!index) {
465 + if (!index)
466 rd.ref_offsets.index_offset = 0;
462 - } else {
463 - EXPECT(rd.ref_offsets.index_offset > 0);
464 - }
467 + else
468 + check_int(rd.ref_offsets.index_offset, >, 0);
469
470 for (i = 1; i < N; i++) {
471 reftable_reader_init_ref_iterator(&rd, &it);
472 err = reftable_iterator_seek_ref(&it, names[i]);
469 - EXPECT_ERR(err);
473 + check(!err);
474 err = reftable_iterator_next_ref(&it, &ref);
471 - EXPECT_ERR(err);
472 - EXPECT(0 == strcmp(names[i], ref.refname));
473 - EXPECT(REFTABLE_REF_VAL1 == ref.value_type);
474 - EXPECT(i == ref.value.val1[0]);
475 + check(!err);
476 + check_str(names[i], ref.refname);
477 + check_int(REFTABLE_REF_VAL1, ==, ref.value_type);
478 + check_int(i, ==, ref.value.val1[0]);
479
480 reftable_ref_record_release(&ref);
481 reftable_iterator_destroy(&it);
@@ -483,40 +487,39 @@ static void test_table_read_write_seek(int index, int hash_id)
487 reftable_reader_init_ref_iterator(&rd, &it);
488 err = reftable_iterator_seek_ref(&it, pastLast.buf);
489 if (err == 0) {
486 - struct reftable_ref_record ref = { NULL };
490 + struct reftable_ref_record ref = { 0 };
491 int err = reftable_iterator_next_ref(&it, &ref);
488 - EXPECT(err > 0);
492 + check_int(err, >, 0);
493 } else {
490 - EXPECT(err > 0);
494 + check_int(err, >, 0);
495 }
496
497 strbuf_release(&pastLast);
498 reftable_iterator_destroy(&it);
499
500 strbuf_release(&buf);
497 - for (i = 0; i < N; i++) {
501 + for (i = 0; i < N; i++)
502 reftable_free(names[i]);
499 - }
503 reftable_free(names);
504 reader_close(&rd);
505 }
506
504 -static void test_table_read_write_seek_linear(void)
507 +static void t_table_read_write_seek_linear(void)
508 {
506 - test_table_read_write_seek(0, GIT_SHA1_FORMAT_ID);
509 + t_table_read_write_seek(0, GIT_SHA1_FORMAT_ID);
510 }
511
509 -static void test_table_read_write_seek_linear_sha256(void)
512 +static void t_table_read_write_seek_linear_sha256(void)
513 {
511 - test_table_read_write_seek(0, GIT_SHA256_FORMAT_ID);
514 + t_table_read_write_seek(0, GIT_SHA256_FORMAT_ID);
515 }
516
514 -static void test_table_read_write_seek_index(void)
517 +static void t_table_read_write_seek_index(void)
518 {
516 - test_table_read_write_seek(1, GIT_SHA1_FORMAT_ID);
519 + t_table_read_write_seek(1, GIT_SHA1_FORMAT_ID);
520 }
521
519 -static void test_table_refs_for(int indexed)
522 +static void t_table_refs_for(int indexed)
523 {
524 int N = 50;
525 char **want_names = reftable_calloc(N + 1, sizeof(*want_names));
@@ -526,18 +529,18 @@ static void test_table_refs_for(int indexed)
529 struct reftable_write_options opts = {
530 .block_size = 256,
531 };
529 - struct reftable_ref_record ref = { NULL };
532 + struct reftable_ref_record ref = { 0 };
533 int i = 0;
534 int n;
535 int err;
536 struct reftable_reader rd;
534 - struct reftable_block_source source = { NULL };
537 + struct reftable_block_source source = { 0 };
538
539 struct strbuf buf = STRBUF_INIT;
540 struct reftable_writer *w =
541 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
542
540 - struct reftable_iterator it = { NULL };
543 + struct reftable_iterator it = { 0 };
544 int j;
545
546 set_test_hash(want_hash, 4);
@@ -546,7 +549,7 @@ static void test_table_refs_for(int indexed)
549 uint8_t hash[GIT_SHA1_RAWSZ];
550 char fill[51] = { 0 };
551 char name[100];
549 - struct reftable_ref_record ref = { NULL };
552 + struct reftable_ref_record ref = { 0 };
553
554 memset(hash, i, sizeof(hash));
555 memset(fill, 'x', 50);
@@ -563,16 +566,15 @@ static void test_table_refs_for(int indexed)
566 */
567 /* blocks. */
568 n = reftable_writer_add_ref(w, &ref);
566 - EXPECT(n == 0);
569 + check_int(n, ==, 0);
570
571 if (!memcmp(ref.value.val2.value, want_hash, GIT_SHA1_RAWSZ) ||
569 - !memcmp(ref.value.val2.target_value, want_hash, GIT_SHA1_RAWSZ)) {
572 + !memcmp(ref.value.val2.target_value, want_hash, GIT_SHA1_RAWSZ))
573 want_names[want_names_len++] = xstrdup(name);
571 - }
574 }
575
576 n = reftable_writer_close(w);
575 - EXPECT(n == 0);
577 + check_int(n, ==, 0);
578
579 reftable_writer_free(w);
580 w = NULL;
@@ -580,33 +582,30 @@ static void test_table_refs_for(int indexed)
582 block_source_from_strbuf(&source, &buf);
583
584 err = init_reader(&rd, &source, "file.ref");
583 - EXPECT_ERR(err);
584 - if (!indexed) {
585 + check(!err);
586 + if (!indexed)
587 rd.obj_offsets.is_present = 0;
586 - }
588
589 reftable_reader_init_ref_iterator(&rd, &it);
590 err = reftable_iterator_seek_ref(&it, "");
590 - EXPECT_ERR(err);
591 + check(!err);
592 reftable_iterator_destroy(&it);
593
594 err = reftable_reader_refs_for(&rd, &it, want_hash);
594 - EXPECT_ERR(err);
595 + check(!err);
596
597 j = 0;
598 while (1) {
599 int err = reftable_iterator_next_ref(&it, &ref);
599 - EXPECT(err >= 0);
600 - if (err > 0) {
600 + check_int(err, >=, 0);
601 + if (err > 0)
602 break;
602 - }
603 -
604 - EXPECT(j < want_names_len);
605 - EXPECT(0 == strcmp(ref.refname, want_names[j]));
603 + check_int(j, <, want_names_len);
604 + check_str(ref.refname, want_names[j]);
605 j++;
606 reftable_ref_record_release(&ref);
607 }
609 - EXPECT(j == want_names_len);
608 + check_int(j, ==, want_names_len);
609
610 strbuf_release(&buf);
611 free_names(want_names);
@@ -614,54 +613,54 @@ static void test_table_refs_for(int indexed)
613 reader_close(&rd);
614 }
615
617 -static void test_table_refs_for_no_index(void)
616 +static void t_table_refs_for_no_index(void)
617 {
619 - test_table_refs_for(0);
618 + t_table_refs_for(0);
619 }
620
622 -static void test_table_refs_for_obj_index(void)
621 +static void t_table_refs_for_obj_index(void)
622 {
624 - test_table_refs_for(1);
623 + t_table_refs_for(1);
624 }
625
627 -static void test_write_empty_table(void)
626 +static void t_write_empty_table(void)
627 {
628 struct reftable_write_options opts = { 0 };
629 struct strbuf buf = STRBUF_INIT;
630 struct reftable_writer *w =
631 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
633 - struct reftable_block_source source = { NULL };
632 + struct reftable_block_source source = { 0 };
633 struct reftable_reader *rd = NULL;
635 - struct reftable_ref_record rec = { NULL };
636 - struct reftable_iterator it = { NULL };
634 + struct reftable_ref_record rec = { 0 };
635 + struct reftable_iterator it = { 0 };
636 int err;
637
638 reftable_writer_set_limits(w, 1, 1);
639
640 err = reftable_writer_close(w);
642 - EXPECT(err == REFTABLE_EMPTY_TABLE_ERROR);
641 + check_int(err, ==, REFTABLE_EMPTY_TABLE_ERROR);
642 reftable_writer_free(w);
643
645 - EXPECT(buf.len == header_size(1) + footer_size(1));
644 + check_int(buf.len, ==, header_size(1) + footer_size(1));
645
646 block_source_from_strbuf(&source, &buf);
647
648 err = reftable_new_reader(&rd, &source, "filename");
650 - EXPECT_ERR(err);
649 + check(!err);
650
651 reftable_reader_init_ref_iterator(rd, &it);
652 err = reftable_iterator_seek_ref(&it, "");
654 - EXPECT_ERR(err);
653 + check(!err);
654
655 err = reftable_iterator_next_ref(&it, &rec);
657 - EXPECT(err > 0);
656 + check_int(err, >, 0);
657
658 reftable_iterator_destroy(&it);
659 reftable_reader_free(rd);
660 strbuf_release(&buf);
661 }
662
664 -static void test_write_object_id_min_length(void)
663 +static void t_write_object_id_min_length(void)
664 {
665 struct reftable_write_options opts = {
666 .block_size = 75,
@@ -686,17 +685,17 @@ static void test_write_object_id_min_length(void)
685 snprintf(name, sizeof(name), "ref%05d", i);
686 ref.refname = name;
687 err = reftable_writer_add_ref(w, &ref);
689 - EXPECT_ERR(err);
688 + check(!err);
689 }
690
691 err = reftable_writer_close(w);
693 - EXPECT_ERR(err);
694 - EXPECT(reftable_writer_stats(w)->object_id_len == 2);
692 + check(!err);
693 + check_int(reftable_writer_stats(w)->object_id_len, ==, 2);
694 reftable_writer_free(w);
695 strbuf_release(&buf);
696 }
697
699 -static void test_write_object_id_length(void)
698 +static void t_write_object_id_length(void)
699 {
700 struct reftable_write_options opts = {
701 .block_size = 75,
@@ -722,17 +721,17 @@ static void test_write_object_id_length(void)
721 ref.refname = name;
722 ref.value.val1[15] = i;
723 err = reftable_writer_add_ref(w, &ref);
725 - EXPECT_ERR(err);
724 + check(!err);
725 }
726
727 err = reftable_writer_close(w);
729 - EXPECT_ERR(err);
730 - EXPECT(reftable_writer_stats(w)->object_id_len == 16);
728 + check(!err);
729 + check_int(reftable_writer_stats(w)->object_id_len, ==, 16);
730 reftable_writer_free(w);
731 strbuf_release(&buf);
732 }
733
735 -static void test_write_empty_key(void)
734 +static void t_write_empty_key(void)
735 {
736 struct reftable_write_options opts = { 0 };
737 struct strbuf buf = STRBUF_INIT;
@@ -747,15 +746,15 @@ static void test_write_empty_key(void)
746
747 reftable_writer_set_limits(w, 1, 1);
748 err = reftable_writer_add_ref(w, &ref);
750 - EXPECT(err == REFTABLE_API_ERROR);
749 + check_int(err, ==, REFTABLE_API_ERROR);
750
751 err = reftable_writer_close(w);
753 - EXPECT(err == REFTABLE_EMPTY_TABLE_ERROR);
752 + check_int(err, ==, REFTABLE_EMPTY_TABLE_ERROR);
753 reftable_writer_free(w);
754 strbuf_release(&buf);
755 }
756
758 -static void test_write_key_order(void)
757 +static void t_write_key_order(void)
758 {
759 struct reftable_write_options opts = { 0 };
760 struct strbuf buf = STRBUF_INIT;
@@ -782,15 +781,15 @@ static void test_write_key_order(void)
781
782 reftable_writer_set_limits(w, 1, 1);
783 err = reftable_writer_add_ref(w, &refs[0]);
785 - EXPECT_ERR(err);
784 + check(!err);
785 err = reftable_writer_add_ref(w, &refs[1]);
787 - EXPECT(err == REFTABLE_API_ERROR);
786 + check_int(err, ==, REFTABLE_API_ERROR);
787 reftable_writer_close(w);
788 reftable_writer_free(w);
789 strbuf_release(&buf);
790 }
791
793 -static void test_write_multiple_indices(void)
792 +static void t_write_multiple_indices(void)
793 {
794 struct reftable_write_options opts = {
795 .block_size = 100,
@@ -817,7 +816,7 @@ static void test_write_multiple_indices(void)
816 ref.refname = buf.buf,
817
818 err = reftable_writer_add_ref(writer, &ref);
820 - EXPECT_ERR(err);
819 + check(!err);
820 }
821
822 for (i = 0; i < 100; i++) {
@@ -835,7 +834,7 @@ static void test_write_multiple_indices(void)
834 log.refname = buf.buf,
835
836 err = reftable_writer_add_log(writer, &log);
838 - EXPECT_ERR(err);
837 + check(!err);
838 }
839
840 reftable_writer_close(writer);
@@ -845,13 +844,13 @@ static void test_write_multiple_indices(void)
844 * for each of the block types.
845 */
846 stats = reftable_writer_stats(writer);
848 - EXPECT(stats->ref_stats.index_offset > 0);
849 - EXPECT(stats->obj_stats.index_offset > 0);
850 - EXPECT(stats->log_stats.index_offset > 0);
847 + check_int(stats->ref_stats.index_offset, >, 0);
848 + check_int(stats->obj_stats.index_offset, >, 0);
849 + check_int(stats->log_stats.index_offset, >, 0);
850
851 block_source_from_strbuf(&source, &writer_buf);
852 err = reftable_new_reader(&reader, &source, "filename");
854 - EXPECT_ERR(err);
853 + check(!err);
854
855 /*
856 * Seeking the log uses the log index now. In case there is any
@@ -859,7 +858,7 @@ static void test_write_multiple_indices(void)
858 */
859 reftable_reader_init_log_iterator(reader, &it);
860 err = reftable_iterator_seek_log(&it, "");
862 - EXPECT_ERR(err);
861 + check(!err);
862
863 reftable_iterator_destroy(&it);
864 reftable_writer_free(writer);
@@ -868,7 +867,7 @@ static void test_write_multiple_indices(void)
867 strbuf_release(&buf);
868 }
869
871 -static void test_write_multi_level_index(void)
870 +static void t_write_multi_level_index(void)
871 {
872 struct reftable_write_options opts = {
873 .block_size = 100,
@@ -895,7 +894,7 @@ static void test_write_multi_level_index(void)
894 ref.refname = buf.buf,
895
896 err = reftable_writer_add_ref(writer, &ref);
898 - EXPECT_ERR(err);
897 + check(!err);
898 }
899 reftable_writer_close(writer);
900
@@ -904,18 +903,18 @@ static void test_write_multi_level_index(void)
903 * multi-level index.
904 */
905 stats = reftable_writer_stats(writer);
907 - EXPECT(stats->ref_stats.max_index_level == 2);
906 + check_int(stats->ref_stats.max_index_level, ==, 2);
907
908 block_source_from_strbuf(&source, &writer_buf);
909 err = reftable_new_reader(&reader, &source, "filename");
911 - EXPECT_ERR(err);
910 + check(!err);
911
912 /*
913 * Seeking the last ref should work as expected.
914 */
915 reftable_reader_init_ref_iterator(reader, &it);
916 err = reftable_iterator_seek_ref(&it, "refs/heads/199");
918 - EXPECT_ERR(err);
917 + check(!err);
918
919 reftable_iterator_destroy(&it);
920 reftable_writer_free(writer);
@@ -924,56 +923,57 @@ static void test_write_multi_level_index(void)
923 strbuf_release(&buf);
924 }
925
927 -static void test_corrupt_table_empty(void)
926 +static void t_corrupt_table_empty(void)
927 {
928 struct strbuf buf = STRBUF_INIT;
930 - struct reftable_block_source source = { NULL };
931 - struct reftable_reader rd = { NULL };
929 + struct reftable_block_source source = { 0 };
930 + struct reftable_reader rd = { 0 };
931 int err;
932
933 block_source_from_strbuf(&source, &buf);
934 err = init_reader(&rd, &source, "file.log");
936 - EXPECT(err == REFTABLE_FORMAT_ERROR);
935 + check_int(err, ==, REFTABLE_FORMAT_ERROR);
936 }
937
939 -static void test_corrupt_table(void)
938 +static void t_corrupt_table(void)
939 {
940 uint8_t zeros[1024] = { 0 };
941 struct strbuf buf = STRBUF_INIT;
943 - struct reftable_block_source source = { NULL };
944 - struct reftable_reader rd = { NULL };
942 + struct reftable_block_source source = { 0 };
943 + struct reftable_reader rd = { 0 };
944 int err;
945 strbuf_add(&buf, zeros, sizeof(zeros));
946
947 block_source_from_strbuf(&source, &buf);
948 err = init_reader(&rd, &source, "file.log");
950 - EXPECT(err == REFTABLE_FORMAT_ERROR);
949 + check_int(err, ==, REFTABLE_FORMAT_ERROR);
950 strbuf_release(&buf);
951 }
952
954 -int readwrite_test_main(int argc, const char *argv[])
953 +int cmd_main(int argc, const char *argv[])
954 {
956 - RUN_TEST(test_log_zlib_corruption);
957 - RUN_TEST(test_corrupt_table);
958 - RUN_TEST(test_corrupt_table_empty);
959 - RUN_TEST(test_log_write_read);
960 - RUN_TEST(test_write_key_order);
961 - RUN_TEST(test_table_read_write_seek_linear_sha256);
962 - RUN_TEST(test_log_buffer_size);
963 - RUN_TEST(test_table_write_small_table);
964 - RUN_TEST(test_buffer);
965 - RUN_TEST(test_table_read_api);
966 - RUN_TEST(test_table_read_write_sequential);
967 - RUN_TEST(test_table_read_write_seek_linear);
968 - RUN_TEST(test_table_read_write_seek_index);
969 - RUN_TEST(test_table_refs_for_no_index);
970 - RUN_TEST(test_table_refs_for_obj_index);
971 - RUN_TEST(test_write_empty_key);
972 - RUN_TEST(test_write_empty_table);
973 - RUN_TEST(test_log_overflow);
974 - RUN_TEST(test_write_object_id_length);
975 - RUN_TEST(test_write_object_id_min_length);
976 - RUN_TEST(test_write_multiple_indices);
977 - RUN_TEST(test_write_multi_level_index);
978 - return 0;
955 + TEST(t_buffer(), "strbuf works as blocksource");
956 + TEST(t_corrupt_table(), "read-write on corrupted table");
957 + TEST(t_corrupt_table_empty(), "read-write on an empty table");
958 + TEST(t_log_buffer_size(), "buffer extension for log compression");
959 + TEST(t_log_overflow(), "log overflow returns expected error");
960 + TEST(t_log_write_read(), "read-write on log records");
961 + TEST(t_log_zlib_corruption(), "reading corrupted log record returns expected error");
962 + TEST(t_table_read_api(), "read on a table");
963 + TEST(t_table_read_write_seek_index(), "read-write on a table with index");
964 + TEST(t_table_read_write_seek_linear(), "read-write on a table without index (SHA1)");
965 + TEST(t_table_read_write_seek_linear_sha256(), "read-write on a table without index (SHA256)");
966 + TEST(t_table_read_write_sequential(), "sequential read-write on a table");
967 + TEST(t_table_refs_for_no_index(), "refs-only table with no index");
968 + TEST(t_table_refs_for_obj_index(), "refs-only table with index");
969 + TEST(t_table_write_small_table(), "write_table works");
970 + TEST(t_write_empty_key(), "write on refs with empty keys");
971 + TEST(t_write_empty_table(), "read-write on empty tables");
972 + TEST(t_write_key_order(), "refs must be written in increasing order");
973 + TEST(t_write_multi_level_index(), "table with multi-level index");
974 + TEST(t_write_multiple_indices(), "table with indices for multiple block types");
975 + TEST(t_write_object_id_length(), "prefix compression on writing refs");
976 + TEST(t_write_object_id_min_length(), "prefix compression on writing refs");
977 +
978 + return test_done();
979 }