]> git.ipfire.org Git - thirdparty/git.git/blob - reftable/tree_test.c
submodule-config.c: strengthen URL fsck check
[thirdparty/git.git] / reftable / tree_test.c
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 "basics.h"
13 #include "record.h"
14 #include "test_framework.h"
15 #include "reftable-tests.h"
16
17 static int test_compare(const void *a, const void *b)
18 {
19 return (char *)a - (char *)b;
20 }
21
22 struct curry {
23 void *last;
24 };
25
26 static void check_increasing(void *arg, void *key)
27 {
28 struct curry *c = arg;
29 if (c->last) {
30 EXPECT(test_compare(c->last, key) < 0);
31 }
32 c->last = key;
33 }
34
35 static void test_tree(void)
36 {
37 struct tree_node *root = NULL;
38
39 void *values[11] = { NULL };
40 struct tree_node *nodes[11] = { NULL };
41 int i = 1;
42 struct curry c = { NULL };
43 do {
44 nodes[i] = tree_search(values + i, &root, &test_compare, 1);
45 i = (i * 7) % 11;
46 } while (i != 1);
47
48 for (i = 1; i < ARRAY_SIZE(nodes); i++) {
49 EXPECT(values + i == nodes[i]->key);
50 EXPECT(nodes[i] ==
51 tree_search(values + i, &root, &test_compare, 0));
52 }
53
54 infix_walk(root, check_increasing, &c);
55 tree_free(root);
56 }
57
58 int tree_test_main(int argc, const char *argv[])
59 {
60 RUN_TEST(test_tree);
61 return 0;
62 }