Han-Wen Nienhuys | 35425d1 | 2021-10-07 20:25:05 +0000 | [diff] [blame] | 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 | |
Elijah Newren | e3a3f5e | 2023-04-22 20:17:29 +0000 | [diff] [blame] | 9 | #include "system.h" |
Han-Wen Nienhuys | 35425d1 | 2021-10-07 20:25:05 +0000 | [diff] [blame] | 10 | #include "tree.h" |
| 11 | |
Han-Wen Nienhuys | 35425d1 | 2021-10-07 20:25:05 +0000 | [diff] [blame] | 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 | } |