Skip to content

Commit 7b11e20

Browse files
committed
Merge branch 'cp/unit-test-reftable-tree'
A test in reftable library has been rewritten using the unit test framework. * cp/unit-test-reftable-tree: t-reftable-tree: improve the test for infix_walk() t-reftable-tree: add test for non-existent key t-reftable-tree: split test_tree() into two sub-test functions t: move reftable/tree_test.c to the unit testing framework reftable: remove unnecessary curly braces in reftable/tree.c
2 parents 61fd5de + 3a498b4 commit 7b11e20

File tree

6 files changed

+90
-73
lines changed

6 files changed

+90
-73
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,7 @@ UNIT_TEST_PROGRAMS += t-reftable-basics
13431343
UNIT_TEST_PROGRAMS += t-reftable-merged
13441344
UNIT_TEST_PROGRAMS += t-reftable-pq
13451345
UNIT_TEST_PROGRAMS += t-reftable-record
1346+
UNIT_TEST_PROGRAMS += t-reftable-tree
13461347
UNIT_TEST_PROGRAMS += t-strbuf
13471348
UNIT_TEST_PROGRAMS += t-strcmp-offset
13481349
UNIT_TEST_PROGRAMS += t-strvec
@@ -2685,7 +2686,6 @@ REFTABLE_TEST_OBJS += reftable/dump.o
26852686
REFTABLE_TEST_OBJS += reftable/readwrite_test.o
26862687
REFTABLE_TEST_OBJS += reftable/stack_test.o
26872688
REFTABLE_TEST_OBJS += reftable/test_framework.o
2688-
REFTABLE_TEST_OBJS += reftable/tree_test.o
26892689

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

reftable/reftable-tests.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ int block_test_main(int argc, const char **argv);
1414
int record_test_main(int argc, const char **argv);
1515
int readwrite_test_main(int argc, const char **argv);
1616
int stack_test_main(int argc, const char **argv);
17-
int tree_test_main(int argc, const char **argv);
1817
int reftable_dump_main(int argc, char *const *argv);
1918

2019
#endif

reftable/tree.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,20 @@ struct tree_node *tree_search(void *key, struct tree_node **rootp,
3939
void infix_walk(struct tree_node *t, void (*action)(void *arg, void *key),
4040
void *arg)
4141
{
42-
if (t->left) {
42+
if (t->left)
4343
infix_walk(t->left, action, arg);
44-
}
4544
action(arg, t->key);
46-
if (t->right) {
45+
if (t->right)
4746
infix_walk(t->right, action, arg);
48-
}
4947
}
5048

5149
void tree_free(struct tree_node *t)
5250
{
53-
if (!t) {
51+
if (!t)
5452
return;
55-
}
56-
if (t->left) {
53+
if (t->left)
5754
tree_free(t->left);
58-
}
59-
if (t->right) {
55+
if (t->right)
6056
tree_free(t->right);
61-
}
6257
reftable_free(t);
6358
}

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
@@ -6,7 +6,6 @@ int cmd__reftable(int argc, const char **argv)
66
{
77
/* test from simple to complex. */
88
block_test_main(argc, argv);
9-
tree_test_main(argc, argv);
109
readwrite_test_main(argc, argv);
1110
stack_test_main(argc, argv);
1211
return 0;

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 **arr;
19+
size_t len;
20+
};
21+
22+
static void store(void *arg, void *key)
23+
{
24+
struct curry *c = arg;
25+
c->arr[c->len++] = key;
26+
}
27+
28+
static void t_tree_search(void)
29+
{
30+
struct tree_node *root = NULL;
31+
void *values[11] = { 0 };
32+
struct tree_node *nodes[11] = { 0 };
33+
size_t i = 1;
34+
35+
/*
36+
* Pseudo-randomly insert the pointers for elements between
37+
* values[1] and values[10] (inclusive) in the tree.
38+
*/
39+
do {
40+
nodes[i] = tree_search(&values[i], &root, &t_compare, 1);
41+
i = (i * 7) % 11;
42+
} while (i != 1);
43+
44+
for (i = 1; i < ARRAY_SIZE(nodes); i++) {
45+
check_pointer_eq(&values[i], nodes[i]->key);
46+
check_pointer_eq(nodes[i], tree_search(&values[i], &root, &t_compare, 0));
47+
}
48+
49+
check(!tree_search(values, &root, t_compare, 0));
50+
tree_free(root);
51+
}
52+
53+
static void t_infix_walk(void)
54+
{
55+
struct tree_node *root = NULL;
56+
void *values[11] = { 0 };
57+
void *out[11] = { 0 };
58+
struct curry c = {
59+
.arr = (void **) &out,
60+
};
61+
size_t i = 1;
62+
size_t count = 0;
63+
64+
do {
65+
tree_search(&values[i], &root, t_compare, 1);
66+
i = (i * 7) % 11;
67+
count++;
68+
} while (i != 1);
69+
70+
infix_walk(root, &store, &c);
71+
for (i = 1; i < ARRAY_SIZE(values); i++)
72+
check_pointer_eq(&values[i], out[i - 1]);
73+
check(!out[i - 1]);
74+
check_int(c.len, ==, count);
75+
tree_free(root);
76+
}
77+
78+
int cmd_main(int argc, const char *argv[])
79+
{
80+
TEST(t_tree_search(), "tree_search works");
81+
TEST(t_infix_walk(), "infix_walk works");
82+
83+
return test_done();
84+
}

0 commit comments

Comments
 (0)