]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/atf/atf-c/detail/list_test.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / atf / atf-c / detail / list_test.c
1 /*
2  * Automated Testing Framework (atf)
3  *
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
28  */
29
30 #include <stdio.h>
31 #include <string.h>
32
33 #include <atf-c.h>
34
35 #include "atf-c/utils.h"
36
37 #include "list.h"
38 #include "test_helpers.h"
39
40 /* ---------------------------------------------------------------------
41  * Tests for the "atf_list" type.
42  * --------------------------------------------------------------------- */
43
44 /*
45  * Constructors and destructors.
46  */
47
48 ATF_TC(list_init);
49 ATF_TC_HEAD(list_init, tc)
50 {
51     atf_tc_set_md_var(tc, "descr", "Checks the atf_list_init function");
52 }
53 ATF_TC_BODY(list_init, tc)
54 {
55     atf_list_t list;
56
57     RE(atf_list_init(&list));
58     ATF_REQUIRE_EQ(atf_list_size(&list), 0);
59     atf_list_fini(&list);
60 }
61
62 /*
63  * Getters.
64  */
65
66 ATF_TC(list_index);
67 ATF_TC_HEAD(list_index, tc)
68 {
69     atf_tc_set_md_var(tc, "descr", "Checks the atf_list_index function");
70 }
71 ATF_TC_BODY(list_index, tc)
72 {
73     atf_list_t list;
74     int i1 = 1;
75     int i2 = 5;
76     int i3 = 9;
77
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));
82
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);
86
87     atf_list_fini(&list);
88 }
89
90 ATF_TC(list_index_c);
91 ATF_TC_HEAD(list_index_c, tc)
92 {
93     atf_tc_set_md_var(tc, "descr", "Checks the atf_list_index_c function");
94 }
95 ATF_TC_BODY(list_index_c, tc)
96 {
97     atf_list_t list;
98     int i1 = 1;
99     int i2 = 5;
100     int i3 = 9;
101
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));
106
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);
110
111     atf_list_fini(&list);
112 }
113
114 ATF_TC_WITHOUT_HEAD(list_to_charpp_empty);
115 ATF_TC_BODY(list_to_charpp_empty, tc)
116 {
117     atf_list_t list;
118     char **array;
119
120     RE(atf_list_init(&list));
121     ATF_REQUIRE((array = atf_list_to_charpp(&list)) != NULL);
122     atf_list_fini(&list);
123
124     ATF_CHECK_EQ(NULL, array[0]);
125     atf_utils_free_charpp(array);
126 }
127
128 ATF_TC_WITHOUT_HEAD(list_to_charpp_some);
129 ATF_TC_BODY(list_to_charpp_some, tc)
130 {
131     atf_list_t list;
132     char **array;
133
134     char s1[] = "one";
135     char s2[] = "two";
136     char s3[] = "three";
137
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);
144
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);
150 }
151
152 /*
153  * Modifiers.
154  */
155
156 ATF_TC(list_append);
157 ATF_TC_HEAD(list_append, tc)
158 {
159     atf_tc_set_md_var(tc, "descr", "Checks the atf_list_append function");
160 }
161 ATF_TC_BODY(list_append, tc)
162 {
163     atf_list_t list;
164     size_t i;
165     char buf[] = "Test string";
166
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));
171     }
172     atf_list_fini(&list);
173 }
174
175 ATF_TC(list_append_list);
176 ATF_TC_HEAD(list_append_list, tc)
177 {
178     atf_tc_set_md_var(tc, "descr", "Checks the atf_list_append_list "
179                       "function");
180 }
181 ATF_TC_BODY(list_append_list, tc)
182 {
183     {
184         atf_list_t l1, l2;
185
186         RE(atf_list_init(&l1));
187         RE(atf_list_init(&l2));
188
189         atf_list_append_list(&l1, &l2);
190         ATF_CHECK_EQ(atf_list_size(&l1), 0);
191
192         atf_list_fini(&l1);
193     }
194
195     {
196         atf_list_t l1, l2;
197         int item = 5;
198
199         RE(atf_list_init(&l1));
200         RE(atf_list_append(&l1, &item, false));
201         RE(atf_list_init(&l2));
202
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);
206
207         atf_list_fini(&l1);
208     }
209
210     {
211         atf_list_t l1, l2;
212         int item = 5;
213
214         RE(atf_list_init(&l1));
215         RE(atf_list_init(&l2));
216         RE(atf_list_append(&l2, &item, false));
217
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);
221
222         atf_list_fini(&l1);
223     }
224
225     {
226         atf_list_t l1, l2;
227         int item1 = 5;
228         int item2 = 9;
229
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));
234
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);
239
240         atf_list_fini(&l1);
241     }
242
243     {
244         atf_list_t l1, l2;
245         atf_list_citer_t end1, end2;
246
247         RE(atf_list_init(&l1));
248         RE(atf_list_init(&l2));
249
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);
254
255         atf_list_append_list(&l1, &l2);
256         ATF_CHECK(atf_list_end_c(&l1).m_entry == end2.m_entry);
257
258         atf_list_fini(&l1);
259     }
260 }
261
262 /*
263  * Macros.
264  */
265
266 ATF_TC(list_for_each);
267 ATF_TC_HEAD(list_for_each, tc)
268 {
269     atf_tc_set_md_var(tc, "descr", "Checks the atf_list_for_each macro");
270 }
271 ATF_TC_BODY(list_for_each, tc)
272 {
273     atf_list_t list;
274     atf_list_iter_t iter;
275     size_t count, i, size;
276     int nums[10];
277
278     printf("Iterating over empty list\n");
279     RE(atf_list_init(&list));
280     count = 0;
281     atf_list_for_each(iter, &list) {
282         count++;
283         printf("Item count is now %zd\n", count);
284     }
285     ATF_REQUIRE_EQ(count, 0);
286     atf_list_fini(&list);
287
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++) {
292             nums[i] = i + 1;
293             RE(atf_list_append(&list, &nums[i], false));
294         }
295         count = 0;
296         atf_list_for_each(iter, &list) {
297             printf("Retrieved item: %d\n", *(int *)atf_list_iter_data(iter));
298             count++;
299         }
300         ATF_REQUIRE_EQ(count, size);
301         atf_list_fini(&list);
302     }
303 }
304
305 ATF_TC(list_for_each_c);
306 ATF_TC_HEAD(list_for_each_c, tc)
307 {
308     atf_tc_set_md_var(tc, "descr", "Checks the atf_list_for_each_c macro");
309 }
310 ATF_TC_BODY(list_for_each_c, tc)
311 {
312     atf_list_t list;
313     atf_list_citer_t iter;
314     size_t count, i, size;
315     int nums[10];
316
317     printf("Iterating over empty list\n");
318     RE(atf_list_init(&list));
319     count = 0;
320     atf_list_for_each_c(iter, &list) {
321         count++;
322         printf("Item count is now %zd\n", count);
323     }
324     ATF_REQUIRE_EQ(count, 0);
325     atf_list_fini(&list);
326
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++) {
331             nums[i] = i + 1;
332             RE(atf_list_append(&list, &nums[i], false));
333         }
334         count = 0;
335         atf_list_for_each_c(iter, &list) {
336             printf("Retrieved item: %d\n",
337                    *(const int *)atf_list_citer_data(iter));
338             count++;
339         }
340         ATF_REQUIRE_EQ(count, size);
341         atf_list_fini(&list);
342     }
343 }
344
345 /* ---------------------------------------------------------------------
346  * Main.
347  * --------------------------------------------------------------------- */
348
349 ATF_TP_ADD_TCS(tp)
350 {
351     /* Constructors and destructors. */
352     ATF_TP_ADD_TC(tp, list_init);
353
354     /* Getters. */
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);
359
360     /* Modifiers. */
361     ATF_TP_ADD_TC(tp, list_append);
362     ATF_TP_ADD_TC(tp, list_append_list);
363
364     /* Macros. */
365     ATF_TP_ADD_TC(tp, list_for_each);
366     ATF_TP_ADD_TC(tp, list_for_each_c);
367
368     return atf_no_error();
369 }