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