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.
37 #include "test_helpers.h"
40 /* ---------------------------------------------------------------------
41 * Auxiliary functions.
42 * --------------------------------------------------------------------- */
44 #define REQUIRE_ERROR(exp) \
46 atf_error_t err = exp; \
47 ATF_REQUIRE(atf_is_error(err)); \
48 atf_error_free(err); \
53 array_size(const char *words[])
59 for (word = words; *word != NULL; word++)
67 check_split(const char *str, const char *delim, const char *words[])
73 printf("Splitting '%s' with delimiter '%s'\n", str, delim);
74 CE(atf_text_split(str, delim, &list));
76 printf("Expecting %zd words\n", array_size(words));
77 ATF_CHECK_EQ(atf_list_size(&list), array_size(words));
79 for (word = words, i = 0; *word != NULL; word++, i++) {
80 printf("Word at position %zd should be '%s'\n", i, words[i]);
81 ATF_CHECK_STREQ((const char *)atf_list_index_c(&list, i), words[i]);
89 word_acum(const char *word, void *data)
95 return atf_no_error();
100 word_count(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)
102 size_t *counter = data;
106 return atf_no_error();
116 word_fail_at(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)
118 struct fail_at *fa = data;
121 if (fa->failpos == fa->curpos)
122 err = atf_no_memory_error(); /* Just a random error. */
125 err = atf_no_error();
131 /* ---------------------------------------------------------------------
132 * Test cases for the free functions.
133 * --------------------------------------------------------------------- */
135 ATF_TC(for_each_word);
136 ATF_TC_HEAD(for_each_word, tc)
138 atf_tc_set_md_var(tc, "descr", "Checks the atf_text_for_each_word"
141 ATF_TC_BODY(for_each_word, tc)
148 RE(atf_text_for_each_word("1 2 3", " ", word_count, &cnt));
149 RE(atf_text_for_each_word("1 2 3", " ", word_acum, acum));
150 ATF_REQUIRE(cnt == 3);
151 ATF_REQUIRE(strcmp(acum, "123") == 0);
155 RE(atf_text_for_each_word("1 2 3", ".", word_count, &cnt));
156 RE(atf_text_for_each_word("1 2 3", ".", word_acum, acum));
157 ATF_REQUIRE(cnt == 1);
158 ATF_REQUIRE(strcmp(acum, "1 2 3") == 0);
162 RE(atf_text_for_each_word("1 2 3 4 5", " ", word_count, &cnt));
163 RE(atf_text_for_each_word("1 2 3 4 5", " ", word_acum, acum));
164 ATF_REQUIRE(cnt == 5);
165 ATF_REQUIRE(strcmp(acum, "12345") == 0);
169 RE(atf_text_for_each_word("1 2.3.4 5", " .", word_count, &cnt));
170 RE(atf_text_for_each_word("1 2.3.4 5", " .", word_acum, acum));
171 ATF_REQUIRE(cnt == 5);
172 ATF_REQUIRE(strcmp(acum, "12345") == 0);
178 atf_error_t err = atf_text_for_each_word("a b c d e", " ",
180 ATF_REQUIRE(atf_is_error(err));
181 ATF_REQUIRE(atf_error_is(err, "no_memory"));
182 ATF_REQUIRE(fa.curpos == 3);
188 ATF_TC_HEAD(format, tc)
190 atf_tc_set_md_var(tc, "descr", "Checks the construction of free-form "
191 "strings using a variable parameters list");
193 ATF_TC_BODY(format, tc)
198 err = atf_text_format(&str, "%s %s %d", "Test", "string", 1);
199 ATF_REQUIRE(!atf_is_error(err));
200 ATF_REQUIRE(strcmp(str, "Test string 1") == 0);
206 format_ap(char **dest, const char *fmt, ...)
212 err = atf_text_format_ap(dest, fmt, ap);
215 ATF_REQUIRE(!atf_is_error(err));
219 ATF_TC_HEAD(format_ap, tc)
221 atf_tc_set_md_var(tc, "descr", "Checks the construction of free-form "
222 "strings using a va_list argument");
224 ATF_TC_BODY(format_ap, tc)
228 format_ap(&str, "%s %s %d", "Test", "string", 1);
229 ATF_REQUIRE(strcmp(str, "Test string 1") == 0);
234 ATF_TC_HEAD(split, tc)
236 atf_tc_set_md_var(tc, "descr", "Checks the split function");
238 ATF_TC_BODY(split, tc)
241 const char *words[] = { NULL };
242 check_split("", " ", words);
246 const char *words[] = { NULL };
247 check_split(" ", " ", words);
251 const char *words[] = { NULL };
252 check_split(" ", " ", words);
256 const char *words[] = { "a", "b", NULL };
257 check_split("a b", " ", words);
261 const char *words[] = { "a", "b", "c", "d", NULL };
262 check_split("a b c d", " ", words);
266 const char *words[] = { "foo", "bar", NULL };
267 check_split("foo bar", " ", words);
271 const char *words[] = { "foo", "bar", "baz", "foobar", NULL };
272 check_split("foo bar baz foobar", " ", words);
276 const char *words[] = { "foo", "bar", NULL };
277 check_split(" foo bar", " ", words);
281 const char *words[] = { "foo", "bar", NULL };
282 check_split("foo bar", " ", words);
286 const char *words[] = { "foo", "bar", NULL };
287 check_split("foo bar ", " ", words);
291 const char *words[] = { "foo", "bar", NULL };
292 check_split(" foo bar ", " ", words);
296 ATF_TC(split_delims);
297 ATF_TC_HEAD(split_delims, tc)
299 atf_tc_set_md_var(tc, "descr", "Checks the split function using "
300 "different delimiters");
302 ATF_TC_BODY(split_delims, tc)
306 const char *words[] = { NULL };
307 check_split("", "/", words);
311 const char *words[] = { " ", NULL };
312 check_split(" ", "/", words);
316 const char *words[] = { " ", NULL };
317 check_split(" ", "/", words);
321 const char *words[] = { "a", "b", NULL };
322 check_split("a/b", "/", words);
326 const char *words[] = { "a", "bcd", "ef", NULL };
327 check_split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM", words);
332 ATF_TC_HEAD(to_bool, tc)
334 atf_tc_set_md_var(tc, "descr", "Checks the atf_text_to_bool function");
336 ATF_TC_BODY(to_bool, tc)
340 RE(atf_text_to_bool("true", &b)); ATF_REQUIRE(b);
341 RE(atf_text_to_bool("TRUE", &b)); ATF_REQUIRE(b);
342 RE(atf_text_to_bool("yes", &b)); ATF_REQUIRE(b);
343 RE(atf_text_to_bool("YES", &b)); ATF_REQUIRE(b);
345 RE(atf_text_to_bool("false", &b)); ATF_REQUIRE(!b);
346 RE(atf_text_to_bool("FALSE", &b)); ATF_REQUIRE(!b);
347 RE(atf_text_to_bool("no", &b)); ATF_REQUIRE(!b);
348 RE(atf_text_to_bool("NO", &b)); ATF_REQUIRE(!b);
351 REQUIRE_ERROR(atf_text_to_bool("", &b));
354 REQUIRE_ERROR(atf_text_to_bool("", &b));
358 REQUIRE_ERROR(atf_text_to_bool("tru", &b));
361 REQUIRE_ERROR(atf_text_to_bool("tru", &b));
365 REQUIRE_ERROR(atf_text_to_bool("true2", &b));
368 REQUIRE_ERROR(atf_text_to_bool("true2", &b));
372 REQUIRE_ERROR(atf_text_to_bool("fals", &b));
375 REQUIRE_ERROR(atf_text_to_bool("fals", &b));
379 REQUIRE_ERROR(atf_text_to_bool("false2", &b));
382 REQUIRE_ERROR(atf_text_to_bool("false2", &b));
387 ATF_TC_HEAD(to_long, tc)
389 atf_tc_set_md_var(tc, "descr", "Checks the atf_text_to_long function");
391 ATF_TC_BODY(to_long, tc)
395 RE(atf_text_to_long("0", &l)); ATF_REQUIRE_EQ(l, 0);
396 RE(atf_text_to_long("-5", &l)); ATF_REQUIRE_EQ(l, -5);
397 RE(atf_text_to_long("5", &l)); ATF_REQUIRE_EQ(l, 5);
398 RE(atf_text_to_long("123456789", &l)); ATF_REQUIRE_EQ(l, 123456789);
401 REQUIRE_ERROR(atf_text_to_long("", &l));
402 ATF_REQUIRE_EQ(l, 1212);
403 REQUIRE_ERROR(atf_text_to_long("foo", &l));
404 ATF_REQUIRE_EQ(l, 1212);
405 REQUIRE_ERROR(atf_text_to_long("1234x", &l));
406 ATF_REQUIRE_EQ(l, 1212);
409 /* ---------------------------------------------------------------------
411 * --------------------------------------------------------------------- */
415 ATF_TP_ADD_TC(tp, for_each_word);
416 ATF_TP_ADD_TC(tp, format);
417 ATF_TP_ADD_TC(tp, format_ap);
418 ATF_TP_ADD_TC(tp, split);
419 ATF_TP_ADD_TC(tp, split_delims);
420 ATF_TP_ADD_TC(tp, to_bool);
421 ATF_TP_ADD_TC(tp, to_long);
423 return atf_no_error();