1 /* $NetBSD: text_test.c,v 1.4 2014/12/10 04:38:03 christos Exp $ */
4 * Automated Testing Framework (atf)
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
19 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include "test_helpers.h"
42 /* ---------------------------------------------------------------------
43 * Auxiliary functions.
44 * --------------------------------------------------------------------- */
46 #define REQUIRE_ERROR(exp) \
48 atf_error_t err = exp; \
49 ATF_REQUIRE(atf_is_error(err)); \
50 atf_error_free(err); \
51 } while (/*CONSTCOND*/0)
55 array_size(const char *words
[])
61 for (word
= words
; *word
!= NULL
; word
++)
69 check_split(const char *str
, const char *delim
, const char *words
[])
75 printf("Splitting '%s' with delimiter '%s'\n", str
, delim
);
76 CE(atf_text_split(str
, delim
, &list
));
78 printf("Expecting %zd words\n", array_size(words
));
79 ATF_CHECK_EQ(atf_list_size(&list
), array_size(words
));
81 for (word
= words
, i
= 0; *word
!= NULL
; word
++, i
++) {
82 printf("Word at position %zd should be '%s'\n", i
, words
[i
]);
83 ATF_CHECK_STREQ((const char *)atf_list_index_c(&list
, i
), words
[i
]);
91 word_acum(const char *word
, void *data
)
97 return atf_no_error();
102 word_count(const char *word ATF_DEFS_ATTRIBUTE_UNUSED
, void *data
)
104 size_t *counter
= data
;
108 return atf_no_error();
118 word_fail_at(const char *word ATF_DEFS_ATTRIBUTE_UNUSED
, void *data
)
120 struct fail_at
*fa
= data
;
123 if (fa
->failpos
== fa
->curpos
)
124 err
= atf_no_memory_error(); /* Just a random error. */
127 err
= atf_no_error();
133 /* ---------------------------------------------------------------------
134 * Test cases for the free functions.
135 * --------------------------------------------------------------------- */
137 ATF_TC(for_each_word
);
138 ATF_TC_HEAD(for_each_word
, tc
)
140 atf_tc_set_md_var(tc
, "descr", "Checks the atf_text_for_each_word"
143 ATF_TC_BODY(for_each_word
, tc
)
150 RE(atf_text_for_each_word("1 2 3", " ", word_count
, &cnt
));
151 RE(atf_text_for_each_word("1 2 3", " ", word_acum
, acum
));
152 ATF_REQUIRE(cnt
== 3);
153 ATF_REQUIRE(strcmp(acum
, "123") == 0);
157 RE(atf_text_for_each_word("1 2 3", ".", word_count
, &cnt
));
158 RE(atf_text_for_each_word("1 2 3", ".", word_acum
, acum
));
159 ATF_REQUIRE(cnt
== 1);
160 ATF_REQUIRE(strcmp(acum
, "1 2 3") == 0);
164 RE(atf_text_for_each_word("1 2 3 4 5", " ", word_count
, &cnt
));
165 RE(atf_text_for_each_word("1 2 3 4 5", " ", word_acum
, acum
));
166 ATF_REQUIRE(cnt
== 5);
167 ATF_REQUIRE(strcmp(acum
, "12345") == 0);
171 RE(atf_text_for_each_word("1 2.3.4 5", " .", word_count
, &cnt
));
172 RE(atf_text_for_each_word("1 2.3.4 5", " .", word_acum
, acum
));
173 ATF_REQUIRE(cnt
== 5);
174 ATF_REQUIRE(strcmp(acum
, "12345") == 0);
180 atf_error_t err
= atf_text_for_each_word("a b c d e", " ",
182 ATF_REQUIRE(atf_is_error(err
));
183 ATF_REQUIRE(atf_error_is(err
, "no_memory"));
184 ATF_REQUIRE(fa
.curpos
== 3);
190 ATF_TC_HEAD(format
, tc
)
192 atf_tc_set_md_var(tc
, "descr", "Checks the construction of free-form "
193 "strings using a variable parameters list");
195 ATF_TC_BODY(format
, tc
)
200 err
= atf_text_format(&str
, "%s %s %d", "Test", "string", 1);
201 ATF_REQUIRE(!atf_is_error(err
));
202 ATF_REQUIRE(strcmp(str
, "Test string 1") == 0);
208 format_ap(char **dest
, const char *fmt
, ...)
214 err
= atf_text_format_ap(dest
, fmt
, ap
);
217 ATF_REQUIRE(!atf_is_error(err
));
221 ATF_TC_HEAD(format_ap
, tc
)
223 atf_tc_set_md_var(tc
, "descr", "Checks the construction of free-form "
224 "strings using a va_list argument");
226 ATF_TC_BODY(format_ap
, tc
)
230 format_ap(&str
, "%s %s %d", "Test", "string", 1);
231 ATF_REQUIRE(strcmp(str
, "Test string 1") == 0);
236 ATF_TC_HEAD(split
, tc
)
238 atf_tc_set_md_var(tc
, "descr", "Checks the split function");
240 ATF_TC_BODY(split
, tc
)
243 const char *words
[] = { NULL
};
244 check_split("", " ", words
);
248 const char *words
[] = { NULL
};
249 check_split(" ", " ", words
);
253 const char *words
[] = { NULL
};
254 check_split(" ", " ", words
);
258 const char *words
[] = { "a", "b", NULL
};
259 check_split("a b", " ", words
);
263 const char *words
[] = { "a", "b", "c", "d", NULL
};
264 check_split("a b c d", " ", words
);
268 const char *words
[] = { "foo", "bar", NULL
};
269 check_split("foo bar", " ", words
);
273 const char *words
[] = { "foo", "bar", "baz", "foobar", NULL
};
274 check_split("foo bar baz foobar", " ", 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 const char *words
[] = { "foo", "bar", NULL
};
294 check_split(" foo bar ", " ", words
);
298 ATF_TC(split_delims
);
299 ATF_TC_HEAD(split_delims
, tc
)
301 atf_tc_set_md_var(tc
, "descr", "Checks the split function using "
302 "different delimiters");
304 ATF_TC_BODY(split_delims
, tc
)
308 const char *words
[] = { NULL
};
309 check_split("", "/", words
);
313 const char *words
[] = { " ", NULL
};
314 check_split(" ", "/", words
);
318 const char *words
[] = { " ", NULL
};
319 check_split(" ", "/", words
);
323 const char *words
[] = { "a", "b", NULL
};
324 check_split("a/b", "/", words
);
328 const char *words
[] = { "a", "bcd", "ef", NULL
};
329 check_split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM", words
);
334 ATF_TC_HEAD(to_bool
, tc
)
336 atf_tc_set_md_var(tc
, "descr", "Checks the atf_text_to_bool function");
338 ATF_TC_BODY(to_bool
, tc
)
342 RE(atf_text_to_bool("true", &b
)); ATF_REQUIRE(b
);
343 RE(atf_text_to_bool("TRUE", &b
)); ATF_REQUIRE(b
);
344 RE(atf_text_to_bool("yes", &b
)); ATF_REQUIRE(b
);
345 RE(atf_text_to_bool("YES", &b
)); ATF_REQUIRE(b
);
347 RE(atf_text_to_bool("false", &b
)); ATF_REQUIRE(!b
);
348 RE(atf_text_to_bool("FALSE", &b
)); ATF_REQUIRE(!b
);
349 RE(atf_text_to_bool("no", &b
)); ATF_REQUIRE(!b
);
350 RE(atf_text_to_bool("NO", &b
)); ATF_REQUIRE(!b
);
353 REQUIRE_ERROR(atf_text_to_bool("", &b
));
356 REQUIRE_ERROR(atf_text_to_bool("", &b
));
360 REQUIRE_ERROR(atf_text_to_bool("tru", &b
));
363 REQUIRE_ERROR(atf_text_to_bool("tru", &b
));
367 REQUIRE_ERROR(atf_text_to_bool("true2", &b
));
370 REQUIRE_ERROR(atf_text_to_bool("true2", &b
));
374 REQUIRE_ERROR(atf_text_to_bool("fals", &b
));
377 REQUIRE_ERROR(atf_text_to_bool("fals", &b
));
381 REQUIRE_ERROR(atf_text_to_bool("false2", &b
));
384 REQUIRE_ERROR(atf_text_to_bool("false2", &b
));
389 ATF_TC_HEAD(to_long
, tc
)
391 atf_tc_set_md_var(tc
, "descr", "Checks the atf_text_to_long function");
393 ATF_TC_BODY(to_long
, tc
)
397 RE(atf_text_to_long("0", &l
)); ATF_REQUIRE_EQ(l
, 0);
398 RE(atf_text_to_long("-5", &l
)); ATF_REQUIRE_EQ(l
, -5);
399 RE(atf_text_to_long("5", &l
)); ATF_REQUIRE_EQ(l
, 5);
400 RE(atf_text_to_long("123456789", &l
)); ATF_REQUIRE_EQ(l
, 123456789);
403 REQUIRE_ERROR(atf_text_to_long("", &l
));
404 ATF_REQUIRE_EQ(l
, 1212);
405 REQUIRE_ERROR(atf_text_to_long("foo", &l
));
406 ATF_REQUIRE_EQ(l
, 1212);
407 REQUIRE_ERROR(atf_text_to_long("1234x", &l
));
408 ATF_REQUIRE_EQ(l
, 1212);
411 /* ---------------------------------------------------------------------
413 * --------------------------------------------------------------------- */
417 ATF_TP_ADD_TC(tp
, for_each_word
);
418 ATF_TP_ADD_TC(tp
, format
);
419 ATF_TP_ADD_TC(tp
, format_ap
);
420 ATF_TP_ADD_TC(tp
, split
);
421 ATF_TP_ADD_TC(tp
, split_delims
);
422 ATF_TP_ADD_TC(tp
, to_bool
);
423 ATF_TP_ADD_TC(tp
, to_long
);
425 return atf_no_error();