xref: /freebsd/lib/libc/tests/stdlib/tsearch_test.c (revision 315ee00f)
1 /*-
2  * Copyright (c) 2015 Nuxi, https://nuxi.nl/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 #include <atf-c.h>
28 #define _SEARCH_PRIVATE
29 #include <search.h>
30 #include <stdbool.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 
34 static int n_nodes = 0;
35 static int n_seen = 0;
36 
37 /* Validates the integrity of an AVL tree. */
38 static inline unsigned int
39 tnode_assert(const posix_tnode *n)
40 {
41 	unsigned int height_left, height_right;
42 	int balance;
43 
44 	if (n == NULL)
45 		return 0;
46 	height_left = tnode_assert(n->llink);
47 	height_right = tnode_assert(n->rlink);
48 	balance = (int)height_left - (int)height_right;
49 	ATF_CHECK(balance >= -1);
50 	ATF_CHECK(balance <= 1);
51 	ATF_CHECK_EQ(balance, n->balance);
52 	return (height_left > height_right ? height_left : height_right) + 1;
53 }
54 
55 static int
56 compar(const void *a, const void *b)
57 {
58 
59 	return *(int *)a - *(int *)b;
60 }
61 
62 static void
63 treewalk(const posix_tnode *node, VISIT v, int level)
64 {
65 
66 	if (v == postorder || v == leaf)
67 		n_seen++;
68 }
69 
70 ATF_TC_WITHOUT_HEAD(tsearch_test);
71 ATF_TC_BODY(tsearch_test, tc)
72 {
73 	/*
74 	 * Run the test below in a deterministic fashion to prevent this
75 	 * test from potentially flapping. We assume that this provides
76 	 * enough coverage.
77 	 */
78 #if 0
79 	unsigned short random_state[3];
80 	arc4random_buf(random_state, sizeof(random_state));
81 #else
82 	unsigned short random_state[3] = { 26554, 13330, 3246 };
83 #endif
84 
85 #define NKEYS 1000
86 	/* Create 1000 possible keys. */
87 	int keys[NKEYS];
88 	for (int i = 0; i < NKEYS; ++i)
89 		keys[i] = i;
90 
91 	/* Apply random operations on a binary tree and check the results. */
92 	posix_tnode *root = NULL;
93 	bool present[NKEYS] = {};
94 	for (int i = 0; i < NKEYS * 10; ++i) {
95 		int key = nrand48(random_state) % NKEYS;
96 		int sample = i;
97 
98 		/*
99 		 * Ensure each case is tested at least 10 times, plus a
100 		 * random sampling.
101 		 */
102 		if ((sample % NKEYS) > 3)
103 			sample = nrand48(random_state) % 3;
104 
105 		switch (sample) {
106 		case 0:  /* tdelete(). */
107 			if (present[key]) {
108 				ATF_CHECK(tdelete(&key, &root, compar) != NULL);
109 				present[key] = false;
110 				ATF_CHECK(n_nodes > 0);
111 				n_nodes--;
112 			} else {
113 				ATF_CHECK_EQ(NULL,
114 				    tdelete(&key, &root, compar));
115 			}
116 			break;
117 		case 1:  /* tfind(). */
118 			if (present[key]) {
119 				ATF_CHECK_EQ(&keys[key],
120 				    *(int **)tfind(&key, &root, compar));
121 			} else {
122 				ATF_CHECK_EQ(NULL, tfind(&key, &root, compar));
123 			}
124 			break;
125 		case 2:  /* tsearch(). */
126 			if (present[key]) {
127 				ATF_CHECK_EQ(&keys[key],
128 				    *(int **)tsearch(&key, &root, compar));
129 			} else {
130 				ATF_CHECK_EQ(&keys[key], *(int **)tsearch(
131 				    &keys[key], &root, compar));
132 				present[key] = true;
133 				n_nodes++;
134 			}
135 			break;
136 		}
137 		tnode_assert(root);
138 	}
139 
140 	/* Walk the tree. */
141 	twalk(root, treewalk);
142 	ATF_CHECK_EQ(n_nodes, n_seen);
143 
144 	/* Remove all entries from the tree. */
145 	for (int key = 0; key < NKEYS; ++key)
146 		if (present[key])
147 			ATF_CHECK(tdelete(&key, &root, compar) != NULL);
148 	ATF_CHECK_EQ(NULL, root);
149 }
150 
151 ATF_TP_ADD_TCS(tp)
152 {
153 
154 	ATF_TP_ADD_TC(tp, tsearch_test);
155 
156 	return (atf_no_error());
157 }
158