Skip to content

Commit 7b75355

Browse files
Chandra Pratapgitster
Chandra Pratap
authored andcommitted
t-reftable-block: add tests for index blocks
In the current testing setup, block operations are left unexercised for index blocks. Add a test that exercises these operations for index blocks. 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>
1 parent 12895b5 commit 7b75355

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

t/unit-tests/t-reftable-block.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,95 @@ static void t_obj_block_read_write(void)
271271
reftable_record_release(&recs[i]);
272272
}
273273

274+
static void t_index_block_read_write(void)
275+
{
276+
const int header_off = 21;
277+
struct reftable_record recs[30];
278+
const size_t N = ARRAY_SIZE(recs);
279+
const size_t block_size = 1024;
280+
struct reftable_block block = { 0 };
281+
struct block_writer bw = {
282+
.last_key = STRBUF_INIT,
283+
};
284+
struct reftable_record rec = {
285+
.type = BLOCK_TYPE_INDEX,
286+
.u.idx.last_key = STRBUF_INIT,
287+
};
288+
size_t i = 0;
289+
int ret;
290+
struct block_reader br = { 0 };
291+
struct block_iter it = BLOCK_ITER_INIT;
292+
struct strbuf want = STRBUF_INIT;
293+
294+
REFTABLE_CALLOC_ARRAY(block.data, block_size);
295+
block.len = block_size;
296+
block.source = malloc_block_source();
297+
block_writer_init(&bw, BLOCK_TYPE_INDEX, block.data, block_size,
298+
header_off, hash_size(GIT_SHA1_FORMAT_ID));
299+
300+
for (i = 0; i < N; i++) {
301+
strbuf_init(&recs[i].u.idx.last_key, 9);
302+
303+
recs[i].type = BLOCK_TYPE_INDEX;
304+
strbuf_addf(&recs[i].u.idx.last_key, "branch%02"PRIuMAX, (uintmax_t)i);
305+
recs[i].u.idx.offset = i;
306+
307+
ret = block_writer_add(&bw, &recs[i]);
308+
check_int(ret, ==, 0);
309+
}
310+
311+
ret = block_writer_finish(&bw);
312+
check_int(ret, >, 0);
313+
314+
block_writer_release(&bw);
315+
316+
block_reader_init(&br, &block, header_off, block_size, GIT_SHA1_RAWSZ);
317+
318+
block_iter_seek_start(&it, &br);
319+
320+
for (i = 0; ; i++) {
321+
ret = block_iter_next(&it, &rec);
322+
check_int(ret, >=, 0);
323+
if (ret > 0) {
324+
check_int(i, ==, N);
325+
break;
326+
}
327+
check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));
328+
}
329+
330+
for (i = 0; i < N; i++) {
331+
block_iter_reset(&it);
332+
reftable_record_key(&recs[i], &want);
333+
334+
ret = block_iter_seek_key(&it, &br, &want);
335+
check_int(ret, ==, 0);
336+
337+
ret = block_iter_next(&it, &rec);
338+
check_int(ret, ==, 0);
339+
340+
check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));
341+
342+
want.len--;
343+
ret = block_iter_seek_key(&it, &br, &want);
344+
check_int(ret, ==, 0);
345+
346+
ret = block_iter_next(&it, &rec);
347+
check_int(ret, ==, 0);
348+
check(reftable_record_equal(&recs[10 * (i / 10)], &rec, GIT_SHA1_RAWSZ));
349+
}
350+
351+
block_reader_release(&br);
352+
block_iter_close(&it);
353+
reftable_record_release(&rec);
354+
reftable_block_done(&br.block);
355+
strbuf_release(&want);
356+
for (i = 0; i < N; i++)
357+
reftable_record_release(&recs[i]);
358+
}
359+
274360
int cmd_main(int argc, const char *argv[])
275361
{
362+
TEST(t_index_block_read_write(), "read-write operations on index blocks work");
276363
TEST(t_log_block_read_write(), "read-write operations on log blocks work");
277364
TEST(t_obj_block_read_write(), "read-write operations on obj blocks work");
278365
TEST(t_ref_block_read_write(), "read-write operations on ref blocks work");

0 commit comments

Comments
 (0)