Add copy of .ttf font with .eot extension for testing
[wine-gecko.git] / xpcom / tests / TestStrings.cpp
blobc916a2bda9a39a4e87de0bb153db52a21d43300a
1 /* vim:set ts=2 sw=2 et cindent: */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
15 * The Original Code is Mozilla.
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.
21 * Contributor(s):
22 * Darin Fisher <darin@meer.net>
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.
36 * ***** END LICENSE BLOCK ***** */
38 #include <stdio.h>
39 #include "nsString.h"
40 #include "nsStringBuffer.h"
41 #include "nsReadableUtils.h"
42 #include "nsCRT.h"
44 namespace TestStrings {
46 void test_assign_helper(const nsACString& in, nsACString &_retval)
48 _retval = in;
51 PRBool test_assign()
53 nsCString result;
54 test_assign_helper(NS_LITERAL_CSTRING("a") + NS_LITERAL_CSTRING("b"), result);
55 PRBool r = strcmp(result.get(), "ab") == 0;
56 if (!r)
57 printf("[result=%s]\n", result.get());
58 return r;
61 PRBool test_assign_c()
63 nsCString c; c.Assign('c');
64 PRBool r = strcmp(c.get(), "c") == 0;
65 if (!r)
66 printf("[result=%s]\n", c.get());
67 return r;
70 PRBool test1()
72 NS_NAMED_LITERAL_STRING(empty, "");
73 const nsAString& aStr = empty;
75 nsAutoString buf(aStr);
77 PRInt32 n = buf.FindChar(',');
79 n = buf.Length();
81 buf.Cut(0, n + 1);
82 n = buf.FindChar(',');
84 if (n != kNotFound)
85 printf("n=%d\n", n);
87 return n == kNotFound;
90 PRBool test2()
92 nsCString data("hello world");
93 const nsACString& aStr = data;
95 nsCString temp(aStr);
96 temp.Cut(0, 6);
98 PRBool r = strcmp(temp.get(), "world") == 0;
99 if (!r)
100 printf("[temp=%s]\n", temp.get());
101 return r;
104 PRBool test_find()
106 nsCString src("<!DOCTYPE blah blah blah>");
108 PRInt32 i = src.Find("DOCTYPE", PR_TRUE, 2, 1);
109 if (i == 2)
110 return PR_TRUE;
112 printf("i=%d\n", i);
113 return PR_FALSE;
116 PRBool test_rfind()
118 const char text[] = "<!DOCTYPE blah blah blah>";
119 const char term[] = "bLaH";
120 nsCString src(text);
121 PRInt32 i;
123 i = src.RFind(term, PR_TRUE, 3, -1);
124 if (i != kNotFound)
126 printf("unexpected result searching from offset=3, i=%d\n", i);
127 return PR_FALSE;
130 i = src.RFind(term, PR_TRUE, -1, -1);
131 if (i != 20)
133 printf("unexpected result searching from offset=-1, i=%d\n", i);
134 return PR_FALSE;
137 i = src.RFind(term, PR_TRUE, 13, -1);
138 if (i != 10)
140 printf("unexpected result searching from offset=13, i=%d\n", i);
141 return PR_FALSE;
144 i = src.RFind(term, PR_TRUE, 22, 3);
145 if (i != 20)
147 printf("unexpected result searching from offset=22, i=%d\n", i);
148 return PR_FALSE;
151 return PR_TRUE;
154 PRBool test_rfind_2()
156 const char text[] = "<!DOCTYPE blah blah blah>";
157 nsCString src(text);
158 PRInt32 i = src.RFind("TYPE", PR_FALSE, 5, -1);
159 if (i == 5)
160 return PR_TRUE;
162 printf("i=%d\n", i);
163 return PR_FALSE;
166 PRBool test_rfind_3()
168 const char text[] = "urn:mozilla:locale:en-US:necko";
169 nsCAutoString value(text);
170 PRInt32 i = value.RFind(":");
171 if (i == 24)
172 return PR_TRUE;
174 printf("i=%d\n", i);
175 return PR_FALSE;
178 PRBool test_rfind_4()
180 nsCString value("a.msf");
181 PRInt32 i = value.RFind(".msf");
182 if (i != 1)
184 printf("i=%d\n", i);
185 return PR_FALSE;
188 return PR_TRUE;
191 PRBool test_findinreadable()
193 const char text[] = "jar:jar:file:///c:/software/mozilla/mozilla_2006_02_21.jar!/browser/chrome/classic.jar!/";
194 nsCAutoString value(text);
196 nsACString::const_iterator begin, end;
197 value.BeginReading(begin);
198 value.EndReading(end);
199 nsACString::const_iterator delim_begin (begin),
200 delim_end (end);
202 // Search for last !/ at the end of the string
203 if (!FindInReadable(NS_LITERAL_CSTRING("!/"), delim_begin, delim_end))
204 return PR_FALSE;
205 char *r = ToNewCString(Substring(delim_begin, delim_end));
206 // Should match the first "!/" but not the last
207 if ((delim_end == end) || (strcmp(r, "!/")!=0))
209 printf("r = %s\n", r);
210 nsMemory::Free(r);
211 return PR_FALSE;
213 nsMemory::Free(r);
215 delim_begin = begin;
216 delim_end = end;
218 // Search for first jar:
219 if (!FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end))
220 return PR_FALSE;
222 r = ToNewCString(Substring(delim_begin, delim_end));
223 // Should not match the first jar:, but the second one
224 if ((delim_begin != begin) || (strcmp(r, "jar:")!=0))
226 printf("r = %s\n", r);
227 nsMemory::Free(r);
228 return PR_FALSE;
230 nsMemory::Free(r);
232 // Search for jar: in a Substring
233 delim_begin = begin; delim_begin++;
234 delim_end = end;
235 if (!FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end))
236 return PR_FALSE;
238 r = ToNewCString(Substring(delim_begin, delim_end));
239 // Should not match the first jar:, but the second one
240 if ((delim_begin == begin) || (strcmp(r, "jar:")!=0))
242 printf("r = %s\n", r);
243 nsMemory::Free(r);
244 return PR_FALSE;
246 nsMemory::Free(r);
248 // Should not find a match
249 if (FindInReadable(NS_LITERAL_CSTRING("gecko"), delim_begin, delim_end))
250 return PR_FALSE;
252 // When no match is found, range should be empty
253 if (delim_begin != delim_end)
254 return PR_FALSE;
256 // Should not find a match (search not beyond Substring)
257 delim_begin = begin; for (int i=0;i<6;i++) delim_begin++;
258 delim_end = end;
259 if (FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end))
260 return PR_FALSE;
262 // When no match is found, range should be empty
263 if (delim_begin != delim_end)
264 return PR_FALSE;
266 // Should not find a match (search not beyond Substring)
267 delim_begin = begin;
268 delim_end = end; for (int i=0;i<7;i++) delim_end--;
269 if (FindInReadable(NS_LITERAL_CSTRING("classic"), delim_begin, delim_end))
270 return PR_FALSE;
272 // When no match is found, range should be empty
273 if (delim_begin != delim_end)
274 return PR_FALSE;
276 return PR_TRUE;
279 PRBool test_rfindinreadable()
281 const char text[] = "jar:jar:file:///c:/software/mozilla/mozilla_2006_02_21.jar!/browser/chrome/classic.jar!/";
282 nsCAutoString value(text);
284 nsACString::const_iterator begin, end;
285 value.BeginReading(begin);
286 value.EndReading(end);
287 nsACString::const_iterator delim_begin (begin),
288 delim_end (end);
290 // Search for last !/ at the end of the string
291 if (!RFindInReadable(NS_LITERAL_CSTRING("!/"), delim_begin, delim_end))
292 return PR_FALSE;
293 char *r = ToNewCString(Substring(delim_begin, delim_end));
294 // Should match the last "!/"
295 if ((delim_end != end) || (strcmp(r, "!/")!=0))
297 printf("r = %s\n", r);
298 nsMemory::Free(r);
299 return PR_FALSE;
301 nsMemory::Free(r);
303 delim_begin = begin;
304 delim_end = end;
306 // Search for last jar: but not the first one...
307 if (!RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end))
308 return PR_FALSE;
310 r = ToNewCString(Substring(delim_begin, delim_end));
311 // Should not match the first jar:, but the second one
312 if ((delim_begin == begin) || (strcmp(r, "jar:")!=0))
314 printf("r = %s\n", r);
315 nsMemory::Free(r);
316 return PR_FALSE;
318 nsMemory::Free(r);
320 // Search for jar: in a Substring
321 delim_begin = begin;
322 delim_end = begin; for (int i=0;i<6;i++) delim_end++;
323 if (!RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)) {
324 printf("Search for jar: in a Substring\n");
325 return PR_FALSE;
328 r = ToNewCString(Substring(delim_begin, delim_end));
329 // Should not match the first jar:, but the second one
330 if ((delim_begin != begin) || (strcmp(r, "jar:")!=0))
332 printf("r = %s\n", r);
333 nsMemory::Free(r);
334 return PR_FALSE;
336 nsMemory::Free(r);
338 // Should not find a match
339 delim_begin = begin;
340 delim_end = end;
341 if (RFindInReadable(NS_LITERAL_CSTRING("gecko"), delim_begin, delim_end)) {
342 printf("Should not find a match\n");
343 return PR_FALSE;
346 // When no match is found, range should be empty
347 if (delim_begin != delim_end) {
348 printf("1: When no match is found, range should be empty\n");
349 return PR_FALSE;
352 // Should not find a match (search not before Substring)
353 delim_begin = begin; for (int i=0;i<6;i++) delim_begin++;
354 delim_end = end;
355 if (RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin, delim_end)) {
356 printf("Should not find a match (search not before Substring)\n");
357 return PR_FALSE;
360 // When no match is found, range should be empty
361 if (delim_begin != delim_end) {
362 printf("2: When no match is found, range should be empty\n");
363 return PR_FALSE;
366 // Should not find a match (search not beyond Substring)
367 delim_begin = begin;
368 delim_end = end; for (int i=0;i<7;i++) delim_end--;
369 if (RFindInReadable(NS_LITERAL_CSTRING("classic"), delim_begin, delim_end)) {
370 printf("Should not find a match (search not beyond Substring)\n");
371 return PR_FALSE;
374 // When no match is found, range should be empty
375 if (delim_begin != delim_end) {
376 printf("3: When no match is found, range should be empty\n");
377 return PR_FALSE;
380 return PR_TRUE;
383 PRBool test_distance()
385 const char text[] = "abc-xyz";
386 nsCString s(text);
387 nsCString::const_iterator begin, end;
388 s.BeginReading(begin);
389 s.EndReading(end);
390 size_t d = Distance(begin, end);
391 PRBool r = (d == sizeof(text)-1);
392 if (!r)
393 printf("d=%u\n", d);
394 return r;
397 PRBool test_length()
399 const char text[] = "abc-xyz";
400 nsCString s(text);
401 size_t d = s.Length();
402 PRBool r = (d == sizeof(text)-1);
403 if (!r)
404 printf("d=%u\n", d);
405 return r;
408 PRBool test_trim()
410 const char text[] = " a\t $ ";
411 const char set[] = " \t$";
413 nsCString s(text);
414 s.Trim(set);
415 PRBool r = strcmp(s.get(), "a") == 0;
416 if (!r)
417 printf("[s=%s]\n", s.get());
418 return r;
421 PRBool test_replace_substr()
423 const char text[] = "abc-ppp-qqq-ppp-xyz";
424 nsCString s(text);
425 s.ReplaceSubstring("ppp", "www");
426 PRBool r = strcmp(s.get(), "abc-www-qqq-www-xyz") == 0;
427 if (!r)
429 printf("[s=%s]\n", s.get());
430 return PR_FALSE;
433 s.Assign("foobar");
434 s.ReplaceSubstring("foo", "bar");
435 s.ReplaceSubstring("bar", "");
436 r = strcmp(s.get(), "") == 0;
437 if (!r)
439 printf("[s=%s]\n", s.get());
440 return PR_FALSE;
443 s.Assign("foofoofoo");
444 s.ReplaceSubstring("foo", "foo");
445 r = strcmp(s.get(), "foofoofoo") == 0;
446 if (!r)
448 printf("[s=%s]\n", s.get());
449 return PR_FALSE;
452 s.Assign("foofoofoo");
453 s.ReplaceSubstring("of", "fo");
454 r = strcmp(s.get(), "fofoofooo") == 0;
455 if (!r)
457 printf("[s=%s]\n", s.get());
458 return PR_FALSE;
461 return PR_TRUE;
464 PRBool test_replace_substr_2()
466 const char *oldName = nsnull;
467 const char *newName = "user";
468 nsString acctName; acctName.AssignLiteral("forums.foo.com");
469 nsAutoString newAcctName, oldVal, newVal;
470 oldVal.AssignWithConversion(oldName);
471 newVal.AssignWithConversion(newName);
472 newAcctName.Assign(acctName);
474 // here, oldVal is empty. we are testing that this function
475 // does not hang. see bug 235355.
476 newAcctName.ReplaceSubstring(oldVal, newVal);
478 // we expect that newAcctName will be unchanged.
479 if (!newAcctName.Equals(acctName))
480 return PR_FALSE;
482 return PR_TRUE;
485 PRBool test_strip_ws()
487 const char text[] = " a $ ";
488 nsCString s(text);
489 s.StripWhitespace();
490 PRBool r = strcmp(s.get(), "a$") == 0;
491 if (!r)
492 printf("[s=%s]\n", s.get());
493 return r;
496 PRBool test_equals_ic()
498 nsCString s;
499 PRBool r = s.LowerCaseEqualsLiteral("view-source");
500 if (r)
501 printf("[r=%d]\n", r);
502 return !r;
505 PRBool test_fixed_string()
507 char buf[256] = "hello world";
509 nsFixedCString s(buf, sizeof(buf));
511 if (s.Length() != strlen(buf))
512 return PR_FALSE;
514 if (strcmp(s.get(), buf) != 0)
515 return PR_FALSE;
517 s.Assign("foopy doopy doo");
518 if (s.get() != buf)
519 return PR_FALSE;
521 return PR_TRUE;
524 PRBool test_concat()
526 nsCString bar("bar");
527 const nsACString& barRef = bar;
529 const nsPromiseFlatCString& result =
530 PromiseFlatCString(NS_LITERAL_CSTRING("foo") +
531 NS_LITERAL_CSTRING(",") +
532 barRef);
533 if (strcmp(result.get(), "foo,bar") == 0)
534 return PR_TRUE;
536 printf("[result=%s]\n", result.get());
537 return PR_FALSE;
540 PRBool test_concat_2()
542 nsCString fieldTextStr("xyz");
543 nsCString text("text");
544 const nsACString& aText = text;
546 nsCAutoString result( fieldTextStr + aText );
548 if (strcmp(result.get(), "xyztext") == 0)
549 return PR_TRUE;
551 printf("[result=%s]\n", result.get());
552 return PR_FALSE;
555 #if 0
556 PRBool test_concat_3()
558 nsCString a("a"), b("b");
560 // THIS DOES NOT COMPILE
561 const nsACString& r = a + b;
563 return PR_TRUE;
565 #endif
567 PRBool test_xpidl_string()
569 nsXPIDLCString a, b;
570 a = b;
571 if (a != b)
572 return PR_FALSE;
574 a.Adopt(0);
575 if (a != b)
576 return PR_FALSE;
578 a.Append("foopy");
579 a.Assign(b);
580 if (a != b)
581 return PR_FALSE;
583 a.Insert("", 0);
584 a.Assign(b);
585 if (a != b)
586 return PR_FALSE;
588 const char text[] = "hello world";
589 *getter_Copies(a) = nsCRT::strdup(text);
590 if (strcmp(a, text) != 0)
591 return PR_FALSE;
593 b = a;
594 if (strcmp(a, b) != 0)
595 return PR_FALSE;
597 a.Adopt(0);
598 nsACString::const_iterator begin, end;
599 a.BeginReading(begin);
600 a.EndReading(end);
601 char *r = ToNewCString(Substring(begin, end));
602 if (strcmp(r, "") != 0)
603 return PR_FALSE;
604 nsMemory::Free(r);
606 a.Adopt(0);
607 if (a != (const char*) 0)
608 return PR_FALSE;
611 PRInt32 index = a.FindCharInSet("xyz");
612 if (index != kNotFound)
613 return PR_FALSE;
616 return PR_TRUE;
619 PRBool test_empty_assign()
621 nsCString a;
622 a.AssignLiteral("");
624 a.AppendLiteral("");
626 nsCString b;
627 b.SetCapacity(0);
628 return PR_TRUE;
631 PRBool test_set_length()
633 const char kText[] = "Default Plugin";
634 nsCString buf;
635 buf.SetCapacity(sizeof(kText)-1);
636 buf.Assign(kText);
637 buf.SetLength(sizeof(kText)-1);
638 if (strcmp(buf.get(), kText) != 0)
639 return PR_FALSE;
640 return PR_TRUE;
643 PRBool test_substring()
645 nsCString super("hello world"), sub("hello");
647 // this tests that |super| starts with |sub|,
649 PRBool r = sub.Equals(StringHead(super, sub.Length()));
650 if (!r)
651 return PR_FALSE;
653 // and verifies that |sub| does not start with |super|.
655 r = super.Equals(StringHead(sub, super.Length()));
656 if (r)
657 return PR_FALSE;
659 return PR_TRUE;
662 PRBool test_appendint64()
664 nsCString str;
666 PRInt64 max = LL_MaxInt();
667 static const char max_expected[] = "9223372036854775807";
668 PRInt64 min = LL_MinInt();
669 static const char min_expected[] = "-9223372036854775808";
670 static const char min_expected_oct[] = "1000000000000000000000";
671 PRInt64 maxint_plus1 = LL_INIT(1, 0);
672 static const char maxint_plus1_expected[] = "4294967296";
673 static const char maxint_plus1_expected_x[] = "100000000";
675 str.AppendInt(max);
677 if (!str.Equals(max_expected)) {
678 fprintf(stderr, "Error appending LL_MaxInt(): Got %s\n", str.get());
679 return PR_FALSE;
682 str.Truncate();
683 str.AppendInt(min);
684 if (!str.Equals(min_expected)) {
685 fprintf(stderr, "Error appending LL_MinInt(): Got %s\n", str.get());
686 return PR_FALSE;
688 str.Truncate();
689 str.AppendInt(min, 8);
690 if (!str.Equals(min_expected_oct)) {
691 fprintf(stderr, "Error appending LL_MinInt() (oct): Got %s\n", str.get());
692 return PR_FALSE;
696 str.Truncate();
697 str.AppendInt(maxint_plus1);
698 if (!str.Equals(maxint_plus1_expected)) {
699 fprintf(stderr, "Error appending PR_UINT32_MAX + 1: Got %s\n", str.get());
700 return PR_FALSE;
702 str.Truncate();
703 str.AppendInt(maxint_plus1, 16);
704 if (!str.Equals(maxint_plus1_expected_x)) {
705 fprintf(stderr, "Error appending PR_UINT32_MAX + 1 (hex): Got %s\n", str.get());
706 return PR_FALSE;
710 return PR_TRUE;
713 PRBool test_appendfloat()
715 nsCString str;
716 double bigdouble = 11223344556.66;
717 static const char double_expected[] = "11223344556.66";
718 static const char float_expected[] = "0.01";
720 // AppendFloat is used to append doubles, therefore the precision must be
721 // large enough (see bug 327719)
722 str.AppendFloat( bigdouble );
723 if (!str.Equals(double_expected)) {
724 fprintf(stderr, "Error appending a big double: Got %s\n", str.get());
725 return PR_FALSE;
728 str.Truncate();
729 // AppendFloat is used to append floats (bug 327719 #27)
730 str.AppendFloat( 0.1f * 0.1f );
731 if (!str.Equals(float_expected)) {
732 fprintf(stderr, "Error appending a float: Got %s\n", str.get());
733 return PR_FALSE;
736 return PR_TRUE;
739 PRBool test_findcharinset()
741 nsCString buf("hello, how are you?");
743 PRInt32 index = buf.FindCharInSet(",?", 5);
744 if (index != 5)
745 return PR_FALSE;
747 index = buf.FindCharInSet("helo", 0);
748 if (index != 0)
749 return PR_FALSE;
751 index = buf.FindCharInSet("z?", 6);
752 if (index != (PRInt32) buf.Length()-1)
753 return PR_FALSE;
755 return PR_TRUE;
758 PRBool test_rfindcharinset()
760 nsCString buf("hello, how are you?");
762 PRInt32 index = buf.RFindCharInSet(",?", 5);
763 if (index != 5)
764 return PR_FALSE;
766 index = buf.RFindCharInSet("helo", 0);
767 if (index != 0)
768 return PR_FALSE;
770 index = buf.RFindCharInSet("z?", 6);
771 if (index != kNotFound)
772 return PR_FALSE;
774 index = buf.RFindCharInSet("l", 5);
775 if (index != 3)
776 return PR_FALSE;
778 buf.Assign("abcdefghijkabc");
780 index = buf.RFindCharInSet("ab");
781 if (index != 12)
782 return PR_FALSE;
784 index = buf.RFindCharInSet("ab", 11);
785 if (index != 11)
786 return PR_FALSE;
788 index = buf.RFindCharInSet("ab", 10);
789 if (index != 1)
790 return PR_FALSE;
792 index = buf.RFindCharInSet("ab", 0);
793 if (index != 0)
794 return PR_FALSE;
796 index = buf.RFindCharInSet("cd", 1);
797 if (index != kNotFound)
798 return PR_FALSE;
800 index = buf.RFindCharInSet("h");
801 if (index != 7)
802 return PR_FALSE;
804 return PR_TRUE;
807 PRBool test_stringbuffer()
809 const char kData[] = "hello world";
811 nsStringBuffer *buf;
813 buf = nsStringBuffer::Alloc(sizeof(kData));
814 if (!buf)
815 return PR_FALSE;
816 buf->Release();
818 buf = nsStringBuffer::Alloc(sizeof(kData));
819 if (!buf)
820 return PR_FALSE;
821 char *data = (char *) buf->Data();
822 memcpy(data, kData, sizeof(kData));
824 nsCString str;
825 buf->ToString(sizeof(kData)-1, str);
827 nsStringBuffer *buf2;
828 buf2 = nsStringBuffer::FromString(str);
830 PRBool rv = (buf == buf2);
832 buf->Release();
833 return rv;
836 PRBool test_voided()
838 const char kData[] = "hello world";
840 nsXPIDLCString str;
841 if (str)
842 return PR_FALSE;
843 if (!str.IsVoid())
844 return PR_FALSE;
845 if (!str.IsEmpty())
846 return PR_FALSE;
848 str.Assign(kData);
849 if (strcmp(str, kData) != 0)
850 return PR_FALSE;
852 str.SetIsVoid(PR_TRUE);
853 if (str)
854 return PR_FALSE;
855 if (!str.IsVoid())
856 return PR_FALSE;
857 if (!str.IsEmpty())
858 return PR_FALSE;
860 str.SetIsVoid(PR_FALSE);
861 if (strcmp(str, "") != 0)
862 return PR_FALSE;
864 return PR_TRUE;
867 PRBool test_voided_autostr()
869 const char kData[] = "hello world";
871 nsCAutoString str;
872 if (str.IsVoid())
873 return PR_FALSE;
874 if (!str.IsEmpty())
875 return PR_FALSE;
877 str.Assign(kData);
878 if (strcmp(str.get(), kData) != 0)
879 return PR_FALSE;
881 str.SetIsVoid(PR_TRUE);
882 if (!str.IsVoid())
883 return PR_FALSE;
884 if (!str.IsEmpty())
885 return PR_FALSE;
887 str.Assign(kData);
888 if (str.IsVoid())
889 return PR_FALSE;
890 if (str.IsEmpty())
891 return PR_FALSE;
892 if (strcmp(str.get(), kData) != 0)
893 return PR_FALSE;
895 return PR_TRUE;
898 //----
900 typedef PRBool (*TestFunc)();
902 static const struct Test
904 const char* name;
905 TestFunc func;
907 tests[] =
909 { "test_assign", test_assign },
910 { "test_assign_c", test_assign_c },
911 { "test1", test1 },
912 { "test2", test2 },
913 { "test_find", test_find },
914 { "test_rfind", test_rfind },
915 { "test_rfind_2", test_rfind_2 },
916 { "test_rfind_3", test_rfind_3 },
917 { "test_rfind_4", test_rfind_4 },
918 { "test_findinreadable", test_findinreadable },
919 { "test_rfindinreadable", test_rfindinreadable },
920 { "test_distance", test_distance },
921 { "test_length", test_length },
922 { "test_trim", test_trim },
923 { "test_replace_substr", test_replace_substr },
924 { "test_replace_substr_2", test_replace_substr_2 },
925 { "test_strip_ws", test_strip_ws },
926 { "test_equals_ic", test_equals_ic },
927 { "test_fixed_string", test_fixed_string },
928 { "test_concat", test_concat },
929 { "test_concat_2", test_concat_2 },
930 { "test_xpidl_string", test_xpidl_string },
931 { "test_empty_assign", test_empty_assign },
932 { "test_set_length", test_set_length },
933 { "test_substring", test_substring },
934 { "test_appendint64", test_appendint64 },
935 { "test_appendfloat", test_appendfloat },
936 { "test_findcharinset", test_findcharinset },
937 { "test_rfindcharinset", test_rfindcharinset },
938 { "test_stringbuffer", test_stringbuffer },
939 { "test_voided", test_voided },
940 { "test_voided_autostr", test_voided_autostr },
941 { nsnull, nsnull }
946 using namespace TestStrings;
948 int main(int argc, char **argv)
950 int count = 1;
951 if (argc > 1)
952 count = atoi(argv[1]);
954 while (count--)
956 for (const Test* t = tests; t->name != nsnull; ++t)
958 printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--");
962 return 0;