Skip to content

Commit 56346ba

Browse files
committed
Merge branch 'cp/reftable-unit-test'
Basic unit tests for reftable have been reimplemented under the unit test framework. * cp/reftable-unit-test: t: improve the test-case for parse_names() t: add test for put_be16() t: move tests from reftable/record_test.c to the new unit test t: move tests from reftable/stack_test.c to the new unit test t: move reftable/basics_test.c to the unit testing framework
2 parents a39e28a + efa8786 commit 56346ba

File tree

6 files changed

+161
-169
lines changed

6 files changed

+161
-169
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,7 @@ THIRD_PARTY_SOURCES += sha1dc/%
13361336
UNIT_TEST_PROGRAMS += t-ctype
13371337
UNIT_TEST_PROGRAMS += t-mem-pool
13381338
UNIT_TEST_PROGRAMS += t-prio-queue
1339+
UNIT_TEST_PROGRAMS += t-reftable-basics
13391340
UNIT_TEST_PROGRAMS += t-strbuf
13401341
UNIT_TEST_PROGRAMS += t-strcmp-offset
13411342
UNIT_TEST_PROGRAMS += t-strvec
@@ -2672,7 +2673,6 @@ REFTABLE_OBJS += reftable/stack.o
26722673
REFTABLE_OBJS += reftable/tree.o
26732674
REFTABLE_OBJS += reftable/writer.o
26742675

2675-
REFTABLE_TEST_OBJS += reftable/basics_test.o
26762676
REFTABLE_TEST_OBJS += reftable/block_test.o
26772677
REFTABLE_TEST_OBJS += reftable/dump.o
26782678
REFTABLE_TEST_OBJS += reftable/merged_test.o

reftable/basics_test.c

Lines changed: 0 additions & 105 deletions
This file was deleted.

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
}

reftable/stack_test.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,6 @@ static void test_read_file(void)
102102
(void) remove(fn);
103103
}
104104

105-
static void test_parse_names(void)
106-
{
107-
char buf[] = "line\n";
108-
char **names = NULL;
109-
parse_names(buf, strlen(buf), &names);
110-
111-
EXPECT(NULL != names[0]);
112-
EXPECT(0 == strcmp(names[0], "line"));
113-
EXPECT(NULL == names[1]);
114-
free_names(names);
115-
}
116-
117-
static void test_names_equal(void)
118-
{
119-
char *a[] = { "a", "b", "c", NULL };
120-
char *b[] = { "a", "b", "d", NULL };
121-
char *c[] = { "a", "b", NULL };
122-
123-
EXPECT(names_equal(a, a));
124-
EXPECT(!names_equal(a, b));
125-
EXPECT(!names_equal(a, c));
126-
}
127-
128105
static int write_test_ref(struct reftable_writer *wr, void *arg)
129106
{
130107
struct reftable_ref_record *ref = arg;
@@ -1034,8 +1011,6 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
10341011
int stack_test_main(int argc, const char *argv[])
10351012
{
10361013
RUN_TEST(test_empty_add);
1037-
RUN_TEST(test_names_equal);
1038-
RUN_TEST(test_parse_names);
10391014
RUN_TEST(test_read_file);
10401015
RUN_TEST(test_reflog_expire);
10411016
RUN_TEST(test_reftable_stack_add);

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-
basics_test_main(argc, argv);
98
record_test_main(argc, argv);
109
block_test_main(argc, argv);
1110
tree_test_main(argc, argv);

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

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
Copyright 2020 Google LLC
3+
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+
*/
8+
9+
#include "test-lib.h"
10+
#include "reftable/basics.h"
11+
12+
struct integer_needle_lesseq_args {
13+
int needle;
14+
int *haystack;
15+
};
16+
17+
static int integer_needle_lesseq(size_t i, void *_args)
18+
{
19+
struct integer_needle_lesseq_args *args = _args;
20+
return args->needle <= args->haystack[i];
21+
}
22+
23+
static void test_binsearch(void)
24+
{
25+
int haystack[] = { 2, 4, 6, 8, 10 };
26+
struct {
27+
int needle;
28+
size_t expected_idx;
29+
} testcases[] = {
30+
{-9000, 0},
31+
{-1, 0},
32+
{0, 0},
33+
{2, 0},
34+
{3, 1},
35+
{4, 1},
36+
{7, 3},
37+
{9, 4},
38+
{10, 4},
39+
{11, 5},
40+
{9000, 5},
41+
};
42+
43+
for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) {
44+
struct integer_needle_lesseq_args args = {
45+
.haystack = haystack,
46+
.needle = testcases[i].needle,
47+
};
48+
size_t idx;
49+
50+
idx = binsearch(ARRAY_SIZE(haystack), &integer_needle_lesseq, &args);
51+
check_int(idx, ==, testcases[i].expected_idx);
52+
}
53+
}
54+
55+
static void test_names_length(void)
56+
{
57+
char *a[] = { "a", "b", NULL };
58+
check_int(names_length(a), ==, 2);
59+
}
60+
61+
static void test_names_equal(void)
62+
{
63+
char *a[] = { "a", "b", "c", NULL };
64+
char *b[] = { "a", "b", "d", NULL };
65+
char *c[] = { "a", "b", NULL };
66+
67+
check(names_equal(a, a));
68+
check(!names_equal(a, b));
69+
check(!names_equal(a, c));
70+
}
71+
72+
static void test_parse_names_normal(void)
73+
{
74+
char in1[] = "line\n";
75+
char in2[] = "a\nb\nc";
76+
char **out = NULL;
77+
parse_names(in1, strlen(in1), &out);
78+
check_str(out[0], "line");
79+
check(!out[1]);
80+
free_names(out);
81+
82+
parse_names(in2, strlen(in2), &out);
83+
check_str(out[0], "a");
84+
check_str(out[1], "b");
85+
check_str(out[2], "c");
86+
check(!out[3]);
87+
free_names(out);
88+
}
89+
90+
static void test_parse_names_drop_empty(void)
91+
{
92+
char in[] = "a\n\nb\n";
93+
char **out = NULL;
94+
parse_names(in, strlen(in), &out);
95+
check_str(out[0], "a");
96+
/* simply '\n' should be dropped as empty string */
97+
check_str(out[1], "b");
98+
check(!out[2]);
99+
free_names(out);
100+
}
101+
102+
static void test_common_prefix(void)
103+
{
104+
struct strbuf a = STRBUF_INIT;
105+
struct strbuf b = STRBUF_INIT;
106+
struct {
107+
const char *a, *b;
108+
int want;
109+
} cases[] = {
110+
{"abcdef", "abc", 3},
111+
{ "abc", "ab", 2 },
112+
{ "", "abc", 0 },
113+
{ "abc", "abd", 2 },
114+
{ "abc", "pqr", 0 },
115+
};
116+
117+
for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
118+
strbuf_addstr(&a, cases[i].a);
119+
strbuf_addstr(&b, cases[i].b);
120+
check_int(common_prefix_size(&a, &b), ==, cases[i].want);
121+
strbuf_reset(&a);
122+
strbuf_reset(&b);
123+
}
124+
strbuf_release(&a);
125+
strbuf_release(&b);
126+
}
127+
128+
static void test_u24_roundtrip(void)
129+
{
130+
uint32_t in = 0x112233;
131+
uint8_t dest[3];
132+
uint32_t out;
133+
put_be24(dest, in);
134+
out = get_be24(dest);
135+
check_int(in, ==, out);
136+
}
137+
138+
static void test_u16_roundtrip(void)
139+
{
140+
uint32_t in = 0xfef1;
141+
uint8_t dest[3];
142+
uint32_t out;
143+
put_be16(dest, in);
144+
out = get_be16(dest);
145+
check_int(in, ==, out);
146+
}
147+
148+
int cmd_main(int argc, const char *argv[])
149+
{
150+
TEST(test_common_prefix(), "common_prefix_size works");
151+
TEST(test_parse_names_normal(), "parse_names works for basic input");
152+
TEST(test_parse_names_drop_empty(), "parse_names drops empty string");
153+
TEST(test_binsearch(), "binary search with binsearch works");
154+
TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
155+
TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
156+
TEST(test_u24_roundtrip(), "put_be24 and get_be24 work");
157+
TEST(test_u16_roundtrip(), "put_be16 and get_be16 work");
158+
159+
return test_done();
160+
}

0 commit comments

Comments
 (0)