1*11be35a1SLionel Sambuc /*
2*11be35a1SLionel Sambuc * Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc *
4*11be35a1SLionel Sambuc * Copyright (c) 2008 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc * are met:
10*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc *
16*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc */
29*11be35a1SLionel Sambuc
30*11be35a1SLionel Sambuc #include <stdarg.h>
31*11be35a1SLionel Sambuc #include <stdint.h>
32*11be35a1SLionel Sambuc #include <stdio.h>
33*11be35a1SLionel Sambuc #include <stdlib.h>
34*11be35a1SLionel Sambuc #include <string.h>
35*11be35a1SLionel Sambuc
36*11be35a1SLionel Sambuc #include <atf-c.h>
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc #include "dynstr.h"
39*11be35a1SLionel Sambuc #include "test_helpers.h"
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
42*11be35a1SLionel Sambuc * Tests for the "atf_dynstr" type.
43*11be35a1SLionel Sambuc * --------------------------------------------------------------------- */
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc /*
46*11be35a1SLionel Sambuc * Constructors and destructors.
47*11be35a1SLionel Sambuc */
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc ATF_TC(init);
ATF_TC_HEAD(init,tc)50*11be35a1SLionel Sambuc ATF_TC_HEAD(init, tc)
51*11be35a1SLionel Sambuc {
52*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks the empty constructor");
53*11be35a1SLionel Sambuc }
ATF_TC_BODY(init,tc)54*11be35a1SLionel Sambuc ATF_TC_BODY(init, tc)
55*11be35a1SLionel Sambuc {
56*11be35a1SLionel Sambuc atf_dynstr_t str;
57*11be35a1SLionel Sambuc
58*11be35a1SLionel Sambuc RE(atf_dynstr_init(&str));
59*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(atf_dynstr_length(&str), 0);
60*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "") == 0);
61*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
62*11be35a1SLionel Sambuc }
63*11be35a1SLionel Sambuc
64*11be35a1SLionel Sambuc static
65*11be35a1SLionel Sambuc void
init_fmt(atf_dynstr_t * str,const char * fmt,...)66*11be35a1SLionel Sambuc init_fmt(atf_dynstr_t *str, const char *fmt, ...)
67*11be35a1SLionel Sambuc {
68*11be35a1SLionel Sambuc va_list ap;
69*11be35a1SLionel Sambuc
70*11be35a1SLionel Sambuc va_start(ap, fmt);
71*11be35a1SLionel Sambuc RE(atf_dynstr_init_ap(str, fmt, ap));
72*11be35a1SLionel Sambuc va_end(ap);
73*11be35a1SLionel Sambuc }
74*11be35a1SLionel Sambuc
75*11be35a1SLionel Sambuc ATF_TC(init_ap);
ATF_TC_HEAD(init_ap,tc)76*11be35a1SLionel Sambuc ATF_TC_HEAD(init_ap, tc)
77*11be35a1SLionel Sambuc {
78*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks the formatted constructor using "
79*11be35a1SLionel Sambuc "a va_list argument");
80*11be35a1SLionel Sambuc }
ATF_TC_BODY(init_ap,tc)81*11be35a1SLionel Sambuc ATF_TC_BODY(init_ap, tc)
82*11be35a1SLionel Sambuc {
83*11be35a1SLionel Sambuc atf_dynstr_t str;
84*11be35a1SLionel Sambuc
85*11be35a1SLionel Sambuc init_fmt(&str, "String 1");
86*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 1") == 0);
87*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
88*11be35a1SLionel Sambuc
89*11be35a1SLionel Sambuc init_fmt(&str, "String %d", 2);
90*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 2") == 0);
91*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
92*11be35a1SLionel Sambuc
93*11be35a1SLionel Sambuc init_fmt(&str, "%s %d", "String", 3);
94*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 3") == 0);
95*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambuc init_fmt(&str, "%s%s%s%s%s%s%s", "This ", "should ", "be ", "a ",
98*11be35a1SLionel Sambuc "large ", "string ", "aaaabbbbccccdddd");
99*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str),
100*11be35a1SLionel Sambuc "This should be a large string "
101*11be35a1SLionel Sambuc "aaaabbbbccccdddd") == 0);
102*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
103*11be35a1SLionel Sambuc }
104*11be35a1SLionel Sambuc
105*11be35a1SLionel Sambuc ATF_TC(init_fmt);
ATF_TC_HEAD(init_fmt,tc)106*11be35a1SLionel Sambuc ATF_TC_HEAD(init_fmt, tc)
107*11be35a1SLionel Sambuc {
108*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks the formatted constructor using "
109*11be35a1SLionel Sambuc "a variable list of parameters");
110*11be35a1SLionel Sambuc }
ATF_TC_BODY(init_fmt,tc)111*11be35a1SLionel Sambuc ATF_TC_BODY(init_fmt, tc)
112*11be35a1SLionel Sambuc {
113*11be35a1SLionel Sambuc atf_dynstr_t str;
114*11be35a1SLionel Sambuc
115*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&str, "String 1"));
116*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 1") == 0);
117*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
118*11be35a1SLionel Sambuc
119*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&str, "String %d", 2));
120*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 2") == 0);
121*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
122*11be35a1SLionel Sambuc
123*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&str, "%s %d", "String", 3));
124*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 3") == 0);
125*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
126*11be35a1SLionel Sambuc
127*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&str, "%s%s%s%s%s%s%s", "This ", "should ",
128*11be35a1SLionel Sambuc "be ", "a ", "large ", "string ",
129*11be35a1SLionel Sambuc "aaaabbbbccccdddd"));
130*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str),
131*11be35a1SLionel Sambuc "This should be a large string "
132*11be35a1SLionel Sambuc "aaaabbbbccccdddd") == 0);
133*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
134*11be35a1SLionel Sambuc }
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc ATF_TC(init_raw);
ATF_TC_HEAD(init_raw,tc)137*11be35a1SLionel Sambuc ATF_TC_HEAD(init_raw, tc)
138*11be35a1SLionel Sambuc {
139*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks the construction of a string "
140*11be35a1SLionel Sambuc "using a raw memory pointer");
141*11be35a1SLionel Sambuc }
ATF_TC_BODY(init_raw,tc)142*11be35a1SLionel Sambuc ATF_TC_BODY(init_raw, tc)
143*11be35a1SLionel Sambuc {
144*11be35a1SLionel Sambuc const char *src = "String 1, String 2";
145*11be35a1SLionel Sambuc atf_dynstr_t str;
146*11be35a1SLionel Sambuc
147*11be35a1SLionel Sambuc RE(atf_dynstr_init_raw(&str, src, 0));
148*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "") == 0);
149*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
150*11be35a1SLionel Sambuc
151*11be35a1SLionel Sambuc RE(atf_dynstr_init_raw(&str, src, 8));
152*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 1") == 0);
153*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
154*11be35a1SLionel Sambuc
155*11be35a1SLionel Sambuc RE(atf_dynstr_init_raw(&str, src + 10, 8));
156*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String 2") == 0);
157*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
158*11be35a1SLionel Sambuc
159*11be35a1SLionel Sambuc RE(atf_dynstr_init_raw(&str, "String\0Lost", 11));
160*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "String") == 0);
161*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
162*11be35a1SLionel Sambuc
163*11be35a1SLionel Sambuc {
164*11be35a1SLionel Sambuc atf_error_t err = atf_dynstr_init_raw(&str, "NULL", SIZE_MAX - 1);
165*11be35a1SLionel Sambuc ATF_REQUIRE(atf_is_error(err));
166*11be35a1SLionel Sambuc ATF_REQUIRE(atf_error_is(err, "no_memory"));
167*11be35a1SLionel Sambuc atf_error_free(err);
168*11be35a1SLionel Sambuc }
169*11be35a1SLionel Sambuc }
170*11be35a1SLionel Sambuc
171*11be35a1SLionel Sambuc ATF_TC(init_rep);
ATF_TC_HEAD(init_rep,tc)172*11be35a1SLionel Sambuc ATF_TC_HEAD(init_rep, tc)
173*11be35a1SLionel Sambuc {
174*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks the construction of a string by "
175*11be35a1SLionel Sambuc "repeating characters");
176*11be35a1SLionel Sambuc }
ATF_TC_BODY(init_rep,tc)177*11be35a1SLionel Sambuc ATF_TC_BODY(init_rep, tc)
178*11be35a1SLionel Sambuc {
179*11be35a1SLionel Sambuc const size_t maxlen = 8192;
180*11be35a1SLionel Sambuc char buf[maxlen + 1];
181*11be35a1SLionel Sambuc size_t i;
182*11be35a1SLionel Sambuc
183*11be35a1SLionel Sambuc buf[0] = '\0';
184*11be35a1SLionel Sambuc
185*11be35a1SLionel Sambuc for (i = 0; i < maxlen; i++) {
186*11be35a1SLionel Sambuc atf_dynstr_t str;
187*11be35a1SLionel Sambuc
188*11be35a1SLionel Sambuc RE(atf_dynstr_init_rep(&str, i, 'a'));
189*11be35a1SLionel Sambuc
190*11be35a1SLionel Sambuc if (strcmp(atf_dynstr_cstring(&str), buf) != 0) {
191*11be35a1SLionel Sambuc fprintf(stderr, "Failed at iteration %zd\n", i);
192*11be35a1SLionel Sambuc atf_tc_fail("Failed to construct dynstr by repeating %zd "
193*11be35a1SLionel Sambuc "times the '%c' character", i, 'a');
194*11be35a1SLionel Sambuc }
195*11be35a1SLionel Sambuc
196*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
197*11be35a1SLionel Sambuc
198*11be35a1SLionel Sambuc strcat(buf, "a");
199*11be35a1SLionel Sambuc }
200*11be35a1SLionel Sambuc
201*11be35a1SLionel Sambuc {
202*11be35a1SLionel Sambuc atf_dynstr_t str;
203*11be35a1SLionel Sambuc atf_error_t err;
204*11be35a1SLionel Sambuc
205*11be35a1SLionel Sambuc err = atf_dynstr_init_rep(&str, SIZE_MAX, 'a');
206*11be35a1SLionel Sambuc ATF_REQUIRE(atf_is_error(err));
207*11be35a1SLionel Sambuc ATF_REQUIRE(atf_error_is(err, "no_memory"));
208*11be35a1SLionel Sambuc atf_error_free(err);
209*11be35a1SLionel Sambuc
210*11be35a1SLionel Sambuc err = atf_dynstr_init_rep(&str, SIZE_MAX - 1, 'a');
211*11be35a1SLionel Sambuc ATF_REQUIRE(atf_is_error(err));
212*11be35a1SLionel Sambuc ATF_REQUIRE(atf_error_is(err, "no_memory"));
213*11be35a1SLionel Sambuc atf_error_free(err);
214*11be35a1SLionel Sambuc }
215*11be35a1SLionel Sambuc }
216*11be35a1SLionel Sambuc
217*11be35a1SLionel Sambuc ATF_TC(init_substr);
ATF_TC_HEAD(init_substr,tc)218*11be35a1SLionel Sambuc ATF_TC_HEAD(init_substr, tc)
219*11be35a1SLionel Sambuc {
220*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks the construction of a string "
221*11be35a1SLionel Sambuc "using a substring of another one");
222*11be35a1SLionel Sambuc }
ATF_TC_BODY(init_substr,tc)223*11be35a1SLionel Sambuc ATF_TC_BODY(init_substr, tc)
224*11be35a1SLionel Sambuc {
225*11be35a1SLionel Sambuc atf_dynstr_t src;
226*11be35a1SLionel Sambuc atf_dynstr_t str;
227*11be35a1SLionel Sambuc
228*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&src, "Str 1, Str 2"));
229*11be35a1SLionel Sambuc
230*11be35a1SLionel Sambuc RE(atf_dynstr_init_substr(&str, &src, 0, 0));
231*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "") == 0);
232*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
233*11be35a1SLionel Sambuc
234*11be35a1SLionel Sambuc RE(atf_dynstr_init_substr(&str, &src, 0, atf_dynstr_npos));
235*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "Str 1, Str 2") == 0);
236*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
237*11be35a1SLionel Sambuc
238*11be35a1SLionel Sambuc RE(atf_dynstr_init_substr(&str, &src, 0, 100));
239*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "Str 1, Str 2") == 0);
240*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
241*11be35a1SLionel Sambuc
242*11be35a1SLionel Sambuc RE(atf_dynstr_init_substr(&str, &src, 0, 5));
243*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "Str 1") == 0);
244*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
245*11be35a1SLionel Sambuc
246*11be35a1SLionel Sambuc RE(atf_dynstr_init_substr(&str, &src, 100, atf_dynstr_npos));
247*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "") == 0);
248*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
249*11be35a1SLionel Sambuc
250*11be35a1SLionel Sambuc RE(atf_dynstr_init_substr(&str, &src, 7, atf_dynstr_npos));
251*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "Str 2") == 0);
252*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
253*11be35a1SLionel Sambuc
254*11be35a1SLionel Sambuc atf_dynstr_fini(&src);
255*11be35a1SLionel Sambuc }
256*11be35a1SLionel Sambuc
257*11be35a1SLionel Sambuc ATF_TC(copy);
ATF_TC_HEAD(copy,tc)258*11be35a1SLionel Sambuc ATF_TC_HEAD(copy, tc)
259*11be35a1SLionel Sambuc {
260*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks the atf_dynstr_copy constructor");
261*11be35a1SLionel Sambuc }
ATF_TC_BODY(copy,tc)262*11be35a1SLionel Sambuc ATF_TC_BODY(copy, tc)
263*11be35a1SLionel Sambuc {
264*11be35a1SLionel Sambuc atf_dynstr_t str, str2;
265*11be35a1SLionel Sambuc
266*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&str, "Test string"));
267*11be35a1SLionel Sambuc RE(atf_dynstr_copy(&str2, &str));
268*11be35a1SLionel Sambuc
269*11be35a1SLionel Sambuc ATF_REQUIRE(atf_equal_dynstr_dynstr(&str, &str2));
270*11be35a1SLionel Sambuc
271*11be35a1SLionel Sambuc RE(atf_dynstr_append_fmt(&str2, " non-shared text"));
272*11be35a1SLionel Sambuc
273*11be35a1SLionel Sambuc ATF_REQUIRE(!atf_equal_dynstr_dynstr(&str, &str2));
274*11be35a1SLionel Sambuc
275*11be35a1SLionel Sambuc atf_dynstr_fini(&str2);
276*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
277*11be35a1SLionel Sambuc }
278*11be35a1SLionel Sambuc
279*11be35a1SLionel Sambuc ATF_TC(fini_disown);
ATF_TC_HEAD(fini_disown,tc)280*11be35a1SLionel Sambuc ATF_TC_HEAD(fini_disown, tc)
281*11be35a1SLionel Sambuc {
282*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks grabbing ownership of the "
283*11be35a1SLionel Sambuc "internal plain C string");
284*11be35a1SLionel Sambuc }
ATF_TC_BODY(fini_disown,tc)285*11be35a1SLionel Sambuc ATF_TC_BODY(fini_disown, tc)
286*11be35a1SLionel Sambuc {
287*11be35a1SLionel Sambuc const char *cstr;
288*11be35a1SLionel Sambuc char *cstr2;
289*11be35a1SLionel Sambuc atf_dynstr_t str;
290*11be35a1SLionel Sambuc
291*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&str, "Test string 1"));
292*11be35a1SLionel Sambuc cstr = atf_dynstr_cstring(&str);
293*11be35a1SLionel Sambuc cstr2 = atf_dynstr_fini_disown(&str);
294*11be35a1SLionel Sambuc
295*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(cstr, cstr2);
296*11be35a1SLionel Sambuc free(cstr2);
297*11be35a1SLionel Sambuc }
298*11be35a1SLionel Sambuc
299*11be35a1SLionel Sambuc /*
300*11be35a1SLionel Sambuc * Getters.
301*11be35a1SLionel Sambuc */
302*11be35a1SLionel Sambuc
303*11be35a1SLionel Sambuc ATF_TC(cstring);
ATF_TC_HEAD(cstring,tc)304*11be35a1SLionel Sambuc ATF_TC_HEAD(cstring, tc)
305*11be35a1SLionel Sambuc {
306*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks the method to obtain a plain C "
307*11be35a1SLionel Sambuc "string");
308*11be35a1SLionel Sambuc }
ATF_TC_BODY(cstring,tc)309*11be35a1SLionel Sambuc ATF_TC_BODY(cstring, tc)
310*11be35a1SLionel Sambuc {
311*11be35a1SLionel Sambuc const char *cstr;
312*11be35a1SLionel Sambuc atf_dynstr_t str;
313*11be35a1SLionel Sambuc
314*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&str, "Test string 1"));
315*11be35a1SLionel Sambuc cstr = atf_dynstr_cstring(&str);
316*11be35a1SLionel Sambuc ATF_REQUIRE(cstr != NULL);
317*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(cstr, "Test string 1") == 0);
318*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
319*11be35a1SLionel Sambuc
320*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&str, "Test string 2"));
321*11be35a1SLionel Sambuc cstr = atf_dynstr_cstring(&str);
322*11be35a1SLionel Sambuc ATF_REQUIRE(cstr != NULL);
323*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(cstr, "Test string 2") == 0);
324*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
325*11be35a1SLionel Sambuc }
326*11be35a1SLionel Sambuc
327*11be35a1SLionel Sambuc ATF_TC(length);
ATF_TC_HEAD(length,tc)328*11be35a1SLionel Sambuc ATF_TC_HEAD(length, tc)
329*11be35a1SLionel Sambuc {
330*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks the method to obtain the length");
331*11be35a1SLionel Sambuc }
ATF_TC_BODY(length,tc)332*11be35a1SLionel Sambuc ATF_TC_BODY(length, tc)
333*11be35a1SLionel Sambuc {
334*11be35a1SLionel Sambuc size_t i;
335*11be35a1SLionel Sambuc
336*11be35a1SLionel Sambuc for (i = 0; i < 8192; i++) {
337*11be35a1SLionel Sambuc atf_dynstr_t str;
338*11be35a1SLionel Sambuc RE(atf_dynstr_init_rep(&str, i, 'a'));
339*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(atf_dynstr_length(&str), i);
340*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
341*11be35a1SLionel Sambuc }
342*11be35a1SLionel Sambuc }
343*11be35a1SLionel Sambuc
344*11be35a1SLionel Sambuc ATF_TC(rfind_ch);
ATF_TC_HEAD(rfind_ch,tc)345*11be35a1SLionel Sambuc ATF_TC_HEAD(rfind_ch, tc)
346*11be35a1SLionel Sambuc {
347*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks the method to locate the first "
348*11be35a1SLionel Sambuc "occurrence of a character starting from the end");
349*11be35a1SLionel Sambuc }
ATF_TC_BODY(rfind_ch,tc)350*11be35a1SLionel Sambuc ATF_TC_BODY(rfind_ch, tc)
351*11be35a1SLionel Sambuc {
352*11be35a1SLionel Sambuc atf_dynstr_t str;
353*11be35a1SLionel Sambuc
354*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&str, "Foo1/Bar2/,.Baz"));
355*11be35a1SLionel Sambuc
356*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, '\0'), atf_dynstr_npos);
357*11be35a1SLionel Sambuc
358*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, '0'), atf_dynstr_npos);
359*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, 'b'), atf_dynstr_npos);
360*11be35a1SLionel Sambuc
361*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, 'F'), 0);
362*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, '/'), 9);
363*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, 'a'), 13);
364*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(atf_dynstr_rfind_ch(&str, 'z'), 14);
365*11be35a1SLionel Sambuc
366*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
367*11be35a1SLionel Sambuc }
368*11be35a1SLionel Sambuc
369*11be35a1SLionel Sambuc /*
370*11be35a1SLionel Sambuc * Modifiers.
371*11be35a1SLionel Sambuc */
372*11be35a1SLionel Sambuc
373*11be35a1SLionel Sambuc static
374*11be35a1SLionel Sambuc void
check_append(atf_error_t (* append)(atf_dynstr_t *,const char *,...))375*11be35a1SLionel Sambuc check_append(atf_error_t (*append)(atf_dynstr_t *, const char *, ...))
376*11be35a1SLionel Sambuc {
377*11be35a1SLionel Sambuc const size_t maxlen = 8192;
378*11be35a1SLionel Sambuc char buf[maxlen + 1];
379*11be35a1SLionel Sambuc size_t i;
380*11be35a1SLionel Sambuc atf_dynstr_t str;
381*11be35a1SLionel Sambuc
382*11be35a1SLionel Sambuc printf("Appending with plain string\n");
383*11be35a1SLionel Sambuc buf[0] = '\0';
384*11be35a1SLionel Sambuc RE(atf_dynstr_init(&str));
385*11be35a1SLionel Sambuc for (i = 0; i < maxlen; i++) {
386*11be35a1SLionel Sambuc if (strcmp(atf_dynstr_cstring(&str), buf) != 0) {
387*11be35a1SLionel Sambuc fprintf(stderr, "Failed at iteration %zd\n", i);
388*11be35a1SLionel Sambuc atf_tc_fail("Failed to append character at iteration %zd", i);
389*11be35a1SLionel Sambuc }
390*11be35a1SLionel Sambuc
391*11be35a1SLionel Sambuc RE(append(&str, "a"));
392*11be35a1SLionel Sambuc strcat(buf, "a");
393*11be35a1SLionel Sambuc }
394*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
395*11be35a1SLionel Sambuc
396*11be35a1SLionel Sambuc printf("Appending with formatted string\n");
397*11be35a1SLionel Sambuc buf[0] = '\0';
398*11be35a1SLionel Sambuc RE(atf_dynstr_init(&str));
399*11be35a1SLionel Sambuc for (i = 0; i < maxlen; i++) {
400*11be35a1SLionel Sambuc if (strcmp(atf_dynstr_cstring(&str), buf) != 0) {
401*11be35a1SLionel Sambuc fprintf(stderr, "Failed at iteration %zd\n", i);
402*11be35a1SLionel Sambuc atf_tc_fail("Failed to append character at iteration %zd", i);
403*11be35a1SLionel Sambuc }
404*11be35a1SLionel Sambuc
405*11be35a1SLionel Sambuc RE(append(&str, "%s", "a"));
406*11be35a1SLionel Sambuc strcat(buf, "a");
407*11be35a1SLionel Sambuc }
408*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
409*11be35a1SLionel Sambuc }
410*11be35a1SLionel Sambuc
411*11be35a1SLionel Sambuc static
412*11be35a1SLionel Sambuc atf_error_t
append_ap_aux(atf_dynstr_t * str,const char * fmt,...)413*11be35a1SLionel Sambuc append_ap_aux(atf_dynstr_t *str, const char *fmt, ...)
414*11be35a1SLionel Sambuc {
415*11be35a1SLionel Sambuc va_list ap;
416*11be35a1SLionel Sambuc atf_error_t err;
417*11be35a1SLionel Sambuc
418*11be35a1SLionel Sambuc va_start(ap, fmt);
419*11be35a1SLionel Sambuc err = atf_dynstr_append_ap(str, fmt, ap);
420*11be35a1SLionel Sambuc va_end(ap);
421*11be35a1SLionel Sambuc
422*11be35a1SLionel Sambuc return err;
423*11be35a1SLionel Sambuc }
424*11be35a1SLionel Sambuc
425*11be35a1SLionel Sambuc ATF_TC(append_ap);
ATF_TC_HEAD(append_ap,tc)426*11be35a1SLionel Sambuc ATF_TC_HEAD(append_ap, tc)
427*11be35a1SLionel Sambuc {
428*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks that appending a string to "
429*11be35a1SLionel Sambuc "another one works");
430*11be35a1SLionel Sambuc }
ATF_TC_BODY(append_ap,tc)431*11be35a1SLionel Sambuc ATF_TC_BODY(append_ap, tc)
432*11be35a1SLionel Sambuc {
433*11be35a1SLionel Sambuc check_append(append_ap_aux);
434*11be35a1SLionel Sambuc }
435*11be35a1SLionel Sambuc
436*11be35a1SLionel Sambuc ATF_TC(append_fmt);
ATF_TC_HEAD(append_fmt,tc)437*11be35a1SLionel Sambuc ATF_TC_HEAD(append_fmt, tc)
438*11be35a1SLionel Sambuc {
439*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks that appending a string to "
440*11be35a1SLionel Sambuc "another one works");
441*11be35a1SLionel Sambuc }
ATF_TC_BODY(append_fmt,tc)442*11be35a1SLionel Sambuc ATF_TC_BODY(append_fmt, tc)
443*11be35a1SLionel Sambuc {
444*11be35a1SLionel Sambuc check_append(atf_dynstr_append_fmt);
445*11be35a1SLionel Sambuc }
446*11be35a1SLionel Sambuc
447*11be35a1SLionel Sambuc ATF_TC(clear);
ATF_TC_HEAD(clear,tc)448*11be35a1SLionel Sambuc ATF_TC_HEAD(clear, tc)
449*11be35a1SLionel Sambuc {
450*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks clearing a string");
451*11be35a1SLionel Sambuc }
ATF_TC_BODY(clear,tc)452*11be35a1SLionel Sambuc ATF_TC_BODY(clear, tc)
453*11be35a1SLionel Sambuc {
454*11be35a1SLionel Sambuc atf_dynstr_t str;
455*11be35a1SLionel Sambuc
456*11be35a1SLionel Sambuc printf("Clear an empty string\n");
457*11be35a1SLionel Sambuc RE(atf_dynstr_init(&str));
458*11be35a1SLionel Sambuc atf_dynstr_clear(&str);
459*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(atf_dynstr_length(&str), 0);
460*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "") == 0);
461*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
462*11be35a1SLionel Sambuc
463*11be35a1SLionel Sambuc printf("Clear a non-empty string\n");
464*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&str, "Not empty"));
465*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(atf_dynstr_length(&str), strlen("Not empty"));
466*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "Not empty") == 0);
467*11be35a1SLionel Sambuc atf_dynstr_clear(&str);
468*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(atf_dynstr_length(&str), 0);
469*11be35a1SLionel Sambuc ATF_REQUIRE(strcmp(atf_dynstr_cstring(&str), "") == 0);
470*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
471*11be35a1SLionel Sambuc }
472*11be35a1SLionel Sambuc
473*11be35a1SLionel Sambuc static
474*11be35a1SLionel Sambuc void
check_prepend(atf_error_t (* prepend)(atf_dynstr_t *,const char *,...))475*11be35a1SLionel Sambuc check_prepend(atf_error_t (*prepend)(atf_dynstr_t *, const char *, ...))
476*11be35a1SLionel Sambuc {
477*11be35a1SLionel Sambuc const size_t maxlen = 8192;
478*11be35a1SLionel Sambuc char buf[maxlen + 1];
479*11be35a1SLionel Sambuc size_t i;
480*11be35a1SLionel Sambuc atf_dynstr_t str;
481*11be35a1SLionel Sambuc
482*11be35a1SLionel Sambuc printf("Prepending with plain string\n");
483*11be35a1SLionel Sambuc buf[0] = '\0';
484*11be35a1SLionel Sambuc RE(atf_dynstr_init(&str));
485*11be35a1SLionel Sambuc for (i = 0; i < maxlen; i++) {
486*11be35a1SLionel Sambuc if (strcmp(atf_dynstr_cstring(&str), buf) != 0) {
487*11be35a1SLionel Sambuc fprintf(stderr, "Failed at iteration %zd\n", i);
488*11be35a1SLionel Sambuc atf_tc_fail("Failed to prepend character at iteration %zd", i);
489*11be35a1SLionel Sambuc }
490*11be35a1SLionel Sambuc
491*11be35a1SLionel Sambuc memmove(buf + 1, buf, i + 1);
492*11be35a1SLionel Sambuc if (i % 2 == 0) {
493*11be35a1SLionel Sambuc RE(prepend(&str, "%s", "a"));
494*11be35a1SLionel Sambuc buf[0] = 'a';
495*11be35a1SLionel Sambuc } else {
496*11be35a1SLionel Sambuc RE(prepend(&str, "%s", "b"));
497*11be35a1SLionel Sambuc buf[0] = 'b';
498*11be35a1SLionel Sambuc }
499*11be35a1SLionel Sambuc }
500*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
501*11be35a1SLionel Sambuc
502*11be35a1SLionel Sambuc printf("Prepending with formatted string\n");
503*11be35a1SLionel Sambuc buf[0] = '\0';
504*11be35a1SLionel Sambuc RE(atf_dynstr_init(&str));
505*11be35a1SLionel Sambuc for (i = 0; i < maxlen; i++) {
506*11be35a1SLionel Sambuc if (strcmp(atf_dynstr_cstring(&str), buf) != 0) {
507*11be35a1SLionel Sambuc fprintf(stderr, "Failed at iteration %zd\n", i);
508*11be35a1SLionel Sambuc atf_tc_fail("Failed to prepend character at iteration %zd", i);
509*11be35a1SLionel Sambuc }
510*11be35a1SLionel Sambuc
511*11be35a1SLionel Sambuc memmove(buf + 1, buf, i + 1);
512*11be35a1SLionel Sambuc if (i % 2 == 0) {
513*11be35a1SLionel Sambuc RE(prepend(&str, "%s", "a"));
514*11be35a1SLionel Sambuc buf[0] = 'a';
515*11be35a1SLionel Sambuc } else {
516*11be35a1SLionel Sambuc RE(prepend(&str, "%s", "b"));
517*11be35a1SLionel Sambuc buf[0] = 'b';
518*11be35a1SLionel Sambuc }
519*11be35a1SLionel Sambuc }
520*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
521*11be35a1SLionel Sambuc }
522*11be35a1SLionel Sambuc
523*11be35a1SLionel Sambuc static
524*11be35a1SLionel Sambuc atf_error_t
prepend_ap_aux(atf_dynstr_t * str,const char * fmt,...)525*11be35a1SLionel Sambuc prepend_ap_aux(atf_dynstr_t *str, const char *fmt, ...)
526*11be35a1SLionel Sambuc {
527*11be35a1SLionel Sambuc va_list ap;
528*11be35a1SLionel Sambuc atf_error_t err;
529*11be35a1SLionel Sambuc
530*11be35a1SLionel Sambuc va_start(ap, fmt);
531*11be35a1SLionel Sambuc err = atf_dynstr_prepend_ap(str, fmt, ap);
532*11be35a1SLionel Sambuc va_end(ap);
533*11be35a1SLionel Sambuc
534*11be35a1SLionel Sambuc return err;
535*11be35a1SLionel Sambuc }
536*11be35a1SLionel Sambuc
537*11be35a1SLionel Sambuc ATF_TC(prepend_ap);
ATF_TC_HEAD(prepend_ap,tc)538*11be35a1SLionel Sambuc ATF_TC_HEAD(prepend_ap, tc)
539*11be35a1SLionel Sambuc {
540*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks that prepending a string to "
541*11be35a1SLionel Sambuc "another one works");
542*11be35a1SLionel Sambuc }
ATF_TC_BODY(prepend_ap,tc)543*11be35a1SLionel Sambuc ATF_TC_BODY(prepend_ap, tc)
544*11be35a1SLionel Sambuc {
545*11be35a1SLionel Sambuc check_prepend(prepend_ap_aux);
546*11be35a1SLionel Sambuc }
547*11be35a1SLionel Sambuc
548*11be35a1SLionel Sambuc ATF_TC(prepend_fmt);
ATF_TC_HEAD(prepend_fmt,tc)549*11be35a1SLionel Sambuc ATF_TC_HEAD(prepend_fmt, tc)
550*11be35a1SLionel Sambuc {
551*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks that prepending a string to "
552*11be35a1SLionel Sambuc "another one works");
553*11be35a1SLionel Sambuc }
ATF_TC_BODY(prepend_fmt,tc)554*11be35a1SLionel Sambuc ATF_TC_BODY(prepend_fmt, tc)
555*11be35a1SLionel Sambuc {
556*11be35a1SLionel Sambuc check_prepend(atf_dynstr_prepend_fmt);
557*11be35a1SLionel Sambuc }
558*11be35a1SLionel Sambuc
559*11be35a1SLionel Sambuc /*
560*11be35a1SLionel Sambuc * Operators.
561*11be35a1SLionel Sambuc */
562*11be35a1SLionel Sambuc
563*11be35a1SLionel Sambuc ATF_TC(equal_cstring);
ATF_TC_HEAD(equal_cstring,tc)564*11be35a1SLionel Sambuc ATF_TC_HEAD(equal_cstring, tc)
565*11be35a1SLionel Sambuc {
566*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks the atf_equal_dynstr_cstring "
567*11be35a1SLionel Sambuc "function");
568*11be35a1SLionel Sambuc }
ATF_TC_BODY(equal_cstring,tc)569*11be35a1SLionel Sambuc ATF_TC_BODY(equal_cstring, tc)
570*11be35a1SLionel Sambuc {
571*11be35a1SLionel Sambuc atf_dynstr_t str;
572*11be35a1SLionel Sambuc
573*11be35a1SLionel Sambuc RE(atf_dynstr_init(&str));
574*11be35a1SLionel Sambuc ATF_REQUIRE( atf_equal_dynstr_cstring(&str, ""));
575*11be35a1SLionel Sambuc ATF_REQUIRE(!atf_equal_dynstr_cstring(&str, "Test"));
576*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
577*11be35a1SLionel Sambuc
578*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&str, "Test"));
579*11be35a1SLionel Sambuc ATF_REQUIRE( atf_equal_dynstr_cstring(&str, "Test"));
580*11be35a1SLionel Sambuc ATF_REQUIRE(!atf_equal_dynstr_cstring(&str, ""));
581*11be35a1SLionel Sambuc ATF_REQUIRE(!atf_equal_dynstr_cstring(&str, "Tes"));
582*11be35a1SLionel Sambuc ATF_REQUIRE(!atf_equal_dynstr_cstring(&str, "Test "));
583*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
584*11be35a1SLionel Sambuc }
585*11be35a1SLionel Sambuc
586*11be35a1SLionel Sambuc ATF_TC(equal_dynstr);
ATF_TC_HEAD(equal_dynstr,tc)587*11be35a1SLionel Sambuc ATF_TC_HEAD(equal_dynstr, tc)
588*11be35a1SLionel Sambuc {
589*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks the atf_equal_dynstr_dynstr "
590*11be35a1SLionel Sambuc "function");
591*11be35a1SLionel Sambuc }
ATF_TC_BODY(equal_dynstr,tc)592*11be35a1SLionel Sambuc ATF_TC_BODY(equal_dynstr, tc)
593*11be35a1SLionel Sambuc {
594*11be35a1SLionel Sambuc atf_dynstr_t str, str2;
595*11be35a1SLionel Sambuc
596*11be35a1SLionel Sambuc RE(atf_dynstr_init(&str));
597*11be35a1SLionel Sambuc RE(atf_dynstr_init_fmt(&str2, "Test"));
598*11be35a1SLionel Sambuc ATF_REQUIRE( atf_equal_dynstr_dynstr(&str, &str));
599*11be35a1SLionel Sambuc ATF_REQUIRE(!atf_equal_dynstr_dynstr(&str, &str2));
600*11be35a1SLionel Sambuc atf_dynstr_fini(&str2);
601*11be35a1SLionel Sambuc atf_dynstr_fini(&str);
602*11be35a1SLionel Sambuc }
603*11be35a1SLionel Sambuc
604*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
605*11be35a1SLionel Sambuc * Main.
606*11be35a1SLionel Sambuc * --------------------------------------------------------------------- */
607*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)608*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
609*11be35a1SLionel Sambuc {
610*11be35a1SLionel Sambuc /* Constructors and destructors. */
611*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, init);
612*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, init_ap);
613*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, init_fmt);
614*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, init_raw);
615*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, init_rep);
616*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, init_substr);
617*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, copy);
618*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, fini_disown);
619*11be35a1SLionel Sambuc
620*11be35a1SLionel Sambuc /* Getters. */
621*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, cstring);
622*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, length);
623*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, rfind_ch);
624*11be35a1SLionel Sambuc
625*11be35a1SLionel Sambuc /* Modifiers. */
626*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, append_ap);
627*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, append_fmt);
628*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, clear);
629*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, prepend_ap);
630*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, prepend_fmt);
631*11be35a1SLionel Sambuc
632*11be35a1SLionel Sambuc /* Operators. */
633*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, equal_cstring);
634*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, equal_dynstr);
635*11be35a1SLionel Sambuc
636*11be35a1SLionel Sambuc return atf_no_error();
637*11be35a1SLionel Sambuc }
638