t: move reftable/tree_test.c to the unit testing framework

reftable/tree_test.c exercises the functions defined in reftable/tree.{c, h}. Migrate reftable/tree_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework and renaming the tests to align with unit-tests' standards. Also add a comment to help understand the test routine. Note that this commit mostly moves the test from reftable/ to t/unit-tests/ and most of the refactoring is performed by the trailing commits. 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>

Chandra Pratap committed Aug 4, 2024 at 19:36 UTC ec9c0704fc6ad646f5482d3e31e3a5b3be1677ef
5 files changed +61 -63
Makefile
+1 -1
@@ -1336,6 +1336,7 @@ THIRD_PARTY_SOURCES += sha1dc/%
1336 UNIT_TEST_PROGRAMS += t-ctype
1337 UNIT_TEST_PROGRAMS += t-mem-pool
1338 UNIT_TEST_PROGRAMS += t-prio-queue
1339 +UNIT_TEST_PROGRAMS += t-reftable-tree
1340 UNIT_TEST_PROGRAMS += t-strbuf
1341 UNIT_TEST_PROGRAMS += t-strcmp-offset
1342 UNIT_TEST_PROGRAMS += t-strvec
@@ -2681,7 +2682,6 @@ REFTABLE_TEST_OBJS += reftable/record_test.o
2682 REFTABLE_TEST_OBJS += reftable/readwrite_test.o
2683 REFTABLE_TEST_OBJS += reftable/stack_test.o
2684 REFTABLE_TEST_OBJS += reftable/test_framework.o
2684 -REFTABLE_TEST_OBJS += reftable/tree_test.o
2685
2686 TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
2687
reftable/reftable-tests.h
-1
@@ -16,7 +16,6 @@ int pq_test_main(int argc, const char **argv);
16 int record_test_main(int argc, const char **argv);
17 int readwrite_test_main(int argc, const char **argv);
18 int stack_test_main(int argc, const char **argv);
19 -int tree_test_main(int argc, const char **argv);
19 int reftable_dump_main(int argc, char *const *argv);
20
21 #endif
reftable/tree_test.c deleted
-60
@@ -1,60 +0,0 @@
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 "system.h"
10 -#include "tree.h"
11 -
12 -#include "test_framework.h"
13 -#include "reftable-tests.h"
14 -
15 -static int test_compare(const void *a, const void *b)
16 -{
17 - return (char *)a - (char *)b;
18 -}
19 -
20 -struct curry {
21 - void *last;
22 -};
23 -
24 -static void check_increasing(void *arg, void *key)
25 -{
26 - struct curry *c = arg;
27 - if (c->last) {
28 - EXPECT(test_compare(c->last, key) < 0);
29 - }
30 - c->last = key;
31 -}
32 -
33 -static void test_tree(void)
34 -{
35 - struct tree_node *root = NULL;
36 -
37 - void *values[11] = { NULL };
38 - struct tree_node *nodes[11] = { NULL };
39 - int i = 1;
40 - struct curry c = { NULL };
41 - do {
42 - nodes[i] = tree_search(values + i, &root, &test_compare, 1);
43 - i = (i * 7) % 11;
44 - } while (i != 1);
45 -
46 - for (i = 1; i < ARRAY_SIZE(nodes); i++) {
47 - EXPECT(values + i == nodes[i]->key);
48 - EXPECT(nodes[i] ==
49 - tree_search(values + i, &root, &test_compare, 0));
50 - }
51 -
52 - infix_walk(root, check_increasing, &c);
53 - tree_free(root);
54 -}
55 -
56 -int tree_test_main(int argc, const char *argv[])
57 -{
58 - RUN_TEST(test_tree);
59 - return 0;
60 -}
t/helper/test-reftable.c
-1
@@ -8,7 +8,6 @@ int cmd__reftable(int argc, const char **argv)
8 basics_test_main(argc, argv);
9 record_test_main(argc, argv);
10 block_test_main(argc, argv);
11 - tree_test_main(argc, argv);
11 pq_test_main(argc, argv);
12 readwrite_test_main(argc, argv);
13 merged_test_main(argc, argv);
t/unit-tests/t-reftable-tree.c new
+60
@@ -0,0 +1,60 @@
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 *last;
19 +};
20 +
21 +static void check_increasing(void *arg, void *key)
22 +{
23 + struct curry *c = arg;
24 + if (c->last)
25 + check_int(t_compare(c->last, key), <, 0);
26 + c->last = key;
27 +}
28 +
29 +static void t_tree(void)
30 +{
31 + struct tree_node *root = NULL;
32 + void *values[11] = { 0 };
33 + struct tree_node *nodes[11] = { 0 };
34 + size_t i = 1;
35 + struct curry c = { 0 };
36 +
37 + /*
38 + * Pseudo-randomly insert the pointers for elements between
39 + * values[1] and values[10] (inclusive) in the tree.
40 + */
41 + do {
42 + nodes[i] = tree_search(&values[i], &root, &t_compare, 1);
43 + i = (i * 7) % 11;
44 + } while (i != 1);
45 +
46 + for (i = 1; i < ARRAY_SIZE(nodes); i++) {
47 + check_pointer_eq(&values[i], nodes[i]->key);
48 + check_pointer_eq(nodes[i], tree_search(&values[i], &root, &t_compare, 0));
49 + }
50 +
51 + infix_walk(root, check_increasing, &c);
52 + tree_free(root);
53 +}
54 +
55 +int cmd_main(int argc, const char *argv[])
56 +{
57 + TEST(t_tree(), "tree_search and infix_walk work");
58 +
59 + return test_done();
60 +}