diff mbox series

[v6,3/5] t-reftable-tree: split test_tree() into two sub-test functions

Message ID 20240802121318.4583-4-chandrapratap3519@gmail.com (mailing list archive)
State Superseded
Headers show
Series t: port reftable/tree_test.c to the unit testing framework | expand

Commit Message

Chandra Pratap Aug. 2, 2024, 12:08 p.m. UTC
In the current testing setup, tests for both tree_search() and
infix_walk() defined by reftable/tree.{c, h} are performed by
a single test function, test_tree(). Split tree_test() into
test_tree_search() and test_infix_walk() responsible for
independently testing tree_search() and infix_walk() respectively.
This improves the overall readability of the test file as well as
simplifies debugging.

Note that the last parameter in the tree_search() functiom is
'int insert' which when set, inserts the key if it is not found
in the tree. Otherwise, the function returns NULL for such cases.

While at it, use 'func' to pass function pointers and not '&func'.

Mentored-by: Patrick Steinhardt <ps@pks.im>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
---
 t/unit-tests/t-reftable-tree.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

Comments

Junio C Hamano Aug. 2, 2024, 4:25 p.m. UTC | #1
Chandra Pratap <chandrapratap3519@gmail.com> writes:

> -	/* pseudo-randomly insert the pointers for elements between
> -	 * values[1] and values[10] (included) in the tree.
> +	/* Pseudo-randomly insert the pointers for elements between
> +	 * values[1] and values[10] (inclusive) in the tree.
>  	 */

If you are "fixing" the comment, let's fix its style as well.

	/*
         * Our multi-line comments begin with slash-asterisk, and
	 * end with asterisk-slash, on their own line.
	 */
diff mbox series

Patch

diff --git a/t/unit-tests/t-reftable-tree.c b/t/unit-tests/t-reftable-tree.c
index 08f1873ad3..5efe34835e 100644
--- a/t/unit-tests/t-reftable-tree.c
+++ b/t/unit-tests/t-reftable-tree.c
@@ -26,16 +26,15 @@  static void check_increasing(void *arg, void *key)
 	c->last = key;
 }
 
-static void t_tree(void)
+static void t_tree_search(void)
 {
 	struct tree_node *root = NULL;
 	void *values[11] = { 0 };
 	struct tree_node *nodes[11] = { 0 };
 	size_t i = 1;
-	struct curry c = { 0 };
 
-	/* pseudo-randomly insert the pointers for elements between
-	 * values[1] and values[10] (included) in the tree.
+	/* Pseudo-randomly insert the pointers for elements between
+	 * values[1] and values[10] (inclusive) in the tree.
 	 */
 	do {
 		nodes[i] = tree_search(&values[i], &root, &t_compare, 1);
@@ -47,13 +46,29 @@  static void t_tree(void)
 		check_pointer_eq(nodes[i], tree_search(&values[i], &root, &t_compare, 0));
 	}
 
-	infix_walk(root, check_increasing, &c);
+	tree_free(root);
+}
+
+static void t_infix_walk(void)
+{
+	struct tree_node *root = NULL;
+	void *values[11] = { 0 };
+	struct curry c = { 0 };
+	size_t i = 1;
+
+	do {
+		tree_search(&values[i], &root, t_compare, 1);
+		i = (i * 7) % 11;
+	} while (i != 1);
+
+	infix_walk(root, &check_increasing, &c);
 	tree_free(root);
 }
 
 int cmd_main(int argc, const char *argv[])
 {
-	TEST(t_tree(), "tree_search and infix_walk work");
+	TEST(t_tree_search(), "tree_search works");
+	TEST(t_infix_walk(), "infix_walk works");
 
 	return test_done();
 }