Skip to content

Commit 3a498b4

Browse files
Chandra Pratapgitster
authored andcommitted
t-reftable-tree: improve the test for infix_walk()
In the current testing setup for infix_walk(), the following properties of an infix traversal of a tree remain untested: - every node of the tree must be visited - every node must be visited exactly once In fact, only the property 'traversal in increasing order' is tested. Modify test_infix_walk() to check for all the properties above. This can be achieved by storing the nodes' keys linearly, in a nullified buffer, as we visit them and then checking the input keys against this buffer in increasing order. By checking that the element just after the last input key is 'NULL' in the output buffer, we ensure that every node is traversed exactly once. 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 c70022c commit 3a498b4

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ static int t_compare(const void *a, const void *b)
1515
}
1616

1717
struct curry {
18-
void *last;
18+
void **arr;
19+
size_t len;
1920
};
2021

21-
static void check_increasing(void *arg, void *key)
22+
static void store(void *arg, void *key)
2223
{
2324
struct curry *c = arg;
24-
if (c->last)
25-
check_int(t_compare(c->last, key), <, 0);
26-
c->last = key;
25+
c->arr[c->len++] = key;
2726
}
2827

2928
static void t_tree_search(void)
@@ -55,15 +54,24 @@ static void t_infix_walk(void)
5554
{
5655
struct tree_node *root = NULL;
5756
void *values[11] = { 0 };
58-
struct curry c = { 0 };
57+
void *out[11] = { 0 };
58+
struct curry c = {
59+
.arr = (void **) &out,
60+
};
5961
size_t i = 1;
62+
size_t count = 0;
6063

6164
do {
6265
tree_search(&values[i], &root, t_compare, 1);
6366
i = (i * 7) % 11;
67+
count++;
6468
} while (i != 1);
6569

66-
infix_walk(root, &check_increasing, &c);
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);
6775
tree_free(root);
6876
}
6977

0 commit comments

Comments
 (0)