a5952b3b52db141e4b254056efc046ecee88954e
[mesa.git] / src / util / rb_tree_test.c
1 /*
2 * Copyright © 2017 Jason Ekstrand
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #undef NDEBUG
24
25 #include "rb_tree.h"
26
27 #include <assert.h>
28 #include <limits.h>
29
30 /* A list of 100 random numbers from 1 to 100. The number 30 is explicitly
31 * missing from this list.
32 */
33 int test_numbers[] = {
34 26, 12, 35, 15, 48, 11, 39, 23, 40, 18,
35 39, 15, 40, 11, 42, 2, 5, 2, 28, 8,
36 10, 22, 23, 38, 47, 12, 31, 22, 26, 39,
37 9, 42, 32, 18, 36, 8, 32, 29, 9, 3,
38 32, 49, 23, 11, 43, 41, 22, 42, 6, 35,
39 38, 48, 5, 35, 39, 44, 22, 16, 16, 32,
40 31, 50, 48, 5, 50, 8, 2, 32, 27, 34,
41 42, 48, 22, 47, 10, 48, 39, 36, 28, 40,
42 32, 33, 21, 17, 14, 38, 27, 6, 25, 18,
43 32, 38, 19, 22, 20, 47, 50, 41, 29, 50,
44 };
45
46 #define NON_EXISTANT_NUMBER 30
47
48 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a))
49
50 struct rb_test_node {
51 int key;
52 struct rb_node node;
53 };
54
55 static int
56 rb_test_node_cmp_void(const struct rb_node *n, const void *v)
57 {
58 struct rb_test_node *tn = rb_node_data(struct rb_test_node, n, node);
59 return *(int *)v - tn->key;
60 }
61
62 static int
63 rb_test_node_cmp(const struct rb_node *a, const struct rb_node *b)
64 {
65 struct rb_test_node *ta = rb_node_data(struct rb_test_node, a, node);
66 struct rb_test_node *tb = rb_node_data(struct rb_test_node, b, node);
67
68 return tb->key - ta->key;
69 }
70
71 static void
72 validate_tree_order(struct rb_tree *tree, unsigned expected_count)
73 {
74 struct rb_test_node *prev = NULL;
75 int max_val = -1;
76 unsigned count = 0;
77 rb_tree_foreach(struct rb_test_node, n, tree, node) {
78 /* Everything should be in increasing order */
79 assert(n->key >= max_val);
80 if (n->key > max_val) {
81 max_val = n->key;
82 } else {
83 /* Things should be stable, i.e., given equal keys, they should
84 * show up in the list in order of insertion. We insert them
85 * in the order they are in in the array.
86 */
87 if (prev == NULL || prev < n);
88 }
89
90 prev = n;
91 count++;
92 }
93 assert(count == expected_count);
94
95 prev = NULL;
96 int min_val = INT_MAX;
97 count = 0;
98 rb_tree_foreach_rev(struct rb_test_node, n, tree, node) {
99 /* Everything should be in increasing order */
100 assert(n->key <= min_val);
101 if (n->key < min_val) {
102 min_val = n->key;
103 } else {
104 /* Things should be stable, i.e., given equal keys, they should
105 * show up in the list in order of insertion. We insert them
106 * in the order they are in in the array.
107 */
108 if (prev == NULL || prev > n);
109 }
110
111 prev = n;
112 count++;
113 }
114 assert(count == expected_count);
115 }
116
117 static void
118 validate_search(struct rb_tree *tree, int first_number,
119 int last_number)
120 {
121 struct rb_node *n;
122 struct rb_test_node *tn;
123
124 /* Search for all of the values */
125 for (int i = first_number; i <= last_number; i++) {
126 n = rb_tree_search(tree, &test_numbers[i], rb_test_node_cmp_void);
127 tn = rb_node_data(struct rb_test_node, n, node);
128 assert(tn->key == test_numbers[i]);
129
130 n = rb_tree_search_sloppy(tree, &test_numbers[i],
131 rb_test_node_cmp_void);
132 tn = rb_node_data(struct rb_test_node, n, node);
133 assert(tn->key == test_numbers[i]);
134 }
135
136 int missing_key = NON_EXISTANT_NUMBER;
137 n = rb_tree_search(tree, &missing_key, rb_test_node_cmp_void);
138 assert(n == NULL);
139
140 n = rb_tree_search_sloppy(tree, &missing_key, rb_test_node_cmp_void);
141 if (rb_tree_is_empty(tree)) {
142 assert(n == NULL);
143 } else {
144 assert(n != NULL);
145 tn = rb_node_data(struct rb_test_node, n, node);
146 assert(tn->key != missing_key);
147 if (tn->key < missing_key) {
148 struct rb_node *next = rb_node_next(n);
149 if (next != NULL) {
150 struct rb_test_node *tnext =
151 rb_node_data(struct rb_test_node, next, node);
152 assert(missing_key < tnext->key);
153 }
154 } else {
155 struct rb_node *prev = rb_node_prev(n);
156 if (prev != NULL) {
157 struct rb_test_node *tprev =
158 rb_node_data(struct rb_test_node, prev, node);
159 assert(missing_key > tprev->key);
160 }
161 }
162 }
163 }
164
165 int
166 main()
167 {
168 struct rb_test_node nodes[ARRAY_SIZE(test_numbers)];
169 struct rb_tree tree;
170
171 rb_tree_init(&tree);
172
173 for (unsigned i = 0; i < ARRAY_SIZE(test_numbers); i++) {
174 nodes[i].key = test_numbers[i];
175 rb_tree_insert(&tree, &nodes[i].node, rb_test_node_cmp);
176 rb_tree_validate(&tree);
177 validate_tree_order(&tree, i + 1);
178 validate_search(&tree, 0, i);
179 }
180
181 for (unsigned i = 0; i < ARRAY_SIZE(test_numbers); i++) {
182 rb_tree_remove(&tree, &nodes[i].node);
183 rb_tree_validate(&tree);
184 validate_tree_order(&tree, ARRAY_SIZE(test_numbers) - i - 1);
185 validate_search(&tree, i + 1, ARRAY_SIZE(test_numbers) - 1);
186 }
187 }