1 /* Copyright (c) 2008 The NetBSD Foundation, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
26 #include "atf-c/detail/map.h"
33 #include "atf-c/detail/test_helpers.h"
34 #include "atf-c/utils.h"
36 /* ---------------------------------------------------------------------
37 * Tests for the "atf_map" type.
38 * --------------------------------------------------------------------- */
41 * Constructors and destructors.
45 ATF_TC_HEAD(map_init, tc)
47 atf_tc_set_md_var(tc, "descr", "Checks the atf_map_init function");
49 ATF_TC_BODY(map_init, tc)
53 RE(atf_map_init(&map));
54 ATF_REQUIRE_EQ(atf_map_size(&map), 0);
58 ATF_TC_WITHOUT_HEAD(map_init_charpp_null);
59 ATF_TC_BODY(map_init_charpp_null, tc)
63 RE(atf_map_init_charpp(&map, NULL));
64 ATF_REQUIRE_EQ(atf_map_size(&map), 0);
68 ATF_TC_WITHOUT_HEAD(map_init_charpp_empty);
69 ATF_TC_BODY(map_init_charpp_empty, tc)
71 const char *const array[] = { NULL };
74 RE(atf_map_init_charpp(&map, array));
75 ATF_REQUIRE_EQ(atf_map_size(&map), 0);
79 ATF_TC_WITHOUT_HEAD(map_init_charpp_some);
80 ATF_TC_BODY(map_init_charpp_some, tc)
82 const char *const array[] = { "K1", "V1", "K2", "V2", NULL };
86 RE(atf_map_init_charpp(&map, array));
87 ATF_REQUIRE_EQ(atf_map_size(&map), 2);
89 iter = atf_map_find_c(&map, "K1");
90 ATF_REQUIRE(!atf_equal_map_citer_map_citer(iter, atf_map_end_c(&map)));
91 ATF_REQUIRE(strcmp(atf_map_citer_key(iter), "K1") == 0);
92 ATF_REQUIRE(strcmp(atf_map_citer_data(iter), "V1") == 0);
94 iter = atf_map_find_c(&map, "K2");
95 ATF_REQUIRE(!atf_equal_map_citer_map_citer(iter, atf_map_end_c(&map)));
96 ATF_REQUIRE(strcmp(atf_map_citer_key(iter), "K2") == 0);
97 ATF_REQUIRE(strcmp(atf_map_citer_data(iter), "V2") == 0);
102 ATF_TC_WITHOUT_HEAD(map_init_charpp_short);
103 ATF_TC_BODY(map_init_charpp_short, tc)
105 const char *const array[] = { "K1", "V1", "K2", NULL };
108 atf_error_t err = atf_map_init_charpp(&map, array);
109 ATF_REQUIRE(atf_is_error(err));
110 ATF_REQUIRE(atf_error_is(err, "libc"));
118 ATF_TC_HEAD(find, tc)
120 atf_tc_set_md_var(tc, "descr", "Checks the atf_map_find function");
122 ATF_TC_BODY(find, tc)
129 RE(atf_map_init(&map));
130 RE(atf_map_insert(&map, "K1", val1, false));
131 RE(atf_map_insert(&map, "K2", val2, false));
133 iter = atf_map_find(&map, "K0");
134 ATF_REQUIRE(atf_equal_map_iter_map_iter(iter, atf_map_end(&map)));
136 iter = atf_map_find(&map, "K1");
137 ATF_REQUIRE(!atf_equal_map_iter_map_iter(iter, atf_map_end(&map)));
138 ATF_REQUIRE(strcmp(atf_map_iter_key(iter), "K1") == 0);
139 ATF_REQUIRE(strcmp(atf_map_iter_data(iter), "V1") == 0);
140 strcpy(atf_map_iter_data(iter), "Z1");
142 iter = atf_map_find(&map, "K1");
143 ATF_REQUIRE(!atf_equal_map_iter_map_iter(iter, atf_map_end(&map)));
144 ATF_REQUIRE(strcmp(atf_map_iter_key(iter), "K1") == 0);
145 ATF_REQUIRE(strcmp(atf_map_iter_data(iter), "Z1") == 0);
147 iter = atf_map_find(&map, "K2");
148 ATF_REQUIRE(!atf_equal_map_iter_map_iter(iter, atf_map_end(&map)));
149 ATF_REQUIRE(strcmp(atf_map_iter_key(iter), "K2") == 0);
150 ATF_REQUIRE(strcmp(atf_map_iter_data(iter), "V2") == 0);
156 ATF_TC_HEAD(find_c, tc)
158 atf_tc_set_md_var(tc, "descr", "Checks the atf_map_find_c function");
160 ATF_TC_BODY(find_c, tc)
165 atf_map_citer_t iter;
167 RE(atf_map_init(&map));
168 RE(atf_map_insert(&map, "K1", val1, false));
169 RE(atf_map_insert(&map, "K2", val2, false));
171 iter = atf_map_find_c(&map, "K0");
172 ATF_REQUIRE(atf_equal_map_citer_map_citer(iter, atf_map_end_c(&map)));
174 iter = atf_map_find_c(&map, "K1");
175 ATF_REQUIRE(!atf_equal_map_citer_map_citer(iter, atf_map_end_c(&map)));
176 ATF_REQUIRE(strcmp(atf_map_citer_key(iter), "K1") == 0);
177 ATF_REQUIRE(strcmp(atf_map_citer_data(iter), "V1") == 0);
179 iter = atf_map_find_c(&map, "K2");
180 ATF_REQUIRE(!atf_equal_map_citer_map_citer(iter, atf_map_end_c(&map)));
181 ATF_REQUIRE(strcmp(atf_map_citer_key(iter), "K2") == 0);
182 ATF_REQUIRE(strcmp(atf_map_citer_data(iter), "V2") == 0);
187 ATF_TC_WITHOUT_HEAD(to_charpp_empty);
188 ATF_TC_BODY(to_charpp_empty, tc)
193 RE(atf_map_init(&map));
194 ATF_REQUIRE((array = atf_map_to_charpp(&map)) != NULL);
197 ATF_CHECK_EQ(NULL, array[0]);
198 atf_utils_free_charpp(array);
201 ATF_TC_WITHOUT_HEAD(to_charpp_some);
202 ATF_TC_BODY(to_charpp_some, tc)
211 RE(atf_map_init(&map));
212 RE(atf_map_insert(&map, "K1", s1, false));
213 RE(atf_map_insert(&map, "K2", s2, false));
214 RE(atf_map_insert(&map, "K3", s3, false));
215 ATF_REQUIRE((array = atf_map_to_charpp(&map)) != NULL);
218 ATF_CHECK_STREQ("K1", array[0]);
219 ATF_CHECK_STREQ("one", array[1]);
220 ATF_CHECK_STREQ("K2", array[2]);
221 ATF_CHECK_STREQ("two", array[3]);
222 ATF_CHECK_STREQ("K3", array[4]);
223 ATF_CHECK_STREQ("three", array[5]);
224 ATF_CHECK_EQ(NULL, array[6]);
225 atf_utils_free_charpp(array);
233 ATF_TC_HEAD(map_insert, tc)
235 atf_tc_set_md_var(tc, "descr", "Checks the atf_map_insert function");
237 ATF_TC_BODY(map_insert, tc)
240 char buf[] = "1st test string";
241 char buf2[] = "2nd test string";
243 atf_map_citer_t iter;
245 RE(atf_map_init(&map));
247 printf("Inserting some values\n");
248 ATF_REQUIRE_EQ(atf_map_size(&map), 0);
249 RE(atf_map_insert(&map, "K1", buf, false));
250 ATF_REQUIRE_EQ(atf_map_size(&map), 1);
251 RE(atf_map_insert(&map, "K2", buf, false));
252 ATF_REQUIRE_EQ(atf_map_size(&map), 2);
253 RE(atf_map_insert(&map, "K3", buf, false));
254 ATF_REQUIRE_EQ(atf_map_size(&map), 3);
256 printf("Replacing a value\n");
257 iter = atf_map_find_c(&map, "K3");
258 ATF_REQUIRE(!atf_equal_map_citer_map_citer(iter, atf_map_end_c(&map)));
259 ptr = atf_map_citer_data(iter);
260 ATF_REQUIRE_EQ(ptr, buf);
261 RE(atf_map_insert(&map, "K3", buf2, false));
262 ATF_REQUIRE_EQ(atf_map_size(&map), 3);
263 iter = atf_map_find_c(&map, "K3");
264 ATF_REQUIRE(!atf_equal_map_citer_map_citer(iter, atf_map_end_c(&map)));
265 ptr = atf_map_citer_data(iter);
266 ATF_REQUIRE_EQ(ptr, buf2);
275 ATF_TC(map_for_each);
276 ATF_TC_HEAD(map_for_each, tc)
278 atf_tc_set_md_var(tc, "descr", "Checks the atf_map_for_each macro");
280 ATF_TC_BODY(map_for_each, tc)
284 size_t count, i, size;
288 printf("Iterating over empty map\n");
289 RE(atf_map_init(&map));
291 atf_map_for_each(iter, &map) {
293 printf("Item count is now %zd\n", count);
295 ATF_REQUIRE_EQ(count, 0);
298 for (size = 0; size <= 10; size++) {
299 printf("Iterating over map of %zd elements\n", size);
300 RE(atf_map_init(&map));
301 for (i = 0; i < size; i++) {
303 snprintf(keys[i], sizeof(keys[i]), "%d", nums[i]);
304 RE(atf_map_insert(&map, keys[i], &nums[i], false));
307 atf_map_for_each(iter, &map) {
308 printf("Retrieved item: %d\n", *(int *)atf_map_iter_data(iter));
311 ATF_REQUIRE_EQ(count, size);
316 ATF_TC(map_for_each_c);
317 ATF_TC_HEAD(map_for_each_c, tc)
319 atf_tc_set_md_var(tc, "descr", "Checks the atf_map_for_each_c macro");
321 ATF_TC_BODY(map_for_each_c, tc)
324 atf_map_citer_t iter;
325 size_t count, i, size;
329 printf("Iterating over empty map\n");
330 RE(atf_map_init(&map));
332 atf_map_for_each_c(iter, &map) {
334 printf("Item count is now %zd\n", count);
336 ATF_REQUIRE_EQ(count, 0);
339 for (size = 0; size <= 10; size++) {
340 printf("Iterating over map of %zd elements\n", size);
341 RE(atf_map_init(&map));
342 for (i = 0; i < size; i++) {
344 snprintf(keys[i], sizeof(keys[i]), "%d", nums[i]);
345 RE(atf_map_insert(&map, keys[i], &nums[i], false));
348 atf_map_for_each_c(iter, &map) {
349 printf("Retrieved item: %d\n",
350 *(const int *)atf_map_citer_data(iter));
353 ATF_REQUIRE_EQ(count, size);
363 ATF_TC_HEAD(stable_keys, tc)
365 atf_tc_set_md_var(tc, "descr", "Checks that the keys do not change "
366 "even if their original values do");
368 ATF_TC_BODY(stable_keys, tc)
371 atf_map_citer_t iter;
374 RE(atf_map_init(&map));
376 RE(atf_map_insert(&map, key, strdup("test-value"), true));
377 iter = atf_map_find_c(&map, "K1");
378 ATF_REQUIRE(!atf_equal_map_citer_map_citer(iter, atf_map_end_c(&map)));
379 iter = atf_map_find_c(&map, "K2");
380 ATF_REQUIRE(atf_equal_map_citer_map_citer(iter, atf_map_end_c(&map)));
383 iter = atf_map_find_c(&map, "K1");
384 ATF_REQUIRE(!atf_equal_map_citer_map_citer(iter, atf_map_end_c(&map)));
385 iter = atf_map_find_c(&map, "K2");
386 ATF_REQUIRE(atf_equal_map_citer_map_citer(iter, atf_map_end_c(&map)));
391 /* ---------------------------------------------------------------------
393 * --------------------------------------------------------------------- */
397 /* Constructors and destructors. */
398 ATF_TP_ADD_TC(tp, map_init);
399 ATF_TP_ADD_TC(tp, map_init_charpp_null);
400 ATF_TP_ADD_TC(tp, map_init_charpp_empty);
401 ATF_TP_ADD_TC(tp, map_init_charpp_some);
402 ATF_TP_ADD_TC(tp, map_init_charpp_short);
405 ATF_TP_ADD_TC(tp, find);
406 ATF_TP_ADD_TC(tp, find_c);
407 ATF_TP_ADD_TC(tp, to_charpp_empty);
408 ATF_TP_ADD_TC(tp, to_charpp_some);
411 ATF_TP_ADD_TC(tp, map_insert);
414 ATF_TP_ADD_TC(tp, map_for_each);
415 ATF_TP_ADD_TC(tp, map_for_each_c);
418 ATF_TP_ADD_TC(tp, stable_keys);
420 return atf_no_error();