t_hsearch.c revision 1.2 1 1.2 jruoho /* $NetBSD: t_hsearch.c,v 1.2 2011/07/07 11:12:18 jruoho Exp $ */
2 1.1 pgoyette
3 1.1 pgoyette /*-
4 1.1 pgoyette * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 pgoyette * All rights reserved.
6 1.1 pgoyette *
7 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1 pgoyette * modification, are permitted provided that the following conditions
9 1.1 pgoyette * are met:
10 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1 pgoyette *
16 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.1 pgoyette */
28 1.1 pgoyette
29 1.1 pgoyette /*
30 1.1 pgoyette * Copyright (c) 2001 Christopher G. Demetriou
31 1.1 pgoyette * All rights reserved.
32 1.2 jruoho *
33 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
34 1.1 pgoyette * modification, are permitted provided that the following conditions
35 1.1 pgoyette * are met:
36 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
37 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
38 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
39 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
40 1.1 pgoyette * documentation and/or other materials provided with the distribution.
41 1.1 pgoyette * 3. All advertising materials mentioning features or use of this software
42 1.1 pgoyette * must display the following acknowledgement:
43 1.1 pgoyette * This product includes software developed for the
44 1.1 pgoyette * NetBSD Project. See http://www.NetBSD.org/ for
45 1.1 pgoyette * information about NetBSD.
46 1.1 pgoyette * 4. The name of the author may not be used to endorse or promote products
47 1.1 pgoyette * derived from this software without specific prior written permission.
48 1.2 jruoho *
49 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 1.1 pgoyette * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 1.1 pgoyette * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 1.1 pgoyette * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 1.1 pgoyette * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 1.1 pgoyette * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 1.1 pgoyette * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 1.1 pgoyette * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 1.1 pgoyette * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 1.1 pgoyette * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 1.2 jruoho *
60 1.1 pgoyette * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
61 1.1 pgoyette */
62 1.1 pgoyette
63 1.1 pgoyette #include <sys/cdefs.h>
64 1.1 pgoyette __COPYRIGHT("@(#) Copyright (c) 2008\
65 1.1 pgoyette The NetBSD Foundation, inc. All rights reserved.");
66 1.2 jruoho __RCSID("$NetBSD: t_hsearch.c,v 1.2 2011/07/07 11:12:18 jruoho Exp $");
67 1.1 pgoyette
68 1.1 pgoyette #include <errno.h>
69 1.1 pgoyette #include <search.h>
70 1.1 pgoyette #include <string.h>
71 1.1 pgoyette #include <stdio.h>
72 1.1 pgoyette
73 1.1 pgoyette #include <atf-c.h>
74 1.1 pgoyette
75 1.1 pgoyette #define REQUIRE_ERRNO(x) ATF_REQUIRE_MSG(x, "%s", strerror(errno))
76 1.1 pgoyette
77 1.2 jruoho ATF_TC(hsearch_basic);
78 1.2 jruoho ATF_TC_HEAD(hsearch_basic, tc)
79 1.1 pgoyette {
80 1.1 pgoyette
81 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks basic insertions and searching");
82 1.1 pgoyette }
83 1.1 pgoyette
84 1.2 jruoho ATF_TC_BODY(hsearch_basic, tc)
85 1.1 pgoyette {
86 1.1 pgoyette ENTRY e, *ep;
87 1.1 pgoyette char ch[2];
88 1.1 pgoyette int i;
89 1.1 pgoyette
90 1.1 pgoyette REQUIRE_ERRNO(hcreate(16) != 0);
91 1.1 pgoyette
92 1.1 pgoyette /* ch[1] should be constant from here on down. */
93 1.1 pgoyette ch[1] = '\0';
94 1.1 pgoyette
95 1.1 pgoyette /* Basic insertions. Check enough that there'll be collisions. */
96 1.1 pgoyette for (i = 0; i < 26; i++) {
97 1.1 pgoyette ch[0] = 'a' + i;
98 1.1 pgoyette e.key = strdup(ch); /* ptr to provided key is kept! */
99 1.1 pgoyette ATF_REQUIRE(e.key != NULL);
100 1.1 pgoyette e.data = (void *)(long)i;
101 1.1 pgoyette
102 1.1 pgoyette ep = hsearch(e, ENTER);
103 1.1 pgoyette
104 1.1 pgoyette ATF_REQUIRE(ep != NULL);
105 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, ch);
106 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, i);
107 1.1 pgoyette }
108 1.1 pgoyette
109 1.1 pgoyette /* e.key should be constant from here on down. */
110 1.1 pgoyette e.key = ch;
111 1.1 pgoyette
112 1.1 pgoyette /* Basic lookups. */
113 1.1 pgoyette for (i = 0; i < 26; i++) {
114 1.1 pgoyette ch[0] = 'a' + i;
115 1.1 pgoyette
116 1.1 pgoyette ep = hsearch(e, FIND);
117 1.1 pgoyette
118 1.1 pgoyette ATF_REQUIRE(ep != NULL);
119 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, ch);
120 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, i);
121 1.1 pgoyette }
122 1.1 pgoyette
123 1.1 pgoyette hdestroy();
124 1.1 pgoyette }
125 1.1 pgoyette
126 1.2 jruoho ATF_TC(hsearch_duplicate);
127 1.2 jruoho ATF_TC_HEAD(hsearch_duplicate, tc)
128 1.1 pgoyette {
129 1.1 pgoyette
130 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks that inserting duplicate "
131 1.1 pgoyette "doesn't overwrite existing data");
132 1.1 pgoyette }
133 1.1 pgoyette
134 1.2 jruoho ATF_TC_BODY(hsearch_duplicate, tc)
135 1.1 pgoyette {
136 1.1 pgoyette ENTRY e, *ep;
137 1.1 pgoyette
138 1.1 pgoyette REQUIRE_ERRNO(hcreate(16));
139 1.1 pgoyette
140 1.1 pgoyette e.key = strdup("a");
141 1.1 pgoyette ATF_REQUIRE(e.key != NULL);
142 1.1 pgoyette e.data = (void *)(long) 0;
143 1.1 pgoyette
144 1.1 pgoyette ep = hsearch(e, ENTER);
145 1.1 pgoyette
146 1.1 pgoyette ATF_REQUIRE(ep != NULL);
147 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, "a");
148 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, 0);
149 1.1 pgoyette
150 1.1 pgoyette e.data = (void *)(long)12345;
151 1.1 pgoyette
152 1.1 pgoyette ep = hsearch(e, ENTER);
153 1.1 pgoyette ep = hsearch(e, FIND);
154 1.1 pgoyette
155 1.1 pgoyette ATF_REQUIRE(ep != NULL);
156 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, "a");
157 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, 0);
158 1.1 pgoyette
159 1.1 pgoyette hdestroy();
160 1.1 pgoyette }
161 1.1 pgoyette
162 1.2 jruoho ATF_TC(hsearch_nonexistent);
163 1.2 jruoho ATF_TC_HEAD(hsearch_nonexistent, tc)
164 1.1 pgoyette {
165 1.1 pgoyette
166 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
167 1.1 pgoyette "Checks searching for non-existent entry");
168 1.1 pgoyette }
169 1.1 pgoyette
170 1.2 jruoho ATF_TC_BODY(hsearch_nonexistent, tc)
171 1.1 pgoyette {
172 1.1 pgoyette ENTRY e, *ep;
173 1.1 pgoyette
174 1.1 pgoyette REQUIRE_ERRNO(hcreate(16));
175 1.1 pgoyette
176 1.1 pgoyette e.key = strdup("A");
177 1.1 pgoyette ep = hsearch(e, FIND);
178 1.1 pgoyette ATF_REQUIRE_EQ(ep, NULL);
179 1.1 pgoyette
180 1.1 pgoyette hdestroy();
181 1.1 pgoyette }
182 1.1 pgoyette
183 1.2 jruoho ATF_TC(hsearch_two);
184 1.2 jruoho ATF_TC_HEAD(hsearch_two, tc)
185 1.1 pgoyette {
186 1.1 pgoyette
187 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
188 1.1 pgoyette "Checks that searching doesn't overwrite previous search results");
189 1.1 pgoyette }
190 1.1 pgoyette
191 1.2 jruoho ATF_TC_BODY(hsearch_two, tc)
192 1.1 pgoyette {
193 1.1 pgoyette ENTRY e, *ep, *ep2;
194 1.1 pgoyette char *sa, *sb;
195 1.1 pgoyette
196 1.1 pgoyette ATF_REQUIRE((sa = strdup("a")) != NULL);
197 1.1 pgoyette ATF_REQUIRE((sb = strdup("b")) != NULL);
198 1.1 pgoyette
199 1.1 pgoyette REQUIRE_ERRNO(hcreate(16));
200 1.1 pgoyette
201 1.1 pgoyette e.key = sa;
202 1.1 pgoyette e.data = (void*)(long)0;
203 1.1 pgoyette
204 1.1 pgoyette ep = hsearch(e, ENTER);
205 1.1 pgoyette
206 1.1 pgoyette ATF_REQUIRE(ep != NULL);
207 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, "a");
208 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, 0);
209 1.1 pgoyette
210 1.1 pgoyette e.key = sb;
211 1.1 pgoyette e.data = (void*)(long)1;
212 1.1 pgoyette
213 1.1 pgoyette ep = hsearch(e, ENTER);
214 1.2 jruoho
215 1.1 pgoyette ATF_REQUIRE(ep != NULL);
216 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, "b");
217 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, 1);
218 1.1 pgoyette
219 1.1 pgoyette e.key = sa;
220 1.1 pgoyette ep = hsearch(e, FIND);
221 1.1 pgoyette
222 1.1 pgoyette e.key = sb;
223 1.1 pgoyette ep2 = hsearch(e, FIND);
224 1.1 pgoyette
225 1.1 pgoyette ATF_REQUIRE(ep != NULL);
226 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, "a");
227 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, 0);
228 1.1 pgoyette
229 1.1 pgoyette ATF_REQUIRE(ep2 != NULL);
230 1.1 pgoyette ATF_REQUIRE_STREQ(ep2->key, "b");
231 1.1 pgoyette ATF_REQUIRE_EQ((long)ep2->data, 1);
232 1.2 jruoho
233 1.1 pgoyette hdestroy();
234 1.1 pgoyette }
235 1.1 pgoyette
236 1.1 pgoyette ATF_TP_ADD_TCS(tp)
237 1.1 pgoyette {
238 1.1 pgoyette
239 1.2 jruoho ATF_TP_ADD_TC(tp, hsearch_basic);
240 1.2 jruoho ATF_TP_ADD_TC(tp, hsearch_duplicate);
241 1.2 jruoho ATF_TP_ADD_TC(tp, hsearch_nonexistent);
242 1.2 jruoho ATF_TP_ADD_TC(tp, hsearch_two);
243 1.1 pgoyette
244 1.1 pgoyette return atf_no_error();
245 1.1 pgoyette }
246