Skip to content

Commit 1528c48

Browse files
Chandra Pratapgitster
authored andcommitted
t-reftable-block: add tests for obj blocks
In the current testing setup, block operations are left unexercised for obj blocks. Add a test that exercises these operations for obj 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 5cba561 commit 1528c48

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,91 @@ static void t_log_block_read_write(void)
191191
reftable_record_release(&recs[i]);
192192
}
193193

194+
static void t_obj_block_read_write(void)
195+
{
196+
const int header_off = 21;
197+
struct reftable_record recs[30];
198+
const size_t N = ARRAY_SIZE(recs);
199+
const size_t block_size = 1024;
200+
struct reftable_block block = { 0 };
201+
struct block_writer bw = {
202+
.last_key = STRBUF_INIT,
203+
};
204+
struct reftable_record rec = {
205+
.type = BLOCK_TYPE_OBJ,
206+
};
207+
size_t i = 0;
208+
int ret;
209+
struct block_reader br = { 0 };
210+
struct block_iter it = BLOCK_ITER_INIT;
211+
struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT;
212+
213+
REFTABLE_CALLOC_ARRAY(block.data, block_size);
214+
block.len = block_size;
215+
block_source_from_strbuf(&block.source, &buf);
216+
block_writer_init(&bw, BLOCK_TYPE_OBJ, block.data, block_size,
217+
header_off, hash_size(GIT_SHA1_FORMAT_ID));
218+
219+
for (i = 0; i < N; i++) {
220+
uint8_t bytes[] = { i, i + 1, i + 2, i + 3, i + 5 }, *allocated;
221+
DUP_ARRAY(allocated, bytes, ARRAY_SIZE(bytes));
222+
223+
rec.u.obj.hash_prefix = allocated;
224+
rec.u.obj.hash_prefix_len = 5;
225+
226+
recs[i] = rec;
227+
ret = block_writer_add(&bw, &rec);
228+
rec.u.obj.hash_prefix = NULL;
229+
rec.u.obj.hash_prefix_len = 0;
230+
check_int(ret, ==, 0);
231+
}
232+
233+
ret = block_writer_finish(&bw);
234+
check_int(ret, >, 0);
235+
236+
block_writer_release(&bw);
237+
238+
block_reader_init(&br, &block, header_off, block_size, GIT_SHA1_RAWSZ);
239+
240+
block_iter_seek_start(&it, &br);
241+
242+
for (i = 0; ; i++) {
243+
ret = block_iter_next(&it, &rec);
244+
check_int(ret, >=, 0);
245+
if (ret > 0) {
246+
check_int(i, ==, N);
247+
break;
248+
}
249+
check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));
250+
}
251+
252+
for (i = 0; i < N; i++) {
253+
block_iter_reset(&it);
254+
reftable_record_key(&recs[i], &want);
255+
256+
ret = block_iter_seek_key(&it, &br, &want);
257+
check_int(ret, ==, 0);
258+
259+
ret = block_iter_next(&it, &rec);
260+
check_int(ret, ==, 0);
261+
262+
check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));
263+
}
264+
265+
block_reader_release(&br);
266+
block_iter_close(&it);
267+
reftable_record_release(&rec);
268+
reftable_block_done(&br.block);
269+
strbuf_release(&want);
270+
strbuf_release(&buf);
271+
for (i = 0; i < N; i++)
272+
reftable_record_release(&recs[i]);
273+
}
274+
194275
int cmd_main(int argc, const char *argv[])
195276
{
196277
TEST(t_log_block_read_write(), "read-write operations on log blocks work");
278+
TEST(t_obj_block_read_write(), "read-write operations on obj blocks work");
197279
TEST(t_ref_block_read_write(), "read-write operations on ref blocks work");
198280

199281
return test_done();

0 commit comments

Comments
 (0)