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
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.
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 ***** */
40 #include "nsStringBuffer.h"
41 #include "nsReadableUtils.h"
44 namespace TestStrings
{
46 void test_assign_helper(const nsACString
& in
, nsACString
&_retval
)
54 test_assign_helper(NS_LITERAL_CSTRING("a") + NS_LITERAL_CSTRING("b"), result
);
55 PRBool r
= strcmp(result
.get(), "ab") == 0;
57 printf("[result=%s]\n", result
.get());
61 PRBool
test_assign_c()
63 nsCString c
; c
.Assign('c');
64 PRBool r
= strcmp(c
.get(), "c") == 0;
66 printf("[result=%s]\n", c
.get());
72 NS_NAMED_LITERAL_STRING(empty
, "");
73 const nsAString
& aStr
= empty
;
75 nsAutoString
buf(aStr
);
77 PRInt32 n
= buf
.FindChar(',');
82 n
= buf
.FindChar(',');
87 return n
== kNotFound
;
92 nsCString
data("hello world");
93 const nsACString
& aStr
= data
;
98 PRBool r
= strcmp(temp
.get(), "world") == 0;
100 printf("[temp=%s]\n", temp
.get());
106 nsCString
src("<!DOCTYPE blah blah blah>");
108 PRInt32 i
= src
.Find("DOCTYPE", PR_TRUE
, 2, 1);
118 const char text
[] = "<!DOCTYPE blah blah blah>";
119 const char term
[] = "bLaH";
123 i
= src
.RFind(term
, PR_TRUE
, 3, -1);
126 printf("unexpected result searching from offset=3, i=%d\n", i
);
130 i
= src
.RFind(term
, PR_TRUE
, -1, -1);
133 printf("unexpected result searching from offset=-1, i=%d\n", i
);
137 i
= src
.RFind(term
, PR_TRUE
, 13, -1);
140 printf("unexpected result searching from offset=13, i=%d\n", i
);
144 i
= src
.RFind(term
, PR_TRUE
, 22, 3);
147 printf("unexpected result searching from offset=22, i=%d\n", i
);
154 PRBool
test_rfind_2()
156 const char text
[] = "<!DOCTYPE blah blah blah>";
158 PRInt32 i
= src
.RFind("TYPE", PR_FALSE
, 5, -1);
166 PRBool
test_rfind_3()
168 const char text
[] = "urn:mozilla:locale:en-US:necko";
169 nsCAutoString
value(text
);
170 PRInt32 i
= value
.RFind(":");
178 PRBool
test_rfind_4()
180 nsCString
value("a.msf");
181 PRInt32 i
= value
.RFind(".msf");
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
),
202 // Search for last !/ at the end of the string
203 if (!FindInReadable(NS_LITERAL_CSTRING("!/"), delim_begin
, delim_end
))
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
);
218 // Search for first jar:
219 if (!FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin
, delim_end
))
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
);
232 // Search for jar: in a Substring
233 delim_begin
= begin
; delim_begin
++;
235 if (!FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin
, delim_end
))
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
);
248 // Should not find a match
249 if (FindInReadable(NS_LITERAL_CSTRING("gecko"), delim_begin
, delim_end
))
252 // When no match is found, range should be empty
253 if (delim_begin
!= delim_end
)
256 // Should not find a match (search not beyond Substring)
257 delim_begin
= begin
; for (int i
=0;i
<6;i
++) delim_begin
++;
259 if (FindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin
, delim_end
))
262 // When no match is found, range should be empty
263 if (delim_begin
!= delim_end
)
266 // Should not find a match (search not beyond Substring)
268 delim_end
= end
; for (int i
=0;i
<7;i
++) delim_end
--;
269 if (FindInReadable(NS_LITERAL_CSTRING("classic"), delim_begin
, delim_end
))
272 // When no match is found, range should be empty
273 if (delim_begin
!= delim_end
)
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
),
290 // Search for last !/ at the end of the string
291 if (!RFindInReadable(NS_LITERAL_CSTRING("!/"), delim_begin
, delim_end
))
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
);
306 // Search for last jar: but not the first one...
307 if (!RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin
, delim_end
))
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
);
320 // Search for jar: in a Substring
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");
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
);
338 // Should not find a match
341 if (RFindInReadable(NS_LITERAL_CSTRING("gecko"), delim_begin
, delim_end
)) {
342 printf("Should not find a match\n");
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");
352 // Should not find a match (search not before Substring)
353 delim_begin
= begin
; for (int i
=0;i
<6;i
++) delim_begin
++;
355 if (RFindInReadable(NS_LITERAL_CSTRING("jar:"), delim_begin
, delim_end
)) {
356 printf("Should not find a match (search not before Substring)\n");
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");
366 // Should not find a match (search not beyond Substring)
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");
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");
383 PRBool
test_distance()
385 const char text
[] = "abc-xyz";
387 nsCString::const_iterator begin
, end
;
388 s
.BeginReading(begin
);
390 size_t d
= Distance(begin
, end
);
391 PRBool r
= (d
== sizeof(text
)-1);
399 const char text
[] = "abc-xyz";
401 size_t d
= s
.Length();
402 PRBool r
= (d
== sizeof(text
)-1);
410 const char text
[] = " a\t $ ";
411 const char set
[] = " \t$";
415 PRBool r
= strcmp(s
.get(), "a") == 0;
417 printf("[s=%s]\n", s
.get());
421 PRBool
test_replace_substr()
423 const char text
[] = "abc-ppp-qqq-ppp-xyz";
425 s
.ReplaceSubstring("ppp", "www");
426 PRBool r
= strcmp(s
.get(), "abc-www-qqq-www-xyz") == 0;
429 printf("[s=%s]\n", s
.get());
434 s
.ReplaceSubstring("foo", "bar");
435 s
.ReplaceSubstring("bar", "");
436 r
= strcmp(s
.get(), "") == 0;
439 printf("[s=%s]\n", s
.get());
443 s
.Assign("foofoofoo");
444 s
.ReplaceSubstring("foo", "foo");
445 r
= strcmp(s
.get(), "foofoofoo") == 0;
448 printf("[s=%s]\n", s
.get());
452 s
.Assign("foofoofoo");
453 s
.ReplaceSubstring("of", "fo");
454 r
= strcmp(s
.get(), "fofoofooo") == 0;
457 printf("[s=%s]\n", s
.get());
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
))
485 PRBool
test_strip_ws()
487 const char text
[] = " a $ ";
490 PRBool r
= strcmp(s
.get(), "a$") == 0;
492 printf("[s=%s]\n", s
.get());
496 PRBool
test_equals_ic()
499 PRBool r
= s
.LowerCaseEqualsLiteral("view-source");
501 printf("[r=%d]\n", r
);
505 PRBool
test_fixed_string()
507 char buf
[256] = "hello world";
509 nsFixedCString
s(buf
, sizeof(buf
));
511 if (s
.Length() != strlen(buf
))
514 if (strcmp(s
.get(), buf
) != 0)
517 s
.Assign("foopy doopy doo");
526 nsCString
bar("bar");
527 const nsACString
& barRef
= bar
;
529 const nsPromiseFlatCString
& result
=
530 PromiseFlatCString(NS_LITERAL_CSTRING("foo") +
531 NS_LITERAL_CSTRING(",") +
533 if (strcmp(result
.get(), "foo,bar") == 0)
536 printf("[result=%s]\n", result
.get());
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)
551 printf("[result=%s]\n", result
.get());
556 PRBool
test_concat_3()
558 nsCString
a("a"), b("b");
560 // THIS DOES NOT COMPILE
561 const nsACString
& r
= a
+ b
;
567 PRBool
test_xpidl_string()
588 const char text
[] = "hello world";
589 *getter_Copies(a
) = nsCRT::strdup(text
);
590 if (strcmp(a
, text
) != 0)
594 if (strcmp(a
, b
) != 0)
598 nsACString::const_iterator begin
, end
;
599 a
.BeginReading(begin
);
601 char *r
= ToNewCString(Substring(begin
, end
));
602 if (strcmp(r
, "") != 0)
607 if (a
!= (const char*) 0)
611 PRInt32 index = a.FindCharInSet("xyz");
612 if (index != kNotFound)
619 PRBool
test_empty_assign()
631 PRBool
test_set_length()
633 const char kText
[] = "Default Plugin";
635 buf
.SetCapacity(sizeof(kText
)-1);
637 buf
.SetLength(sizeof(kText
)-1);
638 if (strcmp(buf
.get(), kText
) != 0)
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()));
653 // and verifies that |sub| does not start with |super|.
655 r
= super
.Equals(StringHead(sub
, super
.Length()));
662 PRBool
test_appendint64()
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";
677 if (!str
.Equals(max_expected
)) {
678 fprintf(stderr
, "Error appending LL_MaxInt(): Got %s\n", str
.get());
684 if (!str
.Equals(min_expected
)) {
685 fprintf(stderr
, "Error appending LL_MinInt(): Got %s\n", str
.get());
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());
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());
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());
713 PRBool
test_appendfloat()
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());
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());
739 PRBool
test_findcharinset()
741 nsCString
buf("hello, how are you?");
743 PRInt32 index
= buf
.FindCharInSet(",?", 5);
747 index
= buf
.FindCharInSet("helo", 0);
751 index
= buf
.FindCharInSet("z?", 6);
752 if (index
!= (PRInt32
) buf
.Length()-1)
758 PRBool
test_rfindcharinset()
760 nsCString
buf("hello, how are you?");
762 PRInt32 index
= buf
.RFindCharInSet(",?", 5);
766 index
= buf
.RFindCharInSet("helo", 0);
770 index
= buf
.RFindCharInSet("z?", 6);
771 if (index
!= kNotFound
)
774 index
= buf
.RFindCharInSet("l", 5);
778 buf
.Assign("abcdefghijkabc");
780 index
= buf
.RFindCharInSet("ab");
784 index
= buf
.RFindCharInSet("ab", 11);
788 index
= buf
.RFindCharInSet("ab", 10);
792 index
= buf
.RFindCharInSet("ab", 0);
796 index
= buf
.RFindCharInSet("cd", 1);
797 if (index
!= kNotFound
)
800 index
= buf
.RFindCharInSet("h");
807 PRBool
test_stringbuffer()
809 const char kData
[] = "hello world";
813 buf
= nsStringBuffer::Alloc(sizeof(kData
));
818 buf
= nsStringBuffer::Alloc(sizeof(kData
));
821 char *data
= (char *) buf
->Data();
822 memcpy(data
, kData
, sizeof(kData
));
825 buf
->ToString(sizeof(kData
)-1, str
);
827 nsStringBuffer
*buf2
;
828 buf2
= nsStringBuffer::FromString(str
);
830 PRBool rv
= (buf
== buf2
);
838 const char kData
[] = "hello world";
849 if (strcmp(str
, kData
) != 0)
852 str
.SetIsVoid(PR_TRUE
);
860 str
.SetIsVoid(PR_FALSE
);
861 if (strcmp(str
, "") != 0)
867 PRBool
test_voided_autostr()
869 const char kData
[] = "hello world";
878 if (strcmp(str
.get(), kData
) != 0)
881 str
.SetIsVoid(PR_TRUE
);
892 if (strcmp(str
.get(), kData
) != 0)
900 typedef PRBool (*TestFunc
)();
902 static const struct Test
909 { "test_assign", test_assign
},
910 { "test_assign_c", test_assign_c
},
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
},
946 using namespace TestStrings
;
948 int main(int argc
, char **argv
)
952 count
= atoi(argv
[1]);
956 for (const Test
* t
= tests
; t
->name
!= nsnull
; ++t
)
958 printf("%25s : %s\n", t
->name
, t
->func() ? "SUCCESS" : "FAILURE <--");