Remove building with NOCRYPTO option
[minix3.git] / external / bsd / atf / dist / atf-c / detail / dynstr_test.c
blob1b68d38b7722fa64065f10a409042153bb8e7575
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
30 #include <stdarg.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include <atf-c.h>
38 #include "dynstr.h"
39 #include "test_helpers.h"
41 /* ---------------------------------------------------------------------
42 * Tests for the "atf_dynstr" type.
43 * --------------------------------------------------------------------- */
46 * Constructors and destructors.
49 ATF_TC(init);
50 ATF_TC_HEAD(init, tc)
52 atf_tc_set_md_var(tc, "descr", "Checks the empty constructor");
54 ATF_TC_BODY(init, tc)
56 atf_dynstr_t str;
58 RE(atf_dynstr_init(&str));
59 ATF_REQUIRE_EQ(atf_dynstr_length(&str), 0);
60 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "") == 0);
61 atf_dynstr_fini(&str);
64 static
65 void
66 init_fmt(atf_dynstr_t *str, const char *fmt, ...)
68 va_list ap;
70 va_start(ap, fmt);
71 RE(atf_dynstr_init_ap(str, fmt, ap));
72 va_end(ap);
75 ATF_TC(init_ap);
76 ATF_TC_HEAD(init_ap, tc)
78 atf_tc_set_md_var(tc, "descr", "Checks the formatted constructor using "
79 "a va_list argument");
81 ATF_TC_BODY(init_ap, tc)
83 atf_dynstr_t str;
85 init_fmt(&str, "String 1");
86 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 1") == 0);
87 atf_dynstr_fini(&str);
89 init_fmt(&str, "String %d", 2);
90 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 2") == 0);
91 atf_dynstr_fini(&str);
93 init_fmt(&str, "%s %d", "String", 3);
94 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 3") == 0);
95 atf_dynstr_fini(&str);
97 init_fmt(&str, "%s%s%s%s%s%s%s", "This ", "should ", "be ", "a ",
98 "large ", "string ", "aaaabbbbccccdddd");
99 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str),
100 "This should be a large string "
101 "aaaabbbbccccdddd") == 0);
102 atf_dynstr_fini(&str);
105 ATF_TC(init_fmt);
106 ATF_TC_HEAD(init_fmt, tc)
108 atf_tc_set_md_var(tc, "descr", "Checks the formatted constructor using "
109 "a variable list of parameters");
111 ATF_TC_BODY(init_fmt, tc)
113 atf_dynstr_t str;
115 RE(atf_dynstr_init_fmt(&str, "String 1"));
116 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 1") == 0);
117 atf_dynstr_fini(&str);
119 RE(atf_dynstr_init_fmt(&str, "String %d", 2));
120 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 2") == 0);
121 atf_dynstr_fini(&str);
123 RE(atf_dynstr_init_fmt(&str, "%s %d", "String", 3));
124 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 3") == 0);
125 atf_dynstr_fini(&str);
127 RE(atf_dynstr_init_fmt(&str, "%s%s%s%s%s%s%s", "This ", "should ",
128 "be ", "a ", "large ", "string ",
129 "aaaabbbbccccdddd"));
130 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str),
131 "This should be a large string "
132 "aaaabbbbccccdddd") == 0);
133 atf_dynstr_fini(&str);
136 ATF_TC(init_raw);
137 ATF_TC_HEAD(init_raw, tc)
139 atf_tc_set_md_var(tc, "descr", "Checks the construction of a string "
140 "using a raw memory pointer");
142 ATF_TC_BODY(init_raw, tc)
144 const char *src = "String 1, String 2";
145 atf_dynstr_t str;
147 RE(atf_dynstr_init_raw(&str, src, 0));
148 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "") == 0);
149 atf_dynstr_fini(&str);
151 RE(atf_dynstr_init_raw(&str, src, 8));
152 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 1") == 0);
153 atf_dynstr_fini(&str);
155 RE(atf_dynstr_init_raw(&str, src + 10, 8));
156 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 2") == 0);
157 atf_dynstr_fini(&str);
159 RE(atf_dynstr_init_raw(&str, "String\0Lost", 11));
160 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String") == 0);
161 atf_dynstr_fini(&str);
164 atf_error_t err = atf_dynstr_init_raw(&str, "NULL", SIZE_MAX - 1);
165 ATF_REQUIRE(atf_is_error(err));
166 ATF_REQUIRE(atf_error_is(err, "no_memory"));
167 atf_error_free(err);
171 ATF_TC(init_rep);
172 ATF_TC_HEAD(init_rep, tc)
174 atf_tc_set_md_var(tc, "descr", "Checks the construction of a string by "
175 "repeating characters");
177 ATF_TC_BODY(init_rep, tc)
179 const size_t maxlen = 8192;
180 char buf[maxlen + 1];
181 size_t i;
183 buf[0] = '\0';
185 for (i = 0; i < maxlen; i++) {
186 atf_dynstr_t str;
188 RE(atf_dynstr_init_rep(&str, i, 'a'));
190 if (strcmp(atf_dynstr_cstring(&str), buf) != 0) {
191 fprintf(stderr, "Failed at iteration %zd\n", i);
192 atf_tc_fail("Failed to construct dynstr by repeating %zd "
193 "times the '%c' character", i, 'a');
196 atf_dynstr_fini(&str);
198 strcat(buf, "a");
202 atf_dynstr_t str;
203 atf_error_t err;
205 err = atf_dynstr_init_rep(&str, SIZE_MAX, 'a');
206 ATF_REQUIRE(atf_is_error(err));
207 ATF_REQUIRE(atf_error_is(err, "no_memory"));
208 atf_error_free(err);
210 err = atf_dynstr_init_rep(&str, SIZE_MAX - 1, 'a');
211 ATF_REQUIRE(atf_is_error(err));
212 ATF_REQUIRE(atf_error_is(err, "no_memory"));
213 atf_error_free(err);
217 ATF_TC(init_substr);
218 ATF_TC_HEAD(init_substr, tc)
220 atf_tc_set_md_var(tc, "descr", "Checks the construction of a string "
221 "using a substring of another one");
223 ATF_TC_BODY(init_substr, tc)
225 atf_dynstr_t src;
226 atf_dynstr_t str;
228 RE(atf_dynstr_init_fmt(&src, "Str 1, Str 2"));
230 RE(atf_dynstr_init_substr(&str, &src, 0, 0));
231 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "") == 0);
232 atf_dynstr_fini(&str);
234 RE(atf_dynstr_init_substr(&str, &src, 0, atf_dynstr_npos));
235 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "Str 1, Str 2") == 0);
236 atf_dynstr_fini(&str);
238 RE(atf_dynstr_init_substr(&str, &src, 0, 100));
239 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "Str 1, Str 2") == 0);
240 atf_dynstr_fini(&str);
242 RE(atf_dynstr_init_substr(&str, &src, 0, 5));
243 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "Str 1") == 0);
244 atf_dynstr_fini(&str);
246 RE(atf_dynstr_init_substr(&str, &src, 100, atf_dynstr_npos));
247 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "") == 0);
248 atf_dynstr_fini(&str);
250 RE(atf_dynstr_init_substr(&str, &src, 7, atf_dynstr_npos));
251 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "Str 2") == 0);
252 atf_dynstr_fini(&str);
254 atf_dynstr_fini(&src);
257 ATF_TC(copy);
258 ATF_TC_HEAD(copy, tc)
260 atf_tc_set_md_var(tc, "descr", "Checks the atf_dynstr_copy constructor");
262 ATF_TC_BODY(copy, tc)
264 atf_dynstr_t str, str2;
266 RE(atf_dynstr_init_fmt(&str, "Test string"));
267 RE(atf_dynstr_copy(&str2, &str));
269 ATF_REQUIRE(atf_equal_dynstr_dynstr(&str, &str2));
271 RE(atf_dynstr_append_fmt(&str2, " non-shared text"));
273 ATF_REQUIRE(!atf_equal_dynstr_dynstr(&str, &str2));
275 atf_dynstr_fini(&str2);
276 atf_dynstr_fini(&str);
279 ATF_TC(fini_disown);
280 ATF_TC_HEAD(fini_disown, tc)
282 atf_tc_set_md_var(tc, "descr", "Checks grabbing ownership of the "
283 "internal plain C string");
285 ATF_TC_BODY(fini_disown, tc)
287 const char *cstr;
288 char *cstr2;
289 atf_dynstr_t str;
291 RE(atf_dynstr_init_fmt(&str, "Test string 1"));
292 cstr = atf_dynstr_cstring(&str);
293 cstr2 = atf_dynstr_fini_disown(&str);
295 ATF_REQUIRE_EQ(cstr, cstr2);
296 free(cstr2);
300 * Getters.
303 ATF_TC(cstring);
304 ATF_TC_HEAD(cstring, tc)
306 atf_tc_set_md_var(tc, "descr", "Checks the method to obtain a plain C "
307 "string");
309 ATF_TC_BODY(cstring, tc)
311 const char *cstr;
312 atf_dynstr_t str;
314 RE(atf_dynstr_init_fmt(&str, "Test string 1"));
315 cstr = atf_dynstr_cstring(&str);
316 ATF_REQUIRE(cstr != NULL);
317 ATF_REQUIRE(strcmp(cstr, "Test string 1") == 0);
318 atf_dynstr_fini(&str);
320 RE(atf_dynstr_init_fmt(&str, "Test string 2"));
321 cstr = atf_dynstr_cstring(&str);
322 ATF_REQUIRE(cstr != NULL);
323 ATF_REQUIRE(strcmp(cstr, "Test string 2") == 0);
324 atf_dynstr_fini(&str);
327 ATF_TC(length);
328 ATF_TC_HEAD(length, tc)
330 atf_tc_set_md_var(tc, "descr", "Checks the method to obtain the length");
332 ATF_TC_BODY(length, tc)
334 size_t i;
336 for (i = 0; i < 8192; i++) {
337 atf_dynstr_t str;
338 RE(atf_dynstr_init_rep(&str, i, 'a'));
339 ATF_REQUIRE_EQ(atf_dynstr_length(&str), i);
340 atf_dynstr_fini(&str);
344 ATF_TC(rfind_ch);
345 ATF_TC_HEAD(rfind_ch, tc)
347 atf_tc_set_md_var(tc, "descr", "Checks the method to locate the first "
348 "occurrence of a character starting from the end");
350 ATF_TC_BODY(rfind_ch, tc)
352 atf_dynstr_t str;
354 RE(atf_dynstr_init_fmt(&str, "Foo1/Bar2/,.Baz"));
356 ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, '\0'), atf_dynstr_npos);
358 ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, '0'), atf_dynstr_npos);
359 ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, 'b'), atf_dynstr_npos);
361 ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, 'F'), 0);
362 ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, '/'), 9);
363 ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, 'a'), 13);
364 ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, 'z'), 14);
366 atf_dynstr_fini(&str);
370 * Modifiers.
373 static
374 void
375 check_append(atf_error_t (*append)(atf_dynstr_t *, const char *, ...))
377 const size_t maxlen = 8192;
378 char buf[maxlen + 1];
379 size_t i;
380 atf_dynstr_t str;
382 printf("Appending with plain string\n");
383 buf[0] = '\0';
384 RE(atf_dynstr_init(&str));
385 for (i = 0; i < maxlen; i++) {
386 if (strcmp(atf_dynstr_cstring(&str), buf) != 0) {
387 fprintf(stderr, "Failed at iteration %zd\n", i);
388 atf_tc_fail("Failed to append character at iteration %zd", i);
391 RE(append(&str, "a"));
392 strcat(buf, "a");
394 atf_dynstr_fini(&str);
396 printf("Appending with formatted string\n");
397 buf[0] = '\0';
398 RE(atf_dynstr_init(&str));
399 for (i = 0; i < maxlen; i++) {
400 if (strcmp(atf_dynstr_cstring(&str), buf) != 0) {
401 fprintf(stderr, "Failed at iteration %zd\n", i);
402 atf_tc_fail("Failed to append character at iteration %zd", i);
405 RE(append(&str, "%s", "a"));
406 strcat(buf, "a");
408 atf_dynstr_fini(&str);
411 static
412 atf_error_t
413 append_ap_aux(atf_dynstr_t *str, const char *fmt, ...)
415 va_list ap;
416 atf_error_t err;
418 va_start(ap, fmt);
419 err = atf_dynstr_append_ap(str, fmt, ap);
420 va_end(ap);
422 return err;
425 ATF_TC(append_ap);
426 ATF_TC_HEAD(append_ap, tc)
428 atf_tc_set_md_var(tc, "descr", "Checks that appending a string to "
429 "another one works");
431 ATF_TC_BODY(append_ap, tc)
433 check_append(append_ap_aux);
436 ATF_TC(append_fmt);
437 ATF_TC_HEAD(append_fmt, tc)
439 atf_tc_set_md_var(tc, "descr", "Checks that appending a string to "
440 "another one works");
442 ATF_TC_BODY(append_fmt, tc)
444 check_append(atf_dynstr_append_fmt);
447 ATF_TC(clear);
448 ATF_TC_HEAD(clear, tc)
450 atf_tc_set_md_var(tc, "descr", "Checks clearing a string");
452 ATF_TC_BODY(clear, tc)
454 atf_dynstr_t str;
456 printf("Clear an empty string\n");
457 RE(atf_dynstr_init(&str));
458 atf_dynstr_clear(&str);
459 ATF_REQUIRE_EQ(atf_dynstr_length(&str), 0);
460 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "") == 0);
461 atf_dynstr_fini(&str);
463 printf("Clear a non-empty string\n");
464 RE(atf_dynstr_init_fmt(&str, "Not empty"));
465 ATF_REQUIRE_EQ(atf_dynstr_length(&str), strlen("Not empty"));
466 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "Not empty") == 0);
467 atf_dynstr_clear(&str);
468 ATF_REQUIRE_EQ(atf_dynstr_length(&str), 0);
469 ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "") == 0);
470 atf_dynstr_fini(&str);
473 static
474 void
475 check_prepend(atf_error_t (*prepend)(atf_dynstr_t *, const char *, ...))
477 const size_t maxlen = 8192;
478 char buf[maxlen + 1];
479 size_t i;
480 atf_dynstr_t str;
482 printf("Prepending with plain string\n");
483 buf[0] = '\0';
484 RE(atf_dynstr_init(&str));
485 for (i = 0; i < maxlen; i++) {
486 if (strcmp(atf_dynstr_cstring(&str), buf) != 0) {
487 fprintf(stderr, "Failed at iteration %zd\n", i);
488 atf_tc_fail("Failed to prepend character at iteration %zd", i);
491 memmove(buf + 1, buf, i + 1);
492 if (i % 2 == 0) {
493 RE(prepend(&str, "%s", "a"));
494 buf[0] = 'a';
495 } else {
496 RE(prepend(&str, "%s", "b"));
497 buf[0] = 'b';
500 atf_dynstr_fini(&str);
502 printf("Prepending with formatted string\n");
503 buf[0] = '\0';
504 RE(atf_dynstr_init(&str));
505 for (i = 0; i < maxlen; i++) {
506 if (strcmp(atf_dynstr_cstring(&str), buf) != 0) {
507 fprintf(stderr, "Failed at iteration %zd\n", i);
508 atf_tc_fail("Failed to prepend character at iteration %zd", i);
511 memmove(buf + 1, buf, i + 1);
512 if (i % 2 == 0) {
513 RE(prepend(&str, "%s", "a"));
514 buf[0] = 'a';
515 } else {
516 RE(prepend(&str, "%s", "b"));
517 buf[0] = 'b';
520 atf_dynstr_fini(&str);
523 static
524 atf_error_t
525 prepend_ap_aux(atf_dynstr_t *str, const char *fmt, ...)
527 va_list ap;
528 atf_error_t err;
530 va_start(ap, fmt);
531 err = atf_dynstr_prepend_ap(str, fmt, ap);
532 va_end(ap);
534 return err;
537 ATF_TC(prepend_ap);
538 ATF_TC_HEAD(prepend_ap, tc)
540 atf_tc_set_md_var(tc, "descr", "Checks that prepending a string to "
541 "another one works");
543 ATF_TC_BODY(prepend_ap, tc)
545 check_prepend(prepend_ap_aux);
548 ATF_TC(prepend_fmt);
549 ATF_TC_HEAD(prepend_fmt, tc)
551 atf_tc_set_md_var(tc, "descr", "Checks that prepending a string to "
552 "another one works");
554 ATF_TC_BODY(prepend_fmt, tc)
556 check_prepend(atf_dynstr_prepend_fmt);
560 * Operators.
563 ATF_TC(equal_cstring);
564 ATF_TC_HEAD(equal_cstring, tc)
566 atf_tc_set_md_var(tc, "descr", "Checks the atf_equal_dynstr_cstring "
567 "function");
569 ATF_TC_BODY(equal_cstring, tc)
571 atf_dynstr_t str;
573 RE(atf_dynstr_init(&str));
574 ATF_REQUIRE( atf_equal_dynstr_cstring(&str, ""));
575 ATF_REQUIRE(!atf_equal_dynstr_cstring(&str, "Test"));
576 atf_dynstr_fini(&str);
578 RE(atf_dynstr_init_fmt(&str, "Test"));
579 ATF_REQUIRE( atf_equal_dynstr_cstring(&str, "Test"));
580 ATF_REQUIRE(!atf_equal_dynstr_cstring(&str, ""));
581 ATF_REQUIRE(!atf_equal_dynstr_cstring(&str, "Tes"));
582 ATF_REQUIRE(!atf_equal_dynstr_cstring(&str, "Test "));
583 atf_dynstr_fini(&str);
586 ATF_TC(equal_dynstr);
587 ATF_TC_HEAD(equal_dynstr, tc)
589 atf_tc_set_md_var(tc, "descr", "Checks the atf_equal_dynstr_dynstr "
590 "function");
592 ATF_TC_BODY(equal_dynstr, tc)
594 atf_dynstr_t str, str2;
596 RE(atf_dynstr_init(&str));
597 RE(atf_dynstr_init_fmt(&str2, "Test"));
598 ATF_REQUIRE( atf_equal_dynstr_dynstr(&str, &str));
599 ATF_REQUIRE(!atf_equal_dynstr_dynstr(&str, &str2));
600 atf_dynstr_fini(&str2);
601 atf_dynstr_fini(&str);
604 /* ---------------------------------------------------------------------
605 * Main.
606 * --------------------------------------------------------------------- */
608 ATF_TP_ADD_TCS(tp)
610 /* Constructors and destructors. */
611 ATF_TP_ADD_TC(tp, init);
612 ATF_TP_ADD_TC(tp, init_ap);
613 ATF_TP_ADD_TC(tp, init_fmt);
614 ATF_TP_ADD_TC(tp, init_raw);
615 ATF_TP_ADD_TC(tp, init_rep);
616 ATF_TP_ADD_TC(tp, init_substr);
617 ATF_TP_ADD_TC(tp, copy);
618 ATF_TP_ADD_TC(tp, fini_disown);
620 /* Getters. */
621 ATF_TP_ADD_TC(tp, cstring);
622 ATF_TP_ADD_TC(tp, length);
623 ATF_TP_ADD_TC(tp, rfind_ch);
625 /* Modifiers. */
626 ATF_TP_ADD_TC(tp, append_ap);
627 ATF_TP_ADD_TC(tp, append_fmt);
628 ATF_TP_ADD_TC(tp, clear);
629 ATF_TP_ADD_TC(tp, prepend_ap);
630 ATF_TP_ADD_TC(tp, prepend_fmt);
632 /* Operators. */
633 ATF_TP_ADD_TC(tp, equal_cstring);
634 ATF_TP_ADD_TC(tp, equal_dynstr);
636 return atf_no_error();