1459d04a5SEd Schouten /*-
2459d04a5SEd Schouten * Copyright (c) 2015 Nuxi, https://nuxi.nl/
3459d04a5SEd Schouten *
4459d04a5SEd Schouten * Redistribution and use in source and binary forms, with or without
5459d04a5SEd Schouten * modification, are permitted provided that the following conditions
6459d04a5SEd Schouten * are met:
7459d04a5SEd Schouten * 1. Redistributions of source code must retain the above copyright
8459d04a5SEd Schouten * notice, this list of conditions and the following disclaimer.
9459d04a5SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright
10459d04a5SEd Schouten * notice, this list of conditions and the following disclaimer in the
11459d04a5SEd Schouten * documentation and/or other materials provided with the distribution.
12459d04a5SEd Schouten *
13459d04a5SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14459d04a5SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15459d04a5SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16459d04a5SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17459d04a5SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18459d04a5SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19459d04a5SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20459d04a5SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21459d04a5SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22459d04a5SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23459d04a5SEd Schouten * SUCH DAMAGE.
24459d04a5SEd Schouten */
25459d04a5SEd Schouten
26459d04a5SEd Schouten #include <atf-c.h>
27459d04a5SEd Schouten #define _SEARCH_PRIVATE
28459d04a5SEd Schouten #include <search.h>
29459d04a5SEd Schouten #include <stdbool.h>
30459d04a5SEd Schouten #include <stdlib.h>
31*c384464dSWill Andrews #include <stdio.h>
32*c384464dSWill Andrews
33*c384464dSWill Andrews static int n_nodes = 0;
34*c384464dSWill Andrews static int n_seen = 0;
35459d04a5SEd Schouten
36459d04a5SEd Schouten /* Validates the integrity of an AVL tree. */
37459d04a5SEd Schouten static inline unsigned int
tnode_assert(const posix_tnode * n)384ef9bd22SEd Schouten tnode_assert(const posix_tnode *n)
39459d04a5SEd Schouten {
40459d04a5SEd Schouten unsigned int height_left, height_right;
41459d04a5SEd Schouten int balance;
42459d04a5SEd Schouten
43459d04a5SEd Schouten if (n == NULL)
44459d04a5SEd Schouten return 0;
45459d04a5SEd Schouten height_left = tnode_assert(n->llink);
46459d04a5SEd Schouten height_right = tnode_assert(n->rlink);
47459d04a5SEd Schouten balance = (int)height_left - (int)height_right;
48459d04a5SEd Schouten ATF_CHECK(balance >= -1);
49459d04a5SEd Schouten ATF_CHECK(balance <= 1);
50459d04a5SEd Schouten ATF_CHECK_EQ(balance, n->balance);
51459d04a5SEd Schouten return (height_left > height_right ? height_left : height_right) + 1;
52459d04a5SEd Schouten }
53459d04a5SEd Schouten
54459d04a5SEd Schouten static int
compar(const void * a,const void * b)55459d04a5SEd Schouten compar(const void *a, const void *b)
56459d04a5SEd Schouten {
57459d04a5SEd Schouten
58459d04a5SEd Schouten return *(int *)a - *(int *)b;
59459d04a5SEd Schouten }
60459d04a5SEd Schouten
61*c384464dSWill Andrews static void
treewalk(const posix_tnode * node,VISIT v,int level)62*c384464dSWill Andrews treewalk(const posix_tnode *node, VISIT v, int level)
63*c384464dSWill Andrews {
64*c384464dSWill Andrews
65*c384464dSWill Andrews if (v == postorder || v == leaf)
66*c384464dSWill Andrews n_seen++;
67*c384464dSWill Andrews }
68*c384464dSWill Andrews
69459d04a5SEd Schouten ATF_TC_WITHOUT_HEAD(tsearch_test);
ATF_TC_BODY(tsearch_test,tc)70459d04a5SEd Schouten ATF_TC_BODY(tsearch_test, tc)
71459d04a5SEd Schouten {
72459d04a5SEd Schouten /*
73459d04a5SEd Schouten * Run the test below in a deterministic fashion to prevent this
74459d04a5SEd Schouten * test from potentially flapping. We assume that this provides
75459d04a5SEd Schouten * enough coverage.
76459d04a5SEd Schouten */
77459d04a5SEd Schouten #if 0
78459d04a5SEd Schouten unsigned short random_state[3];
79459d04a5SEd Schouten arc4random_buf(random_state, sizeof(random_state));
80459d04a5SEd Schouten #else
81459d04a5SEd Schouten unsigned short random_state[3] = { 26554, 13330, 3246 };
82459d04a5SEd Schouten #endif
83459d04a5SEd Schouten
84459d04a5SEd Schouten #define NKEYS 1000
85459d04a5SEd Schouten /* Create 1000 possible keys. */
86459d04a5SEd Schouten int keys[NKEYS];
87459d04a5SEd Schouten for (int i = 0; i < NKEYS; ++i)
88459d04a5SEd Schouten keys[i] = i;
89459d04a5SEd Schouten
90459d04a5SEd Schouten /* Apply random operations on a binary tree and check the results. */
914ef9bd22SEd Schouten posix_tnode *root = NULL;
92459d04a5SEd Schouten bool present[NKEYS] = {};
93459d04a5SEd Schouten for (int i = 0; i < NKEYS * 10; ++i) {
94459d04a5SEd Schouten int key = nrand48(random_state) % NKEYS;
95*c384464dSWill Andrews int sample = i;
96*c384464dSWill Andrews
97*c384464dSWill Andrews /*
98*c384464dSWill Andrews * Ensure each case is tested at least 10 times, plus a
99*c384464dSWill Andrews * random sampling.
100*c384464dSWill Andrews */
101*c384464dSWill Andrews if ((sample % NKEYS) > 3)
102*c384464dSWill Andrews sample = nrand48(random_state) % 3;
103*c384464dSWill Andrews
104*c384464dSWill Andrews switch (sample) {
105459d04a5SEd Schouten case 0: /* tdelete(). */
106459d04a5SEd Schouten if (present[key]) {
107459d04a5SEd Schouten ATF_CHECK(tdelete(&key, &root, compar) != NULL);
108459d04a5SEd Schouten present[key] = false;
109*c384464dSWill Andrews ATF_CHECK(n_nodes > 0);
110*c384464dSWill Andrews n_nodes--;
111459d04a5SEd Schouten } else {
112459d04a5SEd Schouten ATF_CHECK_EQ(NULL,
113459d04a5SEd Schouten tdelete(&key, &root, compar));
114459d04a5SEd Schouten }
115459d04a5SEd Schouten break;
116459d04a5SEd Schouten case 1: /* tfind(). */
117459d04a5SEd Schouten if (present[key]) {
118459d04a5SEd Schouten ATF_CHECK_EQ(&keys[key],
119459d04a5SEd Schouten *(int **)tfind(&key, &root, compar));
120459d04a5SEd Schouten } else {
121459d04a5SEd Schouten ATF_CHECK_EQ(NULL, tfind(&key, &root, compar));
122459d04a5SEd Schouten }
123459d04a5SEd Schouten break;
124459d04a5SEd Schouten case 2: /* tsearch(). */
125459d04a5SEd Schouten if (present[key]) {
126459d04a5SEd Schouten ATF_CHECK_EQ(&keys[key],
127459d04a5SEd Schouten *(int **)tsearch(&key, &root, compar));
128459d04a5SEd Schouten } else {
129459d04a5SEd Schouten ATF_CHECK_EQ(&keys[key], *(int **)tsearch(
130459d04a5SEd Schouten &keys[key], &root, compar));
131459d04a5SEd Schouten present[key] = true;
132*c384464dSWill Andrews n_nodes++;
133459d04a5SEd Schouten }
134459d04a5SEd Schouten break;
135459d04a5SEd Schouten }
136459d04a5SEd Schouten tnode_assert(root);
137459d04a5SEd Schouten }
138459d04a5SEd Schouten
139*c384464dSWill Andrews /* Walk the tree. */
140*c384464dSWill Andrews twalk(root, treewalk);
141*c384464dSWill Andrews ATF_CHECK_EQ(n_nodes, n_seen);
142*c384464dSWill Andrews
143459d04a5SEd Schouten /* Remove all entries from the tree. */
144459d04a5SEd Schouten for (int key = 0; key < NKEYS; ++key)
145459d04a5SEd Schouten if (present[key])
146459d04a5SEd Schouten ATF_CHECK(tdelete(&key, &root, compar) != NULL);
147459d04a5SEd Schouten ATF_CHECK_EQ(NULL, root);
148459d04a5SEd Schouten }
149459d04a5SEd Schouten
ATF_TP_ADD_TCS(tp)150459d04a5SEd Schouten ATF_TP_ADD_TCS(tp)
151459d04a5SEd Schouten {
152459d04a5SEd Schouten
153459d04a5SEd Schouten ATF_TP_ADD_TC(tp, tsearch_test);
154459d04a5SEd Schouten
155459d04a5SEd Schouten return (atf_no_error());
156459d04a5SEd Schouten }
157