1 /* $NetBSD: t_hsearch.c,v 1.4 2014/07/20 20:17:21 christos Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * Copyright (c) 2001 Christopher G. Demetriou
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed for the
44 * NetBSD Project. See http://www.NetBSD.org/ for
45 * information about NetBSD.
46 * 4. The name of the author may not be used to endorse or promote products
47 * derived from this software without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
63 #include <sys/cdefs.h>
64 __COPYRIGHT("@(#) Copyright (c) 2008\
65 The NetBSD Foundation, inc. All rights reserved.");
66 __RCSID("$NetBSD: t_hsearch.c,v 1.4 2014/07/20 20:17:21 christos Exp $");
76 #define REQUIRE_ERRNO(x) ATF_REQUIRE_MSG(x, "%s", strerror(errno))
78 ATF_TC(hsearch_basic
);
79 ATF_TC_HEAD(hsearch_basic
, tc
)
82 atf_tc_set_md_var(tc
, "descr", "Checks basic insertions and searching");
85 ATF_TC_BODY(hsearch_basic
, tc
)
91 REQUIRE_ERRNO(hcreate(16) != 0);
93 /* ch[1] should be constant from here on down. */
96 /* Basic insertions. Check enough that there'll be collisions. */
97 for (i
= 0; i
< 26; i
++) {
99 e
.key
= strdup(ch
); /* ptr to provided key is kept! */
100 ATF_REQUIRE(e
.key
!= NULL
);
101 e
.data
= (void *)(intptr_t)i
;
103 ep
= hsearch(e
, ENTER
);
105 ATF_REQUIRE(ep
!= NULL
);
106 ATF_REQUIRE_STREQ(ep
->key
, ch
);
107 ATF_REQUIRE_EQ((intptr_t)ep
->data
, i
);
110 /* e.key should be constant from here on down. */
114 for (i
= 0; i
< 26; i
++) {
117 ep
= hsearch(e
, FIND
);
119 ATF_REQUIRE(ep
!= NULL
);
120 ATF_REQUIRE_STREQ(ep
->key
, ch
);
121 ATF_REQUIRE_EQ((intptr_t)ep
->data
, i
);
124 hdestroy1(free
, NULL
);
127 ATF_TC(hsearch_duplicate
);
128 ATF_TC_HEAD(hsearch_duplicate
, tc
)
131 atf_tc_set_md_var(tc
, "descr", "Checks that inserting duplicate "
132 "doesn't overwrite existing data");
135 ATF_TC_BODY(hsearch_duplicate
, tc
)
139 REQUIRE_ERRNO(hcreate(16));
141 e
.key
= __UNCONST("a");
142 e
.data
= (void *)(intptr_t) 0;
144 ep
= hsearch(e
, ENTER
);
146 ATF_REQUIRE(ep
!= NULL
);
147 ATF_REQUIRE_STREQ(ep
->key
, "a");
148 ATF_REQUIRE_EQ((intptr_t)ep
->data
, 0);
150 e
.data
= (void *)(intptr_t)12345;
152 ep
= hsearch(e
, ENTER
);
153 ep
= hsearch(e
, FIND
);
155 ATF_REQUIRE(ep
!= NULL
);
156 ATF_REQUIRE_STREQ(ep
->key
, "a");
157 ATF_REQUIRE_EQ((intptr_t)ep
->data
, 0);
162 ATF_TC(hsearch_nonexistent
);
163 ATF_TC_HEAD(hsearch_nonexistent
, tc
)
166 atf_tc_set_md_var(tc
, "descr",
167 "Checks searching for non-existent entry");
170 ATF_TC_BODY(hsearch_nonexistent
, tc
)
174 REQUIRE_ERRNO(hcreate(16));
176 e
.key
= __UNCONST("A");
177 ep
= hsearch(e
, FIND
);
178 ATF_REQUIRE_EQ(ep
, NULL
);
184 ATF_TC_HEAD(hsearch_two
, tc
)
187 atf_tc_set_md_var(tc
, "descr",
188 "Checks that searching doesn't overwrite previous search results");
191 ATF_TC_BODY(hsearch_two
, tc
)
195 REQUIRE_ERRNO(hcreate(16));
197 e
.key
= __UNCONST("a");
198 e
.data
= (void*)(intptr_t)0;
200 ep
= hsearch(e
, ENTER
);
202 ATF_REQUIRE(ep
!= NULL
);
203 ATF_REQUIRE_STREQ(ep
->key
, "a");
204 ATF_REQUIRE_EQ((intptr_t)ep
->data
, 0);
206 e
.key
= __UNCONST("b");
207 e
.data
= (void*)(intptr_t)1;
209 ep
= hsearch(e
, ENTER
);
211 ATF_REQUIRE(ep
!= NULL
);
212 ATF_REQUIRE_STREQ(ep
->key
, "b");
213 ATF_REQUIRE_EQ((intptr_t)ep
->data
, 1);
215 e
.key
= __UNCONST("a");
216 ep
= hsearch(e
, FIND
);
218 e
.key
= __UNCONST("b");
219 ep2
= hsearch(e
, FIND
);
221 ATF_REQUIRE(ep
!= NULL
);
222 ATF_REQUIRE_STREQ(ep
->key
, "a");
223 ATF_REQUIRE_EQ((intptr_t)ep
->data
, 0);
225 ATF_REQUIRE(ep2
!= NULL
);
226 ATF_REQUIRE_STREQ(ep2
->key
, "b");
227 ATF_REQUIRE_EQ((intptr_t)ep2
->data
, 1);
232 ATF_TC(hsearch_r_basic
);
233 ATF_TC_HEAD(hsearch_r_basic
, tc
)
236 atf_tc_set_md_var(tc
, "descr", "Checks basic insertions and searching");
239 ATF_TC_BODY(hsearch_r_basic
, tc
)
244 struct hsearch_data t
;
246 REQUIRE_ERRNO(hcreate_r(16, &t
) != 0);
248 /* ch[1] should be constant from here on down. */
251 /* Basic insertions. Check enough that there'll be collisions. */
252 for (i
= 0; i
< 26; i
++) {
254 e
.key
= strdup(ch
); /* ptr to provided key is kept! */
255 ATF_REQUIRE(e
.key
!= NULL
);
256 e
.data
= (void *)(intptr_t)i
;
258 ATF_REQUIRE(hsearch_r(e
, ENTER
, &ep
, &t
) == 1);
259 ATF_REQUIRE(ep
!= NULL
);
260 ATF_REQUIRE_STREQ(ep
->key
, ch
);
261 ATF_REQUIRE_EQ((intptr_t)ep
->data
, i
);
264 /* e.key should be constant from here on down. */
268 for (i
= 0; i
< 26; i
++) {
271 ATF_REQUIRE(hsearch_r(e
, FIND
, &ep
, &t
) == 1);
272 ATF_REQUIRE(ep
!= NULL
);
273 ATF_REQUIRE_STREQ(ep
->key
, ch
);
274 ATF_REQUIRE_EQ((intptr_t)ep
->data
, i
);
277 hdestroy1_r(&t
, free
, NULL
);
280 ATF_TC(hsearch_r_duplicate
);
281 ATF_TC_HEAD(hsearch_r_duplicate
, tc
)
284 atf_tc_set_md_var(tc
, "descr", "Checks that inserting duplicate "
285 "doesn't overwrite existing data");
288 ATF_TC_BODY(hsearch_r_duplicate
, tc
)
291 struct hsearch_data t
;
293 REQUIRE_ERRNO(hcreate_r(16, &t
));
295 e
.key
= __UNCONST("a");
296 e
.data
= (void *)(intptr_t) 0;
298 ATF_REQUIRE(hsearch_r(e
, ENTER
, &ep
, &t
) == 1);
299 ATF_REQUIRE(ep
!= NULL
);
300 ATF_REQUIRE_STREQ(ep
->key
, "a");
301 ATF_REQUIRE_EQ((intptr_t)ep
->data
, 0);
303 e
.data
= (void *)(intptr_t)12345;
305 ATF_REQUIRE(hsearch_r(e
, ENTER
, &ep
, &t
) == 1);
306 ATF_REQUIRE(hsearch_r(e
, FIND
, &ep
, &t
) == 1);
308 ATF_REQUIRE(ep
!= NULL
);
309 ATF_REQUIRE_STREQ(ep
->key
, "a");
310 ATF_REQUIRE_EQ((intptr_t)ep
->data
, 0);
315 ATF_TC(hsearch_r_nonexistent
);
316 ATF_TC_HEAD(hsearch_r_nonexistent
, tc
)
319 atf_tc_set_md_var(tc
, "descr",
320 "Checks searching for non-existent entry");
323 ATF_TC_BODY(hsearch_r_nonexistent
, tc
)
326 struct hsearch_data t
;
328 REQUIRE_ERRNO(hcreate_r(16, &t
));
330 e
.key
= __UNCONST("A");
331 ATF_REQUIRE(hsearch_r(e
, FIND
, &ep
, &t
) == 1);
332 ATF_REQUIRE_EQ(ep
, NULL
);
337 ATF_TC(hsearch_r_two
);
338 ATF_TC_HEAD(hsearch_r_two
, tc
)
341 atf_tc_set_md_var(tc
, "descr",
342 "Checks that searching doesn't overwrite previous search results");
345 ATF_TC_BODY(hsearch_r_two
, tc
)
348 struct hsearch_data t
;
350 REQUIRE_ERRNO(hcreate_r(16, &t
));
352 e
.key
= __UNCONST("a");
353 e
.data
= (void*)(intptr_t)0;
355 ATF_REQUIRE(hsearch_r(e
, ENTER
, &ep
, &t
) == 1);
356 ATF_REQUIRE(ep
!= NULL
);
357 ATF_REQUIRE_STREQ(ep
->key
, "a");
358 ATF_REQUIRE_EQ((intptr_t)ep
->data
, 0);
360 e
.key
= __UNCONST("b");
361 e
.data
= (void*)(intptr_t)1;
363 ATF_REQUIRE(hsearch_r(e
, ENTER
, &ep
, &t
) == 1);
364 ATF_REQUIRE(ep
!= NULL
);
365 ATF_REQUIRE_STREQ(ep
->key
, "b");
366 ATF_REQUIRE_EQ((intptr_t)ep
->data
, 1);
368 e
.key
= __UNCONST("a");
369 ATF_REQUIRE(hsearch_r(e
, FIND
, &ep
, &t
) == 1);
371 e
.key
= __UNCONST("b");
372 ATF_REQUIRE(hsearch_r(e
, FIND
, &ep2
, &t
) == 1);
374 ATF_REQUIRE(ep
!= NULL
);
375 ATF_REQUIRE_STREQ(ep
->key
, "a");
376 ATF_REQUIRE_EQ((intptr_t)ep
->data
, 0);
378 ATF_REQUIRE(ep2
!= NULL
);
379 ATF_REQUIRE_STREQ(ep2
->key
, "b");
380 ATF_REQUIRE_EQ((intptr_t)ep2
->data
, 1);
388 ATF_TP_ADD_TC(tp
, hsearch_basic
);
389 ATF_TP_ADD_TC(tp
, hsearch_duplicate
);
390 ATF_TP_ADD_TC(tp
, hsearch_nonexistent
);
391 ATF_TP_ADD_TC(tp
, hsearch_two
);
393 ATF_TP_ADD_TC(tp
, hsearch_r_basic
);
394 ATF_TP_ADD_TC(tp
, hsearch_r_duplicate
);
395 ATF_TP_ADD_TC(tp
, hsearch_r_nonexistent
);
396 ATF_TP_ADD_TC(tp
, hsearch_r_two
);
398 return atf_no_error();