Skip to content

Commit ec9c070

Browse files
Chandra Pratapgitster
authored andcommitted
t: move reftable/tree_test.c to the unit testing framework
reftable/tree_test.c exercises the functions defined in reftable/tree.{c, h}. Migrate reftable/tree_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 align with unit-tests' standards. Also add a comment to help understand the test routine. Note that this commit mostly moves the test from reftable/ to t/unit-tests/ and most of the refactoring is performed by the trailing commits. 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 e5a0f70 commit ec9c070

File tree

5 files changed

+61
-63
lines changed

5 files changed

+61
-63
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-tree
13391340
UNIT_TEST_PROGRAMS += t-strbuf
13401341
UNIT_TEST_PROGRAMS += t-strcmp-offset
13411342
UNIT_TEST_PROGRAMS += t-strvec
@@ -2681,7 +2682,6 @@ REFTABLE_TEST_OBJS += reftable/record_test.o
26812682
REFTABLE_TEST_OBJS += reftable/readwrite_test.o
26822683
REFTABLE_TEST_OBJS += reftable/stack_test.o
26832684
REFTABLE_TEST_OBJS += reftable/test_framework.o
2684-
REFTABLE_TEST_OBJS += reftable/tree_test.o
26852685

26862686
TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
26872687

reftable/reftable-tests.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ int pq_test_main(int argc, const char **argv);
1616
int record_test_main(int argc, const char **argv);
1717
int readwrite_test_main(int argc, const char **argv);
1818
int stack_test_main(int argc, const char **argv);
19-
int tree_test_main(int argc, const char **argv);
2019
int reftable_dump_main(int argc, char *const *argv);
2120

2221
#endif

reftable/tree_test.c

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

t/helper/test-reftable.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ int cmd__reftable(int argc, const char **argv)
88
basics_test_main(argc, argv);
99
record_test_main(argc, argv);
1010
block_test_main(argc, argv);
11-
tree_test_main(argc, argv);
1211
pq_test_main(argc, argv);
1312
readwrite_test_main(argc, argv);
1413
merged_test_main(argc, argv);

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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/tree.h"
11+
12+
static int t_compare(const void *a, const void *b)
13+
{
14+
return (char *)a - (char *)b;
15+
}
16+
17+
struct curry {
18+
void *last;
19+
};
20+
21+
static void check_increasing(void *arg, void *key)
22+
{
23+
struct curry *c = arg;
24+
if (c->last)
25+
check_int(t_compare(c->last, key), <, 0);
26+
c->last = key;
27+
}
28+
29+
static void t_tree(void)
30+
{
31+
struct tree_node *root = NULL;
32+
void *values[11] = { 0 };
33+
struct tree_node *nodes[11] = { 0 };
34+
size_t i = 1;
35+
struct curry c = { 0 };
36+
37+
/*
38+
* Pseudo-randomly insert the pointers for elements between
39+
* values[1] and values[10] (inclusive) in the tree.
40+
*/
41+
do {
42+
nodes[i] = tree_search(&values[i], &root, &t_compare, 1);
43+
i = (i * 7) % 11;
44+
} while (i != 1);
45+
46+
for (i = 1; i < ARRAY_SIZE(nodes); i++) {
47+
check_pointer_eq(&values[i], nodes[i]->key);
48+
check_pointer_eq(nodes[i], tree_search(&values[i], &root, &t_compare, 0));
49+
}
50+
51+
infix_walk(root, check_increasing, &c);
52+
tree_free(root);
53+
}
54+
55+
int cmd_main(int argc, const char *argv[])
56+
{
57+
TEST(t_tree(), "tree_search and infix_walk work");
58+
59+
return test_done();
60+
}

0 commit comments

Comments
 (0)