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);
71 archive_string_free(&s);
75 test_archive_strcat(void)
77 struct archive_string s;
79 archive_string_init(&s);
80 assertExactString(0, 0, NULL, s);
82 /* null target, empty source */
83 assert(&s == archive_strcat(&s, ""));
84 assertExactString(0, EXTENT, "", s);
86 /* empty target, empty source */
87 assert(&s == archive_strcat(&s, ""));
88 assertExactString(0, EXTENT, "", s);
90 /* empty target, non-empty source */
91 assert(&s == archive_strcat(&s, "fubar"));
92 assertExactString(5, EXTENT, "fubar", s);
94 /* non-empty target, non-empty source */
95 assert(&s == archive_strcat(&s, "baz"));
96 assertExactString(8, EXTENT, "fubarbaz", s);
98 archive_string_free(&s);
102 test_archive_strappend_char(void)
104 struct archive_string s;
106 archive_string_init(&s);
107 assertExactString(0, 0, NULL, s);
110 archive_strappend_char(&s, 'X');
111 assertExactString(1, EXTENT, "X", s);
113 /* non-empty target */
114 archive_strappend_char(&s, 'Y');
115 assertExactString(2, EXTENT, "XY", s);
117 archive_string_free(&s);
120 /* archive_strnXXX() tests focus on length handling.
121 * other behaviors are tested by proxy through archive_strXXX()
125 test_archive_strncat(void)
127 struct archive_string s;
129 archive_string_init(&s);
130 assertExactString(0, 0, NULL, s);
133 assert(&s == archive_strncat(&s, "snafu", 5));
134 assertExactString(5, EXTENT, "snafu", s);
137 assert(&s == archive_strncat(&s, "barbazqux", 3));
138 assertExactString(8, EXTENT, "snafubar", s);
140 /* long read is ok too! */
141 assert(&s == archive_strncat(&s, "snafu", 8));
142 assertExactString(13, EXTENT, "snafubarsnafu", s);
144 archive_string_free(&s);
148 test_archive_strncpy(void)
150 struct archive_string s;
152 archive_string_init(&s);
153 assertExactString(0, 0, NULL, s);
156 assert(&s == archive_strncpy(&s, "fubar", 5));
157 assertExactString(5, EXTENT, "fubar", s);
160 assert(&s == archive_strncpy(&s, "snafubar", 5));
161 assertExactString(5, EXTENT, "snafu", s);
163 /* long read is ok too! */
164 assert(&s == archive_strncpy(&s, "snafu", 8));
165 assertExactString(5, EXTENT, "snafu", s);
167 archive_string_free(&s);
171 test_archive_strcpy(void)
173 struct archive_string s;
175 archive_string_init(&s);
176 assertExactString(0, 0, NULL, s);
179 assert(&s == archive_strcpy(&s, "snafu"));
180 assertExactString(5, EXTENT, "snafu", s);
183 assert(&s == archive_strcpy(&s, "foo"));
184 assertExactString(3, EXTENT, "foo", s);
186 /* dirty target, empty source */
187 assert(&s == archive_strcpy(&s, ""));
188 assertExactString(0, EXTENT, "", s);
190 archive_string_free(&s);
194 test_archive_string_concat(void)
196 struct archive_string s, t, u, v;
198 archive_string_init(&s);
199 assertExactString(0, 0, NULL, s);
200 archive_string_init(&t);
201 assertExactString(0, 0, NULL, t);
202 archive_string_init(&u);
203 assertExactString(0, 0, NULL, u);
204 archive_string_init(&v);
205 assertExactString(0, 0, NULL, v);
207 /* null target, null source */
208 archive_string_concat(&t, &s);
209 assertExactString(0, 0, NULL, s);
210 assertExactString(0, EXTENT, "", t);
212 /* null target, empty source */
213 assert(&s == archive_strcpy(&s, ""));
214 archive_string_concat(&u, &s);
215 assertExactString(0, EXTENT, "", s);
216 assertExactString(0, EXTENT, "", u);
218 /* null target, non-empty source */
219 assert(&s == archive_strcpy(&s, "foo"));
220 archive_string_concat(&v, &s);
221 assertExactString(3, EXTENT, "foo", s);
222 assertExactString(3, EXTENT, "foo", v);
224 /* empty target, empty source */
225 assert(&s == archive_strcpy(&s, ""));
226 assert(&t == archive_strcpy(&t, ""));
227 archive_string_concat(&t, &s);
228 assertExactString(0, EXTENT, "", s);
229 assertExactString(0, EXTENT, "", t);
231 /* empty target, non-empty source */
232 assert(&s == archive_strcpy(&s, "snafu"));
233 assert(&t == archive_strcpy(&t, ""));
234 archive_string_concat(&t, &s);
235 assertExactString(5, EXTENT, "snafu", s);
236 assertExactString(5, EXTENT, "snafu", t);
238 archive_string_free(&v);
239 archive_string_free(&u);
240 archive_string_free(&t);
241 archive_string_free(&s);
245 test_archive_string_copy(void)
247 struct archive_string s, t, u, v;
249 archive_string_init(&s);
250 assertExactString(0, 0, NULL, s);
251 archive_string_init(&t);
252 assertExactString(0, 0, NULL, t);
253 archive_string_init(&u);
254 assertExactString(0, 0, NULL, u);
255 archive_string_init(&v);
256 assertExactString(0, 0, NULL, v);
258 /* null target, null source */
259 archive_string_copy(&t, &s);
260 assertExactString(0, 0, NULL, s);
261 assertExactString(0, EXTENT, "", t);
263 /* null target, empty source */
264 archive_string_copy(&u, &t);
265 assertExactString(0, EXTENT, "", t);
266 assertExactString(0, EXTENT, "", u);
268 /* empty target, empty source */
269 archive_string_copy(&u, &t);
270 assertExactString(0, EXTENT, "", t);
271 assertExactString(0, EXTENT, "", u);
273 /* null target, non-empty source */
274 assert(NULL != archive_strcpy(&s, "snafubar"));
275 assertExactString(8, EXTENT, "snafubar", s);
277 archive_string_copy(&v, &s);
278 assertExactString(8, EXTENT, "snafubar", s);
279 assertExactString(8, EXTENT, "snafubar", v);
281 /* empty target, non-empty source */
282 assertExactString(0, EXTENT, "", t);
283 archive_string_copy(&t, &s);
284 assertExactString(8, EXTENT, "snafubar", s);
285 assertExactString(8, EXTENT, "snafubar", t);
287 /* non-empty target, non-empty source */
288 assert(NULL != archive_strcpy(&s, "fubar"));
289 assertExactString(5, EXTENT, "fubar", s);
291 archive_string_copy(&t, &s);
292 assertExactString(5, EXTENT, "fubar", s);
293 assertExactString(5, EXTENT, "fubar", t);
295 archive_string_free(&v);
296 archive_string_free(&u);
297 archive_string_free(&t);
298 archive_string_free(&s);
302 test_archive_string_sprintf(void)
304 struct archive_string s;
305 #define S16 "0123456789abcdef"
309 const char *s32 = S32;
310 const char *s33 = S32 "0";
311 const char *s64 = S64;
312 const char *s65 = S64 "0";
313 const char *s128 = S128;
314 const char *s129 = S128 "0";
320 archive_string_init(&s);
321 assertExactString(0, 0, NULL, s);
323 archive_string_sprintf(&s, "%s", "");
324 assertExactString(0, 2 * EXTENT, "", s);
326 archive_string_empty(&s);
327 archive_string_sprintf(&s, "%s", s32);
328 assertExactString(32, 2 * EXTENT, s32, s);
330 archive_string_empty(&s);
331 archive_string_sprintf(&s, "%s", s33);
332 assertExactString(33, 2 * EXTENT, s33, s);
334 archive_string_empty(&s);
335 archive_string_sprintf(&s, "%s", s64);
336 assertExactString(64, 4 * EXTENT, s64, s);
338 archive_string_empty(&s);
339 archive_string_sprintf(&s, "%s", s65);
340 assertExactString(65, 4 * EXTENT, s65, s);
342 archive_string_empty(&s);
343 archive_string_sprintf(&s, "%s", s128);
344 assertExactString(128, 8 * EXTENT, s128, s);
346 archive_string_empty(&s);
347 archive_string_sprintf(&s, "%s", s129);
348 assertExactString(129, 8 * EXTENT, s129, s);
350 archive_string_empty(&s);
351 archive_string_sprintf(&s, "%d", 1234567890);
352 assertExactString(10, 8 * EXTENT, "1234567890", s);
354 archive_string_free(&s);
357 DEFINE_TEST(test_archive_string)
359 test_archive_string_ensure();
360 test_archive_strcat();
361 test_archive_strappend_char();
362 test_archive_strncat();
363 test_archive_strncpy();
364 test_archive_strcpy();
365 test_archive_string_concat();
366 test_archive_string_copy();
367 test_archive_string_sprintf();
370 static const char *strings[] =
402 DEFINE_TEST(test_archive_string_sort)
404 unsigned int i, j, size;
405 char **test_strings, *tmp;
407 srand((unsigned int)time(NULL));
408 size = sizeof(strings) / sizeof(char *);
409 assert((test_strings = (char **)calloc(1, sizeof(strings))) != NULL);
410 for (i = 0; i < (size - 1); i++)
411 assert((test_strings[i] = strdup(strings[i])) != NULL);
413 /* Shuffle the test strings */
414 for (i = 0; i < (size - 1); i++)
416 j = rand() % ((size - 1) - i);
418 tmp = test_strings[i];
419 test_strings[i] = test_strings[j];
420 test_strings[j] = tmp;
424 assertEqualInt(ARCHIVE_OK, archive_utility_string_sort(test_strings));
425 for (i = 0; i < (size - 1); i++)
426 assertEqualString(test_strings[i], strings[i]);
428 for (i = 0; i < (size - 1); i++)
429 free(test_strings[i]);