Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame^] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 5 | * |
| 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 Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 23 | #include <pj/os.h> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 24 | #include "test.h" |
| 25 | |
Benny Prijono | 7d433ec | 2006-02-03 15:23:28 +0000 | [diff] [blame] | 26 | #define THIS_FILE "string.c" |
| 27 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 28 | /** |
| 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 Prijono | c7434f6 | 2007-04-15 09:58:48 +0000 | [diff] [blame] | 52 | * - pj_strtoul2() |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 53 | * - pj_create_random_string() |
Benny Prijono | 8220f90 | 2006-01-09 17:20:59 +0000 | [diff] [blame] | 54 | * - ... and mode.. |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 55 | * |
| 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 Prijono | 7d433ec | 2006-02-03 15:23:28 +0000 | [diff] [blame] | 71 | #if 1 |
| 72 | /* See if both integers have the same sign */ |
| 73 | PJ_INLINE(int) cmp(const char *expr, int i, int j) |
| 74 | { |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 75 | int r = !((i>0 && j>0) || (i<0 && j<0) || (i==0 && j==0)); |
| 76 | if (r) { |
Benny Prijono | 7d433ec | 2006-02-03 15:23:28 +0000 | [diff] [blame] | 77 | PJ_LOG(3,(THIS_FILE," error: %s: expecting %d, got %d", expr, j, i)); |
| 78 | } |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 79 | return r; |
Benny Prijono | 7d433ec | 2006-02-03 15:23:28 +0000 | [diff] [blame] | 80 | } |
| 81 | #else |
| 82 | /* For strict comparison, must be equal */ |
| 83 | PJ_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 Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 92 | static int stricmp_test(void) |
| 93 | { |
Benny Prijono | 8220f90 | 2006-01-09 17:20:59 +0000 | [diff] [blame] | 94 | /* 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 Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 98 | #define STRTEST(res,res2,S1,S2,code) \ |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 99 | do { \ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 100 | s1.ptr=S1; s1.slen=(S1)?len:0; \ |
| 101 | s2.ptr=S2; s2.slen=(S2)?len:0; \ |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 102 | pj_get_timestamp(&t1); \ |
Benny Prijono | 7d433ec | 2006-02-03 15:23:28 +0000 | [diff] [blame] | 103 | if (C(pj_stricmp(&s1,&s2),res)) return code; \ |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 104 | pj_get_timestamp(&t2); \ |
| 105 | pj_sub_timestamp(&t2, &t1); \ |
| 106 | pj_add_timestamp(&e1, &t2); \ |
| 107 | pj_get_timestamp(&t1); \ |
Benny Prijono | 7d433ec | 2006-02-03 15:23:28 +0000 | [diff] [blame] | 108 | if (C(pj_stricmp_alnum(&s1,&s2),res)) return code-1; \ |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 109 | pj_get_timestamp(&t2); \ |
| 110 | pj_sub_timestamp(&t2, &t1); \ |
| 111 | pj_add_timestamp(&e2, &t2); \ |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 112 | if (C(pj_stricmp2(&s1,S2),res2)) return code*10; \ |
Benny Prijono | 7d433ec | 2006-02-03 15:23:28 +0000 | [diff] [blame] | 113 | if (C(pj_strnicmp(&s1,&s2,len),res)) return code*100; \ |
| 114 | if (C(pj_strnicmp2(&s1,S2,len),res)) return code*1000; \ |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 115 | } 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 Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 127 | #define SNULL 0 |
| 128 | |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 129 | /* Compare empty strings. */ |
| 130 | len=0; |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 131 | 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 Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 136 | |
| 137 | /* equal, length=1 |
| 138 | * use buffer to simulate non-aligned string. |
| 139 | */ |
| 140 | buf = "a""A"; |
| 141 | len=1; |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 142 | 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 Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 147 | |
| 148 | /* equal, length=2 |
| 149 | * use buffer to simulate non-aligned string. |
| 150 | */ |
| 151 | buf = "aa""Aa""aA""AA"; |
| 152 | len=2; |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 153 | 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 Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 157 | |
| 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 Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 163 | 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 Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 171 | |
| 172 | /* equal, length=4 */ |
| 173 | len=4; |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 174 | 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 Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 190 | |
| 191 | /* equal, length=5 */ |
| 192 | buf = "aaaAa""AaaaA""AaAaA""AAAAA"; |
| 193 | len=5; |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 194 | 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 Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 198 | |
| 199 | /* not equal, length=1 */ |
| 200 | len=1; |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 201 | STRTEST( -1, -1, "a", "b", -600); |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 202 | |
| 203 | /* not equal, length=2 */ |
| 204 | buf = "ab""ba"; |
| 205 | len=2; |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 206 | STRTEST( -1, -1, "aa", buf+0, -610); |
| 207 | STRTEST( -1, -1, "aa", buf+2, -612); |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 208 | |
| 209 | /* not equal, length=3 */ |
| 210 | buf = "aab""aba""baa"; |
| 211 | len=3; |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 212 | STRTEST( -1, -1, "aaa", buf+0, -620); |
| 213 | STRTEST( -1, -1, "aaa", buf+3, -622); |
| 214 | STRTEST( -1, -1, "aaa", buf+6, -624); |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 215 | |
| 216 | /* not equal, length=4 */ |
| 217 | buf = "aaab""aaba""abaa""baaa"; |
| 218 | len=4; |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 219 | 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 Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 223 | |
| 224 | /* not equal, length=5 */ |
| 225 | buf="aaaab""aaaba""aabaa""abaaa""baaaa"; |
| 226 | len=5; |
Benny Prijono | 18a051b | 2007-06-28 00:50:10 +0000 | [diff] [blame] | 227 | 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 Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 232 | |
| 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 Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 238 | PJ_LOG(3,("", " info: pj_stricmp_alnum is slower than pj_stricmp!")); |
| 239 | //return -700; |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 242 | /* Avoid division by zero */ |
| 243 | if (c2 == 0) c2=1; |
| 244 | |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 245 | 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 Prijono | 8220f90 | 2006-01-09 17:20:59 +0000 | [diff] [blame] | 253 | /* This tests pj_strcmp(), pj_strcmp2(), pj_strncmp(), pj_strncmp2() */ |
| 254 | static 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 Prijono | 7d433ec | 2006-02-03 15:23:28 +0000 | [diff] [blame] | 260 | 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 Prijono | 8220f90 | 2006-01-09 17:20:59 +0000 | [diff] [blame] | 264 | } 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 Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 272 | STR_TEST(0, SNULL, "", -405); |
| 273 | STR_TEST(0, "", SNULL, -410); |
| 274 | STR_TEST(0, SNULL, SNULL, -415); |
Benny Prijono | 8220f90 | 2006-01-09 17:20:59 +0000 | [diff] [blame] | 275 | STR_TEST(0, "hello", "", -420); |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 276 | STR_TEST(0, "hello", SNULL, -425); |
Benny Prijono | 8220f90 | 2006-01-09 17:20:59 +0000 | [diff] [blame] | 277 | |
| 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 Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 283 | STR_TEST(-1, SNULL, "12", -445); |
| 284 | STR_TEST(1, "12", SNULL, -450); |
Benny Prijono | 8220f90 | 2006-01-09 17:20:59 +0000 | [diff] [blame] | 285 | |
| 286 | return 0; |
| 287 | |
| 288 | #undef STR_TEST |
| 289 | } |
| 290 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 291 | int 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 Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 301 | pool = pj_pool_create(mem, SNULL, 4096, 0, SNULL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 302 | 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 Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 337 | s3.ptr = (char*) pj_pool_alloc(pool, 256); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 338 | 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 Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 357 | s5.ptr = (char*) pj_pool_alloc(pool, 16); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 358 | 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 Prijono | c7434f6 | 2007-04-15 09:58:48 +0000 | [diff] [blame] | 368 | /* |
| 369 | * pj_strtoul2() |
| 370 | */ |
| 371 | s5 = pj_str("123456"); |
| 372 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 373 | pj_strtoul2(&s5, SNULL, 10); /* Crash test */ |
Benny Prijono | c7434f6 | 2007-04-15 09:58:48 +0000 | [diff] [blame] | 374 | |
| 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 Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 387 | if (s4.ptr == SNULL || *s4.ptr != 'A') |
Benny Prijono | c7434f6 | 2007-04-15 09:58:48 +0000 | [diff] [blame] | 388 | return -295; |
| 389 | if (pj_strtoul2(&s5, &s4, 16) != 0x123ABCDUL) |
| 390 | return -296; |
| 391 | if (s4.slen != 0) |
| 392 | return -297; |
| 393 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 394 | /* |
| 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 Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 401 | random[i].ptr = (char*) pj_pool_alloc(pool, RLEN); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 402 | 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 Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 416 | |
Benny Prijono | 8220f90 | 2006-01-09 17:20:59 +0000 | [diff] [blame] | 417 | /* 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 Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | #else |
| 431 | /* To prevent warning about "translation unit is empty" |
| 432 | * when this test is disabled. |
| 433 | */ |
| 434 | int dummy_string_test; |
| 435 | #endif /* INCLUDE_STRING_TEST */ |
| 436 | |