2 * Copyright (c) 2011 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 __FBSDID("$FreeBSD$");
28 #define __LIBARCHIVE_TEST
29 #include "archive_string.h"
33 #define assertStringSizes(strlen, buflen, as) \
34 assertEqualInt(strlen, (as).length); \
35 assertEqualInt(buflen, (as).buffer_length);
37 #define assertExactString(strlen, buflen, data, as) \
39 assertStringSizes(strlen, buflen, as); \
40 assertEqualString(data, (as).s); \
43 #define assertNonNULLString(strlen, buflen, as) \
45 assertStringSizes(strlen, buflen, as); \
46 assert(NULL != (as).s); \
50 test_archive_string_ensure(void)
52 struct archive_string s;
54 archive_string_init(&s);
55 assertExactString(0, 0, NULL, s);
57 /* single-extent allocation */
58 assert(&s == archive_string_ensure(&s, 5));
59 assertNonNULLString(0, EXTENT, s);
61 /* what happens around extent boundaries? */
62 assert(&s == archive_string_ensure(&s, EXTENT - 1));
63 assertNonNULLString(0, EXTENT, s);
65 assert(&s == archive_string_ensure(&s, EXTENT));
66 assertNonNULLString(0, EXTENT, s);
68 assert(&s == archive_string_ensure(&s, EXTENT + 1));
69 assertNonNULLString(0, 2 * EXTENT, s);
73 test_archive_strcat(void)
75 struct archive_string s;
77 archive_string_init(&s);
78 assertExactString(0, 0, NULL, s);
80 /* null target, empty source */
81 assert(&s == archive_strcat(&s, ""));
82 assertExactString(0, EXTENT, "", s);
84 /* empty target, empty source */
85 assert(&s == archive_strcat(&s, ""));
86 assertExactString(0, EXTENT, "", s);
88 /* empty target, non-empty source */
89 assert(&s == archive_strcat(&s, "fubar"));
90 assertExactString(5, EXTENT, "fubar", s);
92 /* non-empty target, non-empty source */
93 assert(&s == archive_strcat(&s, "baz"));
94 assertExactString(8, EXTENT, "fubarbaz", s);
98 test_archive_strappend_char(void)
100 struct archive_string s;
102 archive_string_init(&s);
103 assertExactString(0, 0, NULL, s);
106 archive_strappend_char(&s, 'X');
107 assertExactString(1, EXTENT, "X", s);
109 /* non-empty target */
110 archive_strappend_char(&s, 'Y');
111 assertExactString(2, EXTENT, "XY", s);
114 /* archive_strnXXX() tests focus on length handling.
115 * other behaviors are tested by proxy through archive_strXXX()
119 test_archive_strncat(void)
121 struct archive_string s;
123 archive_string_init(&s);
124 assertExactString(0, 0, NULL, s);
127 assert(&s == archive_strncat(&s, "snafu", 5));
128 assertExactString(5, EXTENT, "snafu", s);
131 assert(&s == archive_strncat(&s, "barbazqux", 3));
132 assertExactString(8, EXTENT, "snafubar", s);
134 /* long read is ok too! */
135 assert(&s == archive_strncat(&s, "snafu", 8));
136 assertExactString(13, EXTENT, "snafubarsnafu", s);
140 test_archive_strncpy(void)
142 struct archive_string s;
144 archive_string_init(&s);
145 assertExactString(0, 0, NULL, s);
148 assert(&s == archive_strncpy(&s, "fubar", 5));
149 assertExactString(5, EXTENT, "fubar", s);
152 assert(&s == archive_strncpy(&s, "snafubar", 5));
153 assertExactString(5, EXTENT, "snafu", s);
155 /* long read is ok too! */
156 assert(&s == archive_strncpy(&s, "snafu", 8));
157 assertExactString(5, EXTENT, "snafu", s);
161 test_archive_strcpy(void)
163 struct archive_string s;
165 archive_string_init(&s);
166 assertExactString(0, 0, NULL, s);
169 assert(&s == archive_strcpy(&s, "snafu"));
170 assertExactString(5, EXTENT, "snafu", s);
173 assert(&s == archive_strcpy(&s, "foo"));
174 assertExactString(3, EXTENT, "foo", s);
176 /* dirty target, empty source */
177 assert(&s == archive_strcpy(&s, ""));
178 assertExactString(0, EXTENT, "", s);
182 test_archive_string_concat(void)
184 struct archive_string s, t, u, v;
186 archive_string_init(&s);
187 assertExactString(0, 0, NULL, s);
188 archive_string_init(&t);
189 assertExactString(0, 0, NULL, t);
190 archive_string_init(&u);
191 assertExactString(0, 0, NULL, u);
192 archive_string_init(&v);
193 assertExactString(0, 0, NULL, v);
195 /* null target, null source */
196 archive_string_concat(&t, &s);
197 assertExactString(0, 0, NULL, s);
198 assertExactString(0, EXTENT, "", t);
200 /* null target, empty source */
201 assert(&s == archive_strcpy(&s, ""));
202 archive_string_concat(&u, &s);
203 assertExactString(0, EXTENT, "", s);
204 assertExactString(0, EXTENT, "", u);
206 /* null target, non-empty source */
207 assert(&s == archive_strcpy(&s, "foo"));
208 archive_string_concat(&v, &s);
209 assertExactString(3, EXTENT, "foo", s);
210 assertExactString(3, EXTENT, "foo", v);
212 /* empty target, empty source */
213 assert(&s == archive_strcpy(&s, ""));
214 assert(&t == archive_strcpy(&t, ""));
215 archive_string_concat(&t, &s);
216 assertExactString(0, EXTENT, "", s);
217 assertExactString(0, EXTENT, "", t);
219 /* empty target, non-empty source */
220 assert(&s == archive_strcpy(&s, "snafu"));
221 assert(&t == archive_strcpy(&t, ""));
222 archive_string_concat(&t, &s);
223 assertExactString(5, EXTENT, "snafu", s);
224 assertExactString(5, EXTENT, "snafu", t);
228 test_archive_string_copy(void)
230 struct archive_string s, t, u, v;
232 archive_string_init(&s);
233 assertExactString(0, 0, NULL, s);
234 archive_string_init(&t);
235 assertExactString(0, 0, NULL, t);
236 archive_string_init(&u);
237 assertExactString(0, 0, NULL, u);
238 archive_string_init(&v);
239 assertExactString(0, 0, NULL, v);
241 /* null target, null source */
242 archive_string_copy(&t, &s);
243 assertExactString(0, 0, NULL, s);
244 assertExactString(0, EXTENT, "", t);
246 /* null target, empty source */
247 archive_string_copy(&u, &t);
248 assertExactString(0, EXTENT, "", t);
249 assertExactString(0, EXTENT, "", u);
251 /* empty target, empty source */
252 archive_string_copy(&u, &t);
253 assertExactString(0, EXTENT, "", t);
254 assertExactString(0, EXTENT, "", u);
256 /* null target, non-empty source */
257 assert(NULL != archive_strcpy(&s, "snafubar"));
258 assertExactString(8, EXTENT, "snafubar", s);
260 archive_string_copy(&v, &s);
261 assertExactString(8, EXTENT, "snafubar", s);
262 assertExactString(8, EXTENT, "snafubar", v);
264 /* empty target, non-empty source */
265 assertExactString(0, EXTENT, "", t);
266 archive_string_copy(&t, &s);
267 assertExactString(8, EXTENT, "snafubar", s);
268 assertExactString(8, EXTENT, "snafubar", t);
270 /* non-empty target, non-empty source */
271 assert(NULL != archive_strcpy(&s, "fubar"));
272 assertExactString(5, EXTENT, "fubar", s);
274 archive_string_copy(&t, &s);
275 assertExactString(5, EXTENT, "fubar", s);
276 assertExactString(5, EXTENT, "fubar", t);
280 test_archive_string_sprintf(void)
282 struct archive_string s;
283 #define S16 "0123456789abcdef"
287 const char *s32 = S32;
288 const char *s33 = S32 "0";
289 const char *s64 = S64;
290 const char *s65 = S64 "0";
291 const char *s128 = S128;
292 const char *s129 = S128 "0";
298 archive_string_init(&s);
299 assertExactString(0, 0, NULL, s);
301 archive_string_sprintf(&s, "%s", "");
302 assertExactString(0, 2 * EXTENT, "", s);
304 archive_string_empty(&s);
305 archive_string_sprintf(&s, "%s", s32);
306 assertExactString(32, 2 * EXTENT, s32, s);
308 archive_string_empty(&s);
309 archive_string_sprintf(&s, "%s", s33);
310 assertExactString(33, 2 * EXTENT, s33, s);
312 archive_string_empty(&s);
313 archive_string_sprintf(&s, "%s", s64);
314 assertExactString(64, 4 * EXTENT, s64, s);
316 archive_string_empty(&s);
317 archive_string_sprintf(&s, "%s", s65);
318 assertExactString(65, 4 * EXTENT, s65, s);
320 archive_string_empty(&s);
321 archive_string_sprintf(&s, "%s", s128);
322 assertExactString(128, 8 * EXTENT, s128, s);
324 archive_string_empty(&s);
325 archive_string_sprintf(&s, "%s", s129);
326 assertExactString(129, 8 * EXTENT, s129, s);
328 archive_string_empty(&s);
329 archive_string_sprintf(&s, "%d", 1234567890);
330 assertExactString(10, 8 * EXTENT, "1234567890", s);
333 DEFINE_TEST(test_archive_string)
335 test_archive_string_ensure();
336 test_archive_strcat();
337 test_archive_strappend_char();
338 test_archive_strncat();
339 test_archive_strncpy();
340 test_archive_strcpy();
341 test_archive_string_concat();
342 test_archive_string_copy();
343 test_archive_string_sprintf();