Skip to content

Commit ba9661b

Browse files
Chandra Pratapgitster
authored andcommitted
t: move reftable/record_test.c to the unit testing framework
reftable/record_test.c exercises the functions defined in reftable/record.{c, h}. Migrate reftable/record_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework, and renaming the tests to fit unit-tests' naming scheme. While at it, change the type of index variable 'i' to 'size_t' from 'int'. This is because 'i' is used in comparison against 'ARRAY_SIZE(x)' which is of type 'size_t'. Also, use set_hash() which is defined locally in the test file instead of set_test_hash() which is defined by reftable/test_framework.{c, h}. This is fine to do as both these functions are similarly implemented, and reftable/test_framework.{c, h} is not #included in the ported test. Get rid of reftable_record_print() from the tests as well, because it clutters the test framework's output and we have no way of verifying the output. Mentored-by: Patrick Steinhardt <[email protected]> Mentored-by: Christian Couder <[email protected]> Signed-off-by: Chandra Pratap <[email protected]> Acked-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d63586c commit ba9661b

File tree

3 files changed

+61
-73
lines changed

3 files changed

+61
-73
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,7 @@ UNIT_TEST_PROGRAMS += t-hash
13381338
UNIT_TEST_PROGRAMS += t-mem-pool
13391339
UNIT_TEST_PROGRAMS += t-prio-queue
13401340
UNIT_TEST_PROGRAMS += t-reftable-basics
1341+
UNIT_TEST_PROGRAMS += t-reftable-record
13411342
UNIT_TEST_PROGRAMS += t-strbuf
13421343
UNIT_TEST_PROGRAMS += t-strcmp-offset
13431344
UNIT_TEST_PROGRAMS += t-strvec
@@ -2678,7 +2679,6 @@ REFTABLE_TEST_OBJS += reftable/block_test.o
26782679
REFTABLE_TEST_OBJS += reftable/dump.o
26792680
REFTABLE_TEST_OBJS += reftable/merged_test.o
26802681
REFTABLE_TEST_OBJS += reftable/pq_test.o
2681-
REFTABLE_TEST_OBJS += reftable/record_test.o
26822682
REFTABLE_TEST_OBJS += reftable/readwrite_test.o
26832683
REFTABLE_TEST_OBJS += reftable/stack_test.o
26842684
REFTABLE_TEST_OBJS += reftable/test_framework.o

t/helper/test-reftable.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
int cmd__reftable(int argc, const char **argv)
66
{
77
/* test from simple to complex. */
8-
record_test_main(argc, argv);
98
block_test_main(argc, argv);
109
tree_test_main(argc, argv);
1110
pq_test_main(argc, argv);

reftable/record_test.c renamed to t/unit-tests/t-reftable-record.c

Lines changed: 60 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66
https://developers.google.com/open-source/licenses/bsd
77
*/
88

9-
#include "record.h"
9+
#include "test-lib.h"
10+
#include "reftable/constants.h"
11+
#include "reftable/record.h"
1012

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)
1814
{
1915
struct reftable_record copy;
2016
uint8_t typ;
@@ -24,15 +20,12 @@ static void test_copy(struct reftable_record *rec)
2420
reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
2521
/* do it twice to catch memory leaks */
2622
reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
27-
EXPECT(reftable_record_equal(rec, &copy, GIT_SHA1_RAWSZ));
28-
29-
puts("testing print coverage:\n");
30-
reftable_record_print(&copy, GIT_SHA1_RAWSZ);
23+
check(reftable_record_equal(rec, &copy, GIT_SHA1_RAWSZ));
3124

3225
reftable_record_release(&copy);
3326
}
3427

35-
static void test_varint_roundtrip(void)
28+
static void t_varint_roundtrip(void)
3629
{
3730
uint64_t inputs[] = { 0,
3831
1,
@@ -43,8 +36,8 @@ static void test_varint_roundtrip(void)
4336
4096,
4437
((uint64_t)1 << 63),
4538
((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++) {
4841
uint8_t dest[10];
4942

5043
struct string_view out = {
@@ -55,29 +48,26 @@ static void test_varint_roundtrip(void)
5548
int n = put_var_int(&out, in);
5649
uint64_t got = 0;
5750

58-
EXPECT(n > 0);
51+
check_int(n, >, 0);
5952
out.len = n;
6053
n = get_var_int(&got, &out);
61-
EXPECT(n > 0);
54+
check_int(n, >, 0);
6255

63-
EXPECT(got == in);
56+
check_int(got, ==, in);
6457
}
6558
}
6659

6760
static void set_hash(uint8_t *h, int j)
6861
{
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++)
7163
h[i] = (j >> i) & 0xff;
72-
}
7364
}
7465

75-
static void test_reftable_ref_record_roundtrip(void)
66+
static void t_reftable_ref_record_roundtrip(void)
7667
{
7768
struct strbuf scratch = STRBUF_INIT;
78-
int i = 0;
7969

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++) {
8171
struct reftable_record in = {
8272
.type = BLOCK_TYPE_REF,
8373
};
@@ -107,19 +97,19 @@ static void test_reftable_ref_record_roundtrip(void)
10797
}
10898
in.u.ref.refname = xstrdup("refs/heads/master");
10999

110-
test_copy(&in);
100+
t_copy(&in);
111101

112-
EXPECT(reftable_record_val_type(&in) == i);
102+
check_int(reftable_record_val_type(&in), ==, i);
113103

114104
reftable_record_key(&in, &key);
115105
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
116-
EXPECT(n > 0);
106+
check_int(n, >, 0);
117107

118108
/* decode into a non-zero reftable_record to test for leaks. */
119109
m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ, &scratch);
120-
EXPECT(n == m);
110+
check_int(n, ==, m);
121111

122-
EXPECT(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
112+
check(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
123113
GIT_SHA1_RAWSZ));
124114
reftable_record_release(&in);
125115

@@ -130,7 +120,7 @@ static void test_reftable_ref_record_roundtrip(void)
130120
strbuf_release(&scratch);
131121
}
132122

133-
static void test_reftable_log_record_equal(void)
123+
static void t_reftable_log_record_equal(void)
134124
{
135125
struct reftable_log_record in[2] = {
136126
{
@@ -143,16 +133,15 @@ static void test_reftable_log_record_equal(void)
143133
}
144134
};
145135

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));
147137
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));
149139
reftable_log_record_release(&in[0]);
150140
reftable_log_record_release(&in[1]);
151141
}
152142

153-
static void test_reftable_log_record_roundtrip(void)
143+
static void t_reftable_log_record_roundtrip(void)
154144
{
155-
int i;
156145
struct reftable_log_record in[] = {
157146
{
158147
.refname = xstrdup("refs/heads/master"),
@@ -180,12 +169,12 @@ static void test_reftable_log_record_roundtrip(void)
180169
}
181170
};
182171
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);
183176

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++) {
189178
struct reftable_record rec = { .type = BLOCK_TYPE_LOG };
190179
struct strbuf key = STRBUF_INIT;
191180
uint8_t buffer[1024] = { 0 };
@@ -212,18 +201,18 @@ static void test_reftable_log_record_roundtrip(void)
212201

213202
rec.u.log = in[i];
214203

215-
test_copy(&rec);
204+
t_copy(&rec);
216205

217206
reftable_record_key(&rec, &key);
218207

219208
n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
220-
EXPECT(n >= 0);
209+
check_int(n, >=, 0);
221210
valtype = reftable_record_val_type(&rec);
222211
m = reftable_record_decode(&out, key, valtype, dest,
223212
GIT_SHA1_RAWSZ, &scratch);
224-
EXPECT(n == m);
213+
check_int(n, ==, m);
225214

226-
EXPECT(reftable_log_record_equal(&in[i], &out.u.log,
215+
check(reftable_log_record_equal(&in[i], &out.u.log,
227216
GIT_SHA1_RAWSZ));
228217
reftable_log_record_release(&in[i]);
229218
strbuf_release(&key);
@@ -233,7 +222,7 @@ static void test_reftable_log_record_roundtrip(void)
233222
strbuf_release(&scratch);
234223
}
235224

236-
static void test_key_roundtrip(void)
225+
static void t_key_roundtrip(void)
237226
{
238227
uint8_t buffer[1024] = { 0 };
239228
struct string_view dest = {
@@ -252,21 +241,21 @@ static void test_key_roundtrip(void)
252241
strbuf_addstr(&key, "refs/tags/bla");
253242
extra = 6;
254243
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);
257246

258247
strbuf_addstr(&roundtrip, "refs/heads/master");
259248
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);
263252

264253
strbuf_release(&last_key);
265254
strbuf_release(&key);
266255
strbuf_release(&roundtrip);
267256
}
268257

269-
static void test_reftable_obj_record_roundtrip(void)
258+
static void t_reftable_obj_record_roundtrip(void)
270259
{
271260
uint8_t testHash1[GIT_SHA1_RAWSZ] = { 1, 2, 3, 4, 0 };
272261
uint64_t till9[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
@@ -289,9 +278,8 @@ static void test_reftable_obj_record_roundtrip(void)
289278
},
290279
};
291280
struct strbuf scratch = STRBUF_INIT;
292-
int i = 0;
293281

294-
for (i = 0; i < ARRAY_SIZE(recs); i++) {
282+
for (size_t i = 0; i < ARRAY_SIZE(recs); i++) {
295283
uint8_t buffer[1024] = { 0 };
296284
struct string_view dest = {
297285
.buf = buffer,
@@ -308,24 +296,24 @@ static void test_reftable_obj_record_roundtrip(void)
308296
int n, m;
309297
uint8_t extra;
310298

311-
test_copy(&in);
299+
t_copy(&in);
312300
reftable_record_key(&in, &key);
313301
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
314-
EXPECT(n > 0);
302+
check_int(n, >, 0);
315303
extra = reftable_record_val_type(&in);
316304
m = reftable_record_decode(&out, key, extra, dest,
317305
GIT_SHA1_RAWSZ, &scratch);
318-
EXPECT(n == m);
306+
check_int(n, ==, m);
319307

320-
EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
308+
check(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
321309
strbuf_release(&key);
322310
reftable_record_release(&out);
323311
}
324312

325313
strbuf_release(&scratch);
326314
}
327315

328-
static void test_reftable_index_record_roundtrip(void)
316+
static void t_reftable_index_record_roundtrip(void)
329317
{
330318
struct reftable_record in = {
331319
.type = BLOCK_TYPE_INDEX,
@@ -350,33 +338,34 @@ static void test_reftable_index_record_roundtrip(void)
350338

351339
strbuf_addstr(&in.u.idx.last_key, "refs/heads/master");
352340
reftable_record_key(&in, &key);
353-
test_copy(&in);
341+
t_copy(&in);
354342

355-
EXPECT(0 == strbuf_cmp(&key, &in.u.idx.last_key));
343+
check(!strbuf_cmp(&key, &in.u.idx.last_key));
356344
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
357-
EXPECT(n > 0);
345+
check_int(n, >, 0);
358346

359347
extra = reftable_record_val_type(&in);
360348
m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ,
361349
&scratch);
362-
EXPECT(m == n);
350+
check_int(m, ==, n);
363351

364-
EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
352+
check(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
365353

366354
reftable_record_release(&out);
367355
strbuf_release(&key);
368356
strbuf_release(&scratch);
369357
strbuf_release(&in.u.idx.last_key);
370358
}
371359

372-
int record_test_main(int argc, const char *argv[])
360+
int cmd_main(int argc, const char *argv[])
373361
{
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();
382371
}

0 commit comments

Comments
 (0)