VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/TestMinStringAPI.cpp@ 4837

Last change on this file since 4837 was 1, checked in by vboxsync, 54 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 KB
Line 
1/* vim:set ts=2 sw=2 et cindent: */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is Mozilla.
16 *
17 * The Initial Developer of the Original Code is IBM Corporation.
18 * Portions created by IBM Corporation are Copyright (C) 2003
19 * IBM Corporation. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Darin Fisher <darin@meer.net>
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#include <stdio.h>
39#include "nsStringAPI.h"
40#include "nsCRT.h"
41
42static const char kAsciiData[] = "hello world";
43
44static const PRUnichar kUnicodeData[] =
45 {'h','e','l','l','o',' ','w','o','r','l','d','\0'};
46
47static PRBool test_basic_1()
48 {
49 nsCStringContainer s;
50 NS_CStringContainerInit(s);
51
52 const char *ptr;
53 PRUint32 len;
54 char *clone;
55
56 NS_CStringGetData(s, &ptr);
57 if (ptr == nsnull || *ptr != '\0')
58 {
59 NS_ERROR("unexpected result");
60 return PR_FALSE;
61 }
62
63 NS_CStringSetData(s, kAsciiData, PR_UINT32_MAX);
64 len = NS_CStringGetData(s, &ptr);
65 if (ptr == nsnull || strcmp(ptr, kAsciiData) != 0)
66 {
67 NS_ERROR("unexpected result");
68 return PR_FALSE;
69 }
70 if (len != sizeof(kAsciiData)-1)
71 {
72 NS_ERROR("unexpected result");
73 return PR_FALSE;
74 }
75
76 clone = NS_CStringCloneData(s);
77 if (ptr == nsnull || strcmp(ptr, kAsciiData) != 0)
78 {
79 NS_ERROR("unexpected result");
80 return PR_FALSE;
81 }
82 nsMemory::Free(clone);
83
84 nsCStringContainer temp;
85 NS_CStringContainerInit(temp);
86 NS_CStringCopy(temp, s);
87
88 len = NS_CStringGetData(temp, &ptr);
89 if (ptr == nsnull || strcmp(ptr, kAsciiData) != 0)
90 {
91 NS_ERROR("unexpected result");
92 return PR_FALSE;
93 }
94 if (len != sizeof(kAsciiData)-1)
95 {
96 NS_ERROR("unexpected result");
97 return PR_FALSE;
98 }
99
100 NS_CStringContainerFinish(temp);
101
102 NS_CStringContainerFinish(s);
103 return PR_TRUE;
104 }
105
106static PRBool test_basic_2()
107 {
108 nsStringContainer s;
109 NS_StringContainerInit(s);
110
111 const PRUnichar *ptr;
112 PRUint32 len;
113 PRUnichar *clone;
114
115 NS_StringGetData(s, &ptr);
116 if (ptr == nsnull || *ptr != '\0')
117 {
118 NS_ERROR("unexpected result");
119 return PR_FALSE;
120 }
121
122 NS_StringSetData(s, kUnicodeData, PR_UINT32_MAX);
123 len = NS_StringGetData(s, &ptr);
124 if (ptr == nsnull || nsCRT::strcmp(ptr, kUnicodeData) != 0)
125 {
126 NS_ERROR("unexpected result");
127 return PR_FALSE;
128 }
129 if (len != sizeof(kUnicodeData)/2 - 1)
130 {
131 NS_ERROR("unexpected result");
132 return PR_FALSE;
133 }
134
135 clone = NS_StringCloneData(s);
136 if (ptr == nsnull || nsCRT::strcmp(ptr, kUnicodeData) != 0)
137 {
138 NS_ERROR("unexpected result");
139 return PR_FALSE;
140 }
141 nsMemory::Free(clone);
142
143 nsStringContainer temp;
144 NS_StringContainerInit(temp);
145 NS_StringCopy(temp, s);
146
147 len = NS_StringGetData(temp, &ptr);
148 if (ptr == nsnull || nsCRT::strcmp(ptr, kUnicodeData) != 0)
149 {
150 NS_ERROR("unexpected result");
151 return PR_FALSE;
152 }
153 if (len != sizeof(kUnicodeData)/2 - 1)
154 {
155 NS_ERROR("unexpected result");
156 return PR_FALSE;
157 }
158
159 NS_StringContainerFinish(temp);
160
161 NS_StringContainerFinish(s);
162
163 return PR_TRUE;
164 }
165
166static PRBool test_convert()
167 {
168 nsStringContainer s;
169 NS_StringContainerInit(s);
170 NS_StringSetData(s, kUnicodeData, sizeof(kUnicodeData)/2 - 1);
171
172 nsCStringContainer temp;
173 NS_CStringContainerInit(temp);
174
175 const char *data;
176
177 NS_UTF16ToCString(s, NS_CSTRING_ENCODING_ASCII, temp);
178 NS_CStringGetData(temp, &data);
179 if (strcmp(data, kAsciiData) != 0)
180 return PR_FALSE;
181
182 NS_UTF16ToCString(s, NS_CSTRING_ENCODING_UTF8, temp);
183 NS_CStringGetData(temp, &data);
184 if (strcmp(data, kAsciiData) != 0)
185 return PR_FALSE;
186
187 NS_CStringContainerFinish(temp);
188
189 NS_StringContainerFinish(s);
190 return PR_TRUE;
191 }
192
193static PRBool test_append()
194 {
195 nsCStringContainer s;
196 NS_CStringContainerInit(s);
197
198 NS_CStringSetData(s, "foo");
199 NS_CStringAppendData(s, "bar");
200
201 NS_CStringContainerFinish(s);
202 return PR_TRUE;
203 }
204
205// Replace all occurances of |matchVal| with |newVal|
206static void ReplaceSubstring( nsACString& str,
207 const nsACString& matchVal,
208 const nsACString& newVal )
209 {
210 const char* sp, *mp, *np;
211 PRUint32 sl, ml, nl;
212
213 sl = NS_CStringGetData(str, &sp);
214 ml = NS_CStringGetData(matchVal, &mp);
215 nl = NS_CStringGetData(newVal, &np);
216
217 for (const char* iter = sp; iter <= sp + sl - ml; ++iter)
218 {
219 if (memcmp(iter, mp, ml) == 0)
220 {
221 PRUint32 offset = iter - sp;
222
223 NS_CStringSetDataRange(str, offset, ml, np, nl);
224
225 sl = NS_CStringGetData(str, &sp);
226
227 iter = sp + offset + nl - 1;
228 }
229 }
230 }
231
232static PRBool test_replace_driver(const char *strVal,
233 const char *matchVal,
234 const char *newVal,
235 const char *finalVal)
236 {
237 nsCStringContainer a;
238 NS_CStringContainerInit(a);
239 NS_CStringSetData(a, strVal);
240
241 nsCStringContainer b;
242 NS_CStringContainerInit(b);
243 NS_CStringSetData(b, matchVal);
244
245 nsCStringContainer c;
246 NS_CStringContainerInit(c);
247 NS_CStringSetData(c, newVal);
248
249 ReplaceSubstring(a, b, c);
250
251 const char *data;
252 NS_CStringGetData(a, &data);
253 if (strcmp(data, finalVal) != 0)
254 return PR_FALSE;
255
256 NS_CStringContainerFinish(c);
257 NS_CStringContainerFinish(b);
258 NS_CStringContainerFinish(a);
259 return PR_TRUE;
260 }
261
262static PRBool test_replace()
263 {
264 PRBool rv;
265
266 rv = test_replace_driver("hello world, hello again!",
267 "hello",
268 "goodbye",
269 "goodbye world, goodbye again!");
270 if (!rv)
271 return rv;
272
273 rv = test_replace_driver("foofoofoofoo!",
274 "foo",
275 "bar",
276 "barbarbarbar!");
277 if (!rv)
278 return rv;
279
280 rv = test_replace_driver("foo bar systems",
281 "xyz",
282 "crazy",
283 "foo bar systems");
284 if (!rv)
285 return rv;
286
287 rv = test_replace_driver("oh",
288 "xyz",
289 "crazy",
290 "oh");
291 if (!rv)
292 return rv;
293
294 return PR_TRUE;
295 }
296
297//----
298
299typedef PRBool (*TestFunc)();
300
301static const struct Test
302 {
303 const char* name;
304 TestFunc func;
305 }
306tests[] =
307 {
308 { "test_basic_1", test_basic_1 },
309 { "test_basic_2", test_basic_2 },
310 { "test_convert", test_convert },
311 { "test_append", test_append },
312 { "test_replace", test_replace },
313 { nsnull, nsnull }
314 };
315
316//----
317
318int main(int argc, char **argv)
319 {
320 int count = 1;
321 if (argc > 1)
322 count = atoi(argv[1]);
323
324 while (count--)
325 {
326 for (const Test* t = tests; t->name != nsnull; ++t)
327 {
328 printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE");
329 }
330 }
331
332 return 0;
333 }
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use