t-reftable-block: use xstrfmt() instead of xstrdup()
[git/gitster.git] / t / unit-tests / t-reftable-block.c
blob81484bc646b3763570eb52ce83306873a004bd22
1 /*
2 Copyright 2020 Google LLC
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://developers.google.com/open-source/licenses/bsd
7 */
9 #include "test-lib.h"
10 #include "reftable/block.h"
11 #include "reftable/blocksource.h"
12 #include "reftable/constants.h"
13 #include "reftable/reftable-error.h"
15 static void t_block_read_write(void)
17 const int header_off = 21; /* random */
18 struct reftable_record recs[30];
19 const size_t N = ARRAY_SIZE(recs);
20 const size_t block_size = 1024;
21 struct reftable_block block = { 0 };
22 struct block_writer bw = {
23 .last_key = STRBUF_INIT,
25 struct reftable_record rec = {
26 .type = BLOCK_TYPE_REF,
28 size_t i = 0;
29 int ret;
30 struct block_reader br = { 0 };
31 struct block_iter it = BLOCK_ITER_INIT;
32 size_t j = 0;
33 struct strbuf want = STRBUF_INIT;
35 REFTABLE_CALLOC_ARRAY(block.data, block_size);
36 block.len = block_size;
37 block.source = malloc_block_source();
38 block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size,
39 header_off, hash_size(GIT_SHA1_FORMAT_ID));
41 rec.u.ref.refname = (char *) "";
42 rec.u.ref.value_type = REFTABLE_REF_DELETION;
43 ret = block_writer_add(&bw, &rec);
44 check_int(ret, ==, REFTABLE_API_ERROR);
46 for (i = 0; i < N; i++) {
47 rec.u.ref.refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i);
48 rec.u.ref.value_type = REFTABLE_REF_VAL1;
49 memset(rec.u.ref.value.val1, i, GIT_SHA1_RAWSZ);
51 recs[i] = rec;
52 ret = block_writer_add(&bw, &rec);
53 rec.u.ref.refname = NULL;
54 rec.u.ref.value_type = REFTABLE_REF_DELETION;
55 check_int(ret, ==, 0);
58 ret = block_writer_finish(&bw);
59 check_int(ret, >, 0);
61 block_writer_release(&bw);
63 block_reader_init(&br, &block, header_off, block_size, GIT_SHA1_RAWSZ);
65 block_iter_seek_start(&it, &br);
67 while (1) {
68 ret = block_iter_next(&it, &rec);
69 check_int(ret, >=, 0);
70 if (ret > 0) {
71 check_int(i, ==, N);
72 break;
74 check(reftable_record_equal(&recs[j], &rec, GIT_SHA1_RAWSZ));
75 j++;
78 for (i = 0; i < N; i++) {
79 block_iter_reset(&it);
80 reftable_record_key(&recs[i], &want);
82 ret = block_iter_seek_key(&it, &br, &want);
83 check_int(ret, ==, 0);
85 ret = block_iter_next(&it, &rec);
86 check_int(ret, ==, 0);
88 check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));
90 want.len--;
91 ret = block_iter_seek_key(&it, &br, &want);
92 check_int(ret, ==, 0);
94 ret = block_iter_next(&it, &rec);
95 check_int(ret, ==, 0);
96 check(reftable_record_equal(&recs[10 * (i / 10)], &rec, GIT_SHA1_RAWSZ));
99 block_reader_release(&br);
100 block_iter_close(&it);
101 reftable_record_release(&rec);
102 reftable_block_done(&br.block);
103 strbuf_release(&want);
104 for (i = 0; i < N; i++)
105 reftable_record_release(&recs[i]);
108 int cmd_main(int argc, const char *argv[])
110 TEST(t_block_read_write(), "read-write operations on blocks work");
112 return test_done();