2 * Automated Testing Framework (atf)
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
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "atf-c/utils.h"
38 #include "test_helpers.h"
40 /* ---------------------------------------------------------------------
41 * Tests for the "atf_list" type.
42 * --------------------------------------------------------------------- */
45 * Constructors and destructors.
49 ATF_TC_HEAD(list_init
, tc
)
51 atf_tc_set_md_var(tc
, "descr", "Checks the atf_list_init function");
53 ATF_TC_BODY(list_init
, tc
)
57 RE(atf_list_init(&list
));
58 ATF_REQUIRE_EQ(atf_list_size(&list
), 0);
67 ATF_TC_HEAD(list_index
, tc
)
69 atf_tc_set_md_var(tc
, "descr", "Checks the atf_list_index function");
71 ATF_TC_BODY(list_index
, tc
)
78 RE(atf_list_init(&list
));
79 RE(atf_list_append(&list
, &i1
, false));
80 RE(atf_list_append(&list
, &i2
, false));
81 RE(atf_list_append(&list
, &i3
, false));
83 ATF_CHECK_EQ(*(int *)atf_list_index(&list
, 0), 1);
84 ATF_CHECK_EQ(*(int *)atf_list_index(&list
, 1), 5);
85 ATF_CHECK_EQ(*(int *)atf_list_index(&list
, 2), 9);
91 ATF_TC_HEAD(list_index_c
, tc
)
93 atf_tc_set_md_var(tc
, "descr", "Checks the atf_list_index_c function");
95 ATF_TC_BODY(list_index_c
, tc
)
102 RE(atf_list_init(&list
));
103 RE(atf_list_append(&list
, &i1
, false));
104 RE(atf_list_append(&list
, &i2
, false));
105 RE(atf_list_append(&list
, &i3
, false));
107 ATF_CHECK_EQ(*(const int *)atf_list_index_c(&list
, 0), 1);
108 ATF_CHECK_EQ(*(const int *)atf_list_index_c(&list
, 1), 5);
109 ATF_CHECK_EQ(*(const int *)atf_list_index_c(&list
, 2), 9);
111 atf_list_fini(&list
);
114 ATF_TC_WITHOUT_HEAD(list_to_charpp_empty
);
115 ATF_TC_BODY(list_to_charpp_empty
, tc
)
120 RE(atf_list_init(&list
));
121 ATF_REQUIRE((array
= atf_list_to_charpp(&list
)) != NULL
);
122 atf_list_fini(&list
);
124 ATF_CHECK_EQ(NULL
, array
[0]);
125 atf_utils_free_charpp(array
);
128 ATF_TC_WITHOUT_HEAD(list_to_charpp_some
);
129 ATF_TC_BODY(list_to_charpp_some
, tc
)
138 RE(atf_list_init(&list
));
139 RE(atf_list_append(&list
, s1
, false));
140 RE(atf_list_append(&list
, s2
, false));
141 RE(atf_list_append(&list
, s3
, false));
142 ATF_REQUIRE((array
= atf_list_to_charpp(&list
)) != NULL
);
143 atf_list_fini(&list
);
145 ATF_CHECK_STREQ("one", array
[0]);
146 ATF_CHECK_STREQ("two", array
[1]);
147 ATF_CHECK_STREQ("three", array
[2]);
148 ATF_CHECK_EQ(NULL
, array
[3]);
149 atf_utils_free_charpp(array
);
157 ATF_TC_HEAD(list_append
, tc
)
159 atf_tc_set_md_var(tc
, "descr", "Checks the atf_list_append function");
161 ATF_TC_BODY(list_append
, tc
)
165 char buf
[] = "Test string";
167 RE(atf_list_init(&list
));
168 for (i
= 0; i
< 1024; i
++) {
169 ATF_REQUIRE_EQ(atf_list_size(&list
), i
);
170 RE(atf_list_append(&list
, buf
, false));
172 atf_list_fini(&list
);
175 ATF_TC(list_append_list
);
176 ATF_TC_HEAD(list_append_list
, tc
)
178 atf_tc_set_md_var(tc
, "descr", "Checks the atf_list_append_list "
181 ATF_TC_BODY(list_append_list
, tc
)
186 RE(atf_list_init(&l1
));
187 RE(atf_list_init(&l2
));
189 atf_list_append_list(&l1
, &l2
);
190 ATF_CHECK_EQ(atf_list_size(&l1
), 0);
199 RE(atf_list_init(&l1
));
200 RE(atf_list_append(&l1
, &item
, false));
201 RE(atf_list_init(&l2
));
203 atf_list_append_list(&l1
, &l2
);
204 ATF_CHECK_EQ(atf_list_size(&l1
), 1);
205 ATF_CHECK_EQ(*(int *)atf_list_index(&l1
, 0), item
);
214 RE(atf_list_init(&l1
));
215 RE(atf_list_init(&l2
));
216 RE(atf_list_append(&l2
, &item
, false));
218 atf_list_append_list(&l1
, &l2
);
219 ATF_CHECK_EQ(atf_list_size(&l1
), 1);
220 ATF_CHECK_EQ(*(int *)atf_list_index(&l1
, 0), item
);
230 RE(atf_list_init(&l1
));
231 RE(atf_list_append(&l1
, &item1
, false));
232 RE(atf_list_init(&l2
));
233 RE(atf_list_append(&l2
, &item2
, false));
235 atf_list_append_list(&l1
, &l2
);
236 ATF_CHECK_EQ(atf_list_size(&l1
), 2);
237 ATF_CHECK_EQ(*(int *)atf_list_index(&l1
, 0), item1
);
238 ATF_CHECK_EQ(*(int *)atf_list_index(&l1
, 1), item2
);
245 atf_list_citer_t end1
, end2
;
247 RE(atf_list_init(&l1
));
248 RE(atf_list_init(&l2
));
250 end1
= atf_list_end_c(&l1
);
251 end2
= atf_list_end_c(&l2
);
252 /* XXX Shouldn't query m_entry here. */
253 ATF_CHECK(end1
.m_entry
!= end2
.m_entry
);
255 atf_list_append_list(&l1
, &l2
);
256 ATF_CHECK(atf_list_end_c(&l1
).m_entry
== end2
.m_entry
);
266 ATF_TC(list_for_each
);
267 ATF_TC_HEAD(list_for_each
, tc
)
269 atf_tc_set_md_var(tc
, "descr", "Checks the atf_list_for_each macro");
271 ATF_TC_BODY(list_for_each
, tc
)
274 atf_list_iter_t iter
;
275 size_t count
, i
, size
;
278 printf("Iterating over empty list\n");
279 RE(atf_list_init(&list
));
281 atf_list_for_each(iter
, &list
) {
283 printf("Item count is now %zd\n", count
);
285 ATF_REQUIRE_EQ(count
, 0);
286 atf_list_fini(&list
);
288 for (size
= 0; size
<= 10; size
++) {
289 printf("Iterating over list of %zd elements\n", size
);
290 RE(atf_list_init(&list
));
291 for (i
= 0; i
< size
; i
++) {
293 RE(atf_list_append(&list
, &nums
[i
], false));
296 atf_list_for_each(iter
, &list
) {
297 printf("Retrieved item: %d\n", *(int *)atf_list_iter_data(iter
));
300 ATF_REQUIRE_EQ(count
, size
);
301 atf_list_fini(&list
);
305 ATF_TC(list_for_each_c
);
306 ATF_TC_HEAD(list_for_each_c
, tc
)
308 atf_tc_set_md_var(tc
, "descr", "Checks the atf_list_for_each_c macro");
310 ATF_TC_BODY(list_for_each_c
, tc
)
313 atf_list_citer_t iter
;
314 size_t count
, i
, size
;
317 printf("Iterating over empty list\n");
318 RE(atf_list_init(&list
));
320 atf_list_for_each_c(iter
, &list
) {
322 printf("Item count is now %zd\n", count
);
324 ATF_REQUIRE_EQ(count
, 0);
325 atf_list_fini(&list
);
327 for (size
= 0; size
<= 10; size
++) {
328 printf("Iterating over list of %zd elements\n", size
);
329 RE(atf_list_init(&list
));
330 for (i
= 0; i
< size
; i
++) {
332 RE(atf_list_append(&list
, &nums
[i
], false));
335 atf_list_for_each_c(iter
, &list
) {
336 printf("Retrieved item: %d\n",
337 *(const int *)atf_list_citer_data(iter
));
340 ATF_REQUIRE_EQ(count
, size
);
341 atf_list_fini(&list
);
345 /* ---------------------------------------------------------------------
347 * --------------------------------------------------------------------- */
351 /* Constructors and destructors. */
352 ATF_TP_ADD_TC(tp
, list_init
);
355 ATF_TP_ADD_TC(tp
, list_index
);
356 ATF_TP_ADD_TC(tp
, list_index_c
);
357 ATF_TP_ADD_TC(tp
, list_to_charpp_empty
);
358 ATF_TP_ADD_TC(tp
, list_to_charpp_some
);
361 ATF_TP_ADD_TC(tp
, list_append
);
362 ATF_TP_ADD_TC(tp
, list_append_list
);
365 ATF_TP_ADD_TC(tp
, list_for_each
);
366 ATF_TP_ADD_TC(tp
, list_for_each_c
);
368 return atf_no_error();