blob: 96a4ade14d734db653747c69c8400d2a9126fa40 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
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/pool.h>
21#include <pj/log.h>
22#include "test.h"
23
24/**
25 * \page page_pjlib_string_test Test: String
26 *
27 * This file provides implementation of \b string_test(). It tests the
28 * functionality of the string API.
29 *
30 * \section sleep_test_sec Scope of the Test
31 *
32 * API tested:
33 * - pj_str()
34 * - pj_strcmp()
35 * - pj_strcmp2()
36 * - pj_stricmp()
37 * - pj_strlen()
38 * - pj_strncmp()
39 * - pj_strnicmp()
40 * - pj_strchr()
41 * - pj_strdup()
42 * - pj_strdup2()
43 * - pj_strcpy()
44 * - pj_strcat()
45 * - pj_strtrim()
46 * - pj_utoa()
47 * - pj_strtoul()
48 * - pj_create_random_string()
49 *
50 *
51 * This file is <b>pjlib-test/string.c</b>
52 *
53 * \include pjlib-test/string.c
54 */
55
56#if INCLUDE_STRING_TEST
57
58#ifdef _MSC_VER
59# pragma warning(disable: 4204)
60#endif
61
62#define HELLO_WORLD "Hello World"
63#define JUST_HELLO "Hello"
64#define UL_VALUE 3456789012UL
65
66int string_test(void)
67{
68 const pj_str_t hello_world = { HELLO_WORLD, strlen(HELLO_WORLD) };
69 const pj_str_t just_hello = { JUST_HELLO, strlen(JUST_HELLO) };
70 pj_str_t s1, s2, s3, s4, s5;
71 enum { RCOUNT = 10, RLEN = 16 };
72 pj_str_t random[RCOUNT];
73 pj_pool_t *pool;
74 int i;
75
76 pool = pj_pool_create(mem, NULL, 4096, 0, NULL);
77 if (!pool) return -5;
78
79 /*
80 * pj_str(), pj_strcmp(), pj_stricmp(), pj_strlen(),
81 * pj_strncmp(), pj_strchr()
82 */
83 s1 = pj_str(HELLO_WORLD);
84 if (pj_strcmp(&s1, &hello_world) != 0)
85 return -10;
86 if (pj_stricmp(&s1, &hello_world) != 0)
87 return -20;
88 if (pj_strcmp(&s1, &just_hello) <= 0)
89 return -30;
90 if (pj_stricmp(&s1, &just_hello) <= 0)
91 return -40;
92 if (pj_strlen(&s1) != strlen(HELLO_WORLD))
93 return -50;
94 if (pj_strncmp(&s1, &hello_world, 5) != 0)
95 return -60;
96 if (pj_strnicmp(&s1, &hello_world, 5) != 0)
97 return -70;
98 if (pj_strchr(&s1, HELLO_WORLD[1]) != s1.ptr+1)
99 return -80;
100
101 /*
102 * pj_strdup()
103 */
104 if (!pj_strdup(pool, &s2, &s1))
105 return -100;
106 if (pj_strcmp(&s1, &s2) != 0)
107 return -110;
108
109 /*
110 * pj_strcpy(), pj_strcat()
111 */
112 s3.ptr = pj_pool_alloc(pool, 256);
113 if (!s3.ptr)
114 return -200;
115 pj_strcpy(&s3, &s2);
116 pj_strcat(&s3, &just_hello);
117
118 if (pj_strcmp2(&s3, HELLO_WORLD JUST_HELLO) != 0)
119 return -210;
120
121 /*
122 * pj_strdup2(), pj_strtrim().
123 */
124 pj_strdup2(pool, &s4, " " HELLO_WORLD "\t ");
125 pj_strtrim(&s4);
126 if (pj_strcmp2(&s4, HELLO_WORLD) != 0)
127 return -250;
128
129 /*
130 * pj_utoa()
131 */
132 s5.ptr = pj_pool_alloc(pool, 16);
133 if (!s5.ptr)
134 return -270;
135 s5.slen = pj_utoa(UL_VALUE, s5.ptr);
136
137 /*
138 * pj_strtoul()
139 */
140 if (pj_strtoul(&s5) != UL_VALUE)
141 return -280;
142
143 /*
144 * pj_create_random_string()
145 * Check that no duplicate strings are returned.
146 */
147 for (i=0; i<RCOUNT; ++i) {
148 int j;
149
150 random[i].ptr = pj_pool_alloc(pool, RLEN);
151 if (!random[i].ptr)
152 return -320;
153
154 random[i].slen = RLEN;
155 pj_create_random_string(random[i].ptr, RLEN);
156
157 for (j=0; j<i; ++j) {
158 if (pj_strcmp(&random[i], &random[j])==0)
159 return -330;
160 }
161 }
162
163 /* Done. */
164 pj_pool_release(pool);
165 return 0;
166}
167
168#else
169/* To prevent warning about "translation unit is empty"
170 * when this test is disabled.
171 */
172int dummy_string_test;
173#endif /* INCLUDE_STRING_TEST */
174