Skip to content

Commit afe5b9e

Browse files
Chandra Pratapgitster
authored andcommitted
t: move tests from reftable/record_test.c to the new unit test
common_prefix_size(), get_be24() and put_be24() are functions defined in reftable/basics.{c, h}. Move the tests for these functions from reftable/record_test.c to the newly ported test. Mentored-by: Patrick Steinhardt <[email protected]> Mentored-by: Christian Couder <[email protected]> Signed-off-by: Chandra Pratap <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f74e186 commit afe5b9e

File tree

2 files changed

+33
-44
lines changed

2 files changed

+33
-44
lines changed

reftable/record_test.c

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,6 @@ static void test_varint_roundtrip(void)
6464
}
6565
}
6666

67-
static void test_common_prefix(void)
68-
{
69-
struct {
70-
const char *a, *b;
71-
int want;
72-
} cases[] = {
73-
{ "abc", "ab", 2 },
74-
{ "", "abc", 0 },
75-
{ "abc", "abd", 2 },
76-
{ "abc", "pqr", 0 },
77-
};
78-
79-
int i = 0;
80-
for (i = 0; i < ARRAY_SIZE(cases); i++) {
81-
struct strbuf a = STRBUF_INIT;
82-
struct strbuf b = STRBUF_INIT;
83-
strbuf_addstr(&a, cases[i].a);
84-
strbuf_addstr(&b, cases[i].b);
85-
EXPECT(common_prefix_size(&a, &b) == cases[i].want);
86-
87-
strbuf_release(&a);
88-
strbuf_release(&b);
89-
}
90-
}
91-
9267
static void set_hash(uint8_t *h, int j)
9368
{
9469
int i = 0;
@@ -258,16 +233,6 @@ static void test_reftable_log_record_roundtrip(void)
258233
strbuf_release(&scratch);
259234
}
260235

261-
static void test_u24_roundtrip(void)
262-
{
263-
uint32_t in = 0x112233;
264-
uint8_t dest[3];
265-
uint32_t out;
266-
put_be24(dest, in);
267-
out = get_be24(dest);
268-
EXPECT(in == out);
269-
}
270-
271236
static void test_key_roundtrip(void)
272237
{
273238
uint8_t buffer[1024] = { 0 };
@@ -411,9 +376,7 @@ int record_test_main(int argc, const char *argv[])
411376
RUN_TEST(test_reftable_ref_record_roundtrip);
412377
RUN_TEST(test_varint_roundtrip);
413378
RUN_TEST(test_key_roundtrip);
414-
RUN_TEST(test_common_prefix);
415379
RUN_TEST(test_reftable_obj_record_roundtrip);
416380
RUN_TEST(test_reftable_index_record_roundtrip);
417-
RUN_TEST(test_u24_roundtrip);
418381
return 0;
419382
}

t/unit-tests/t-reftable-basics.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,38 @@ static void test_parse_names_drop_empty(void)
9999

100100
static void test_common_prefix(void)
101101
{
102-
struct strbuf s1 = STRBUF_INIT;
103-
struct strbuf s2 = STRBUF_INIT;
104-
strbuf_addstr(&s1, "abcdef");
105-
strbuf_addstr(&s2, "abc");
106-
check_int(common_prefix_size(&s1, &s2), ==, 3);
107-
strbuf_release(&s1);
108-
strbuf_release(&s2);
102+
struct strbuf a = STRBUF_INIT;
103+
struct strbuf b = STRBUF_INIT;
104+
struct {
105+
const char *a, *b;
106+
int want;
107+
} cases[] = {
108+
{"abcdef", "abc", 3},
109+
{ "abc", "ab", 2 },
110+
{ "", "abc", 0 },
111+
{ "abc", "abd", 2 },
112+
{ "abc", "pqr", 0 },
113+
};
114+
115+
for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
116+
strbuf_addstr(&a, cases[i].a);
117+
strbuf_addstr(&b, cases[i].b);
118+
check_int(common_prefix_size(&a, &b), ==, cases[i].want);
119+
strbuf_reset(&a);
120+
strbuf_reset(&b);
121+
}
122+
strbuf_release(&a);
123+
strbuf_release(&b);
124+
}
125+
126+
static void test_u24_roundtrip(void)
127+
{
128+
uint32_t in = 0x112233;
129+
uint8_t dest[3];
130+
uint32_t out;
131+
put_be24(dest, in);
132+
out = get_be24(dest);
133+
check_int(in, ==, out);
109134
}
110135

111136
int cmd_main(int argc, const char *argv[])
@@ -116,6 +141,7 @@ int cmd_main(int argc, const char *argv[])
116141
TEST(test_binsearch(), "binary search with binsearch works");
117142
TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
118143
TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
144+
TEST(test_u24_roundtrip(), "put_be24 and get_be24 work");
119145

120146
return test_done();
121147
}

0 commit comments

Comments
 (0)