6
https://developers.google.com/open-source/licenses/bsd
7
*/
8
9
-#include "record.h"
9
+#include "test-lib.h"
10
+#include "reftable/constants.h"
11
+#include "reftable/record.h"
12
11
-#include "system.h"
12
-#include "basics.h"
13
-#include "constants.h"
14
-#include "test_framework.h"
15
-#include "reftable-tests.h"
16
-
17
-static void test_copy(struct reftable_record *rec)
13
+static void t_copy(struct reftable_record *rec)
14
{
15
struct reftable_record copy;
16
uint8_t typ;
20
reftable_record_copy_from(©, rec, GIT_SHA1_RAWSZ);
21
/* do it twice to catch memory leaks */
22
reftable_record_copy_from(©, rec, GIT_SHA1_RAWSZ);
27
- EXPECT(reftable_record_equal(rec, ©, GIT_SHA1_RAWSZ));
28
-
29
- puts("testing print coverage:\n");
30
- reftable_record_print(©, GIT_SHA1_RAWSZ);
23
+ check(reftable_record_equal(rec, ©, GIT_SHA1_RAWSZ));
24
25
reftable_record_release(©);
26
}
27
35
-static void test_varint_roundtrip(void)
28
+static void t_varint_roundtrip(void)
29
{
30
uint64_t inputs[] = { 0,
31
1,
36
4096,
37
((uint64_t)1 << 63),
38
((uint64_t)1 << 63) + ((uint64_t)1 << 63) - 1 };
46
- int i = 0;
47
- for (i = 0; i < ARRAY_SIZE(inputs); i++) {
39
+
40
+ for (size_t i = 0; i < ARRAY_SIZE(inputs); i++) {
41
uint8_t dest[10];
42
43
struct string_view out = {
48
int n = put_var_int(&out, in);
49
uint64_t got = 0;
50
58
- EXPECT(n > 0);
51
+ check_int(n, >, 0);
52
out.len = n;
53
n = get_var_int(&got, &out);
61
- EXPECT(n > 0);
54
+ check_int(n, >, 0);
55
63
- EXPECT(got == in);
56
+ check_int(got, ==, in);
57
}
58
}
59
60
static void set_hash(uint8_t *h, int j)
61
{
69
- int i = 0;
70
- for (i = 0; i < hash_size(GIT_SHA1_FORMAT_ID); i++) {
62
+ for (int i = 0; i < hash_size(GIT_SHA1_FORMAT_ID); i++)
63
h[i] = (j >> i) & 0xff;
72
- }
64
}
65
75
-static void test_reftable_ref_record_roundtrip(void)
66
+static void t_reftable_ref_record_roundtrip(void)
67
{
68
struct strbuf scratch = STRBUF_INIT;
78
- int i = 0;
69
80
- for (i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) {
70
+ for (int i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) {
71
struct reftable_record in = {
72
.type = BLOCK_TYPE_REF,
73
};
97
}
98
in.u.ref.refname = xstrdup("refs/heads/master");
99
110
- test_copy(&in);
100
+ t_copy(&in);
101
112
- EXPECT(reftable_record_val_type(&in) == i);
102
+ check_int(reftable_record_val_type(&in), ==, i);
103
104
reftable_record_key(&in, &key);
105
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
116
- EXPECT(n > 0);
106
+ check_int(n, >, 0);
107
108
/* decode into a non-zero reftable_record to test for leaks. */
109
m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ, &scratch);
120
- EXPECT(n == m);
110
+ check_int(n, ==, m);
111
122
- EXPECT(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
112
+ check(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
113
GIT_SHA1_RAWSZ));
114
reftable_record_release(&in);
115
120
strbuf_release(&scratch);
121
}
122
133
-static void test_reftable_log_record_equal(void)
123
+static void t_reftable_log_record_equal(void)
124
{
125
struct reftable_log_record in[2] = {
126
{
133
}
134
};
135
146
- EXPECT(!reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
136
+ check(!reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
137
in[1].update_index = in[0].update_index;
148
- EXPECT(reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
138
+ check(reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
139
reftable_log_record_release(&in[0]);
140
reftable_log_record_release(&in[1]);
141
}
142
153
-static void test_reftable_log_record_roundtrip(void)
143
+static void t_reftable_log_record_roundtrip(void)
144
{
155
- int i;
145
struct reftable_log_record in[] = {
146
{
147
.refname = xstrdup("refs/heads/master"),
169
}
170
};
171
struct strbuf scratch = STRBUF_INIT;
172
+ set_hash(in[0].value.update.new_hash, 1);
173
+ set_hash(in[0].value.update.old_hash, 2);
174
+ set_hash(in[2].value.update.new_hash, 3);
175
+ set_hash(in[2].value.update.old_hash, 4);
176
184
- set_test_hash(in[0].value.update.new_hash, 1);
185
- set_test_hash(in[0].value.update.old_hash, 2);
186
- set_test_hash(in[2].value.update.new_hash, 3);
187
- set_test_hash(in[2].value.update.old_hash, 4);
188
- for (i = 0; i < ARRAY_SIZE(in); i++) {
177
+ for (size_t i = 0; i < ARRAY_SIZE(in); i++) {
178
struct reftable_record rec = { .type = BLOCK_TYPE_LOG };
179
struct strbuf key = STRBUF_INIT;
180
uint8_t buffer[1024] = { 0 };
201
202
rec.u.log = in[i];
203
215
- test_copy(&rec);
204
+ t_copy(&rec);
205
206
reftable_record_key(&rec, &key);
207
208
n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
220
- EXPECT(n >= 0);
209
+ check_int(n, >=, 0);
210
valtype = reftable_record_val_type(&rec);
211
m = reftable_record_decode(&out, key, valtype, dest,
212
GIT_SHA1_RAWSZ, &scratch);
224
- EXPECT(n == m);
213
+ check_int(n, ==, m);
214
226
- EXPECT(reftable_log_record_equal(&in[i], &out.u.log,
215
+ check(reftable_log_record_equal(&in[i], &out.u.log,
216
GIT_SHA1_RAWSZ));
217
reftable_log_record_release(&in[i]);
218
strbuf_release(&key);
222
strbuf_release(&scratch);
223
}
224
236
-static void test_key_roundtrip(void)
225
+static void t_key_roundtrip(void)
226
{
227
uint8_t buffer[1024] = { 0 };
228
struct string_view dest = {
241
strbuf_addstr(&key, "refs/tags/bla");
242
extra = 6;
243
n = reftable_encode_key(&restart, dest, last_key, key, extra);
255
- EXPECT(!restart);
256
- EXPECT(n > 0);
244
+ check(!restart);
245
+ check_int(n, >, 0);
246
247
strbuf_addstr(&roundtrip, "refs/heads/master");
248
m = reftable_decode_key(&roundtrip, &rt_extra, dest);
260
- EXPECT(n == m);
261
- EXPECT(0 == strbuf_cmp(&key, &roundtrip));
262
- EXPECT(rt_extra == extra);
249
+ check_int(n, ==, m);
250
+ check(!strbuf_cmp(&key, &roundtrip));
251
+ check_int(rt_extra, ==, extra);
252
253
strbuf_release(&last_key);
254
strbuf_release(&key);
255
strbuf_release(&roundtrip);
256
}
257
269
-static void test_reftable_obj_record_roundtrip(void)
258
+static void t_reftable_obj_record_roundtrip(void)
259
{
260
uint8_t testHash1[GIT_SHA1_RAWSZ] = { 1, 2, 3, 4, 0 };
261
uint64_t till9[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
278
},
279
};
280
struct strbuf scratch = STRBUF_INIT;
292
- int i = 0;
281
294
- for (i = 0; i < ARRAY_SIZE(recs); i++) {
282
+ for (size_t i = 0; i < ARRAY_SIZE(recs); i++) {
283
uint8_t buffer[1024] = { 0 };
284
struct string_view dest = {
285
.buf = buffer,
296
int n, m;
297
uint8_t extra;
298
311
- test_copy(&in);
299
+ t_copy(&in);
300
reftable_record_key(&in, &key);
301
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
314
- EXPECT(n > 0);
302
+ check_int(n, >, 0);
303
extra = reftable_record_val_type(&in);
304
m = reftable_record_decode(&out, key, extra, dest,
305
GIT_SHA1_RAWSZ, &scratch);
318
- EXPECT(n == m);
306
+ check_int(n, ==, m);
307
320
- EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
308
+ check(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
309
strbuf_release(&key);
310
reftable_record_release(&out);
311
}
313
strbuf_release(&scratch);
314
}
315
328
-static void test_reftable_index_record_roundtrip(void)
316
+static void t_reftable_index_record_roundtrip(void)
317
{
318
struct reftable_record in = {
319
.type = BLOCK_TYPE_INDEX,
338
339
strbuf_addstr(&in.u.idx.last_key, "refs/heads/master");
340
reftable_record_key(&in, &key);
353
- test_copy(&in);
341
+ t_copy(&in);
342
355
- EXPECT(0 == strbuf_cmp(&key, &in.u.idx.last_key));
343
+ check(!strbuf_cmp(&key, &in.u.idx.last_key));
344
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
357
- EXPECT(n > 0);
345
+ check_int(n, >, 0);
346
347
extra = reftable_record_val_type(&in);
348
m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ,
349
&scratch);
362
- EXPECT(m == n);
350
+ check_int(m, ==, n);
351
364
- EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
352
+ check(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
353
354
reftable_record_release(&out);
355
strbuf_release(&key);
357
strbuf_release(&in.u.idx.last_key);
358
}
359
372
-int record_test_main(int argc, const char *argv[])
360
+int cmd_main(int argc, const char *argv[])
361
{
374
- RUN_TEST(test_reftable_log_record_equal);
375
- RUN_TEST(test_reftable_log_record_roundtrip);
376
- RUN_TEST(test_reftable_ref_record_roundtrip);
377
- RUN_TEST(test_varint_roundtrip);
378
- RUN_TEST(test_key_roundtrip);
379
- RUN_TEST(test_reftable_obj_record_roundtrip);
380
- RUN_TEST(test_reftable_index_record_roundtrip);
381
- return 0;
362
+ TEST(t_reftable_log_record_equal(), "reftable_log_record_equal works");
363
+ TEST(t_reftable_log_record_roundtrip(), "record operations work on log record");
364
+ TEST(t_reftable_ref_record_roundtrip(), "record operations work on ref record");
365
+ TEST(t_varint_roundtrip(), "put_var_int and get_var_int work");
366
+ TEST(t_key_roundtrip(), "reftable_encode_key and reftable_decode_key work");
367
+ TEST(t_reftable_obj_record_roundtrip(), "record operations work on obj record");
368
+ TEST(t_reftable_index_record_roundtrip(), "record operations work on index record");
369
+
370
+ return test_done();
371
}