1 /* $NetBSD: text_test.c,v 1.4 2014/12/10 04:38:03 christos Exp $ */
2
3 /*
4 * Automated Testing Framework (atf)
5 *
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 *
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.
30 */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <atf-c.h>
37
38 #include "sanity.h"
39 #include "test_helpers.h"
40 #include "text.h"
41
42 /* ---------------------------------------------------------------------
43 * Auxiliary functions.
44 * --------------------------------------------------------------------- */
45
46 #define REQUIRE_ERROR(exp) \
47 do { \
48 atf_error_t err = exp; \
49 ATF_REQUIRE(atf_is_error(err)); \
50 atf_error_free(err); \
51 } while (/*CONSTCOND*/0)
52
53 static
54 size_t
array_size(const char * words[])55 array_size(const char *words[])
56 {
57 size_t count;
58 const char **word;
59
60 count = 0;
61 for (word = words; *word != NULL; word++)
62 count++;
63
64 return count;
65 }
66
67 static
68 void
check_split(const char * str,const char * delim,const char * words[])69 check_split(const char *str, const char *delim, const char *words[])
70 {
71 atf_list_t list;
72 const char **word;
73 size_t i;
74
75 printf("Splitting '%s' with delimiter '%s'\n", str, delim);
76 CE(atf_text_split(str, delim, &list));
77
78 printf("Expecting %zd words\n", array_size(words));
79 ATF_CHECK_EQ(atf_list_size(&list), array_size(words));
80
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]);
84 }
85
86 atf_list_fini(&list);
87 }
88
89 static
90 atf_error_t
word_acum(const char * word,void * data)91 word_acum(const char *word, void *data)
92 {
93 char *acum = data;
94
95 strcat(acum, word);
96
97 return atf_no_error();
98 }
99
100 static
101 atf_error_t
word_count(const char * word ATF_DEFS_ATTRIBUTE_UNUSED,void * data)102 word_count(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)
103 {
104 size_t *counter = data;
105
106 (*counter)++;
107
108 return atf_no_error();
109 }
110
111 struct fail_at {
112 int failpos;
113 int curpos;
114 };
115
116 static
117 atf_error_t
word_fail_at(const char * word ATF_DEFS_ATTRIBUTE_UNUSED,void * data)118 word_fail_at(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)
119 {
120 struct fail_at *fa = data;
121 atf_error_t err;
122
123 if (fa->failpos == fa->curpos)
124 err = atf_no_memory_error(); /* Just a random error. */
125 else {
126 fa->curpos++;
127 err = atf_no_error();
128 }
129
130 return err;
131 }
132
133 /* ---------------------------------------------------------------------
134 * Test cases for the free functions.
135 * --------------------------------------------------------------------- */
136
137 ATF_TC(for_each_word);
ATF_TC_HEAD(for_each_word,tc)138 ATF_TC_HEAD(for_each_word, tc)
139 {
140 atf_tc_set_md_var(tc, "descr", "Checks the atf_text_for_each_word"
141 "function");
142 }
ATF_TC_BODY(for_each_word,tc)143 ATF_TC_BODY(for_each_word, tc)
144 {
145 size_t cnt;
146 char acum[1024];
147
148 cnt = 0;
149 strcpy(acum, "");
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);
154
155 cnt = 0;
156 strcpy(acum, "");
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);
161
162 cnt = 0;
163 strcpy(acum, "");
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);
168
169 cnt = 0;
170 strcpy(acum, "");
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);
175
176 {
177 struct fail_at fa;
178 fa.failpos = 3;
179 fa.curpos = 0;
180 atf_error_t err = atf_text_for_each_word("a b c d e", " ",
181 word_fail_at, &fa);
182 ATF_REQUIRE(atf_is_error(err));
183 ATF_REQUIRE(atf_error_is(err, "no_memory"));
184 ATF_REQUIRE(fa.curpos == 3);
185 atf_error_free(err);
186 }
187 }
188
189 ATF_TC(format);
ATF_TC_HEAD(format,tc)190 ATF_TC_HEAD(format, tc)
191 {
192 atf_tc_set_md_var(tc, "descr", "Checks the construction of free-form "
193 "strings using a variable parameters list");
194 }
ATF_TC_BODY(format,tc)195 ATF_TC_BODY(format, tc)
196 {
197 char *str;
198 atf_error_t err;
199
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);
203 free(str);
204 }
205
206 static
207 void
format_ap(char ** dest,const char * fmt,...)208 format_ap(char **dest, const char *fmt, ...)
209 {
210 va_list ap;
211 atf_error_t err;
212
213 va_start(ap, fmt);
214 err = atf_text_format_ap(dest, fmt, ap);
215 va_end(ap);
216
217 ATF_REQUIRE(!atf_is_error(err));
218 }
219
220 ATF_TC(format_ap);
ATF_TC_HEAD(format_ap,tc)221 ATF_TC_HEAD(format_ap, tc)
222 {
223 atf_tc_set_md_var(tc, "descr", "Checks the construction of free-form "
224 "strings using a va_list argument");
225 }
ATF_TC_BODY(format_ap,tc)226 ATF_TC_BODY(format_ap, tc)
227 {
228 char *str;
229
230 format_ap(&str, "%s %s %d", "Test", "string", 1);
231 ATF_REQUIRE(strcmp(str, "Test string 1") == 0);
232 free(str);
233 }
234
235 ATF_TC(split);
ATF_TC_HEAD(split,tc)236 ATF_TC_HEAD(split, tc)
237 {
238 atf_tc_set_md_var(tc, "descr", "Checks the split function");
239 }
ATF_TC_BODY(split,tc)240 ATF_TC_BODY(split, tc)
241 {
242 {
243 const char *words[] = { NULL };
244 check_split("", " ", words);
245 }
246
247 {
248 const char *words[] = { NULL };
249 check_split(" ", " ", words);
250 }
251
252 {
253 const char *words[] = { NULL };
254 check_split(" ", " ", words);
255 }
256
257 {
258 const char *words[] = { "a", "b", NULL };
259 check_split("a b", " ", words);
260 }
261
262 {
263 const char *words[] = { "a", "b", "c", "d", NULL };
264 check_split("a b c d", " ", words);
265 }
266
267 {
268 const char *words[] = { "foo", "bar", NULL };
269 check_split("foo bar", " ", words);
270 }
271
272 {
273 const char *words[] = { "foo", "bar", "baz", "foobar", NULL };
274 check_split("foo bar baz foobar", " ", words);
275 }
276
277 {
278 const char *words[] = { "foo", "bar", NULL };
279 check_split(" foo bar", " ", words);
280 }
281
282 {
283 const char *words[] = { "foo", "bar", NULL };
284 check_split("foo bar", " ", words);
285 }
286
287 {
288 const char *words[] = { "foo", "bar", NULL };
289 check_split("foo bar ", " ", words);
290 }
291
292 {
293 const char *words[] = { "foo", "bar", NULL };
294 check_split(" foo bar ", " ", words);
295 }
296 }
297
298 ATF_TC(split_delims);
ATF_TC_HEAD(split_delims,tc)299 ATF_TC_HEAD(split_delims, tc)
300 {
301 atf_tc_set_md_var(tc, "descr", "Checks the split function using "
302 "different delimiters");
303 }
ATF_TC_BODY(split_delims,tc)304 ATF_TC_BODY(split_delims, tc)
305 {
306
307 {
308 const char *words[] = { NULL };
309 check_split("", "/", words);
310 }
311
312 {
313 const char *words[] = { " ", NULL };
314 check_split(" ", "/", words);
315 }
316
317 {
318 const char *words[] = { " ", NULL };
319 check_split(" ", "/", words);
320 }
321
322 {
323 const char *words[] = { "a", "b", NULL };
324 check_split("a/b", "/", words);
325 }
326
327 {
328 const char *words[] = { "a", "bcd", "ef", NULL };
329 check_split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM", words);
330 }
331 }
332
333 ATF_TC(to_bool);
ATF_TC_HEAD(to_bool,tc)334 ATF_TC_HEAD(to_bool, tc)
335 {
336 atf_tc_set_md_var(tc, "descr", "Checks the atf_text_to_bool function");
337 }
ATF_TC_BODY(to_bool,tc)338 ATF_TC_BODY(to_bool, tc)
339 {
340 bool b;
341
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);
346
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);
351
352 b = false;
353 REQUIRE_ERROR(atf_text_to_bool("", &b));
354 ATF_REQUIRE(!b);
355 b = true;
356 REQUIRE_ERROR(atf_text_to_bool("", &b));
357 ATF_REQUIRE(b);
358
359 b = false;
360 REQUIRE_ERROR(atf_text_to_bool("tru", &b));
361 ATF_REQUIRE(!b);
362 b = true;
363 REQUIRE_ERROR(atf_text_to_bool("tru", &b));
364 ATF_REQUIRE(b);
365
366 b = false;
367 REQUIRE_ERROR(atf_text_to_bool("true2", &b));
368 ATF_REQUIRE(!b);
369 b = true;
370 REQUIRE_ERROR(atf_text_to_bool("true2", &b));
371 ATF_REQUIRE(b);
372
373 b = false;
374 REQUIRE_ERROR(atf_text_to_bool("fals", &b));
375 ATF_REQUIRE(!b);
376 b = true;
377 REQUIRE_ERROR(atf_text_to_bool("fals", &b));
378 ATF_REQUIRE(b);
379
380 b = false;
381 REQUIRE_ERROR(atf_text_to_bool("false2", &b));
382 ATF_REQUIRE(!b);
383 b = true;
384 REQUIRE_ERROR(atf_text_to_bool("false2", &b));
385 ATF_REQUIRE(b);
386 }
387
388 ATF_TC(to_long);
ATF_TC_HEAD(to_long,tc)389 ATF_TC_HEAD(to_long, tc)
390 {
391 atf_tc_set_md_var(tc, "descr", "Checks the atf_text_to_long function");
392 }
ATF_TC_BODY(to_long,tc)393 ATF_TC_BODY(to_long, tc)
394 {
395 long l;
396
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);
401
402 l = 1212;
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);
409 }
410
411 /* ---------------------------------------------------------------------
412 * Main.
413 * --------------------------------------------------------------------- */
414
ATF_TP_ADD_TCS(tp)415 ATF_TP_ADD_TCS(tp)
416 {
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);
424
425 return atf_no_error();
426 }
427