Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / unit / atf-src / atf-c / detail / text_test.c
blobd96101c087f569aa6684f3cdac5626b6ecdaef87
1 /* $NetBSD: text_test.c,v 1.4 2014/12/10 04:38:03 christos Exp $ */
3 /*
4 * Automated Testing Framework (atf)
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * All rights reserved.
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.
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.
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include <atf-c.h>
38 #include "sanity.h"
39 #include "test_helpers.h"
40 #include "text.h"
42 /* ---------------------------------------------------------------------
43 * Auxiliary functions.
44 * --------------------------------------------------------------------- */
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)
53 static
54 size_t
55 array_size(const char *words[])
57 size_t count;
58 const char **word;
60 count = 0;
61 for (word = words; *word != NULL; word++)
62 count++;
64 return count;
67 static
68 void
69 check_split(const char *str, const char *delim, const char *words[])
71 atf_list_t list;
72 const char **word;
73 size_t i;
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]);
86 atf_list_fini(&list);
89 static
90 atf_error_t
91 word_acum(const char *word, void *data)
93 char *acum = data;
95 strcat(acum, word);
97 return atf_no_error();
100 static
101 atf_error_t
102 word_count(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)
104 size_t *counter = data;
106 (*counter)++;
108 return atf_no_error();
111 struct fail_at {
112 int failpos;
113 int curpos;
116 static
117 atf_error_t
118 word_fail_at(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)
120 struct fail_at *fa = data;
121 atf_error_t err;
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();
130 return err;
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"
141 "function");
143 ATF_TC_BODY(for_each_word, tc)
145 size_t cnt;
146 char acum[1024];
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);
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);
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);
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);
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);
189 ATF_TC(format);
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)
197 char *str;
198 atf_error_t err;
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);
206 static
207 void
208 format_ap(char **dest, const char *fmt, ...)
210 va_list ap;
211 atf_error_t err;
213 va_start(ap, fmt);
214 err = atf_text_format_ap(dest, fmt, ap);
215 va_end(ap);
217 ATF_REQUIRE(!atf_is_error(err));
220 ATF_TC(format_ap);
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)
228 char *str;
230 format_ap(&str, "%s %s %d", "Test", "string", 1);
231 ATF_REQUIRE(strcmp(str, "Test string 1") == 0);
232 free(str);
235 ATF_TC(split);
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);
333 ATF_TC(to_bool);
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)
340 bool b;
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);
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);
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);
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);
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);
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);
388 ATF_TC(to_long);
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)
395 long l;
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);
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);
411 /* ---------------------------------------------------------------------
412 * Main.
413 * --------------------------------------------------------------------- */
415 ATF_TP_ADD_TCS(tp)
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();