t/unit-tests: convert reftable readwrite test to use clar

Adapt reftable readwrite test file to use clar by using clar assertions where necessary. Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Seyi Kuforiji committed Jul 24, 2025 at 15:28 UTC ee0a88dadb08320d88fab0e3ad2dd17ecfee8496
3 files changed +179 -230
Makefile
+1 -1
@@ -1368,6 +1368,7 @@ CLAR_TEST_SUITES += u-reftable-basics
1368 CLAR_TEST_SUITES += u-reftable-block
1369 CLAR_TEST_SUITES += u-reftable-merged
1370 CLAR_TEST_SUITES += u-reftable-pq
1371 +CLAR_TEST_SUITES += u-reftable-readwrite
1372 CLAR_TEST_SUITES += u-reftable-table
1373 CLAR_TEST_SUITES += u-reftable-tree
1374 CLAR_TEST_SUITES += u-strbuf
@@ -1382,7 +1383,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
1383 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
1384 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-reftable-clar.o
1385
1385 -UNIT_TEST_PROGRAMS += t-reftable-readwrite
1386 UNIT_TEST_PROGRAMS += t-reftable-record
1387 UNIT_TEST_PROGRAMS += t-reftable-stack
1388 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
t/meson.build
+1 -1
@@ -12,6 +12,7 @@ clar_test_suites = [
12 'unit-tests/u-reftable-block.c',
13 'unit-tests/u-reftable-merged.c',
14 'unit-tests/u-reftable-pq.c',
15 + 'unit-tests/u-reftable-readwrite.c',
16 'unit-tests/u-reftable-table.c',
17 'unit-tests/u-reftable-tree.c',
18 'unit-tests/u-strbuf.c',
@@ -60,7 +61,6 @@ clar_unit_tests = executable('unit-tests',
61 test('unit-tests', clar_unit_tests)
62
63 unit_test_programs = [
63 - 'unit-tests/t-reftable-readwrite.c',
64 'unit-tests/t-reftable-record.c',
65 'unit-tests/t-reftable-stack.c',
66 ]
t/unit-tests/u-reftable-readwrite.c renamed
+177 -228
@@ -8,8 +8,8 @@ https://developers.google.com/open-source/licenses/bsd
8
9 #define DISABLE_SIGN_COMPARE_WARNINGS
10
11 -#include "test-lib.h"
12 -#include "lib-reftable.h"
11 +#include "unit-test.h"
12 +#include "lib-reftable-clar.h"
13 #include "reftable/basics.h"
14 #include "reftable/blocksource.h"
15 #include "reftable/reftable-error.h"
@@ -19,24 +19,24 @@ https://developers.google.com/open-source/licenses/bsd
19
20 static const int update_index = 5;
21
22 -static void t_buffer(void)
22 +void test_reftable_readwrite__buffer(void)
23 {
24 struct reftable_buf buf = REFTABLE_BUF_INIT;
25 struct reftable_block_source source = { 0 };
26 struct reftable_block_data out = { 0 };
27 int n;
28 uint8_t in[] = "hello";
29 - check(!reftable_buf_add(&buf, in, sizeof(in)));
29 + cl_assert_equal_i(reftable_buf_add(&buf, in, sizeof(in)), 0);
30 block_source_from_buf(&source, &buf);
31 - check_int(block_source_size(&source), ==, 6);
31 + cl_assert_equal_i(block_source_size(&source), 6);
32 n = block_source_read_data(&source, &out, 0, sizeof(in));
33 - check_int(n, ==, sizeof(in));
34 - check(!memcmp(in, out.data, n));
33 + cl_assert_equal_i(n, sizeof(in));
34 + cl_assert(!memcmp(in, out.data, n));
35 block_source_release_data(&out);
36
37 n = block_source_read_data(&source, &out, 1, 2);
38 - check_int(n, ==, 2);
39 - check(!memcmp(out.data, "el", 2));
38 + cl_assert_equal_i(n, 2);
39 + cl_assert(!memcmp(out.data, "el", 2));
40
41 block_source_release_data(&out);
42 block_source_close(&source);
@@ -55,41 +55,41 @@ static void write_table(char ***names, struct reftable_buf *buf, int N,
55 int i;
56
57 REFTABLE_CALLOC_ARRAY(*names, N + 1);
58 - check(*names != NULL);
58 + cl_assert(*names != NULL);
59 REFTABLE_CALLOC_ARRAY(refs, N);
60 - check(refs != NULL);
60 + cl_assert(refs != NULL);
61 REFTABLE_CALLOC_ARRAY(logs, N);
62 - check(logs != NULL);
62 + cl_assert(logs != NULL);
63
64 for (i = 0; i < N; i++) {
65 refs[i].refname = (*names)[i] = xstrfmt("refs/heads/branch%02d", i);
66 refs[i].update_index = update_index;
67 refs[i].value_type = REFTABLE_REF_VAL1;
68 - t_reftable_set_hash(refs[i].value.val1, i, REFTABLE_HASH_SHA1);
68 + cl_reftable_set_hash(refs[i].value.val1, i,
69 + REFTABLE_HASH_SHA1);
70 }
71
72 for (i = 0; i < N; i++) {
73 logs[i].refname = (*names)[i];
74 logs[i].update_index = update_index;
75 logs[i].value_type = REFTABLE_LOG_UPDATE;
75 - t_reftable_set_hash(logs[i].value.update.new_hash, i,
76 - REFTABLE_HASH_SHA1);
76 + cl_reftable_set_hash(logs[i].value.update.new_hash, i,
77 + REFTABLE_HASH_SHA1);
78 logs[i].value.update.message = (char *) "message";
79 }
80
80 - t_reftable_write_to_buf(buf, refs, N, logs, N, &opts);
81 + cl_reftable_write_to_buf(buf, refs, N, logs, N, &opts);
82
83 reftable_free(refs);
84 reftable_free(logs);
85 }
86
86 -static void t_log_buffer_size(void)
87 +void test_reftable_readwrite__log_buffer_size(void)
88 {
89 struct reftable_buf buf = REFTABLE_BUF_INIT;
90 struct reftable_write_options opts = {
91 .block_size = 4096,
92 };
92 - int err;
93 int i;
94 struct reftable_log_record
95 log = { .refname = (char *) "refs/heads/master",
@@ -102,7 +102,8 @@ static void t_log_buffer_size(void)
102 .time = 0x5e430672,
103 .message = (char *) "commit: 9\n",
104 } } };
105 - struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
105 + struct reftable_writer *w = cl_reftable_strbuf_writer(&buf,
106 + &opts);
107
108 /* This tests buffer extension for log compression. Must use a random
109 hash, to ensure that the compressed part is larger than the original.
@@ -112,22 +113,19 @@ static void t_log_buffer_size(void)
113 log.value.update.new_hash[i] = (uint8_t)(git_rand(0) % 256);
114 }
115 reftable_writer_set_limits(w, update_index, update_index);
115 - err = reftable_writer_add_log(w, &log);
116 - check(!err);
117 - err = reftable_writer_close(w);
118 - check(!err);
116 + cl_assert_equal_i(reftable_writer_add_log(w, &log), 0);
117 + cl_assert_equal_i(reftable_writer_close(w), 0);
118 reftable_writer_free(w);
119 reftable_buf_release(&buf);
120 }
121
123 -static void t_log_overflow(void)
122 +void test_reftable_readwrite__log_overflow(void)
123 {
124 struct reftable_buf buf = REFTABLE_BUF_INIT;
125 char msg[256] = { 0 };
126 struct reftable_write_options opts = {
127 .block_size = ARRAY_SIZE(msg),
128 };
130 - int err;
129 struct reftable_log_record log = {
130 .refname = (char *) "refs/heads/master",
131 .update_index = update_index,
@@ -144,21 +142,22 @@ static void t_log_overflow(void)
142 },
143 },
144 };
147 - struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
145 + struct reftable_writer *w = cl_reftable_strbuf_writer(&buf,
146 + &opts);
147
148 memset(msg, 'x', sizeof(msg) - 1);
149 reftable_writer_set_limits(w, update_index, update_index);
151 - err = reftable_writer_add_log(w, &log);
152 - check_int(err, ==, REFTABLE_ENTRY_TOO_BIG_ERROR);
150 + cl_assert_equal_i(reftable_writer_add_log(w, &log), REFTABLE_ENTRY_TOO_BIG_ERROR);
151 reftable_writer_free(w);
152 reftable_buf_release(&buf);
153 }
154
157 -static void t_log_write_limits(void)
155 +void test_reftable_readwrite__log_write_limits(void)
156 {
157 struct reftable_write_options opts = { 0 };
158 struct reftable_buf buf = REFTABLE_BUF_INIT;
161 - struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
159 + struct reftable_writer *w = cl_reftable_strbuf_writer(&buf,
160 + &opts);
161 struct reftable_log_record log = {
162 .refname = (char *)"refs/head/master",
163 .update_index = 0,
@@ -174,29 +173,25 @@ static void t_log_write_limits(void)
173 },
174 },
175 };
177 - int err;
176
177 reftable_writer_set_limits(w, 1, 1);
178
179 /* write with update_index (0) below set limits (1, 1) */
182 - err = reftable_writer_add_log(w, &log);
183 - check_int(err, ==, 0);
180 + cl_assert_equal_i(reftable_writer_add_log(w, &log), 0);
181
182 /* write with update_index (1) in the set limits (1, 1) */
183 log.update_index = 1;
187 - err = reftable_writer_add_log(w, &log);
188 - check_int(err, ==, 0);
184 + cl_assert_equal_i(reftable_writer_add_log(w, &log), 0);
185
186 /* write with update_index (3) above set limits (1, 1) */
187 log.update_index = 3;
192 - err = reftable_writer_add_log(w, &log);
193 - check_int(err, ==, REFTABLE_API_ERROR);
188 + cl_assert_equal_i(reftable_writer_add_log(w, &log), REFTABLE_API_ERROR);
189
190 reftable_writer_free(w);
191 reftable_buf_release(&buf);
192 }
193
199 -static void t_log_write_read(void)
194 +void test_reftable_readwrite__log_write_read(void)
195 {
196 struct reftable_write_options opts = {
197 .block_size = 256,
@@ -207,13 +202,14 @@ static void t_log_write_read(void)
202 struct reftable_table *table;
203 struct reftable_block_source source = { 0 };
204 struct reftable_buf buf = REFTABLE_BUF_INIT;
210 - struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
205 + struct reftable_writer *w = cl_reftable_strbuf_writer(&buf, &opts);
206 const struct reftable_stats *stats = NULL;
212 - int N = 2, err, i, n;
207 + int N = 2, i;
208 char **names;
209 + int err;
210
211 names = reftable_calloc(N + 1, sizeof(*names));
216 - check(names != NULL);
212 + cl_assert(names != NULL);
213
214 reftable_writer_set_limits(w, 0, N);
215
@@ -225,8 +221,7 @@ static void t_log_write_read(void)
221 ref.refname = name;
222 ref.update_index = i;
223
228 - err = reftable_writer_add_ref(w, &ref);
229 - check(!err);
224 + cl_assert_equal_i(reftable_writer_add_ref(w, &ref), 0);
225 }
226
227 for (i = 0; i < N; i++) {
@@ -235,60 +230,57 @@ static void t_log_write_read(void)
230 log.refname = names[i];
231 log.update_index = i;
232 log.value_type = REFTABLE_LOG_UPDATE;
238 - t_reftable_set_hash(log.value.update.old_hash, i,
239 - REFTABLE_HASH_SHA1);
240 - t_reftable_set_hash(log.value.update.new_hash, i + 1,
241 - REFTABLE_HASH_SHA1);
233 + cl_reftable_set_hash(log.value.update.old_hash, i,
234 + REFTABLE_HASH_SHA1);
235 + cl_reftable_set_hash(log.value.update.new_hash, i + 1,
236 + REFTABLE_HASH_SHA1);
237
243 - err = reftable_writer_add_log(w, &log);
244 - check(!err);
238 + cl_assert_equal_i(reftable_writer_add_log(w, &log), 0);
239 }
240
247 - n = reftable_writer_close(w);
248 - check_int(n, ==, 0);
241 + cl_assert_equal_i(reftable_writer_close(w), 0);
242
243 stats = reftable_writer_stats(w);
251 - check_int(stats->log_stats.blocks, >, 0);
244 + cl_assert(stats->log_stats.blocks > 0);
245 reftable_writer_free(w);
246 w = NULL;
247
248 block_source_from_buf(&source, &buf);
249
250 err = reftable_table_new(&table, &source, "file.log");
258 - check(!err);
251 + cl_assert(!err);
252
253 err = reftable_table_init_ref_iterator(table, &it);
261 - check(!err);
254 + cl_assert(!err);
255
256 err = reftable_iterator_seek_ref(&it, names[N - 1]);
264 - check(!err);
257 + cl_assert(!err);
258
259 err = reftable_iterator_next_ref(&it, &ref);
267 - check(!err);
260 + cl_assert(!err);
261
262 /* end of iteration. */
270 - err = reftable_iterator_next_ref(&it, &ref);
271 - check_int(err, >, 0);
263 + cl_assert(reftable_iterator_next_ref(&it, &ref) > 0);
264
265 reftable_iterator_destroy(&it);
266 reftable_ref_record_release(&ref);
267
268 err = reftable_table_init_log_iterator(table, &it);
277 - check(!err);
269 + cl_assert(!err);
270 err = reftable_iterator_seek_log(&it, "");
279 - check(!err);
271 + cl_assert(!err);
272
273 for (i = 0; ; i++) {
274 int err = reftable_iterator_next_log(&it, &log);
275 if (err > 0)
276 break;
285 - check(!err);
286 - check_str(names[i], log.refname);
287 - check_int(i, ==, log.update_index);
277 + cl_assert(!err);
278 + cl_assert_equal_s(names[i], log.refname);
279 + cl_assert_equal_i(i, log.update_index);
280 reftable_log_record_release(&log);
281 }
282
291 - check_int(i, ==, N);
283 + cl_assert_equal_i(i, N);
284 reftable_iterator_destroy(&it);
285
286 /* cleanup. */
@@ -297,7 +289,7 @@ static void t_log_write_read(void)
289 reftable_table_decref(table);
290 }
291
300 -static void t_log_zlib_corruption(void)
292 +void test_reftable_readwrite__log_zlib_corruption(void)
293 {
294 struct reftable_write_options opts = {
295 .block_size = 256,
@@ -306,10 +298,12 @@ static void t_log_zlib_corruption(void)
298 struct reftable_table *table;
299 struct reftable_block_source source = { 0 };
300 struct reftable_buf buf = REFTABLE_BUF_INIT;
309 - struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
301 + struct reftable_writer *w = cl_reftable_strbuf_writer(&buf,
302 + &opts);
303 const struct reftable_stats *stats = NULL;
304 char message[100] = { 0 };
312 - int err, i, n;
305 + int i;
306 + int err;
307 struct reftable_log_record log = {
308 .refname = (char *) "refname",
309 .value_type = REFTABLE_LOG_UPDATE,
@@ -329,14 +323,11 @@ static void t_log_zlib_corruption(void)
323
324 reftable_writer_set_limits(w, 1, 1);
325
332 - err = reftable_writer_add_log(w, &log);
333 - check(!err);
334 -
335 - n = reftable_writer_close(w);
336 - check_int(n, ==, 0);
326 + cl_assert_equal_i(reftable_writer_add_log(w, &log), 0);
327 + cl_assert_equal_i(reftable_writer_close(w), 0);
328
329 stats = reftable_writer_stats(w);
339 - check_int(stats->log_stats.blocks, >, 0);
330 + cl_assert(stats->log_stats.blocks > 0);
331 reftable_writer_free(w);
332 w = NULL;
333
@@ -346,12 +337,12 @@ static void t_log_zlib_corruption(void)
337 block_source_from_buf(&source, &buf);
338
339 err = reftable_table_new(&table, &source, "file.log");
349 - check(!err);
340 + cl_assert(!err);
341
342 err = reftable_table_init_log_iterator(table, &it);
352 - check(!err);
343 + cl_assert(!err);
344 err = reftable_iterator_seek_log(&it, "refname");
354 - check_int(err, ==, REFTABLE_ZLIB_ERROR);
345 + cl_assert_equal_i(err, REFTABLE_ZLIB_ERROR);
346
347 reftable_iterator_destroy(&it);
348
@@ -360,7 +351,7 @@ static void t_log_zlib_corruption(void)
351 reftable_buf_release(&buf);
352 }
353
363 -static void t_table_read_write_sequential(void)
354 +void test_reftable_readwrite__table_read_write_sequential(void)
355 {
356 char **names;
357 struct reftable_buf buf = REFTABLE_BUF_INIT;
@@ -376,24 +367,24 @@ static void t_table_read_write_sequential(void)
367 block_source_from_buf(&source, &buf);
368
369 err = reftable_table_new(&table, &source, "file.ref");
379 - check(!err);
370 + cl_assert(!err);
371
372 err = reftable_table_init_ref_iterator(table, &it);
382 - check(!err);
373 + cl_assert(!err);
374 err = reftable_iterator_seek_ref(&it, "");
384 - check(!err);
375 + cl_assert(!err);
376
377 for (j = 0; ; j++) {
378 struct reftable_ref_record ref = { 0 };
379 int r = reftable_iterator_next_ref(&it, &ref);
389 - check_int(r, >=, 0);
380 + cl_assert(r >= 0);
381 if (r > 0)
382 break;
392 - check_str(names[j], ref.refname);
393 - check_int(update_index, ==, ref.update_index);
383 + cl_assert_equal_s(names[j], ref.refname);
384 + cl_assert_equal_i(update_index, ref.update_index);
385 reftable_ref_record_release(&ref);
386 }
396 - check_int(j, ==, N);
387 + cl_assert_equal_i(j, N);
388
389 reftable_iterator_destroy(&it);
390 reftable_table_decref(table);
@@ -401,42 +392,42 @@ static void t_table_read_write_sequential(void)
392 free_names(names);
393 }
394
404 -static void t_table_write_small_table(void)
395 +void test_reftable_readwrite__table_write_small_table(void)
396 {
397 char **names;
398 struct reftable_buf buf = REFTABLE_BUF_INIT;
399 int N = 1;
400 write_table(&names, &buf, N, 4096, REFTABLE_HASH_SHA1);
410 - check_int(buf.len, <, 200);
401 + cl_assert(buf.len < 200);
402 reftable_buf_release(&buf);
403 free_names(names);
404 }
405
415 -static void t_table_read_api(void)
406 +void test_reftable_readwrite__table_read_api(void)
407 {
408 char **names;
409 struct reftable_buf buf = REFTABLE_BUF_INIT;
410 int N = 50;
411 struct reftable_table *table;
412 struct reftable_block_source source = { 0 };
422 - int err;
413 struct reftable_log_record log = { 0 };
414 struct reftable_iterator it = { 0 };
415 + int err;
416
417 write_table(&names, &buf, N, 256, REFTABLE_HASH_SHA1);
418
419 block_source_from_buf(&source, &buf);
420
421 err = reftable_table_new(&table, &source, "file.ref");
431 - check(!err);
422 + cl_assert(!err);
423
424 err = reftable_table_init_ref_iterator(table, &it);
434 - check(!err);
425 + cl_assert(!err);
426 err = reftable_iterator_seek_ref(&it, names[0]);
436 - check(!err);
427 + cl_assert(!err);
428
429 err = reftable_iterator_next_log(&it, &log);
439 - check_int(err, ==, REFTABLE_API_ERROR);
430 + cl_assert_equal_i(err, REFTABLE_API_ERROR);
431
432 reftable_buf_release(&buf);
433 free_names(names);
@@ -464,42 +455,43 @@ static void t_table_read_write_seek(int index, enum reftable_hash hash_id)
455 block_source_from_buf(&source, &buf);
456
457 err = reftable_table_new(&table, &source, "file.ref");
467 - check(!err);
468 - check_int(hash_id, ==, reftable_table_hash_id(table));
458 + cl_assert(!err);
459 + cl_assert_equal_i(hash_id, reftable_table_hash_id(table));
460
461 if (!index) {
462 table->ref_offsets.index_offset = 0;
463 } else {
473 - check_int(table->ref_offsets.index_offset, >, 0);
464 + cl_assert(table->ref_offsets.index_offset > 0);
465 }
466
467 for (i = 1; i < N; i++) {
468 err = reftable_table_init_ref_iterator(table, &it);
478 - check(!err);
469 + cl_assert(!err);
470 err = reftable_iterator_seek_ref(&it, names[i]);
480 - check(!err);
471 + cl_assert(!err);
472 err = reftable_iterator_next_ref(&it, &ref);
482 - check(!err);
483 - check_str(names[i], ref.refname);
484 - check_int(REFTABLE_REF_VAL1, ==, ref.value_type);
485 - check_int(i, ==, ref.value.val1[0]);
473 + cl_assert(!err);
474 + cl_assert_equal_s(names[i], ref.refname);
475 + cl_assert_equal_i(REFTABLE_REF_VAL1, ref.value_type);
476 + cl_assert_equal_i(i, ref.value.val1[0]);
477
478 reftable_ref_record_release(&ref);
479 reftable_iterator_destroy(&it);
480 }
481
491 - check(!reftable_buf_addstr(&pastLast, names[N - 1]));
492 - check(!reftable_buf_addstr(&pastLast, "/"));
482 + cl_assert_equal_i(reftable_buf_addstr(&pastLast, names[N - 1]),
483 + 0);
484 + cl_assert_equal_i(reftable_buf_addstr(&pastLast, "/"), 0);
485
486 err = reftable_table_init_ref_iterator(table, &it);
495 - check(!err);
487 + cl_assert(!err);
488 err = reftable_iterator_seek_ref(&it, pastLast.buf);
489 if (err == 0) {
490 struct reftable_ref_record ref = { 0 };
491 int err = reftable_iterator_next_ref(&it, &ref);
500 - check_int(err, >, 0);
492 + cl_assert(err > 0);
493 } else {
502 - check_int(err, >, 0);
494 + cl_assert(err > 0);
495 }
496
497 reftable_buf_release(&pastLast);
@@ -510,17 +502,17 @@ static void t_table_read_write_seek(int index, enum reftable_hash hash_id)
502 reftable_table_decref(table);
503 }
504
513 -static void t_table_read_write_seek_linear(void)
505 +void test_reftable_readwrite__table_read_write_seek_linear(void)
506 {
507 t_table_read_write_seek(0, REFTABLE_HASH_SHA1);
508 }
509
518 -static void t_table_read_write_seek_linear_sha256(void)
510 +void test_reftable_readwrite__table_read_write_seek_linear_sha256(void)
511 {
512 t_table_read_write_seek(0, REFTABLE_HASH_SHA256);
513 }
514
523 -static void t_table_read_write_seek_index(void)
515 +void test_reftable_readwrite__table_read_write_seek_index(void)
516 {
517 t_table_read_write_seek(1, REFTABLE_HASH_SHA1);
518 }
@@ -538,14 +530,16 @@ static void t_table_refs_for(int indexed)
530 struct reftable_table *table;
531 struct reftable_block_source source = { 0 };
532 struct reftable_buf buf = REFTABLE_BUF_INIT;
541 - struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
533 + struct reftable_writer *w = cl_reftable_strbuf_writer(&buf,
534 + &opts);
535 struct reftable_iterator it = { 0 };
543 - int N = 50, n, j, err, i;
536 + int N = 50, j, i;
537 + int err;
538
539 want_names = reftable_calloc(N + 1, sizeof(*want_names));
546 - check(want_names != NULL);
540 + cl_assert(want_names != NULL);
541
548 - t_reftable_set_hash(want_hash, 4, REFTABLE_HASH_SHA1);
542 + cl_reftable_set_hash(want_hash, 4, REFTABLE_HASH_SHA1);
543
544 for (i = 0; i < N; i++) {
545 uint8_t hash[REFTABLE_HASH_SIZE_SHA1];
@@ -561,24 +555,22 @@ static void t_table_refs_for(int indexed)
555 ref.refname = name;
556
557 ref.value_type = REFTABLE_REF_VAL2;
564 - t_reftable_set_hash(ref.value.val2.value, i / 4,
565 - REFTABLE_HASH_SHA1);
566 - t_reftable_set_hash(ref.value.val2.target_value, 3 + i / 4,
567 - REFTABLE_HASH_SHA1);
558 + cl_reftable_set_hash(ref.value.val2.value, i / 4,
559 + REFTABLE_HASH_SHA1);
560 + cl_reftable_set_hash(ref.value.val2.target_value,
561 + 3 + i / 4, REFTABLE_HASH_SHA1);
562
563 /* 80 bytes / entry, so 3 entries per block. Yields 17
564 */
565 /* blocks. */
572 - n = reftable_writer_add_ref(w, &ref);
573 - check_int(n, ==, 0);
566 + cl_assert_equal_i(reftable_writer_add_ref(w, &ref), 0);
567
568 if (!memcmp(ref.value.val2.value, want_hash, REFTABLE_HASH_SIZE_SHA1) ||
569 !memcmp(ref.value.val2.target_value, want_hash, REFTABLE_HASH_SIZE_SHA1))
570 want_names[want_names_len++] = xstrdup(name);
571 }
572
580 - n = reftable_writer_close(w);
581 - check_int(n, ==, 0);
573 + cl_assert_equal_i(reftable_writer_close(w), 0);
574
575 reftable_writer_free(w);
576 w = NULL;
@@ -586,29 +578,29 @@ static void t_table_refs_for(int indexed)
578 block_source_from_buf(&source, &buf);
579
580 err = reftable_table_new(&table, &source, "file.ref");
589 - check(!err);
581 + cl_assert(!err);
582 if (!indexed)
583 table->obj_offsets.is_present = 0;
584
585 err = reftable_table_init_ref_iterator(table, &it);
594 - check(!err);
586 + cl_assert(!err);
587 err = reftable_iterator_seek_ref(&it, "");
596 - check(!err);
588 + cl_assert(!err);
589 reftable_iterator_destroy(&it);
590
591 err = reftable_table_refs_for(table, &it, want_hash);
600 - check(!err);
592 + cl_assert(!err);
593
594 for (j = 0; ; j++) {
595 int err = reftable_iterator_next_ref(&it, &ref);
604 - check_int(err, >=, 0);
596 + cl_assert(err >= 0);
597 if (err > 0)
598 break;
607 - check_int(j, <, want_names_len);
608 - check_str(ref.refname, want_names[j]);
599 + cl_assert(j < want_names_len);
600 + cl_assert_equal_s(ref.refname, want_names[j]);
601 reftable_ref_record_release(&ref);
602 }
611 - check_int(j, ==, want_names_len);
603 + cl_assert_equal_i(j, want_names_len);
604
605 reftable_buf_release(&buf);
606 free_names(want_names);
@@ -616,21 +608,21 @@ static void t_table_refs_for(int indexed)
608 reftable_table_decref(table);
609 }
610
619 -static void t_table_refs_for_no_index(void)
611 +void test_reftable_readwrite__table_refs_for_no_index(void)
612 {
613 t_table_refs_for(0);
614 }
615
624 -static void t_table_refs_for_obj_index(void)
616 +void test_reftable_readwrite__table_refs_for_obj_index(void)
617 {
618 t_table_refs_for(1);
619 }
620
629 -static void t_write_empty_table(void)
621 +void test_reftable_readwrite__write_empty_table(void)
622 {
623 struct reftable_write_options opts = { 0 };
624 struct reftable_buf buf = REFTABLE_BUF_INIT;
633 - struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
625 + struct reftable_writer *w = cl_reftable_strbuf_writer(&buf, &opts);
626 struct reftable_block_source source = { 0 };
627 struct reftable_table *table = NULL;
628 struct reftable_ref_record rec = { 0 };
@@ -639,43 +631,41 @@ static void t_write_empty_table(void)
631
632 reftable_writer_set_limits(w, 1, 1);
633
642 - err = reftable_writer_close(w);
643 - check_int(err, ==, REFTABLE_EMPTY_TABLE_ERROR);
634 + cl_assert_equal_i(reftable_writer_close(w), REFTABLE_EMPTY_TABLE_ERROR);
635 reftable_writer_free(w);
636
646 - check_uint(buf.len, ==, header_size(1) + footer_size(1));
637 + cl_assert_equal_i(buf.len, header_size(1) + footer_size(1));
638
639 block_source_from_buf(&source, &buf);
640
641 err = reftable_table_new(&table, &source, "filename");
651 - check(!err);
642 + cl_assert(!err);
643
644 err = reftable_table_init_ref_iterator(table, &it);
654 - check(!err);
645 + cl_assert(!err);
646 err = reftable_iterator_seek_ref(&it, "");
656 - check(!err);
647 + cl_assert(!err);
648
649 err = reftable_iterator_next_ref(&it, &rec);
659 - check_int(err, >, 0);
650 + cl_assert(err > 0);
651
652 reftable_iterator_destroy(&it);
653 reftable_table_decref(table);
654 reftable_buf_release(&buf);
655 }
656
666 -static void t_write_object_id_min_length(void)
657 +void test_reftable_readwrite__write_object_id_min_length(void)
658 {
659 struct reftable_write_options opts = {
660 .block_size = 75,
661 };
662 struct reftable_buf buf = REFTABLE_BUF_INIT;
672 - struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
663 + struct reftable_writer *w = cl_reftable_strbuf_writer(&buf, &opts);
664 struct reftable_ref_record ref = {
665 .update_index = 1,
666 .value_type = REFTABLE_REF_VAL1,
667 .value.val1 = {42},
668 };
678 - int err;
669 int i;
670
671 reftable_writer_set_limits(w, 1, 1);
@@ -686,30 +676,27 @@ static void t_write_object_id_min_length(void)
676 char name[256];
677 snprintf(name, sizeof(name), "ref%05d", i);
678 ref.refname = name;
689 - err = reftable_writer_add_ref(w, &ref);
690 - check(!err);
679 + cl_assert_equal_i(reftable_writer_add_ref(w, &ref), 0);
680 }
681
693 - err = reftable_writer_close(w);
694 - check(!err);
695 - check_int(reftable_writer_stats(w)->object_id_len, ==, 2);
682 + cl_assert_equal_i(reftable_writer_close(w), 0);
683 + cl_assert_equal_i(reftable_writer_stats(w)->object_id_len, 2);
684 reftable_writer_free(w);
685 reftable_buf_release(&buf);
686 }
687
700 -static void t_write_object_id_length(void)
688 +void test_reftable_readwrite__write_object_id_length(void)
689 {
690 struct reftable_write_options opts = {
691 .block_size = 75,
692 };
693 struct reftable_buf buf = REFTABLE_BUF_INIT;
706 - struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
694 + struct reftable_writer *w = cl_reftable_strbuf_writer(&buf, &opts);
695 struct reftable_ref_record ref = {
696 .update_index = 1,
697 .value_type = REFTABLE_REF_VAL1,
698 .value.val1 = {42},
699 };
712 - int err;
700 int i;
701
702 reftable_writer_set_limits(w, 1, 1);
@@ -721,44 +708,39 @@ static void t_write_object_id_length(void)
708 snprintf(name, sizeof(name), "ref%05d", i);
709 ref.refname = name;
710 ref.value.val1[15] = i;
724 - err = reftable_writer_add_ref(w, &ref);
725 - check(!err);
711 + cl_assert(reftable_writer_add_ref(w, &ref) == 0);
712 }
713
728 - err = reftable_writer_close(w);
729 - check(!err);
730 - check_int(reftable_writer_stats(w)->object_id_len, ==, 16);
714 + cl_assert_equal_i(reftable_writer_close(w), 0);
715 + cl_assert_equal_i(reftable_writer_stats(w)->object_id_len, 16);
716 reftable_writer_free(w);
717 reftable_buf_release(&buf);
718 }
719
735 -static void t_write_empty_key(void)
720 +void test_reftable_readwrite__write_empty_key(void)
721 {
722 struct reftable_write_options opts = { 0 };
723 struct reftable_buf buf = REFTABLE_BUF_INIT;
739 - struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
724 + struct reftable_writer *w = cl_reftable_strbuf_writer(&buf, &opts);
725 struct reftable_ref_record ref = {
726 .refname = (char *) "",
727 .update_index = 1,
728 .value_type = REFTABLE_REF_DELETION,
729 };
745 - int err;
730
731 reftable_writer_set_limits(w, 1, 1);
748 - err = reftable_writer_add_ref(w, &ref);
749 - check_int(err, ==, REFTABLE_API_ERROR);
750 -
751 - err = reftable_writer_close(w);
752 - check_int(err, ==, REFTABLE_EMPTY_TABLE_ERROR);
732 + cl_assert_equal_i(reftable_writer_add_ref(w, &ref), REFTABLE_API_ERROR);
733 + cl_assert_equal_i(reftable_writer_close(w),
734 + REFTABLE_EMPTY_TABLE_ERROR);
735 reftable_writer_free(w);
736 reftable_buf_release(&buf);
737 }
738
757 -static void t_write_key_order(void)
739 +void test_reftable_readwrite__write_key_order(void)
740 {
741 struct reftable_write_options opts = { 0 };
742 struct reftable_buf buf = REFTABLE_BUF_INIT;
761 - struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
743 + struct reftable_writer *w = cl_reftable_strbuf_writer(&buf, &opts);
744 struct reftable_ref_record refs[2] = {
745 {
746 .refname = (char *) "b",
@@ -776,24 +758,21 @@ static void t_write_key_order(void)
758 },
759 }
760 };
779 - int err;
761
762 reftable_writer_set_limits(w, 1, 1);
782 - err = reftable_writer_add_ref(w, &refs[0]);
783 - check(!err);
784 - err = reftable_writer_add_ref(w, &refs[1]);
785 - check_int(err, ==, REFTABLE_API_ERROR);
763 + cl_assert_equal_i(reftable_writer_add_ref(w, &refs[0]), 0);
764 + cl_assert_equal_i(reftable_writer_add_ref(w, &refs[1]),
765 + REFTABLE_API_ERROR);
766
767 refs[0].update_index = 2;
788 - err = reftable_writer_add_ref(w, &refs[0]);
789 - check_int(err, ==, REFTABLE_API_ERROR);
768 + cl_assert_equal_i(reftable_writer_add_ref(w, &refs[0]), REFTABLE_API_ERROR);
769
770 reftable_writer_close(w);
771 reftable_writer_free(w);
772 reftable_buf_release(&buf);
773 }
774
796 -static void t_write_multiple_indices(void)
775 +void test_reftable_readwrite__write_multiple_indices(void)
776 {
777 struct reftable_write_options opts = {
778 .block_size = 100,
@@ -805,9 +784,10 @@ static void t_write_multiple_indices(void)
784 struct reftable_writer *writer;
785 struct reftable_table *table;
786 char buf[128];
808 - int err, i;
787 + int i;
788 + int err;
789
810 - writer = t_reftable_strbuf_writer(&writer_buf, &opts);
790 + writer = cl_reftable_strbuf_writer(&writer_buf, &opts);
791 reftable_writer_set_limits(writer, 1, 1);
792 for (i = 0; i < 100; i++) {
793 struct reftable_ref_record ref = {
@@ -819,8 +799,7 @@ static void t_write_multiple_indices(void)
799 snprintf(buf, sizeof(buf), "refs/heads/%04d", i);
800 ref.refname = buf;
801
822 - err = reftable_writer_add_ref(writer, &ref);
823 - check(!err);
802 + cl_assert_equal_i(reftable_writer_add_ref(writer, &ref), 0);
803 }
804
805 for (i = 0; i < 100; i++) {
@@ -836,8 +815,7 @@ static void t_write_multiple_indices(void)
815 snprintf(buf, sizeof(buf), "refs/heads/%04d", i);
816 log.refname = buf;
817
839 - err = reftable_writer_add_log(writer, &log);
840 - check(!err);
818 + cl_assert_equal_i(reftable_writer_add_log(writer, &log), 0);
819 }
820
821 reftable_writer_close(writer);
@@ -847,22 +825,22 @@ static void t_write_multiple_indices(void)
825 * for each of the block types.
826 */
827 stats = reftable_writer_stats(writer);
850 - check_int(stats->ref_stats.index_offset, >, 0);
851 - check_int(stats->obj_stats.index_offset, >, 0);
852 - check_int(stats->log_stats.index_offset, >, 0);
828 + cl_assert(stats->ref_stats.index_offset > 0);
829 + cl_assert(stats->obj_stats.index_offset > 0);
830 + cl_assert(stats->log_stats.index_offset > 0);
831
832 block_source_from_buf(&source, &writer_buf);
833 err = reftable_table_new(&table, &source, "filename");
856 - check(!err);
834 + cl_assert(!err);
835
836 /*
837 * Seeking the log uses the log index now. In case there is any
838 * confusion regarding indices we would notice here.
839 */
840 err = reftable_table_init_log_iterator(table, &it);
863 - check(!err);
841 + cl_assert(!err);
842 err = reftable_iterator_seek_log(&it, "");
865 - check(!err);
843 + cl_assert(!err);
844
845 reftable_iterator_destroy(&it);
846 reftable_writer_free(writer);
@@ -870,7 +848,7 @@ static void t_write_multiple_indices(void)
848 reftable_buf_release(&writer_buf);
849 }
850
873 -static void t_write_multi_level_index(void)
851 +void test_reftable_readwrite__write_multi_level_index(void)
852 {
853 struct reftable_write_options opts = {
854 .block_size = 100,
@@ -883,7 +861,7 @@ static void t_write_multi_level_index(void)
861 struct reftable_table *table;
862 int err;
863
886 - writer = t_reftable_strbuf_writer(&writer_buf, &opts);
864 + writer = cl_reftable_strbuf_writer(&writer_buf, &opts);
865 reftable_writer_set_limits(writer, 1, 1);
866 for (size_t i = 0; i < 200; i++) {
867 struct reftable_ref_record ref = {
@@ -896,8 +874,7 @@ static void t_write_multi_level_index(void)
874 snprintf(buf, sizeof(buf), "refs/heads/%03" PRIuMAX, (uintmax_t)i);
875 ref.refname = buf;
876
899 - err = reftable_writer_add_ref(writer, &ref);
900 - check(!err);
877 + cl_assert_equal_i(reftable_writer_add_ref(writer, &ref), 0);
878 }
879 reftable_writer_close(writer);
880
@@ -906,19 +883,19 @@ static void t_write_multi_level_index(void)
883 * multi-level index.
884 */
885 stats = reftable_writer_stats(writer);
909 - check_int(stats->ref_stats.max_index_level, ==, 2);
886 + cl_assert_equal_i(stats->ref_stats.max_index_level, 2);
887
888 block_source_from_buf(&source, &writer_buf);
889 err = reftable_table_new(&table, &source, "filename");
913 - check(!err);
890 + cl_assert(!err);
891
892 /*
893 * Seeking the last ref should work as expected.
894 */
895 err = reftable_table_init_ref_iterator(table, &it);
919 - check(!err);
896 + cl_assert(!err);
897 err = reftable_iterator_seek_ref(&it, "refs/heads/199");
921 - check(!err);
898 + cl_assert(!err);
899
900 reftable_iterator_destroy(&it);
901 reftable_writer_free(writer);
@@ -927,7 +904,7 @@ static void t_write_multi_level_index(void)
904 reftable_buf_release(&buf);
905 }
906
930 -static void t_corrupt_table_empty(void)
907 +void test_reftable_readwrite__corrupt_table_empty(void)
908 {
909 struct reftable_buf buf = REFTABLE_BUF_INIT;
910 struct reftable_block_source source = { 0 };
@@ -936,50 +913,22 @@ static void t_corrupt_table_empty(void)
913
914 block_source_from_buf(&source, &buf);
915 err = reftable_table_new(&table, &source, "file.log");
939 - check_int(err, ==, REFTABLE_FORMAT_ERROR);
916 + cl_assert_equal_i(err, REFTABLE_FORMAT_ERROR);
917 }
918
942 -static void t_corrupt_table(void)
919 +void test_reftable_readwrite__corrupt_table(void)
920 {
921 uint8_t zeros[1024] = { 0 };
922 struct reftable_buf buf = REFTABLE_BUF_INIT;
923 struct reftable_block_source source = { 0 };
924 struct reftable_table *table;
925 int err;
949 - check(!reftable_buf_add(&buf, zeros, sizeof(zeros)));
926 +
927 + cl_assert(!reftable_buf_add(&buf, zeros, sizeof(zeros)));
928
929 block_source_from_buf(&source, &buf);
930 err = reftable_table_new(&table, &source, "file.log");
953 - check_int(err, ==, REFTABLE_FORMAT_ERROR);
931 + cl_assert_equal_i(err, REFTABLE_FORMAT_ERROR);
932
933 reftable_buf_release(&buf);
934 }
957 -
958 -int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
959 -{
960 - TEST(t_buffer(), "strbuf works as blocksource");
961 - TEST(t_corrupt_table(), "read-write on corrupted table");
962 - TEST(t_corrupt_table_empty(), "read-write on an empty table");
963 - TEST(t_log_buffer_size(), "buffer extension for log compression");
964 - TEST(t_log_overflow(), "log overflow returns expected error");
965 - TEST(t_log_write_limits(), "writer limits for writing log records");
966 - TEST(t_log_write_read(), "read-write on log records");
967 - TEST(t_log_zlib_corruption(), "reading corrupted log record returns expected error");
968 - TEST(t_table_read_api(), "read on a table");
969 - TEST(t_table_read_write_seek_index(), "read-write on a table with index");
970 - TEST(t_table_read_write_seek_linear(), "read-write on a table without index (SHA1)");
971 - TEST(t_table_read_write_seek_linear_sha256(), "read-write on a table without index (SHA256)");
972 - TEST(t_table_read_write_sequential(), "sequential read-write on a table");
973 - TEST(t_table_refs_for_no_index(), "refs-only table with no index");
974 - TEST(t_table_refs_for_obj_index(), "refs-only table with index");
975 - TEST(t_table_write_small_table(), "write_table works");
976 - TEST(t_write_empty_key(), "write on refs with empty keys");
977 - TEST(t_write_empty_table(), "read-write on empty tables");
978 - TEST(t_write_key_order(), "refs must be written in increasing order");
979 - TEST(t_write_multi_level_index(), "table with multi-level index");
980 - TEST(t_write_multiple_indices(), "table with indices for multiple block types");
981 - TEST(t_write_object_id_length(), "prefix compression on writing refs");
982 - TEST(t_write_object_id_min_length(), "prefix compression on writing refs");
983 -
984 - return test_done();
985 -}