blob: cbff12558868a2ad29337da146ed7c34d063634c [file] [log] [blame]
Han-Wen Nienhuys35425d12021-10-07 20:25:05 +00001/*
2Copyright 2020 Google LLC
3
4Use of this source code is governed by a BSD-style
5license that can be found in the LICENSE file or at
6https://developers.google.com/open-source/licenses/bsd
7*/
8
9#include "tree.h"
10
11#include "basics.h"
12#include "record.h"
13#include "test_framework.h"
14#include "reftable-tests.h"
15
16static int test_compare(const void *a, const void *b)
17{
18 return (char *)a - (char *)b;
19}
20
21struct curry {
22 void *last;
23};
24
25static void check_increasing(void *arg, void *key)
26{
27 struct curry *c = arg;
28 if (c->last) {
29 EXPECT(test_compare(c->last, key) < 0);
30 }
31 c->last = key;
32}
33
34static void test_tree(void)
35{
36 struct tree_node *root = NULL;
37
38 void *values[11] = { NULL };
39 struct tree_node *nodes[11] = { NULL };
40 int i = 1;
41 struct curry c = { NULL };
42 do {
43 nodes[i] = tree_search(values + i, &root, &test_compare, 1);
44 i = (i * 7) % 11;
45 } while (i != 1);
46
47 for (i = 1; i < ARRAY_SIZE(nodes); i++) {
48 EXPECT(values + i == nodes[i]->key);
49 EXPECT(nodes[i] ==
50 tree_search(values + i, &root, &test_compare, 0));
51 }
52
53 infix_walk(root, check_increasing, &c);
54 tree_free(root);
55}
56
57int tree_test_main(int argc, const char *argv[])
58{
59 RUN_TEST(test_tree);
60 return 0;
61}