aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChandra Pratap <chandrapratap3519@gmail.com>2024-08-04 19:36:49 +0530
committerJunio C Hamano <gitster@pobox.com>2024-08-04 09:50:27 -0700
commit3a498b49d1b7d1ffc939b87310533a40ecbd8c43 (patch)
tree97b7f4cdad3de1dbb9b48c4e7e058fb559a5ec93
parentc70022c1b9b3a6ea4d4d3f4b54edf0d759e520e3 (diff)
downloadgit-3a498b49d1b7d1ffc939b87310533a40ecbd8c43.tar.gz
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 <ps@pks.im> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--t/unit-tests/t-reftable-tree.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/t/unit-tests/t-reftable-tree.c b/t/unit-tests/t-reftable-tree.c
index 2220414a18..e7d774d774 100644
--- a/t/unit-tests/t-reftable-tree.c
+++ b/t/unit-tests/t-reftable-tree.c
@@ -15,15 +15,14 @@ static int t_compare(const void *a, const void *b)
}
struct curry {
- void *last;
+ void **arr;
+ size_t len;
};
-static void check_increasing(void *arg, void *key)
+static void store(void *arg, void *key)
{
struct curry *c = arg;
- if (c->last)
- check_int(t_compare(c->last, key), <, 0);
- c->last = key;
+ c->arr[c->len++] = key;
}
static void t_tree_search(void)
@@ -55,15 +54,24 @@ static void t_infix_walk(void)
{
struct tree_node *root = NULL;
void *values[11] = { 0 };
- struct curry c = { 0 };
+ void *out[11] = { 0 };
+ struct curry c = {
+ .arr = (void **) &out,
+ };
size_t i = 1;
+ size_t count = 0;
do {
tree_search(&values[i], &root, t_compare, 1);
i = (i * 7) % 11;
+ count++;
} while (i != 1);
- infix_walk(root, &check_increasing, &c);
+ infix_walk(root, &store, &c);
+ for (i = 1; i < ARRAY_SIZE(values); i++)
+ check_pointer_eq(&values[i], out[i - 1]);
+ check(!out[i - 1]);
+ check_int(c.len, ==, count);
tree_free(root);
}