VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/TestStrings.cpp@ 106945

Last change on this file since 106945 was 104482, checked in by vboxsync, 10 months ago

libs/xpcom: Some fixes in testcases, bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.7 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 "nsString.h"
40#include "nsReadableUtils.h"
41#include "nsCRT.h"
42
43static void test_assign_helper(const nsACString& in, nsACString &_retval)
44 {
45 _retval = in;
46 }
47
48static PRBool test_assign()
49 {
50 nsCString result;
51 test_assign_helper(NS_LITERAL_CSTRING("a") + NS_LITERAL_CSTRING("b"), result);
52 PRBool r = strcmp(result.get(), "ab") == 0;
53 if (!r)
54 printf("[result=%s]\n", result.get());
55 return r;
56 }
57
58static PRBool test_assign_c()
59 {
60 nsCString c; c.Assign('c');
61 PRBool r = strcmp(c.get(), "c") == 0;
62 if (!r)
63 printf("[result=%s]\n", c.get());
64 return r;
65 }
66
67static PRBool test1()
68 {
69 NS_NAMED_LITERAL_STRING(empty, "");
70 const nsAString& aStr = empty;
71
72 nsAutoString buf(aStr);
73
74 PRInt32 n = buf.Length();
75
76 buf.Cut(0, n + 1);
77 n = buf.FindChar(',');
78
79 if (n != kNotFound)
80 printf("n=%d\n", n);
81
82 return n == kNotFound;
83 }
84
85static PRBool test2()
86 {
87 nsCString data("hello world");
88 const nsACString& aStr = data;
89
90 nsCString temp(aStr);
91 temp.Cut(0, 6);
92
93 PRBool r = strcmp(temp.get(), "world") == 0;
94 if (!r)
95 printf("[temp=%s]\n", temp.get());
96 return r;
97 }
98
99static PRBool test_find()
100 {
101 nsCString src("<!DOCTYPE blah blah blah>");
102
103 PRInt32 i = src.Find("DOCTYPE", PR_TRUE, 2, 1);
104 if (i == 2)
105 return PR_TRUE;
106
107 printf("i=%d\n", i);
108 return PR_FALSE;
109 }
110
111static PRBool test_rfind()
112 {
113 const char text[] = "<!DOCTYPE blah blah blah>";
114 const char term[] = "bLaH";
115 nsCString src(text);
116 PRInt32 i;
117
118 i = src.RFind(term, PR_TRUE, 3, -1);
119 if (i != kNotFound)
120 {
121 printf("unexpected result searching from offset=3, i=%d\n", i);
122 return PR_FALSE;
123 }
124
125 i = src.RFind(term, PR_TRUE, -1, -1);
126 if (i != 20)
127 {
128 printf("unexpected result searching from offset=-1, i=%d\n", i);
129 return PR_FALSE;
130 }
131
132 i = src.RFind(term, PR_TRUE, 13, -1);
133 if (i != 10)
134 {
135 printf("unexpected result searching from offset=13, i=%d\n", i);
136 return PR_FALSE;
137 }
138
139 i = src.RFind(term, PR_TRUE, 22, 3);
140 if (i != 20)
141 {
142 printf("unexpected result searching from offset=22, i=%d\n", i);
143 return PR_FALSE;
144 }
145
146 return PR_TRUE;
147 }
148
149static PRBool test_rfind_2()
150 {
151 const char text[] = "<!DOCTYPE blah blah blah>";
152 nsCString src(text);
153 PRInt32 i = src.RFind("TYPE", PR_FALSE, 5, -1);
154 if (i == 5)
155 return PR_TRUE;
156
157 printf("i=%d\n", i);
158 return PR_FALSE;
159 }
160
161static PRBool test_rfind_3()
162 {
163 const char text[] = "urn:mozilla:locale:en-US:necko";
164 nsCAutoString value(text);
165 PRInt32 i = value.RFind(":");
166 if (i == 24)
167 return PR_TRUE;
168
169 printf("i=%d\n", i);
170 return PR_FALSE;
171 }
172
173static PRBool test_rfind_4()
174 {
175 nsCString value("a.msf");
176 PRInt32 i = value.RFind(".msf");
177 if (i != 1)
178 {
179 printf("i=%d\n", i);
180 return PR_FALSE;
181 }
182
183 return PR_TRUE;
184 }
185
186static PRBool test_distance()
187 {
188 const char text[] = "abc-xyz";
189 nsCString s(text);
190 nsCString::const_iterator begin, end;
191 s.BeginReading(begin);
192 s.EndReading(end);
193 size_t d = Distance(begin, end);
194 PRBool r = (d == sizeof(text)-1);
195 if (!r)
196 printf("d=%zu\n", d);
197 return r;
198 }
199
200static PRBool test_length()
201 {
202 const char text[] = "abc-xyz";
203 nsCString s(text);
204 size_t d = s.Length();
205 PRBool r = (d == sizeof(text)-1);
206 if (!r)
207 printf("d=%zu\n", d);
208 return r;
209 }
210
211static PRBool test_trim()
212 {
213 const char text[] = " a\t $ ";
214 const char set[] = " \t$";
215
216 nsCString s(text);
217 s.Trim(set);
218 PRBool r = strcmp(s.get(), "a") == 0;
219 if (!r)
220 printf("[s=%s]\n", s.get());
221 return r;
222 }
223
224static PRBool test_replace_substr()
225 {
226 const char text[] = "abc-ppp-qqq-ppp-xyz";
227 nsCString s(text);
228 s.ReplaceSubstring("ppp", "www");
229 PRBool r = strcmp(s.get(), "abc-www-qqq-www-xyz") == 0;
230 if (!r)
231 {
232 printf("[s=%s]\n", s.get());
233 return PR_FALSE;
234 }
235
236 s.Assign("foobar");
237 s.ReplaceSubstring("foo", "bar");
238 s.ReplaceSubstring("bar", "");
239 r = strcmp(s.get(), "") == 0;
240 if (!r)
241 {
242 printf("[s=%s]\n", s.get());
243 return PR_FALSE;
244 }
245
246 s.Assign("foofoofoo");
247 s.ReplaceSubstring("foo", "foo");
248 r = strcmp(s.get(), "foofoofoo") == 0;
249 if (!r)
250 {
251 printf("[s=%s]\n", s.get());
252 return PR_FALSE;
253 }
254
255 s.Assign("foofoofoo");
256 s.ReplaceSubstring("of", "fo");
257 r = strcmp(s.get(), "fofoofooo") == 0;
258 if (!r)
259 {
260 printf("[s=%s]\n", s.get());
261 return PR_FALSE;
262 }
263
264 return PR_TRUE;
265 }
266
267static PRBool test_replace_substr_2()
268 {
269 const char *oldName = nsnull;
270 const char *newName = "user";
271 nsString acctName; acctName.AssignLiteral("forums.foo.com");
272 nsAutoString newAcctName, oldVal, newVal;
273 oldVal.AssignWithConversion(oldName);
274 newVal.AssignWithConversion(newName);
275 newAcctName.Assign(acctName);
276
277 // here, oldVal is empty. we are testing that this function
278 // does not hang. see bug 235355.
279 newAcctName.ReplaceSubstring(oldVal, newVal);
280
281 // we expect that newAcctName will be unchanged.
282 if (!newAcctName.Equals(acctName))
283 return PR_FALSE;
284
285 return PR_TRUE;
286 }
287
288static PRBool test_strip_ws()
289 {
290 const char text[] = " a $ ";
291 nsCString s(text);
292 s.StripWhitespace();
293 PRBool r = strcmp(s.get(), "a$") == 0;
294 if (!r)
295 printf("[s=%s]\n", s.get());
296 return r;
297 }
298
299static PRBool test_equals_ic()
300 {
301 nsCString s;
302 PRBool r = s.LowerCaseEqualsLiteral("view-source");
303 if (r)
304 printf("[r=%d]\n", r);
305 return !r;
306 }
307
308static PRBool test_fixed_string()
309 {
310 char buf[256] = "hello world";
311
312 nsFixedCString s(buf, sizeof(buf));
313
314 if (s.Length() != strlen(buf))
315 return PR_FALSE;
316
317 if (strcmp(s.get(), buf) != 0)
318 return PR_FALSE;
319
320 s.Assign("foopy doopy doo");
321 if (s.get() != buf)
322 return PR_FALSE;
323
324 return PR_TRUE;
325 }
326
327static PRBool test_concat()
328 {
329 nsCString bar("bar");
330 const nsACString& barRef = bar;
331
332 const nsPromiseFlatCString& result =
333 PromiseFlatCString(NS_LITERAL_CSTRING("foo") +
334 NS_LITERAL_CSTRING(",") +
335 barRef);
336 if (strcmp(result.get(), "foo,bar") == 0)
337 return PR_TRUE;
338
339 printf("[result=%s]\n", result.get());
340 return PR_FALSE;
341 }
342
343static PRBool test_concat_2()
344 {
345 nsCString fieldTextStr("xyz");
346 nsCString text("text");
347 const nsACString& aText = text;
348
349 nsCAutoString result( fieldTextStr + aText );
350
351 if (strcmp(result.get(), "xyztext") == 0)
352 return PR_TRUE;
353
354 printf("[result=%s]\n", result.get());
355 return PR_FALSE;
356 }
357
358#if 0
359PRBool test_concat_3()
360 {
361 nsCString a("a"), b("b");
362
363 // THIS DOES NOT COMPILE
364 const nsACString& r = a + b;
365
366 return PR_TRUE;
367 }
368#endif
369
370static PRBool test_xpidl_string()
371 {
372 nsXPIDLCString a, b;
373 a = b;
374 if (a != b)
375 return PR_FALSE;
376
377 a.Adopt(0);
378 if (a != b)
379 return PR_FALSE;
380
381 a.Append("foopy");
382 a.Assign(b);
383 if (a != b)
384 return PR_FALSE;
385
386 a.Insert("", 0);
387 a.Assign(b);
388 if (a != b)
389 return PR_FALSE;
390
391 const char text[] = "hello world";
392 *getter_Copies(a) = nsCRT::strdup(text);
393 if (strcmp(a, text) != 0)
394 return PR_FALSE;
395
396 b = a;
397 if (strcmp(a, b) != 0)
398 return PR_FALSE;
399
400 a.Adopt(0);
401 nsACString::const_iterator begin, end;
402 a.BeginReading(begin);
403 a.EndReading(end);
404 char *r = ToNewCString(Substring(begin, end));
405 if (strcmp(r, "") != 0)
406 return PR_FALSE;
407 nsMemory::Free(r);
408
409 a.Adopt(0);
410 if (a != (const char*) 0)
411 return PR_FALSE;
412
413 /*
414 PRInt32 index = a.FindCharInSet("xyz");
415 if (index != kNotFound)
416 return PR_FALSE;
417 */
418
419 return PR_TRUE;
420 }
421
422static PRBool test_empty_assign()
423 {
424 nsCString a;
425 a.AssignLiteral("");
426
427 a.AppendLiteral("");
428
429 nsCString b;
430 b.SetCapacity(0);
431 return PR_TRUE;
432 }
433
434static PRBool test_set_length()
435 {
436 const char kText[] = "Default Plugin";
437 nsCString buf;
438 buf.SetCapacity(sizeof(kText)-1);
439 buf.Assign(kText);
440 buf.SetLength(sizeof(kText)-1);
441 if (strcmp(buf.get(), kText) != 0)
442 return PR_FALSE;
443 return PR_TRUE;
444 }
445
446static PRBool test_substring()
447 {
448 nsCString super("hello world"), sub("hello");
449
450 // this tests that |super| starts with |sub|,
451
452 PRBool r = sub.Equals(StringHead(super, sub.Length()));
453 if (!r)
454 return PR_FALSE;
455
456 // and verifies that |sub| does not start with |super|.
457
458 r = super.Equals(StringHead(sub, super.Length()));
459 if (r)
460 return PR_FALSE;
461
462 return PR_TRUE;
463 }
464
465static PRBool test_appendint64()
466 {
467 nsCString str;
468
469 PRInt64 max = LL_MaxInt();
470 static const char max_expected[] = "9223372036854775807";
471 PRInt64 min = LL_MinInt();
472 static const char min_expected[] = "-9223372036854775808";
473 static const char min_expected_oct[] = "1000000000000000000000";
474 PRInt64 maxint_plus1 = LL_INIT(1, 0);
475 static const char maxint_plus1_expected[] = "4294967296";
476 static const char maxint_plus1_expected_x[] = "100000000";
477
478 str.AppendInt(max);
479
480 if (!str.Equals(max_expected)) {
481 fprintf(stderr, "Error appending LL_MaxInt(): Got %s\n", str.get());
482 return PR_FALSE;
483 }
484
485 str.Truncate();
486 str.AppendInt(min);
487 if (!str.Equals(min_expected)) {
488 fprintf(stderr, "Error appending LL_MinInt(): Got %s\n", str.get());
489 return PR_FALSE;
490 }
491 str.Truncate();
492 str.AppendInt(min, 8);
493 if (!str.Equals(min_expected_oct)) {
494 fprintf(stderr, "Error appending LL_MinInt() (oct): Got %s\n", str.get());
495 return PR_FALSE;
496 }
497
498
499 str.Truncate();
500 str.AppendInt(maxint_plus1);
501 if (!str.Equals(maxint_plus1_expected)) {
502 fprintf(stderr, "Error appending PR_UINT32_MAX + 1: Got %s\n", str.get());
503 return PR_FALSE;
504 }
505 str.Truncate();
506 str.AppendInt(maxint_plus1, 16);
507 if (!str.Equals(maxint_plus1_expected_x)) {
508 fprintf(stderr, "Error appending PR_UINT32_MAX + 1 (hex): Got %s\n", str.get());
509 return PR_FALSE;
510 }
511
512
513 return PR_TRUE;
514 }
515
516static PRBool test_findcharinset()
517 {
518 nsCString buf("hello, how are you?");
519
520 PRInt32 index = buf.FindCharInSet(",?", 5);
521 if (index != 5)
522 return PR_FALSE;
523
524 index = buf.FindCharInSet("helo", 0);
525 if (index != 0)
526 return PR_FALSE;
527
528 index = buf.FindCharInSet("z?", 6);
529 if (index != (PRInt32) buf.Length()-1)
530 return PR_FALSE;
531
532 return PR_TRUE;
533 }
534
535static PRBool test_rfindcharinset()
536 {
537 nsCString buf("hello, how are you?");
538
539 PRInt32 index = buf.RFindCharInSet(",?", 5);
540 if (index != 5)
541 return PR_FALSE;
542
543 index = buf.RFindCharInSet("helo", 0);
544 if (index != 0)
545 return PR_FALSE;
546
547 index = buf.RFindCharInSet("z?", 6);
548 if (index != kNotFound)
549 return PR_FALSE;
550
551 index = buf.RFindCharInSet("l", 5);
552 if (index != 3)
553 return PR_FALSE;
554
555 buf.Assign("abcdefghijkabc");
556
557 index = buf.RFindCharInSet("ab");
558 if (index != 12)
559 return PR_FALSE;
560
561 index = buf.RFindCharInSet("ab", 11);
562 if (index != 11)
563 return PR_FALSE;
564
565 index = buf.RFindCharInSet("ab", 10);
566 if (index != 1)
567 return PR_FALSE;
568
569 index = buf.RFindCharInSet("ab", 0);
570 if (index != 0)
571 return PR_FALSE;
572
573 index = buf.RFindCharInSet("cd", 1);
574 if (index != kNotFound)
575 return PR_FALSE;
576
577 index = buf.RFindCharInSet("h");
578 if (index != 7)
579 return PR_FALSE;
580
581 return PR_TRUE;
582 }
583
584//----
585
586typedef PRBool (*TestFunc)();
587
588static const struct Test
589 {
590 const char* name;
591 TestFunc func;
592 }
593tests[] =
594 {
595 { "test_assign", test_assign },
596 { "test_assign_c", test_assign_c },
597 { "test1", test1 },
598 { "test2", test2 },
599 { "test_find", test_find },
600 { "test_rfind", test_rfind },
601 { "test_rfind_2", test_rfind_2 },
602 { "test_rfind_3", test_rfind_3 },
603 { "test_rfind_4", test_rfind_4 },
604 { "test_distance", test_distance },
605 { "test_length", test_length },
606 { "test_trim", test_trim },
607 { "test_replace_substr", test_replace_substr },
608 { "test_replace_substr_2", test_replace_substr_2 },
609 { "test_strip_ws", test_strip_ws },
610 { "test_equals_ic", test_equals_ic },
611 { "test_fixed_string", test_fixed_string },
612 { "test_concat", test_concat },
613 { "test_concat_2", test_concat_2 },
614 { "test_xpidl_string", test_xpidl_string },
615 { "test_empty_assign", test_empty_assign },
616 { "test_set_length", test_set_length },
617 { "test_substring", test_substring },
618 { "test_appendint64", test_appendint64 },
619 { "test_findcharinset", test_findcharinset },
620 { "test_rfindcharinset", test_rfindcharinset },
621 { nsnull, nsnull }
622 };
623
624//----
625
626int main(int argc, char **argv)
627 {
628 int count = 1;
629 if (argc > 1)
630 count = atoi(argv[1]);
631
632 while (count--)
633 {
634 for (const Test* t = tests; t->name != nsnull; ++t)
635 {
636 printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--");
637 }
638 }
639
640 return 0;
641 }
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette