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
12 #include "blocksource.h"
14 #include "constants.h"
16 #include "test_framework.h"
17 #include "reftable-tests.h"
19 static void test_block_read_write(void)
21 const int header_off
= 21; /* random */
23 const int N
= ARRAY_SIZE(names
);
24 const int block_size
= 1024;
25 struct reftable_block block
= { NULL
};
26 struct block_writer bw
= {
27 .last_key
= STRBUF_INIT
,
29 struct reftable_record rec
= {
30 .type
= BLOCK_TYPE_REF
,
34 struct block_reader br
= { 0 };
35 struct block_iter it
= BLOCK_ITER_INIT
;
37 struct strbuf want
= STRBUF_INIT
;
39 REFTABLE_CALLOC_ARRAY(block
.data
, block_size
);
40 block
.len
= block_size
;
41 block
.source
= malloc_block_source();
42 block_writer_init(&bw
, BLOCK_TYPE_REF
, block
.data
, block_size
,
43 header_off
, hash_size(GIT_SHA1_FORMAT_ID
));
45 rec
.u
.ref
.refname
= (char *) "";
46 rec
.u
.ref
.value_type
= REFTABLE_REF_DELETION
;
47 n
= block_writer_add(&bw
, &rec
);
48 EXPECT(n
== REFTABLE_API_ERROR
);
50 for (i
= 0; i
< N
; i
++) {
52 snprintf(name
, sizeof(name
), "branch%02d", i
);
54 rec
.u
.ref
.refname
= name
;
55 rec
.u
.ref
.value_type
= REFTABLE_REF_VAL1
;
56 memset(rec
.u
.ref
.value
.val1
, i
, GIT_SHA1_RAWSZ
);
58 names
[i
] = xstrdup(name
);
59 n
= block_writer_add(&bw
, &rec
);
60 rec
.u
.ref
.refname
= NULL
;
61 rec
.u
.ref
.value_type
= REFTABLE_REF_DELETION
;
65 n
= block_writer_finish(&bw
);
68 block_writer_release(&bw
);
70 block_reader_init(&br
, &block
, header_off
, block_size
, GIT_SHA1_RAWSZ
);
72 block_iter_seek_start(&it
, &br
);
75 int r
= block_iter_next(&it
, &rec
);
80 EXPECT_STREQ(names
[j
], rec
.u
.ref
.refname
);
84 reftable_record_release(&rec
);
85 block_iter_close(&it
);
87 for (i
= 0; i
< N
; i
++) {
88 struct block_iter it
= BLOCK_ITER_INIT
;
90 strbuf_addstr(&want
, names
[i
]);
92 n
= block_iter_seek_key(&it
, &br
, &want
);
95 n
= block_iter_next(&it
, &rec
);
98 EXPECT_STREQ(names
[i
], rec
.u
.ref
.refname
);
101 n
= block_iter_seek_key(&it
, &br
, &want
);
104 n
= block_iter_next(&it
, &rec
);
106 EXPECT_STREQ(names
[10 * (i
/ 10)], rec
.u
.ref
.refname
);
108 block_iter_close(&it
);
111 reftable_record_release(&rec
);
112 reftable_block_done(&br
.block
);
113 strbuf_release(&want
);
114 for (i
= 0; i
< N
; i
++) {
115 reftable_free(names
[i
]);
119 int block_test_main(int argc
, const char *argv
[])
121 RUN_TEST(test_block_read_write
);