t_hsearch.c revision 1.1 1 1.1 pgoyette /* $NetBSD: t_hsearch.c,v 1.1 2011/01/13 14:32:35 pgoyette 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.1 pgoyette *
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.1 pgoyette *
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.1 pgoyette *
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.1 pgoyette __RCSID("$NetBSD: t_hsearch.c,v 1.1 2011/01/13 14:32:35 pgoyette 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.1 pgoyette ATF_TC(basic);
78 1.1 pgoyette
79 1.1 pgoyette ATF_TC_HEAD(basic, tc)
80 1.1 pgoyette {
81 1.1 pgoyette
82 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks basic insertions and searching");
83 1.1 pgoyette }
84 1.1 pgoyette
85 1.1 pgoyette ATF_TC_BODY(basic, tc)
86 1.1 pgoyette {
87 1.1 pgoyette ENTRY e, *ep;
88 1.1 pgoyette char ch[2];
89 1.1 pgoyette int i;
90 1.1 pgoyette
91 1.1 pgoyette REQUIRE_ERRNO(hcreate(16) != 0);
92 1.1 pgoyette
93 1.1 pgoyette /* ch[1] should be constant from here on down. */
94 1.1 pgoyette ch[1] = '\0';
95 1.1 pgoyette
96 1.1 pgoyette /* Basic insertions. Check enough that there'll be collisions. */
97 1.1 pgoyette for (i = 0; i < 26; i++) {
98 1.1 pgoyette ch[0] = 'a' + i;
99 1.1 pgoyette e.key = strdup(ch); /* ptr to provided key is kept! */
100 1.1 pgoyette ATF_REQUIRE(e.key != NULL);
101 1.1 pgoyette e.data = (void *)(long)i;
102 1.1 pgoyette
103 1.1 pgoyette ep = hsearch(e, ENTER);
104 1.1 pgoyette
105 1.1 pgoyette ATF_REQUIRE(ep != NULL);
106 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, ch);
107 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, i);
108 1.1 pgoyette }
109 1.1 pgoyette
110 1.1 pgoyette /* e.key should be constant from here on down. */
111 1.1 pgoyette e.key = ch;
112 1.1 pgoyette
113 1.1 pgoyette /* Basic lookups. */
114 1.1 pgoyette for (i = 0; i < 26; i++) {
115 1.1 pgoyette ch[0] = 'a' + i;
116 1.1 pgoyette
117 1.1 pgoyette ep = hsearch(e, FIND);
118 1.1 pgoyette
119 1.1 pgoyette ATF_REQUIRE(ep != NULL);
120 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, ch);
121 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, i);
122 1.1 pgoyette }
123 1.1 pgoyette
124 1.1 pgoyette hdestroy();
125 1.1 pgoyette }
126 1.1 pgoyette
127 1.1 pgoyette ATF_TC(duplicate);
128 1.1 pgoyette
129 1.1 pgoyette ATF_TC_HEAD(duplicate, tc)
130 1.1 pgoyette {
131 1.1 pgoyette
132 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks that inserting duplicate "
133 1.1 pgoyette "doesn't overwrite existing data");
134 1.1 pgoyette }
135 1.1 pgoyette
136 1.1 pgoyette ATF_TC_BODY(duplicate, tc)
137 1.1 pgoyette {
138 1.1 pgoyette ENTRY e, *ep;
139 1.1 pgoyette
140 1.1 pgoyette REQUIRE_ERRNO(hcreate(16));
141 1.1 pgoyette
142 1.1 pgoyette e.key = strdup("a");
143 1.1 pgoyette ATF_REQUIRE(e.key != NULL);
144 1.1 pgoyette e.data = (void *)(long) 0;
145 1.1 pgoyette
146 1.1 pgoyette ep = hsearch(e, ENTER);
147 1.1 pgoyette
148 1.1 pgoyette ATF_REQUIRE(ep != NULL);
149 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, "a");
150 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, 0);
151 1.1 pgoyette
152 1.1 pgoyette e.data = (void *)(long)12345;
153 1.1 pgoyette
154 1.1 pgoyette ep = hsearch(e, ENTER);
155 1.1 pgoyette ep = hsearch(e, FIND);
156 1.1 pgoyette
157 1.1 pgoyette ATF_REQUIRE(ep != NULL);
158 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, "a");
159 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, 0);
160 1.1 pgoyette
161 1.1 pgoyette hdestroy();
162 1.1 pgoyette }
163 1.1 pgoyette
164 1.1 pgoyette ATF_TC(nonexistent);
165 1.1 pgoyette
166 1.1 pgoyette ATF_TC_HEAD(nonexistent, tc)
167 1.1 pgoyette {
168 1.1 pgoyette
169 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
170 1.1 pgoyette "Checks searching for non-existent entry");
171 1.1 pgoyette }
172 1.1 pgoyette
173 1.1 pgoyette ATF_TC_BODY(nonexistent, tc)
174 1.1 pgoyette {
175 1.1 pgoyette ENTRY e, *ep;
176 1.1 pgoyette
177 1.1 pgoyette REQUIRE_ERRNO(hcreate(16));
178 1.1 pgoyette
179 1.1 pgoyette e.key = strdup("A");
180 1.1 pgoyette ep = hsearch(e, FIND);
181 1.1 pgoyette ATF_REQUIRE_EQ(ep, NULL);
182 1.1 pgoyette
183 1.1 pgoyette hdestroy();
184 1.1 pgoyette }
185 1.1 pgoyette
186 1.1 pgoyette ATF_TC(two);
187 1.1 pgoyette
188 1.1 pgoyette ATF_TC_HEAD(two, tc)
189 1.1 pgoyette {
190 1.1 pgoyette
191 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
192 1.1 pgoyette "Checks that searching doesn't overwrite previous search results");
193 1.1 pgoyette }
194 1.1 pgoyette
195 1.1 pgoyette ATF_TC_BODY(two, tc)
196 1.1 pgoyette {
197 1.1 pgoyette ENTRY e, *ep, *ep2;
198 1.1 pgoyette char *sa, *sb;
199 1.1 pgoyette
200 1.1 pgoyette ATF_REQUIRE((sa = strdup("a")) != NULL);
201 1.1 pgoyette ATF_REQUIRE((sb = strdup("b")) != NULL);
202 1.1 pgoyette
203 1.1 pgoyette REQUIRE_ERRNO(hcreate(16));
204 1.1 pgoyette
205 1.1 pgoyette e.key = sa;
206 1.1 pgoyette e.data = (void*)(long)0;
207 1.1 pgoyette
208 1.1 pgoyette ep = hsearch(e, ENTER);
209 1.1 pgoyette
210 1.1 pgoyette ATF_REQUIRE(ep != NULL);
211 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, "a");
212 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, 0);
213 1.1 pgoyette
214 1.1 pgoyette e.key = sb;
215 1.1 pgoyette e.data = (void*)(long)1;
216 1.1 pgoyette
217 1.1 pgoyette ep = hsearch(e, ENTER);
218 1.1 pgoyette
219 1.1 pgoyette ATF_REQUIRE(ep != NULL);
220 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, "b");
221 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, 1);
222 1.1 pgoyette
223 1.1 pgoyette e.key = sa;
224 1.1 pgoyette ep = hsearch(e, FIND);
225 1.1 pgoyette
226 1.1 pgoyette e.key = sb;
227 1.1 pgoyette ep2 = hsearch(e, FIND);
228 1.1 pgoyette
229 1.1 pgoyette ATF_REQUIRE(ep != NULL);
230 1.1 pgoyette ATF_REQUIRE_STREQ(ep->key, "a");
231 1.1 pgoyette ATF_REQUIRE_EQ((long)ep->data, 0);
232 1.1 pgoyette
233 1.1 pgoyette ATF_REQUIRE(ep2 != NULL);
234 1.1 pgoyette ATF_REQUIRE_STREQ(ep2->key, "b");
235 1.1 pgoyette ATF_REQUIRE_EQ((long)ep2->data, 1);
236 1.1 pgoyette
237 1.1 pgoyette hdestroy();
238 1.1 pgoyette }
239 1.1 pgoyette
240 1.1 pgoyette ATF_TP_ADD_TCS(tp)
241 1.1 pgoyette {
242 1.1 pgoyette
243 1.1 pgoyette ATF_TP_ADD_TC(tp, basic);
244 1.1 pgoyette ATF_TP_ADD_TC(tp, duplicate);
245 1.1 pgoyette ATF_TP_ADD_TC(tp, nonexistent);
246 1.1 pgoyette ATF_TP_ADD_TC(tp, two);
247 1.1 pgoyette
248 1.1 pgoyette return atf_no_error();
249 1.1 pgoyette }
250