VirtualBox

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

Last change on this file since 62330 was 40300, checked in by vboxsync, 13 years ago

libs/xpcom: format strings in testcases (unimportant)

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