blob: 18cfb6c5ddc861256649713203dbf5a8f7af9ae5 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pj/string.h>
21#include <pj/pool.h>
22#include <pj/log.h>
Benny Prijonoa7944bb2005-11-21 17:01:50 +000023#include <pj/os.h>
Benny Prijono5dcb38d2005-11-21 01:55:47 +000024#include "test.h"
25
Benny Prijono7d433ec2006-02-03 15:23:28 +000026#define THIS_FILE "string.c"
27
Benny Prijono5dcb38d2005-11-21 01:55:47 +000028/**
29 * \page page_pjlib_string_test Test: String
30 *
31 * This file provides implementation of \b string_test(). It tests the
32 * functionality of the string API.
33 *
34 * \section sleep_test_sec Scope of the Test
35 *
36 * API tested:
37 * - pj_str()
38 * - pj_strcmp()
39 * - pj_strcmp2()
40 * - pj_stricmp()
41 * - pj_strlen()
42 * - pj_strncmp()
43 * - pj_strnicmp()
44 * - pj_strchr()
45 * - pj_strdup()
46 * - pj_strdup2()
47 * - pj_strcpy()
48 * - pj_strcat()
49 * - pj_strtrim()
50 * - pj_utoa()
51 * - pj_strtoul()
Benny Prijonoc7434f62007-04-15 09:58:48 +000052 * - pj_strtoul2()
Benny Prijono5dcb38d2005-11-21 01:55:47 +000053 * - pj_create_random_string()
Benny Prijono8220f902006-01-09 17:20:59 +000054 * - ... and mode..
Benny Prijono5dcb38d2005-11-21 01:55:47 +000055 *
56 * This file is <b>pjlib-test/string.c</b>
57 *
58 * \include pjlib-test/string.c
59 */
60
61#if INCLUDE_STRING_TEST
62
63#ifdef _MSC_VER
64# pragma warning(disable: 4204)
65#endif
66
67#define HELLO_WORLD "Hello World"
68#define JUST_HELLO "Hello"
69#define UL_VALUE 3456789012UL
70
Benny Prijono7d433ec2006-02-03 15:23:28 +000071#if 1
72/* See if both integers have the same sign */
73PJ_INLINE(int) cmp(const char *expr, int i, int j)
74{
Benny Prijono18a051b2007-06-28 00:50:10 +000075 int r = !((i>0 && j>0) || (i<0 && j<0) || (i==0 && j==0));
76 if (r) {
Benny Prijono7d433ec2006-02-03 15:23:28 +000077 PJ_LOG(3,(THIS_FILE," error: %s: expecting %d, got %d", expr, j, i));
78 }
Benny Prijono18a051b2007-06-28 00:50:10 +000079 return r;
Benny Prijono7d433ec2006-02-03 15:23:28 +000080}
81#else
82/* For strict comparison, must be equal */
83PJ_INLINE(int) cmp(const char *expr, int i, int j)
84{
85 PJ_UNUSED_ARG(expr);
86 return i!=j;
87}
88#endif
89
90#define C(expr, res) cmp(#expr, expr, res)
91
Benny Prijonoa7944bb2005-11-21 17:01:50 +000092static int stricmp_test(void)
93{
Benny Prijono8220f902006-01-09 17:20:59 +000094/* This specificly tests and benchmark pj_stricmp(), pj_stricmp_alnum().
95 * In addition, it also tests pj_stricmp2(), pj_strnicmp(), and
96 * pj_strnicmp2().
97 */
Benny Prijono18a051b2007-06-28 00:50:10 +000098#define STRTEST(res,res2,S1,S2,code) \
Benny Prijonoa7944bb2005-11-21 17:01:50 +000099 do { \
Benny Prijonoa1e69682007-05-11 15:14:34 +0000100 s1.ptr=S1; s1.slen=(S1)?len:0; \
101 s2.ptr=S2; s2.slen=(S2)?len:0; \
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000102 pj_get_timestamp(&t1); \
Benny Prijono7d433ec2006-02-03 15:23:28 +0000103 if (C(pj_stricmp(&s1,&s2),res)) return code; \
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000104 pj_get_timestamp(&t2); \
105 pj_sub_timestamp(&t2, &t1); \
106 pj_add_timestamp(&e1, &t2); \
107 pj_get_timestamp(&t1); \
Benny Prijono7d433ec2006-02-03 15:23:28 +0000108 if (C(pj_stricmp_alnum(&s1,&s2),res)) return code-1; \
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000109 pj_get_timestamp(&t2); \
110 pj_sub_timestamp(&t2, &t1); \
111 pj_add_timestamp(&e2, &t2); \
Benny Prijono18a051b2007-06-28 00:50:10 +0000112 if (C(pj_stricmp2(&s1,S2),res2)) return code*10; \
Benny Prijono7d433ec2006-02-03 15:23:28 +0000113 if (C(pj_strnicmp(&s1,&s2,len),res)) return code*100; \
114 if (C(pj_strnicmp2(&s1,S2,len),res)) return code*1000; \
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000115 } while (0)
116
117 char *buf;
118 pj_str_t s1, s2;
119 pj_timestamp t1, t2, e1, e2, zero;
120 pj_uint32_t c1, c2;
121 int len;
122
123 e1.u32.hi = e1.u32.lo = e2.u32.hi = e2.u32.lo = 0;
124
125 pj_thread_sleep(0);
126
Benny Prijonoa1e69682007-05-11 15:14:34 +0000127#define SNULL 0
128
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000129 /* Compare empty strings. */
130 len=0;
Benny Prijono18a051b2007-06-28 00:50:10 +0000131 STRTEST( 0, 0, "","",-500);
132 STRTEST( 0, 0, SNULL,"",-502);
133 STRTEST( 0, 0, "",SNULL,-504);
134 STRTEST( 0, 0, SNULL,SNULL,-506);
135 STRTEST( 0, -1, "hello","world",-508);
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000136
137 /* equal, length=1
138 * use buffer to simulate non-aligned string.
139 */
140 buf = "a""A";
141 len=1;
Benny Prijono18a051b2007-06-28 00:50:10 +0000142 STRTEST( 0, -1, "a",buf+0,-510);
143 STRTEST( 0, 0, "a",buf+1,-512);
144 STRTEST(-1, -1, "O", "P", -514);
145 STRTEST(-1, -1, SNULL, "a", -516);
146 STRTEST( 1, 1, "a", SNULL, -518);
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000147
148 /* equal, length=2
149 * use buffer to simulate non-aligned string.
150 */
151 buf = "aa""Aa""aA""AA";
152 len=2;
Benny Prijono18a051b2007-06-28 00:50:10 +0000153 STRTEST( 0, -1, "aa",buf+0,-520);
154 STRTEST( 0, -1, "aa",buf+2,-522);
155 STRTEST( 0, -1, "aa",buf+4,-524);
156 STRTEST( 0, 0, "aa",buf+6,-524);
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000157
158 /* equal, length=3
159 * use buffer to simulate non-aligned string.
160 */
161 buf = "aaa""Aaa""aAa""aaA""AAa""aAA""AaA""AAA";
162 len=3;
Benny Prijono18a051b2007-06-28 00:50:10 +0000163 STRTEST( 0, -1, "aaa",buf+0,-530);
164 STRTEST( 0, -1, "aaa",buf+3,-532);
165 STRTEST( 0, -1, "aaa",buf+6,-534);
166 STRTEST( 0, -1, "aaa",buf+9,-536);
167 STRTEST( 0, -1, "aaa",buf+12,-538);
168 STRTEST( 0, -1, "aaa",buf+15,-540);
169 STRTEST( 0, -1, "aaa",buf+18,-542);
170 STRTEST( 0, 0, "aaa",buf+21,-534);
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000171
172 /* equal, length=4 */
173 len=4;
Benny Prijono18a051b2007-06-28 00:50:10 +0000174 STRTEST( 0, 0, "aaaa","aaaa",-540);
175 STRTEST( 0, 0, "aaaa","Aaaa",-542);
176 STRTEST( 0, 0, "aaaa","aAaa",-544);
177 STRTEST( 0, 0, "aaaa","aaAa",-546);
178 STRTEST( 0, 0, "aaaa","aaaA",-548);
179 STRTEST( 0, 0, "aaaa","AAaa",-550);
180 STRTEST( 0, 0, "aaaa","aAAa",-552);
181 STRTEST( 0, 0, "aaaa","aaAA",-554);
182 STRTEST( 0, 0, "aaaa","AaAa",-556);
183 STRTEST( 0, 0, "aaaa","aAaA",-558);
184 STRTEST( 0, 0, "aaaa","AaaA",-560);
185 STRTEST( 0, 0, "aaaa","AAAa",-562);
186 STRTEST( 0, 0, "aaaa","aAAA",-564);
187 STRTEST( 0, 0, "aaaa","AAaA",-566);
188 STRTEST( 0, 0, "aaaa","AaAA",-568);
189 STRTEST( 0, 0, "aaaa","AAAA",-570);
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000190
191 /* equal, length=5 */
192 buf = "aaaAa""AaaaA""AaAaA""AAAAA";
193 len=5;
Benny Prijono18a051b2007-06-28 00:50:10 +0000194 STRTEST( 0, -1, "aaaaa",buf+0,-580);
195 STRTEST( 0, -1, "aaaaa",buf+5,-582);
196 STRTEST( 0, -1, "aaaaa",buf+10,-584);
197 STRTEST( 0, 0, "aaaaa",buf+15,-586);
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000198
199 /* not equal, length=1 */
200 len=1;
Benny Prijono18a051b2007-06-28 00:50:10 +0000201 STRTEST( -1, -1, "a", "b", -600);
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000202
203 /* not equal, length=2 */
204 buf = "ab""ba";
205 len=2;
Benny Prijono18a051b2007-06-28 00:50:10 +0000206 STRTEST( -1, -1, "aa", buf+0, -610);
207 STRTEST( -1, -1, "aa", buf+2, -612);
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000208
209 /* not equal, length=3 */
210 buf = "aab""aba""baa";
211 len=3;
Benny Prijono18a051b2007-06-28 00:50:10 +0000212 STRTEST( -1, -1, "aaa", buf+0, -620);
213 STRTEST( -1, -1, "aaa", buf+3, -622);
214 STRTEST( -1, -1, "aaa", buf+6, -624);
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000215
216 /* not equal, length=4 */
217 buf = "aaab""aaba""abaa""baaa";
218 len=4;
Benny Prijono18a051b2007-06-28 00:50:10 +0000219 STRTEST( -1, -1, "aaaa", buf+0, -630);
220 STRTEST( -1, -1, "aaaa", buf+4, -632);
221 STRTEST( -1, -1, "aaaa", buf+8, -634);
222 STRTEST( -1, -1, "aaaa", buf+12, -636);
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000223
224 /* not equal, length=5 */
225 buf="aaaab""aaaba""aabaa""abaaa""baaaa";
226 len=5;
Benny Prijono18a051b2007-06-28 00:50:10 +0000227 STRTEST( -1, -1, "aaaaa", buf+0, -640);
228 STRTEST( -1, -1, "aaaaa", buf+5, -642);
229 STRTEST( -1, -1, "aaaaa", buf+10, -644);
230 STRTEST( -1, -1, "aaaaa", buf+15, -646);
231 STRTEST( -1, -1, "aaaaa", buf+20, -648);
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000232
233 zero.u32.hi = zero.u32.lo = 0;
234 c1 = pj_elapsed_cycle(&zero, &e1);
235 c2 = pj_elapsed_cycle(&zero, &e2);
236
237 if (c1 < c2) {
Benny Prijono37e8d332006-01-20 21:03:36 +0000238 PJ_LOG(3,("", " info: pj_stricmp_alnum is slower than pj_stricmp!"));
239 //return -700;
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000240 }
241
Benny Prijonof260e462007-04-30 21:03:32 +0000242 /* Avoid division by zero */
243 if (c2 == 0) c2=1;
244
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000245 PJ_LOG(3, ("", " time: stricmp=%u, stricmp_alnum=%u (speedup=%d.%02dx)",
246 c1, c2,
247 (c1 * 100 / c2) / 100,
248 (c1 * 100 / c2) % 100));
249 return 0;
250#undef STRTEST
251}
252
Benny Prijono8220f902006-01-09 17:20:59 +0000253/* This tests pj_strcmp(), pj_strcmp2(), pj_strncmp(), pj_strncmp2() */
254static int strcmp_test(void)
255{
256#define STR_TEST(res,S1,S2,code) \
257 do { \
258 s1.ptr=S1; s1.slen=S1?len:0; \
259 s2.ptr=S2; s2.slen=S2?len:0; \
Benny Prijono7d433ec2006-02-03 15:23:28 +0000260 if (C(pj_strcmp(&s1,&s2),res)) return code; \
261 if (C(pj_strcmp2(&s1,S2),res)) return code-1; \
262 if (C(pj_strncmp(&s1,&s2,len),res)) return code-2; \
263 if (C(pj_strncmp2(&s1,S2,len),res)) return code-3; \
Benny Prijono8220f902006-01-09 17:20:59 +0000264 } while (0)
265
266 pj_str_t s1, s2;
267 int len;
268
269 /* Test with length == 0 */
270 len=0;
271 STR_TEST(0, "", "", -400);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000272 STR_TEST(0, SNULL, "", -405);
273 STR_TEST(0, "", SNULL, -410);
274 STR_TEST(0, SNULL, SNULL, -415);
Benny Prijono8220f902006-01-09 17:20:59 +0000275 STR_TEST(0, "hello", "", -420);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000276 STR_TEST(0, "hello", SNULL, -425);
Benny Prijono8220f902006-01-09 17:20:59 +0000277
278 /* Test with length != 0 */
279 len = 2;
280 STR_TEST(0, "12", "12", -430);
281 STR_TEST(1, "12", "1", -435);
282 STR_TEST(-1, "1", "12", -440);
Benny Prijonoa1e69682007-05-11 15:14:34 +0000283 STR_TEST(-1, SNULL, "12", -445);
284 STR_TEST(1, "12", SNULL, -450);
Benny Prijono8220f902006-01-09 17:20:59 +0000285
286 return 0;
287
288#undef STR_TEST
289}
290
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000291int string_test(void)
292{
293 const pj_str_t hello_world = { HELLO_WORLD, strlen(HELLO_WORLD) };
294 const pj_str_t just_hello = { JUST_HELLO, strlen(JUST_HELLO) };
295 pj_str_t s1, s2, s3, s4, s5;
296 enum { RCOUNT = 10, RLEN = 16 };
297 pj_str_t random[RCOUNT];
298 pj_pool_t *pool;
299 int i;
300
Benny Prijonoa1e69682007-05-11 15:14:34 +0000301 pool = pj_pool_create(mem, SNULL, 4096, 0, SNULL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000302 if (!pool) return -5;
303
304 /*
305 * pj_str(), pj_strcmp(), pj_stricmp(), pj_strlen(),
306 * pj_strncmp(), pj_strchr()
307 */
308 s1 = pj_str(HELLO_WORLD);
309 if (pj_strcmp(&s1, &hello_world) != 0)
310 return -10;
311 if (pj_stricmp(&s1, &hello_world) != 0)
312 return -20;
313 if (pj_strcmp(&s1, &just_hello) <= 0)
314 return -30;
315 if (pj_stricmp(&s1, &just_hello) <= 0)
316 return -40;
317 if (pj_strlen(&s1) != strlen(HELLO_WORLD))
318 return -50;
319 if (pj_strncmp(&s1, &hello_world, 5) != 0)
320 return -60;
321 if (pj_strnicmp(&s1, &hello_world, 5) != 0)
322 return -70;
323 if (pj_strchr(&s1, HELLO_WORLD[1]) != s1.ptr+1)
324 return -80;
325
326 /*
327 * pj_strdup()
328 */
329 if (!pj_strdup(pool, &s2, &s1))
330 return -100;
331 if (pj_strcmp(&s1, &s2) != 0)
332 return -110;
333
334 /*
335 * pj_strcpy(), pj_strcat()
336 */
Benny Prijonof260e462007-04-30 21:03:32 +0000337 s3.ptr = (char*) pj_pool_alloc(pool, 256);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000338 if (!s3.ptr)
339 return -200;
340 pj_strcpy(&s3, &s2);
341 pj_strcat(&s3, &just_hello);
342
343 if (pj_strcmp2(&s3, HELLO_WORLD JUST_HELLO) != 0)
344 return -210;
345
346 /*
347 * pj_strdup2(), pj_strtrim().
348 */
349 pj_strdup2(pool, &s4, " " HELLO_WORLD "\t ");
350 pj_strtrim(&s4);
351 if (pj_strcmp2(&s4, HELLO_WORLD) != 0)
352 return -250;
353
354 /*
355 * pj_utoa()
356 */
Benny Prijonof260e462007-04-30 21:03:32 +0000357 s5.ptr = (char*) pj_pool_alloc(pool, 16);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000358 if (!s5.ptr)
359 return -270;
360 s5.slen = pj_utoa(UL_VALUE, s5.ptr);
361
362 /*
363 * pj_strtoul()
364 */
365 if (pj_strtoul(&s5) != UL_VALUE)
366 return -280;
367
Benny Prijonoc7434f62007-04-15 09:58:48 +0000368 /*
369 * pj_strtoul2()
370 */
371 s5 = pj_str("123456");
372
Benny Prijonoa1e69682007-05-11 15:14:34 +0000373 pj_strtoul2(&s5, SNULL, 10); /* Crash test */
Benny Prijonoc7434f62007-04-15 09:58:48 +0000374
375 if (pj_strtoul2(&s5, &s4, 10) != 123456UL)
376 return -290;
377 if (s4.slen != 0)
378 return -291;
379 if (pj_strtoul2(&s5, &s4, 16) != 0x123456UL)
380 return -292;
381
382 s5 = pj_str("0123ABCD");
383 if (pj_strtoul2(&s5, &s4, 10) != 123)
384 return -293;
385 if (s4.slen != 4)
386 return -294;
Benny Prijonoa1e69682007-05-11 15:14:34 +0000387 if (s4.ptr == SNULL || *s4.ptr != 'A')
Benny Prijonoc7434f62007-04-15 09:58:48 +0000388 return -295;
389 if (pj_strtoul2(&s5, &s4, 16) != 0x123ABCDUL)
390 return -296;
391 if (s4.slen != 0)
392 return -297;
393
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000394 /*
395 * pj_create_random_string()
396 * Check that no duplicate strings are returned.
397 */
398 for (i=0; i<RCOUNT; ++i) {
399 int j;
400
Benny Prijonof260e462007-04-30 21:03:32 +0000401 random[i].ptr = (char*) pj_pool_alloc(pool, RLEN);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000402 if (!random[i].ptr)
403 return -320;
404
405 random[i].slen = RLEN;
406 pj_create_random_string(random[i].ptr, RLEN);
407
408 for (j=0; j<i; ++j) {
409 if (pj_strcmp(&random[i], &random[j])==0)
410 return -330;
411 }
412 }
413
414 /* Done. */
415 pj_pool_release(pool);
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000416
Benny Prijono8220f902006-01-09 17:20:59 +0000417 /* Case sensitive comparison test. */
418 i = strcmp_test();
419 if (i != 0)
420 return i;
421
422 /* Caseless comparison test. */
423 i = stricmp_test();
424 if (i != 0)
425 return i;
426
427 return 0;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000428}
429
430#else
431/* To prevent warning about "translation unit is empty"
432 * when this test is disabled.
433 */
434int dummy_string_test;
435#endif /* INCLUDE_STRING_TEST */
436