Skip to content

Commit

Permalink
t-reftable-tree: improve the test for infix_walk()
Browse files Browse the repository at this point in the history
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 only
and only the property of '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]>
  • Loading branch information
Chandra Pratap committed Jun 11, 2024
1 parent c976a37 commit c501403
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions t/unit-tests/t-reftable-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ static int test_compare(const void *a, const void *b)
}

struct curry {
void *last;
void **arr;
size_t i;
};

static void check_increasing(void *arg, void *key)
static void store(void *arg, void *key)
{
struct curry *c = arg;
if (c->last)
check_int(test_compare(c->last, key), <, 0);
c->last = key;
c->arr[c->i++] = key;
}

static void test_tree_search(void)
Expand Down Expand Up @@ -51,6 +50,7 @@ static void test_infix_walk(void)
{
struct tree_node *root = NULL;
void *values[11] = { 0 };
void *out[20] = { 0 };
struct curry c = { 0 };
size_t i = 1;

Expand All @@ -59,7 +59,11 @@ static void test_infix_walk(void)
i = (i * 7) % 11;
} while (i != 1);

infix_walk(root, &check_increasing, &c);
c.arr = (void **) &out;
infix_walk(root, &store, &c);
for (i = 1; i < ARRAY_SIZE(values); i++)
check_pointer_eq(values + i, out[i - 1]);
check(!out[i]);
tree_free(root);
}

Expand Down

0 comments on commit c501403

Please sign in to comment.