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/text.h"
34 #include "atf-c/detail/sanity.h"
35 #include "atf-c/detail/test_helpers.h"
37 /* ---------------------------------------------------------------------
38 * Auxiliary functions.
39 * --------------------------------------------------------------------- */
41 #define REQUIRE_ERROR(exp) \
43 atf_error_t err = exp; \
44 ATF_REQUIRE(atf_is_error(err)); \
45 atf_error_free(err); \
50 array_size(const char *words[])
56 for (word = words; *word != NULL; word++)
64 check_split(const char *str, const char *delim, const char *words[])
70 printf("Splitting '%s' with delimiter '%s'\n", str, delim);
71 CE(atf_text_split(str, delim, &list));
73 printf("Expecting %zd words\n", array_size(words));
74 ATF_CHECK_EQ(atf_list_size(&list), array_size(words));
76 for (word = words, i = 0; *word != NULL; word++, i++) {
77 printf("Word at position %zd should be '%s'\n", i, words[i]);
78 ATF_CHECK_STREQ((const char *)atf_list_index_c(&list, i), words[i]);
86 word_acum(const char *word, void *data)
92 return atf_no_error();
97 word_count(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)
99 size_t *counter = data;
103 return atf_no_error();
113 word_fail_at(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)
115 struct fail_at *fa = data;
118 if (fa->failpos == fa->curpos)
119 err = atf_no_memory_error(); /* Just a random error. */
122 err = atf_no_error();
128 /* ---------------------------------------------------------------------
129 * Test cases for the free functions.
130 * --------------------------------------------------------------------- */
132 ATF_TC(for_each_word);
133 ATF_TC_HEAD(for_each_word, tc)
135 atf_tc_set_md_var(tc, "descr", "Checks the atf_text_for_each_word"
138 ATF_TC_BODY(for_each_word, tc)
145 RE(atf_text_for_each_word("1 2 3", " ", word_count, &cnt));
146 RE(atf_text_for_each_word("1 2 3", " ", word_acum, acum));
147 ATF_REQUIRE(cnt == 3);
148 ATF_REQUIRE(strcmp(acum, "123") == 0);
152 RE(atf_text_for_each_word("1 2 3", ".", word_count, &cnt));
153 RE(atf_text_for_each_word("1 2 3", ".", word_acum, acum));
154 ATF_REQUIRE(cnt == 1);
155 ATF_REQUIRE(strcmp(acum, "1 2 3") == 0);
159 RE(atf_text_for_each_word("1 2 3 4 5", " ", word_count, &cnt));
160 RE(atf_text_for_each_word("1 2 3 4 5", " ", word_acum, acum));
161 ATF_REQUIRE(cnt == 5);
162 ATF_REQUIRE(strcmp(acum, "12345") == 0);
166 RE(atf_text_for_each_word("1 2.3.4 5", " .", word_count, &cnt));
167 RE(atf_text_for_each_word("1 2.3.4 5", " .", word_acum, acum));
168 ATF_REQUIRE(cnt == 5);
169 ATF_REQUIRE(strcmp(acum, "12345") == 0);
175 atf_error_t err = atf_text_for_each_word("a b c d e", " ",
177 ATF_REQUIRE(atf_is_error(err));
178 ATF_REQUIRE(atf_error_is(err, "no_memory"));
179 ATF_REQUIRE(fa.curpos == 3);
185 ATF_TC_HEAD(format, tc)
187 atf_tc_set_md_var(tc, "descr", "Checks the construction of free-form "
188 "strings using a variable parameters list");
190 ATF_TC_BODY(format, tc)
195 err = atf_text_format(&str, "%s %s %d", "Test", "string", 1);
196 ATF_REQUIRE(!atf_is_error(err));
197 ATF_REQUIRE(strcmp(str, "Test string 1") == 0);
203 format_ap(char **dest, const char *fmt, ...)
209 err = atf_text_format_ap(dest, fmt, ap);
212 ATF_REQUIRE(!atf_is_error(err));
216 ATF_TC_HEAD(format_ap, tc)
218 atf_tc_set_md_var(tc, "descr", "Checks the construction of free-form "
219 "strings using a va_list argument");
221 ATF_TC_BODY(format_ap, tc)
225 format_ap(&str, "%s %s %d", "Test", "string", 1);
226 ATF_REQUIRE(strcmp(str, "Test string 1") == 0);
231 ATF_TC_HEAD(split, tc)
233 atf_tc_set_md_var(tc, "descr", "Checks the split function");
235 ATF_TC_BODY(split, tc)
238 const char *words[] = { NULL };
239 check_split("", " ", words);
243 const char *words[] = { NULL };
244 check_split(" ", " ", words);
248 const char *words[] = { NULL };
249 check_split(" ", " ", words);
253 const char *words[] = { "a", "b", NULL };
254 check_split("a b", " ", words);
258 const char *words[] = { "a", "b", "c", "d", NULL };
259 check_split("a b c d", " ", words);
263 const char *words[] = { "foo", "bar", NULL };
264 check_split("foo bar", " ", words);
268 const char *words[] = { "foo", "bar", "baz", "foobar", NULL };
269 check_split("foo bar baz foobar", " ", words);
273 const char *words[] = { "foo", "bar", NULL };
274 check_split(" foo bar", " ", words);
278 const char *words[] = { "foo", "bar", NULL };
279 check_split("foo bar", " ", words);
283 const char *words[] = { "foo", "bar", NULL };
284 check_split("foo bar ", " ", words);
288 const char *words[] = { "foo", "bar", NULL };
289 check_split(" foo bar ", " ", words);
293 ATF_TC(split_delims);
294 ATF_TC_HEAD(split_delims, tc)
296 atf_tc_set_md_var(tc, "descr", "Checks the split function using "
297 "different delimiters");
299 ATF_TC_BODY(split_delims, tc)
303 const char *words[] = { NULL };
304 check_split("", "/", words);
308 const char *words[] = { " ", NULL };
309 check_split(" ", "/", words);
313 const char *words[] = { " ", NULL };
314 check_split(" ", "/", words);
318 const char *words[] = { "a", "b", NULL };
319 check_split("a/b", "/", words);
323 const char *words[] = { "a", "bcd", "ef", NULL };
324 check_split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM", words);
329 ATF_TC_HEAD(to_bool, tc)
331 atf_tc_set_md_var(tc, "descr", "Checks the atf_text_to_bool function");
333 ATF_TC_BODY(to_bool, tc)
337 RE(atf_text_to_bool("true", &b)); ATF_REQUIRE(b);
338 RE(atf_text_to_bool("TRUE", &b)); ATF_REQUIRE(b);
339 RE(atf_text_to_bool("yes", &b)); ATF_REQUIRE(b);
340 RE(atf_text_to_bool("YES", &b)); ATF_REQUIRE(b);
342 RE(atf_text_to_bool("false", &b)); ATF_REQUIRE(!b);
343 RE(atf_text_to_bool("FALSE", &b)); ATF_REQUIRE(!b);
344 RE(atf_text_to_bool("no", &b)); ATF_REQUIRE(!b);
345 RE(atf_text_to_bool("NO", &b)); ATF_REQUIRE(!b);
348 REQUIRE_ERROR(atf_text_to_bool("", &b));
351 REQUIRE_ERROR(atf_text_to_bool("", &b));
355 REQUIRE_ERROR(atf_text_to_bool("tru", &b));
358 REQUIRE_ERROR(atf_text_to_bool("tru", &b));
362 REQUIRE_ERROR(atf_text_to_bool("true2", &b));
365 REQUIRE_ERROR(atf_text_to_bool("true2", &b));
369 REQUIRE_ERROR(atf_text_to_bool("fals", &b));
372 REQUIRE_ERROR(atf_text_to_bool("fals", &b));
376 REQUIRE_ERROR(atf_text_to_bool("false2", &b));
379 REQUIRE_ERROR(atf_text_to_bool("false2", &b));
384 ATF_TC_HEAD(to_long, tc)
386 atf_tc_set_md_var(tc, "descr", "Checks the atf_text_to_long function");
388 ATF_TC_BODY(to_long, tc)
392 RE(atf_text_to_long("0", &l)); ATF_REQUIRE_EQ(l, 0);
393 RE(atf_text_to_long("-5", &l)); ATF_REQUIRE_EQ(l, -5);
394 RE(atf_text_to_long("5", &l)); ATF_REQUIRE_EQ(l, 5);
395 RE(atf_text_to_long("123456789", &l)); ATF_REQUIRE_EQ(l, 123456789);
398 REQUIRE_ERROR(atf_text_to_long("", &l));
399 ATF_REQUIRE_EQ(l, 1212);
400 REQUIRE_ERROR(atf_text_to_long("foo", &l));
401 ATF_REQUIRE_EQ(l, 1212);
402 REQUIRE_ERROR(atf_text_to_long("1234x", &l));
403 ATF_REQUIRE_EQ(l, 1212);
406 /* ---------------------------------------------------------------------
408 * --------------------------------------------------------------------- */
412 ATF_TP_ADD_TC(tp, for_each_word);
413 ATF_TP_ADD_TC(tp, format);
414 ATF_TP_ADD_TC(tp, format_ap);
415 ATF_TP_ADD_TC(tp, split);
416 ATF_TP_ADD_TC(tp, split_delims);
417 ATF_TP_ADD_TC(tp, to_bool);
418 ATF_TP_ADD_TC(tp, to_long);
420 return atf_no_error();