2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
39 #include "test_helpers.h"
41 /* ---------------------------------------------------------------------
42 * Tests for the "atf_dynstr" type.
43 * --------------------------------------------------------------------- */
46 * Constructors and destructors.
52 atf_tc_set_md_var(tc
, "descr", "Checks the empty constructor");
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
);
66 init_fmt(atf_dynstr_t
*str
, const char *fmt
, ...)
71 RE(atf_dynstr_init_ap(str
, fmt
, 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
)
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
);
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
)
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
);
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";
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"));
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];
185 for (i
= 0; i
< maxlen
; i
++) {
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
);
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"));
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"));
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
)
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
);
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
);
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
)
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
);
304 ATF_TC_HEAD(cstring
, tc
)
306 atf_tc_set_md_var(tc
, "descr", "Checks the method to obtain a plain C "
309 ATF_TC_BODY(cstring
, tc
)
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
);
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
)
336 for (i
= 0; i
< 8192; i
++) {
338 RE(atf_dynstr_init_rep(&str
, i
, 'a'));
339 ATF_REQUIRE_EQ(atf_dynstr_length(&str
), i
);
340 atf_dynstr_fini(&str
);
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
)
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
);
375 check_append(atf_error_t (*append
)(atf_dynstr_t
*, const char *, ...))
377 const size_t maxlen
= 8192;
378 char buf
[maxlen
+ 1];
382 printf("Appending with plain string\n");
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"));
394 atf_dynstr_fini(&str
);
396 printf("Appending with formatted string\n");
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"));
408 atf_dynstr_fini(&str
);
413 append_ap_aux(atf_dynstr_t
*str
, const char *fmt
, ...)
419 err
= atf_dynstr_append_ap(str
, fmt
, 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
);
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
);
448 ATF_TC_HEAD(clear
, tc
)
450 atf_tc_set_md_var(tc
, "descr", "Checks clearing a string");
452 ATF_TC_BODY(clear
, tc
)
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
);
475 check_prepend(atf_error_t (*prepend
)(atf_dynstr_t
*, const char *, ...))
477 const size_t maxlen
= 8192;
478 char buf
[maxlen
+ 1];
482 printf("Prepending with plain string\n");
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);
493 RE(prepend(&str
, "%s", "a"));
496 RE(prepend(&str
, "%s", "b"));
500 atf_dynstr_fini(&str
);
502 printf("Prepending with formatted string\n");
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);
513 RE(prepend(&str
, "%s", "a"));
516 RE(prepend(&str
, "%s", "b"));
520 atf_dynstr_fini(&str
);
525 prepend_ap_aux(atf_dynstr_t
*str
, const char *fmt
, ...)
531 err
= atf_dynstr_prepend_ap(str
, fmt
, 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
);
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
);
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 "
569 ATF_TC_BODY(equal_cstring
, tc
)
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 "
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 /* ---------------------------------------------------------------------
606 * --------------------------------------------------------------------- */
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
);
621 ATF_TP_ADD_TC(tp
, cstring
);
622 ATF_TP_ADD_TC(tp
, length
);
623 ATF_TP_ADD_TC(tp
, rfind_ch
);
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
);
633 ATF_TP_ADD_TC(tp
, equal_cstring
);
634 ATF_TP_ADD_TC(tp
, equal_dynstr
);
636 return atf_no_error();