Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/string.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pj/string.h>
20 #include <pj/assert.h>
21 #include <pj/errno.h>
22 #include <pj/pool.h>
23 #include <pj/log.h>
24 #include <pj/os.h>
25 #include "test.h"
26
27 #define THIS_FILE "string.c"
28
29 /**
30 * \page page_pjlib_string_test Test: String
31 *
32 * This file provides implementation of \b string_test(). It tests the
33 * functionality of the string API.
34 *
35 * \section sleep_test_sec Scope of the Test
36 *
37 * API tested:
38 * - pj_str()
39 * - pj_strcmp()
40 * - pj_strcmp2()
41 * - pj_stricmp()
42 * - pj_strlen()
43 * - pj_strncmp()
44 * - pj_strnicmp()
45 * - pj_strchr()
46 * - pj_strdup()
47 * - pj_strdup2()
48 * - pj_strcpy()
49 * - pj_strcat()
50 * - pj_strtrim()
51 * - pj_utoa()
52 * - pj_strtoul()
53 * - pj_strtoul2()
54 * - pj_create_random_string()
55 * - ... and mode..
56 *
57 * This file is <b>pjlib-test/string.c</b>
58 *
59 * \include pjlib-test/string.c
60 */
61
62 #if INCLUDE_STRING_TEST
63
64 #ifdef _MSC_VER
65 # pragma warning(disable: 4204)
66 #endif
67
68 #define HELLO_WORLD "Hello World"
69 #define HELLO_WORLD_LEN 11
70 #define JUST_HELLO "Hello"
71 #define JUST_HELLO_LEN 5
72 #define UL_VALUE 3456789012UL
73
74 #if 1
75 /* See if both integers have the same sign */
76 PJ_INLINE(int) cmp(const char *expr, int i, int j)
77 {
78 int r = !((i>0 && j>0) || (i<0 && j<0) || (i==0 && j==0));
79 if (r) {
80 PJ_LOG(3,(THIS_FILE," error: %s: expecting %d, got %d", expr, j, i));
81 }
82 return r;
83 }
84 #else
85 /* For strict comparison, must be equal */
86 PJ_INLINE(int) cmp(const char *expr, int i, int j)
87 {
88 PJ_UNUSED_ARG(expr);
89 return i!=j;
90 }
91 #endif
92
93 #define C(expr, res) cmp(#expr, expr, res)
94
95 static int stricmp_test(void)
96 {
97 /* This specificly tests and benchmark pj_stricmp(), pj_stricmp_alnum().
98 * In addition, it also tests pj_stricmp2(), pj_strnicmp(), and
99 * pj_strnicmp2().
100 */
101 #define STRTEST(res,res2,S1,S2,code) \
102 do { \
103 s1.ptr=S1; s1.slen=(S1)?len:0; \
104 s2.ptr=S2; s2.slen=(S2)?len:0; \
105 pj_get_timestamp(&t1); \
106 if (C(pj_stricmp(&s1,&s2),res)) return code; \
107 pj_get_timestamp(&t2); \
108 pj_sub_timestamp(&t2, &t1); \
109 pj_add_timestamp(&e1, &t2); \
110 pj_get_timestamp(&t1); \
111 if (C(pj_stricmp_alnum(&s1,&s2),res)) return code-1; \
112 pj_get_timestamp(&t2); \
113 pj_sub_timestamp(&t2, &t1); \
114 pj_add_timestamp(&e2, &t2); \
115 if (C(pj_stricmp2(&s1,S2),res2)) return code*10; \
116 if (C(pj_strnicmp(&s1,&s2,len),res)) return code*100; \
117 if (C(pj_strnicmp2(&s1,S2,len),res)) return code*1000; \
118 } while (0)
119
120 char *buf;
121 pj_str_t s1, s2;
122 pj_timestamp t1, t2, e1, e2, zero;
123 pj_uint32_t c1, c2;
124 int len;
125
126 e1.u32.hi = e1.u32.lo = e2.u32.hi = e2.u32.lo = 0;
127
128 pj_thread_sleep(0);
129
130 #define SNULL 0
131
132 /* Compare empty strings. */
133 len=0;
134 STRTEST( 0, 0, "","",-500);
135 STRTEST( 0, 0, SNULL,"",-502);
136 STRTEST( 0, 0, "",SNULL,-504);
137 STRTEST( 0, 0, SNULL,SNULL,-506);
138 STRTEST( 0, -1, "hello","world",-508);
139
140 /* equal, length=1
141 * use buffer to simulate non-aligned string.
142 */
143 buf = "a""A";
144 len=1;
145 STRTEST( 0, -1, "a",buf+0,-510);
146 STRTEST( 0, 0, "a",buf+1,-512);
147 STRTEST(-1, -1, "O", "P", -514);
148 STRTEST(-1, -1, SNULL, "a", -516);
149 STRTEST( 1, 1, "a", SNULL, -518);
150
151 /* equal, length=2
152 * use buffer to simulate non-aligned string.
153 */
154 buf = "aa""Aa""aA""AA";
155 len=2;
156 STRTEST( 0, -1, "aa",buf+0,-520);
157 STRTEST( 0, -1, "aa",buf+2,-522);
158 STRTEST( 0, -1, "aa",buf+4,-524);
159 STRTEST( 0, 0, "aa",buf+6,-524);
160
161 /* equal, length=3
162 * use buffer to simulate non-aligned string.
163 */
164 buf = "aaa""Aaa""aAa""aaA""AAa""aAA""AaA""AAA";
165 len=3;
166 STRTEST( 0, -1, "aaa",buf+0,-530);
167 STRTEST( 0, -1, "aaa",buf+3,-532);
168 STRTEST( 0, -1, "aaa",buf+6,-534);
169 STRTEST( 0, -1, "aaa",buf+9,-536);
170 STRTEST( 0, -1, "aaa",buf+12,-538);
171 STRTEST( 0, -1, "aaa",buf+15,-540);
172 STRTEST( 0, -1, "aaa",buf+18,-542);
173 STRTEST( 0, 0, "aaa",buf+21,-534);
174
175 /* equal, length=4 */
176 len=4;
177 STRTEST( 0, 0, "aaaa","aaaa",-540);
178 STRTEST( 0, 0, "aaaa","Aaaa",-542);
179 STRTEST( 0, 0, "aaaa","aAaa",-544);
180 STRTEST( 0, 0, "aaaa","aaAa",-546);
181 STRTEST( 0, 0, "aaaa","aaaA",-548);
182 STRTEST( 0, 0, "aaaa","AAaa",-550);
183 STRTEST( 0, 0, "aaaa","aAAa",-552);
184 STRTEST( 0, 0, "aaaa","aaAA",-554);
185 STRTEST( 0, 0, "aaaa","AaAa",-556);
186 STRTEST( 0, 0, "aaaa","aAaA",-558);
187 STRTEST( 0, 0, "aaaa","AaaA",-560);
188 STRTEST( 0, 0, "aaaa","AAAa",-562);
189 STRTEST( 0, 0, "aaaa","aAAA",-564);
190 STRTEST( 0, 0, "aaaa","AAaA",-566);
191 STRTEST( 0, 0, "aaaa","AaAA",-568);
192 STRTEST( 0, 0, "aaaa","AAAA",-570);
193
194 /* equal, length=5 */
195 buf = "aaaAa""AaaaA""AaAaA""AAAAA";
196 len=5;
197 STRTEST( 0, -1, "aaaaa",buf+0,-580);
198 STRTEST( 0, -1, "aaaaa",buf+5,-582);
199 STRTEST( 0, -1, "aaaaa",buf+10,-584);
200 STRTEST( 0, 0, "aaaaa",buf+15,-586);
201
202 /* not equal, length=1 */
203 len=1;
204 STRTEST( -1, -1, "a", "b", -600);
205
206 /* not equal, length=2 */
207 buf = "ab""ba";
208 len=2;
209 STRTEST( -1, -1, "aa", buf+0, -610);
210 STRTEST( -1, -1, "aa", buf+2, -612);
211
212 /* not equal, length=3 */
213 buf = "aab""aba""baa";
214 len=3;
215 STRTEST( -1, -1, "aaa", buf+0, -620);
216 STRTEST( -1, -1, "aaa", buf+3, -622);
217 STRTEST( -1, -1, "aaa", buf+6, -624);
218
219 /* not equal, length=4 */
220 buf = "aaab""aaba""abaa""baaa";
221 len=4;
222 STRTEST( -1, -1, "aaaa", buf+0, -630);
223 STRTEST( -1, -1, "aaaa", buf+4, -632);
224 STRTEST( -1, -1, "aaaa", buf+8, -634);
225 STRTEST( -1, -1, "aaaa", buf+12, -636);
226
227 /* not equal, length=5 */
228 buf="aaaab""aaaba""aabaa""abaaa""baaaa";
229 len=5;
230 STRTEST( -1, -1, "aaaaa", buf+0, -640);
231 STRTEST( -1, -1, "aaaaa", buf+5, -642);
232 STRTEST( -1, -1, "aaaaa", buf+10, -644);
233 STRTEST( -1, -1, "aaaaa", buf+15, -646);
234 STRTEST( -1, -1, "aaaaa", buf+20, -648);
235
236 zero.u32.hi = zero.u32.lo = 0;
237 c1 = pj_elapsed_cycle(&zero, &e1);
238 c2 = pj_elapsed_cycle(&zero, &e2);
239
240 if (c1 < c2) {
241 PJ_LOG(3,("", " info: pj_stricmp_alnum is slower than pj_stricmp!"));
242 //return -700;
243 }
244
245 /* Avoid division by zero */
246 if (c2 == 0) c2=1;
247
248 PJ_LOG(3, ("", " time: stricmp=%u, stricmp_alnum=%u (speedup=%d.%02dx)",
249 c1, c2,
250 (c1 * 100 / c2) / 100,
251 (c1 * 100 / c2) % 100));
252 return 0;
253 #undef STRTEST
254 }
255
256 /* This tests pj_strcmp(), pj_strcmp2(), pj_strncmp(), pj_strncmp2() */
257 static int strcmp_test(void)
258 {
259 #define STR_TEST(res,S1,S2,code) \
260 do { \
261 s1.ptr=S1; s1.slen=S1?len:0; \
262 s2.ptr=S2; s2.slen=S2?len:0; \
263 if (C(pj_strcmp(&s1,&s2),res)) return code; \
264 if (C(pj_strcmp2(&s1,S2),res)) return code-1; \
265 if (C(pj_strncmp(&s1,&s2,len),res)) return code-2; \
266 if (C(pj_strncmp2(&s1,S2,len),res)) return code-3; \
267 } while (0)
268
269 pj_str_t s1, s2;
270 int len;
271
272 /* Test with length == 0 */
273 len=0;
274 STR_TEST(0, "", "", -400);
275 STR_TEST(0, SNULL, "", -405);
276 STR_TEST(0, "", SNULL, -410);
277 STR_TEST(0, SNULL, SNULL, -415);
278 STR_TEST(0, "hello", "", -420);
279 STR_TEST(0, "hello", SNULL, -425);
280
281 /* Test with length != 0 */
282 len = 2;
283 STR_TEST(0, "12", "12", -430);
284 STR_TEST(1, "12", "1", -435);
285 STR_TEST(-1, "1", "12", -440);
286 STR_TEST(-1, SNULL, "12", -445);
287 STR_TEST(1, "12", SNULL, -450);
288
289 return 0;
290
291 #undef STR_TEST
292 }
293
294 static int verify_strxcpy(const char *src, int dst_size, int exp_ret,
295 const char *exp_dst)
296 {
297 char dst[6];
298 const char GUARDS[2] = {'@', '\0'};
299 int i, ig, ret;
300
301 PJ_ASSERT_RETURN(src && dst_size <= 5, -700);
302
303 for (ig=0; ig<(int)sizeof(GUARDS); ++ig) {
304 char GUARD = GUARDS[ig];
305
306 memset(dst, GUARD, sizeof(dst));
307
308 ret = pj_ansi_strxcpy(dst, src, dst_size);
309
310 /* verify return value */
311 if (ret != exp_ret) {
312 PJ_LOG(3,("", " strxcpy \"%s\", dst_size=%d: ret %d != %d",
313 src, dst_size, ret, exp_ret));
314 return -704;
315 }
316
317 /* expected dst content */
318 if (exp_dst) {
319 if (strcmp(dst, exp_dst)) {
320 PJ_LOG(3,("", " strxcpy \"%s\", dst_size=%d: "
321 "dst content mismatch: \"%s\"!=\"%s\"",
322 src, dst_size, dst, exp_dst));
323 return -708;
324 }
325 }
326
327 /* verify not writing pass buffer */
328 for (i=exp_dst?(int)strlen(exp_dst)+1:0; i<(int)sizeof(dst); ++i) {
329 if (dst[i] != GUARD) {
330 PJ_LOG(3,("", " strxcpy \"%s\", dst_size=%d: overflow at %d",
331 src, dst_size, i));
332 return -710;
333 }
334 }
335 }
336
337 return 0;
338 }
339
340 static int strxcpy_test(void)
341 {
342 int rc;
343 #define CHECK_(src, dst_size, exp_ret, exp_dst) \
344 rc = verify_strxcpy(src, dst_size, exp_ret, exp_dst); \
345 if (rc) return rc
346
347 CHECK_( "", 0, -PJ_ETOOBIG, NULL);
348 CHECK_( "a", 0, -PJ_ETOOBIG, NULL);
349
350 {
351 /* special test 1 (dst contains null) */
352 char dst[4];
353 pj_bzero(dst, sizeof(dst));
354 rc = pj_ansi_strxcpy(dst, "a", 1);
355 if (rc != -PJ_ETOOBIG) {
356 PJ_LOG(3,("", " pj_ansi_strxcpy special test 1: ret %d!=%d",
357 rc, -PJ_ETOOBIG));
358 return -700;
359 }
360 }
361
362 CHECK_( "", 1, 0, "");
363 CHECK_( "a", 1, -PJ_ETOOBIG, "");
364 CHECK_( "ab", 1, -PJ_ETOOBIG, "");
365 CHECK_( "abcd", 1, -PJ_ETOOBIG, "");
366
367 CHECK_( "abc", 2, -PJ_ETOOBIG, "a");
368 CHECK_( "ab", 2, -PJ_ETOOBIG, "a");
369 CHECK_( "a", 2, 1, "a");
370 CHECK_( "", 2, 0, "");
371
372 CHECK_( "abcd", 3, -PJ_ETOOBIG, "ab");
373 CHECK_( "abc", 3, -PJ_ETOOBIG, "ab");
374 CHECK_( "ab", 3, 2, "ab");
375 CHECK_( "a", 3, 1, "a");
376 CHECK_( "", 3, 0, "");
377
378 CHECK_( "abcde", 4, -PJ_ETOOBIG, "abc");
379 CHECK_( "abcd", 4, -PJ_ETOOBIG, "abc");
380 CHECK_( "abc", 4, 3, "abc");
381 CHECK_( "ab", 4, 2, "ab");
382 CHECK_( "a", 4, 1, "a");
383 CHECK_( "", 4, 0, "");
384
385 CHECK_( "abcdef", 5, -PJ_ETOOBIG, "abcd");
386 CHECK_( "abcde", 5, -PJ_ETOOBIG, "abcd");
387 CHECK_( "abcd", 5, 4, "abcd");
388 CHECK_( "abc", 5, 3, "abc");
389 CHECK_( "ab", 5, 2, "ab");
390 CHECK_( "a", 5, 1, "a");
391 CHECK_( "", 5, 0, "");
392
393 #undef CHECK_
394
395 return 0;
396 }
397
398 static int verify_strxcpy2(const pj_str_t *src, int dst_size, int exp_ret,
399 const char *exp_dst)
400 {
401 char dst[6];
402 const char GUARDS[2] = {'@', '\0'};
403 int i, ret, ig;
404
405 PJ_ASSERT_RETURN(src && dst_size <= 5, -720);
406
407 for (ig=0; ig<(int)sizeof(GUARDS); ++ig) {
408 char GUARD = GUARDS[ig];
409
410 memset(dst, GUARD, sizeof(dst));
411
412 ret = pj_ansi_strxcpy2(dst, src, dst_size);
413
414 /* verify return value */
415 if (ret != exp_ret) {
416 PJ_LOG(3,("", " strxcpy2 \"%.*s\" slen=%ld, dst_size=%d: "
417 "ret %d!=%d",
418 (int)src->slen, src->ptr, src->slen, dst_size,
419 ret, exp_ret));
420 return -724;
421 }
422
423 /* expected dst content */
424 if (exp_dst) {
425 if (strcmp(dst, exp_dst)) {
426 PJ_LOG(3,("", " strxcpy2 \"%.*s\" slen=%ld, dst_size=%d: "
427 "dst content mismatch: \"%s\"!=\"%s\"",
428 (int)src->slen, src->ptr, src->slen, dst_size, dst,
429 exp_dst));
430 return -726;
431 }
432 }
433
434 /* verify not writing pass buffer */
435 for (i=dst_size; i<(int)sizeof(dst); ++i) {
436 if (dst[i] != GUARD) {
437 PJ_LOG(3,("", " strxcpy2 \"%.*s\" slen=%ld, dst_size=%d: "
438 "overflow at %d (chr %d)",
439 (int)src->slen, src->ptr, src->slen, dst_size, i,
440 (char)(dst[i] & 0xFF)));
441 return -728;
442 }
443 }
444 }
445
446 return 0;
447 }
448
449
450 static int strxcpy2_test(void)
451 {
452 pj_str_t src;
453 char nulls[6];
454 int rc;
455
456 pj_bzero(nulls, sizeof(nulls));
457
458 #define CHECK2_(s, src_len, dst_size, exp_ret, exp_dst) \
459 src.ptr = s; src.slen = src_len; \
460 rc = verify_strxcpy2(&src, dst_size, exp_ret, exp_dst); \
461 if (rc) return rc
462
463 CHECK2_( NULL, 0, 0, -PJ_ETOOBIG, NULL);
464 CHECK2_( "a!", 1, 0, -PJ_ETOOBIG, NULL);
465
466 CHECK2_( "abc!", 3, 1, -PJ_ETOOBIG, "");
467 CHECK2_( "ab!", 2, 1, -PJ_ETOOBIG, "");
468
469 /* note for test below: although src contains null and the strlen
470 of result (i.e. dst) is zero, strxcpy2 would still return
471 -PJ_ETOOBIG because the required buffer size is assumed to
472 be 2 (one for the src->ptr content (although the content is a
473 null character), one for the null terminator)
474 */
475 CHECK2_( nulls, 1, 1, 0, "");
476 CHECK2_( "a!", 1, 1, -PJ_ETOOBIG, "");
477 CHECK2_( "a", 1, 1, -PJ_ETOOBIG, "");
478 CHECK2_( "", 0, 1, 0, "");
479 CHECK2_( NULL, 0, 1, 0, "");
480
481 CHECK2_( "abc", 3, 2, -PJ_ETOOBIG, "a");
482 CHECK2_( "ab", 2, 2, -PJ_ETOOBIG, "a");
483 CHECK2_( "a!", 1, 2, 1, "a");
484 CHECK2_( nulls, 1, 2, 0, "");
485 CHECK2_( "!", 0, 2, 0, "");
486 CHECK2_( NULL, 0, 2, 0, "");
487
488 CHECK2_( "abc", 3, 3, -PJ_ETOOBIG, "ab");
489 CHECK2_( "ab", 3, 3, 2, "ab");
490 CHECK2_( nulls, 3, 3, 0, "");
491 CHECK2_( "abc", 2, 3, 2, "ab");
492 CHECK2_( "ab", 2, 3, 2, "ab");
493 CHECK2_( "a", 2, 3, 1, "a");
494 CHECK2_( nulls, 2, 3, 0, "");
495 CHECK2_( "a", 1, 3, 1, "a");
496 CHECK2_( "", 1, 3, 0, "");
497 CHECK2_( "", 0, 3, 0, "");
498 CHECK2_( NULL, 0, 3, 0, "");
499
500 CHECK2_( "abcde",5, 4, -PJ_ETOOBIG, "abc");
501 CHECK2_( "abcd", 4, 4, -PJ_ETOOBIG, "abc");
502 CHECK2_( "abc", 4, 4, 3, "abc");
503 CHECK2_( "ab", 4, 4, 2, "ab");
504 CHECK2_( "a", 4, 4, 1, "a");
505 CHECK2_( nulls, 4, 4, 0, "");
506 CHECK2_( "abc", 3, 4, 3, "abc");
507 CHECK2_( "ab", 3, 4, 2, "ab");
508 CHECK2_( "ab", 2, 4, 2, "ab");
509 CHECK2_( "a", 2, 4, 1, "a");
510 CHECK2_( "", 2, 4, 0, "");
511 CHECK2_( nulls, 2, 4, 0, "");
512 CHECK2_( "a", 1, 4, 1, "a");
513 CHECK2_( nulls, 1, 4, 0, "");
514 CHECK2_( "a", 0, 4, 0, "");
515 CHECK2_( "", 0, 4, 0, "");
516 CHECK2_( NULL, 0, 4, 0, "");
517
518 #undef CHECK2_
519
520 return 0;
521 }
522
523
524 static int verify_strxcat(const char *cdst, const char *src, int dst_size,
525 int exp_ret, const char *exp_dst)
526 {
527 char dst[6];
528 const char GUARDS[2] = {'@', '\0'};
529 int i, ret, ig;
530
531 PJ_ASSERT_RETURN(src && strlen(cdst) <= 4, -730);
532 PJ_ASSERT_RETURN((int)strlen(cdst) < dst_size ||
533 (strlen(cdst)==0 && dst_size==0), -731);
534
535 for (ig=0; ig<(int)sizeof(GUARDS); ++ig) {
536 char GUARD = GUARDS[ig];
537
538 memset(dst, GUARD, sizeof(dst));
539 if (dst_size) {
540 ret = pj_ansi_strxcpy(dst, cdst, dst_size);
541 PJ_ASSERT_RETURN(ret==(int)strlen(cdst), -732);
542 }
543
544 ret = pj_ansi_strxcat(dst, src, dst_size);
545
546 /* verify return value */
547 if (ret != exp_ret) {
548 PJ_LOG(3,("", " strxcat \"%s\", \"%s\", dst_size=%d: ret %d!=%d",
549 cdst, src, dst_size, ret, exp_ret));
550 return -734;
551 }
552
553 /* expected dst content */
554 if (exp_dst) {
555 if (strcmp(dst, exp_dst)) {
556 PJ_LOG(3,("", " strxcat \"%s\", \"%s\", dst_size=%d: "
557 "dst content mismatch: \"%s\"!=\"%s\"",
558 cdst, src, dst_size, dst, exp_dst));
559 return -736;
560 }
561 }
562
563 /* verify not writing past buffer */
564 for (i=exp_dst?(int)strlen(exp_dst)+1:0; i<(int)sizeof(dst); ++i) {
565 if (dst[i] != GUARD) {
566 PJ_LOG(3,("", " strxcat \"%s\", \"%s\", dst_size=%d: "
567 "overflow at %d",
568 cdst, src, dst_size, i));
569 return -738;
570 }
571 }
572 }
573
574 return 0;
575 }
576
577
578 static int strxcat_test(void)
579 {
580 int rc;
581 #define CHECK3_(dst, src, dst_size, exp_ret, exp_dst) \
582 rc = verify_strxcat(dst, src, dst_size, exp_ret, exp_dst); \
583 if (rc) return rc
584
585 CHECK3_( "", "", 0, -PJ_ETOOBIG, NULL);
586 CHECK3_( "", "a", 0, -PJ_ETOOBIG, NULL);
587
588 CHECK3_( "", "", 1, 0, "");
589 CHECK3_( "", "a", 1, -PJ_ETOOBIG, "");
590
591 CHECK3_( "", "a", 2, 1, "a");
592 CHECK3_( "", "ab", 2, -PJ_ETOOBIG, "a");
593 CHECK3_( "0", "", 2, 1, "0");
594 CHECK3_( "0", "a", 2, -PJ_ETOOBIG, "0");
595
596 CHECK3_( "", "a", 3, 1, "a");
597 CHECK3_( "", "ab", 3, 2, "ab");
598 CHECK3_( "", "abc", 3, -PJ_ETOOBIG, "ab");
599 CHECK3_( "0", "", 3, 1, "0");
600 CHECK3_( "0", "a", 3, 2, "0a");
601 CHECK3_( "0", "ab", 3, -PJ_ETOOBIG, "0a");
602 CHECK3_( "01", "", 3, 2, "01");
603 CHECK3_( "01", "a", 3, -PJ_ETOOBIG, "01");
604 CHECK3_( "01", "ab", 3, -PJ_ETOOBIG, "01");
605
606 CHECK3_( "", "a", 4, 1, "a");
607 CHECK3_( "", "ab", 4, 2, "ab");
608 CHECK3_( "", "abc", 4, 3, "abc");
609 CHECK3_( "", "abcd", 4, -PJ_ETOOBIG, "abc");
610 CHECK3_( "0", "", 4, 1, "0");
611 CHECK3_( "0", "a", 4, 2, "0a");
612 CHECK3_( "0", "ab", 4, 3, "0ab");
613 CHECK3_( "0", "abc", 4, -PJ_ETOOBIG, "0ab");
614 CHECK3_( "01", "", 4, 2, "01");
615 CHECK3_( "01", "a", 4, 3, "01a");
616 CHECK3_( "01", "ab", 4, -PJ_ETOOBIG, "01a");
617 CHECK3_( "01", "abc", 4, -PJ_ETOOBIG, "01a");
618 CHECK3_( "012", "", 4, 3, "012");
619 CHECK3_( "012", "a", 4, -PJ_ETOOBIG, "012");
620 CHECK3_( "012", "ab", 4, -PJ_ETOOBIG, "012");
621 CHECK3_( "012", "abc",4, -PJ_ETOOBIG, "012");
622
623 #undef CHECK3_
624 return 0;
625 }
626
627 int string_test(void)
628 {
629 const pj_str_t hello_world = { HELLO_WORLD, HELLO_WORLD_LEN };
630 const pj_str_t just_hello = { JUST_HELLO, JUST_HELLO_LEN };
631 pj_str_t s1, s2, s3, s4, s5;
632 enum { RCOUNT = 10, RLEN = 16 };
633 pj_str_t random[RCOUNT];
634 pj_pool_t *pool;
635 int i;
636
637 pool = pj_pool_create(mem, SNULL, 4096, 0, SNULL);
638 if (!pool) return -5;
639
640 /*
641 * pj_str(), pj_strcmp(), pj_stricmp(), pj_strlen(),
642 * pj_strncmp(), pj_strchr()
643 */
644 s1 = pj_str(HELLO_WORLD);
645 if (pj_strcmp(&s1, &hello_world) != 0)
646 return -10;
647 if (pj_stricmp(&s1, &hello_world) != 0)
648 return -20;
649 if (pj_strcmp(&s1, &just_hello) <= 0)
650 return -30;
651 if (pj_stricmp(&s1, &just_hello) <= 0)
652 return -40;
653 if (pj_strlen(&s1) != strlen(HELLO_WORLD))
654 return -50;
655 if (pj_strncmp(&s1, &hello_world, 5) != 0)
656 return -60;
657 if (pj_strnicmp(&s1, &hello_world, 5) != 0)
658 return -70;
659 if (pj_strchr(&s1, HELLO_WORLD[1]) != s1.ptr+1)
660 return -80;
661
662 /*
663 * pj_strdup()
664 */
665 if (!pj_strdup(pool, &s2, &s1))
666 return -100;
667 if (pj_strcmp(&s1, &s2) != 0)
668 return -110;
669
670 /*
671 * pj_strcpy(), pj_strcat()
672 */
673 s3.ptr = (char*) pj_pool_alloc(pool, 256);
674 if (!s3.ptr)
675 return -200;
676 pj_strcpy(&s3, &s2);
677 pj_strcat(&s3, &just_hello);
678
679 if (pj_strcmp2(&s3, HELLO_WORLD JUST_HELLO) != 0)
680 return -210;
681
682 /*
683 * pj_strdup2(), pj_strtrim().
684 */
685 pj_strdup2(pool, &s4, " " HELLO_WORLD "\t ");
686 pj_strtrim(&s4);
687 if (pj_strcmp2(&s4, HELLO_WORLD) != 0)
688 return -250;
689
690 /*
691 * pj_utoa()
692 */
693 s5.ptr = (char*) pj_pool_alloc(pool, 16);
694 if (!s5.ptr)
695 return -270;
696 s5.slen = pj_utoa(UL_VALUE, s5.ptr);
697
698 /*
699 * pj_strtoul()
700 */
701 if (pj_strtoul(&s5) != UL_VALUE)
702 return -280;
703
704 /*
705 * pj_strtoul2()
706 */
707 s5 = pj_str("123456");
708
709 pj_strtoul2(&s5, SNULL, 10); /* Crash test */
710
711 if (pj_strtoul2(&s5, &s4, 10) != 123456UL)
712 return -290;
713 if (s4.slen != 0)
714 return -291;
715 if (pj_strtoul2(&s5, &s4, 16) != 0x123456UL)
716 return -292;
717
718 s5 = pj_str("0123ABCD");
719 if (pj_strtoul2(&s5, &s4, 10) != 123)
720 return -293;
721 if (s4.slen != 4)
722 return -294;
723 if (s4.ptr == SNULL || *s4.ptr != 'A')
724 return -295;
725 if (pj_strtoul2(&s5, &s4, 16) != 0x123ABCDUL)
726 return -296;
727 if (s4.slen != 0)
728 return -297;
729
730 /*
731 * pj_create_random_string()
732 * Check that no duplicate strings are returned.
733 */
734 for (i=0; i<RCOUNT; ++i) {
735 int j;
736
737 random[i].ptr = (char*) pj_pool_alloc(pool, RLEN);
738 if (!random[i].ptr)
739 return -320;
740
741 random[i].slen = RLEN;
742 pj_create_random_string(random[i].ptr, RLEN);
743
744 for (j=0; j<i; ++j) {
745 if (pj_strcmp(&random[i], &random[j])==0)
746 return -330;
747 }
748 }
749
750 /* Done. */
751 pj_pool_release(pool);
752
753 /* Case sensitive comparison test. */
754 i = strcmp_test();
755 if (i != 0)
756 return i;
757
758 /* Caseless comparison test. */
759 i = stricmp_test();
760 if (i != 0)
761 return i;
762
763 /* strxcpy test */
764 i = strxcpy_test();
765 if (i != 0)
766 return i;
767
768 /* strxcpy2 test */
769 i = strxcpy2_test();
770 if (i != 0)
771 return i;
772
773 /* strxcat test */
774 i = strxcat_test();
775 if (i != 0)
776 return i;
777
778 return 0;
779 }
780
781 #else
782 /* To prevent warning about "translation unit is empty"
783 * when this test is disabled.
784 */
785 int dummy_string_test;
786 #endif /* INCLUDE_STRING_TEST */
787