Skip to content

Commit 772408f

Browse files
Chandra Pratapgitster
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 <[email protected]> Mentored-by: Christian Couder <[email protected]> Signed-off-by: Chandra Pratap <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1528c48 commit 772408f

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,96 @@ static void t_obj_block_read_write(void)
272272
reftable_record_release(&recs[i]);
273273
}
274274

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

0 commit comments

Comments
 (0)