1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_hsearch.c,v 1.4 2014/07/20 20:17:21 christos Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2008 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc * are met:
1011be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc *
1611be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1711be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1811be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1911be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2011be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2111be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2211be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2311be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2411be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2511be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2611be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
2711be35a1SLionel Sambuc */
2811be35a1SLionel Sambuc
2911be35a1SLionel Sambuc /*
3011be35a1SLionel Sambuc * Copyright (c) 2001 Christopher G. Demetriou
3111be35a1SLionel Sambuc * All rights reserved.
3211be35a1SLionel Sambuc *
3311be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
3411be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
3511be35a1SLionel Sambuc * are met:
3611be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
3711be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
3811be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
3911be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
4011be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
4111be35a1SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
4211be35a1SLionel Sambuc * must display the following acknowledgement:
4311be35a1SLionel Sambuc * This product includes software developed for the
4411be35a1SLionel Sambuc * NetBSD Project. See http://www.NetBSD.org/ for
4511be35a1SLionel Sambuc * information about NetBSD.
4611be35a1SLionel Sambuc * 4. The name of the author may not be used to endorse or promote products
4711be35a1SLionel Sambuc * derived from this software without specific prior written permission.
4811be35a1SLionel Sambuc *
4911be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
5011be35a1SLionel Sambuc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5111be35a1SLionel Sambuc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5211be35a1SLionel Sambuc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
5311be35a1SLionel Sambuc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
5411be35a1SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5511be35a1SLionel Sambuc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5611be35a1SLionel Sambuc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5711be35a1SLionel Sambuc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
5811be35a1SLionel Sambuc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5911be35a1SLionel Sambuc *
6011be35a1SLionel Sambuc * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
6111be35a1SLionel Sambuc */
6211be35a1SLionel Sambuc
6311be35a1SLionel Sambuc #include <sys/cdefs.h>
6411be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008\
6511be35a1SLionel Sambuc The NetBSD Foundation, inc. All rights reserved.");
66*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_hsearch.c,v 1.4 2014/07/20 20:17:21 christos Exp $");
6711be35a1SLionel Sambuc
6811be35a1SLionel Sambuc #include <errno.h>
6911be35a1SLionel Sambuc #include <search.h>
7011be35a1SLionel Sambuc #include <string.h>
7111be35a1SLionel Sambuc #include <stdio.h>
72*0a6a1f1dSLionel Sambuc #include <stdlib.h>
7311be35a1SLionel Sambuc
7411be35a1SLionel Sambuc #include <atf-c.h>
7511be35a1SLionel Sambuc
7611be35a1SLionel Sambuc #define REQUIRE_ERRNO(x) ATF_REQUIRE_MSG(x, "%s", strerror(errno))
7711be35a1SLionel Sambuc
7811be35a1SLionel Sambuc ATF_TC(hsearch_basic);
ATF_TC_HEAD(hsearch_basic,tc)7911be35a1SLionel Sambuc ATF_TC_HEAD(hsearch_basic, tc)
8011be35a1SLionel Sambuc {
8111be35a1SLionel Sambuc
8211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks basic insertions and searching");
8311be35a1SLionel Sambuc }
8411be35a1SLionel Sambuc
ATF_TC_BODY(hsearch_basic,tc)8511be35a1SLionel Sambuc ATF_TC_BODY(hsearch_basic, tc)
8611be35a1SLionel Sambuc {
8711be35a1SLionel Sambuc ENTRY e, *ep;
8811be35a1SLionel Sambuc char ch[2];
8911be35a1SLionel Sambuc int i;
9011be35a1SLionel Sambuc
9111be35a1SLionel Sambuc REQUIRE_ERRNO(hcreate(16) != 0);
9211be35a1SLionel Sambuc
9311be35a1SLionel Sambuc /* ch[1] should be constant from here on down. */
9411be35a1SLionel Sambuc ch[1] = '\0';
9511be35a1SLionel Sambuc
9611be35a1SLionel Sambuc /* Basic insertions. Check enough that there'll be collisions. */
9711be35a1SLionel Sambuc for (i = 0; i < 26; i++) {
9811be35a1SLionel Sambuc ch[0] = 'a' + i;
9911be35a1SLionel Sambuc e.key = strdup(ch); /* ptr to provided key is kept! */
10011be35a1SLionel Sambuc ATF_REQUIRE(e.key != NULL);
101*0a6a1f1dSLionel Sambuc e.data = (void *)(intptr_t)i;
10211be35a1SLionel Sambuc
10311be35a1SLionel Sambuc ep = hsearch(e, ENTER);
10411be35a1SLionel Sambuc
10511be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
10611be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, ch);
107*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, i);
10811be35a1SLionel Sambuc }
10911be35a1SLionel Sambuc
11011be35a1SLionel Sambuc /* e.key should be constant from here on down. */
11111be35a1SLionel Sambuc e.key = ch;
11211be35a1SLionel Sambuc
11311be35a1SLionel Sambuc /* Basic lookups. */
11411be35a1SLionel Sambuc for (i = 0; i < 26; i++) {
11511be35a1SLionel Sambuc ch[0] = 'a' + i;
11611be35a1SLionel Sambuc
11711be35a1SLionel Sambuc ep = hsearch(e, FIND);
11811be35a1SLionel Sambuc
11911be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
12011be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, ch);
121*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, i);
12211be35a1SLionel Sambuc }
12311be35a1SLionel Sambuc
124*0a6a1f1dSLionel Sambuc hdestroy1(free, NULL);
12511be35a1SLionel Sambuc }
12611be35a1SLionel Sambuc
12711be35a1SLionel Sambuc ATF_TC(hsearch_duplicate);
ATF_TC_HEAD(hsearch_duplicate,tc)12811be35a1SLionel Sambuc ATF_TC_HEAD(hsearch_duplicate, tc)
12911be35a1SLionel Sambuc {
13011be35a1SLionel Sambuc
13111be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks that inserting duplicate "
13211be35a1SLionel Sambuc "doesn't overwrite existing data");
13311be35a1SLionel Sambuc }
13411be35a1SLionel Sambuc
ATF_TC_BODY(hsearch_duplicate,tc)13511be35a1SLionel Sambuc ATF_TC_BODY(hsearch_duplicate, tc)
13611be35a1SLionel Sambuc {
13711be35a1SLionel Sambuc ENTRY e, *ep;
13811be35a1SLionel Sambuc
13911be35a1SLionel Sambuc REQUIRE_ERRNO(hcreate(16));
14011be35a1SLionel Sambuc
141*0a6a1f1dSLionel Sambuc e.key = __UNCONST("a");
142*0a6a1f1dSLionel Sambuc e.data = (void *)(intptr_t) 0;
14311be35a1SLionel Sambuc
14411be35a1SLionel Sambuc ep = hsearch(e, ENTER);
14511be35a1SLionel Sambuc
14611be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
14711be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, "a");
148*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, 0);
14911be35a1SLionel Sambuc
150*0a6a1f1dSLionel Sambuc e.data = (void *)(intptr_t)12345;
15111be35a1SLionel Sambuc
15211be35a1SLionel Sambuc ep = hsearch(e, ENTER);
15311be35a1SLionel Sambuc ep = hsearch(e, FIND);
15411be35a1SLionel Sambuc
15511be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
15611be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, "a");
157*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, 0);
15811be35a1SLionel Sambuc
15911be35a1SLionel Sambuc hdestroy();
16011be35a1SLionel Sambuc }
16111be35a1SLionel Sambuc
16211be35a1SLionel Sambuc ATF_TC(hsearch_nonexistent);
ATF_TC_HEAD(hsearch_nonexistent,tc)16311be35a1SLionel Sambuc ATF_TC_HEAD(hsearch_nonexistent, tc)
16411be35a1SLionel Sambuc {
16511be35a1SLionel Sambuc
16611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
16711be35a1SLionel Sambuc "Checks searching for non-existent entry");
16811be35a1SLionel Sambuc }
16911be35a1SLionel Sambuc
ATF_TC_BODY(hsearch_nonexistent,tc)17011be35a1SLionel Sambuc ATF_TC_BODY(hsearch_nonexistent, tc)
17111be35a1SLionel Sambuc {
17211be35a1SLionel Sambuc ENTRY e, *ep;
17311be35a1SLionel Sambuc
17411be35a1SLionel Sambuc REQUIRE_ERRNO(hcreate(16));
17511be35a1SLionel Sambuc
176*0a6a1f1dSLionel Sambuc e.key = __UNCONST("A");
17711be35a1SLionel Sambuc ep = hsearch(e, FIND);
17811be35a1SLionel Sambuc ATF_REQUIRE_EQ(ep, NULL);
17911be35a1SLionel Sambuc
18011be35a1SLionel Sambuc hdestroy();
18111be35a1SLionel Sambuc }
18211be35a1SLionel Sambuc
18311be35a1SLionel Sambuc ATF_TC(hsearch_two);
ATF_TC_HEAD(hsearch_two,tc)18411be35a1SLionel Sambuc ATF_TC_HEAD(hsearch_two, tc)
18511be35a1SLionel Sambuc {
18611be35a1SLionel Sambuc
18711be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
18811be35a1SLionel Sambuc "Checks that searching doesn't overwrite previous search results");
18911be35a1SLionel Sambuc }
19011be35a1SLionel Sambuc
ATF_TC_BODY(hsearch_two,tc)19111be35a1SLionel Sambuc ATF_TC_BODY(hsearch_two, tc)
19211be35a1SLionel Sambuc {
19311be35a1SLionel Sambuc ENTRY e, *ep, *ep2;
19411be35a1SLionel Sambuc
19511be35a1SLionel Sambuc REQUIRE_ERRNO(hcreate(16));
19611be35a1SLionel Sambuc
197*0a6a1f1dSLionel Sambuc e.key = __UNCONST("a");
198*0a6a1f1dSLionel Sambuc e.data = (void*)(intptr_t)0;
19911be35a1SLionel Sambuc
20011be35a1SLionel Sambuc ep = hsearch(e, ENTER);
20111be35a1SLionel Sambuc
20211be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
20311be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, "a");
204*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, 0);
20511be35a1SLionel Sambuc
206*0a6a1f1dSLionel Sambuc e.key = __UNCONST("b");
207*0a6a1f1dSLionel Sambuc e.data = (void*)(intptr_t)1;
20811be35a1SLionel Sambuc
20911be35a1SLionel Sambuc ep = hsearch(e, ENTER);
21011be35a1SLionel Sambuc
21111be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
21211be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, "b");
213*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, 1);
21411be35a1SLionel Sambuc
215*0a6a1f1dSLionel Sambuc e.key = __UNCONST("a");
21611be35a1SLionel Sambuc ep = hsearch(e, FIND);
21711be35a1SLionel Sambuc
218*0a6a1f1dSLionel Sambuc e.key = __UNCONST("b");
21911be35a1SLionel Sambuc ep2 = hsearch(e, FIND);
22011be35a1SLionel Sambuc
22111be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
22211be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, "a");
223*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, 0);
22411be35a1SLionel Sambuc
22511be35a1SLionel Sambuc ATF_REQUIRE(ep2 != NULL);
22611be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep2->key, "b");
227*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep2->data, 1);
22811be35a1SLionel Sambuc
22911be35a1SLionel Sambuc hdestroy();
23011be35a1SLionel Sambuc }
23111be35a1SLionel Sambuc
23211be35a1SLionel Sambuc ATF_TC(hsearch_r_basic);
ATF_TC_HEAD(hsearch_r_basic,tc)23311be35a1SLionel Sambuc ATF_TC_HEAD(hsearch_r_basic, tc)
23411be35a1SLionel Sambuc {
23511be35a1SLionel Sambuc
23611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks basic insertions and searching");
23711be35a1SLionel Sambuc }
23811be35a1SLionel Sambuc
ATF_TC_BODY(hsearch_r_basic,tc)23911be35a1SLionel Sambuc ATF_TC_BODY(hsearch_r_basic, tc)
24011be35a1SLionel Sambuc {
24111be35a1SLionel Sambuc ENTRY e, *ep;
24211be35a1SLionel Sambuc char ch[2];
24311be35a1SLionel Sambuc int i;
24411be35a1SLionel Sambuc struct hsearch_data t;
24511be35a1SLionel Sambuc
24611be35a1SLionel Sambuc REQUIRE_ERRNO(hcreate_r(16, &t) != 0);
24711be35a1SLionel Sambuc
24811be35a1SLionel Sambuc /* ch[1] should be constant from here on down. */
24911be35a1SLionel Sambuc ch[1] = '\0';
25011be35a1SLionel Sambuc
25111be35a1SLionel Sambuc /* Basic insertions. Check enough that there'll be collisions. */
25211be35a1SLionel Sambuc for (i = 0; i < 26; i++) {
25311be35a1SLionel Sambuc ch[0] = 'a' + i;
25411be35a1SLionel Sambuc e.key = strdup(ch); /* ptr to provided key is kept! */
25511be35a1SLionel Sambuc ATF_REQUIRE(e.key != NULL);
256*0a6a1f1dSLionel Sambuc e.data = (void *)(intptr_t)i;
25711be35a1SLionel Sambuc
25811be35a1SLionel Sambuc ATF_REQUIRE(hsearch_r(e, ENTER, &ep, &t) == 1);
25911be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
26011be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, ch);
261*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, i);
26211be35a1SLionel Sambuc }
26311be35a1SLionel Sambuc
26411be35a1SLionel Sambuc /* e.key should be constant from here on down. */
26511be35a1SLionel Sambuc e.key = ch;
26611be35a1SLionel Sambuc
26711be35a1SLionel Sambuc /* Basic lookups. */
26811be35a1SLionel Sambuc for (i = 0; i < 26; i++) {
26911be35a1SLionel Sambuc ch[0] = 'a' + i;
27011be35a1SLionel Sambuc
27111be35a1SLionel Sambuc ATF_REQUIRE(hsearch_r(e, FIND, &ep, &t) == 1);
27211be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
27311be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, ch);
274*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, i);
27511be35a1SLionel Sambuc }
27611be35a1SLionel Sambuc
277*0a6a1f1dSLionel Sambuc hdestroy1_r(&t, free, NULL);
27811be35a1SLionel Sambuc }
27911be35a1SLionel Sambuc
28011be35a1SLionel Sambuc ATF_TC(hsearch_r_duplicate);
ATF_TC_HEAD(hsearch_r_duplicate,tc)28111be35a1SLionel Sambuc ATF_TC_HEAD(hsearch_r_duplicate, tc)
28211be35a1SLionel Sambuc {
28311be35a1SLionel Sambuc
28411be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks that inserting duplicate "
28511be35a1SLionel Sambuc "doesn't overwrite existing data");
28611be35a1SLionel Sambuc }
28711be35a1SLionel Sambuc
ATF_TC_BODY(hsearch_r_duplicate,tc)28811be35a1SLionel Sambuc ATF_TC_BODY(hsearch_r_duplicate, tc)
28911be35a1SLionel Sambuc {
29011be35a1SLionel Sambuc ENTRY e, *ep;
29111be35a1SLionel Sambuc struct hsearch_data t;
29211be35a1SLionel Sambuc
29311be35a1SLionel Sambuc REQUIRE_ERRNO(hcreate_r(16, &t));
29411be35a1SLionel Sambuc
295*0a6a1f1dSLionel Sambuc e.key = __UNCONST("a");
296*0a6a1f1dSLionel Sambuc e.data = (void *)(intptr_t) 0;
29711be35a1SLionel Sambuc
29811be35a1SLionel Sambuc ATF_REQUIRE(hsearch_r(e, ENTER, &ep, &t) == 1);
29911be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
30011be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, "a");
301*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, 0);
30211be35a1SLionel Sambuc
303*0a6a1f1dSLionel Sambuc e.data = (void *)(intptr_t)12345;
30411be35a1SLionel Sambuc
30511be35a1SLionel Sambuc ATF_REQUIRE(hsearch_r(e, ENTER, &ep, &t) == 1);
30611be35a1SLionel Sambuc ATF_REQUIRE(hsearch_r(e, FIND, &ep, &t) == 1);
30711be35a1SLionel Sambuc
30811be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
30911be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, "a");
310*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, 0);
31111be35a1SLionel Sambuc
31211be35a1SLionel Sambuc hdestroy_r(&t);
31311be35a1SLionel Sambuc }
31411be35a1SLionel Sambuc
31511be35a1SLionel Sambuc ATF_TC(hsearch_r_nonexistent);
ATF_TC_HEAD(hsearch_r_nonexistent,tc)31611be35a1SLionel Sambuc ATF_TC_HEAD(hsearch_r_nonexistent, tc)
31711be35a1SLionel Sambuc {
31811be35a1SLionel Sambuc
31911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
32011be35a1SLionel Sambuc "Checks searching for non-existent entry");
32111be35a1SLionel Sambuc }
32211be35a1SLionel Sambuc
ATF_TC_BODY(hsearch_r_nonexistent,tc)32311be35a1SLionel Sambuc ATF_TC_BODY(hsearch_r_nonexistent, tc)
32411be35a1SLionel Sambuc {
32511be35a1SLionel Sambuc ENTRY e, *ep;
32611be35a1SLionel Sambuc struct hsearch_data t;
32711be35a1SLionel Sambuc
32811be35a1SLionel Sambuc REQUIRE_ERRNO(hcreate_r(16, &t));
32911be35a1SLionel Sambuc
330*0a6a1f1dSLionel Sambuc e.key = __UNCONST("A");
33111be35a1SLionel Sambuc ATF_REQUIRE(hsearch_r(e, FIND, &ep, &t) == 1);
33211be35a1SLionel Sambuc ATF_REQUIRE_EQ(ep, NULL);
33311be35a1SLionel Sambuc
33411be35a1SLionel Sambuc hdestroy_r(&t);
33511be35a1SLionel Sambuc }
33611be35a1SLionel Sambuc
33711be35a1SLionel Sambuc ATF_TC(hsearch_r_two);
ATF_TC_HEAD(hsearch_r_two,tc)33811be35a1SLionel Sambuc ATF_TC_HEAD(hsearch_r_two, tc)
33911be35a1SLionel Sambuc {
34011be35a1SLionel Sambuc
34111be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
34211be35a1SLionel Sambuc "Checks that searching doesn't overwrite previous search results");
34311be35a1SLionel Sambuc }
34411be35a1SLionel Sambuc
ATF_TC_BODY(hsearch_r_two,tc)34511be35a1SLionel Sambuc ATF_TC_BODY(hsearch_r_two, tc)
34611be35a1SLionel Sambuc {
34711be35a1SLionel Sambuc ENTRY e, *ep, *ep2;
34811be35a1SLionel Sambuc struct hsearch_data t;
34911be35a1SLionel Sambuc
35011be35a1SLionel Sambuc REQUIRE_ERRNO(hcreate_r(16, &t));
35111be35a1SLionel Sambuc
352*0a6a1f1dSLionel Sambuc e.key = __UNCONST("a");
353*0a6a1f1dSLionel Sambuc e.data = (void*)(intptr_t)0;
35411be35a1SLionel Sambuc
35511be35a1SLionel Sambuc ATF_REQUIRE(hsearch_r(e, ENTER, &ep, &t) == 1);
35611be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
35711be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, "a");
358*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, 0);
35911be35a1SLionel Sambuc
360*0a6a1f1dSLionel Sambuc e.key = __UNCONST("b");
361*0a6a1f1dSLionel Sambuc e.data = (void*)(intptr_t)1;
36211be35a1SLionel Sambuc
36311be35a1SLionel Sambuc ATF_REQUIRE(hsearch_r(e, ENTER, &ep, &t) == 1);
36411be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
36511be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, "b");
366*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, 1);
36711be35a1SLionel Sambuc
368*0a6a1f1dSLionel Sambuc e.key = __UNCONST("a");
36911be35a1SLionel Sambuc ATF_REQUIRE(hsearch_r(e, FIND, &ep, &t) == 1);
37011be35a1SLionel Sambuc
371*0a6a1f1dSLionel Sambuc e.key = __UNCONST("b");
37211be35a1SLionel Sambuc ATF_REQUIRE(hsearch_r(e, FIND, &ep2, &t) == 1);
37311be35a1SLionel Sambuc
37411be35a1SLionel Sambuc ATF_REQUIRE(ep != NULL);
37511be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep->key, "a");
376*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep->data, 0);
37711be35a1SLionel Sambuc
37811be35a1SLionel Sambuc ATF_REQUIRE(ep2 != NULL);
37911be35a1SLionel Sambuc ATF_REQUIRE_STREQ(ep2->key, "b");
380*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ((intptr_t)ep2->data, 1);
38111be35a1SLionel Sambuc
38211be35a1SLionel Sambuc hdestroy_r(&t);
38311be35a1SLionel Sambuc }
38411be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)38511be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
38611be35a1SLionel Sambuc {
38711be35a1SLionel Sambuc
38811be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, hsearch_basic);
38911be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, hsearch_duplicate);
39011be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, hsearch_nonexistent);
39111be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, hsearch_two);
39211be35a1SLionel Sambuc
39311be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, hsearch_r_basic);
39411be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, hsearch_r_duplicate);
39511be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, hsearch_r_nonexistent);
39611be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, hsearch_r_two);
39711be35a1SLionel Sambuc
39811be35a1SLionel Sambuc return atf_no_error();
39911be35a1SLionel Sambuc }
400