Skip to content

Commit 02212bf

Browse files
author
Chandra Pratap
committed
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 <ps@pks.im> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
1 parent 07dbda8 commit 02212bf

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
@@ -190,9 +190,91 @@ static void t_log_block_read_write(void)
190190
reftable_record_release(&recs[i]);
191191
}
192192

193+
static void t_obj_block_read_write(void)
194+
{
195+
const int header_off = 21;
196+
struct reftable_record recs[30];
197+
const size_t N = ARRAY_SIZE(recs);
198+
const size_t block_size = 1024;
199+
struct reftable_block block = { 0 };
200+
struct block_writer bw = {
201+
.last_key = STRBUF_INIT,
202+
};
203+
struct reftable_record rec = {
204+
.type = BLOCK_TYPE_OBJ,
205+
};
206+
size_t i = 0;
207+
int ret;
208+
struct block_reader br = { 0 };
209+
struct block_iter it = BLOCK_ITER_INIT;
210+
struct strbuf want = STRBUF_INIT;
211+
212+
REFTABLE_CALLOC_ARRAY(block.data, block_size);
213+
block.len = block_size;
214+
block.source = malloc_block_source();
215+
block_writer_init(&bw, BLOCK_TYPE_OBJ, block.data, block_size,
216+
header_off, hash_size(GIT_SHA1_FORMAT_ID));
217+
218+
for (i = 0; i < N; i++) {
219+
uint8_t bytes[] = { i, i + 1, i + 2, i + 3, i + 5 }, *allocated;
220+
allocated = reftable_malloc(ARRAY_SIZE(bytes));
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+
for (i = 0; i < N; i++)
271+
reftable_record_release(&recs[i]);
272+
}
273+
193274
int cmd_main(int argc, const char *argv[])
194275
{
195276
TEST(t_log_block_read_write(), "read-write operations on log blocks work");
277+
TEST(t_obj_block_read_write(), "read-write operations on obj blocks work");
196278
TEST(t_ref_block_read_write(), "read-write operations on ref blocks work");
197279

198280
return test_done();

0 commit comments

Comments
 (0)