CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / xpcom / tests / TestStringAPI.cpp
blob91031cf39a6cb99c49c8e895a0ce040a0b11dde8
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is XPCOM external strings test.
16 * The Initial Developer of the Original Code is
17 * Mook <mook.moz@gmail.com>.
18 * Portions created by the Initial Developer are Copyright (C) 2007
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Prasad Sunkari <prasad@medhas.org>
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 "nsStringAPI.h"
41 #define CHECK(x) \
42 _doCheck(x, #x, __LINE__)
44 int _doCheck(bool cond, const char* msg, int line) {
45 if (cond) return 0;
46 fprintf(stderr, "FAIL: line %d: %s\n", line, msg);
47 return 1;
50 int testEmpty() {
51 nsString s;
52 return CHECK(0 == s.Length()) +
53 CHECK(s.IsEmpty());
56 int testAccess() {
57 nsString s;
58 s.Assign(NS_LITERAL_STRING("hello"));
59 int res = CHECK(5 == s.Length()) +
60 CHECK(s.EqualsLiteral("hello"));
61 const PRUnichar *it, *end;
62 int len = s.BeginReading(&it, &end);
63 res += CHECK(5 == len);
64 res += CHECK(PRUnichar('h') == it[0]) +
65 CHECK(PRUnichar('e') == it[1]) +
66 CHECK(PRUnichar('l') == it[2]) +
67 CHECK(PRUnichar('l') == it[3]) +
68 CHECK(PRUnichar('o') == it[4]) +
69 CHECK(it + len == end);
70 res += CHECK(s[0] == s.First());
71 for (int i = 0; i < len; ++i) {
72 res += CHECK(s[i] == it[i]);
73 res += CHECK(s[i] == s.CharAt(i));
75 res += CHECK(it == s.BeginReading());
76 res += CHECK(end == s.EndReading());
77 return res;
80 int testWrite() {
81 nsString s(NS_LITERAL_STRING("xyzz"));
82 PRUnichar *begin, *end;
83 int res = CHECK(4 == s.Length());
84 PRUint32 len = s.BeginWriting(&begin, &end, 5);
85 res += CHECK(5 == s.Length()) +
86 CHECK(5 == len) +
87 CHECK(end == begin + 5) +
88 CHECK(begin == s.BeginWriting()) +
89 CHECK(end == s.EndWriting());
90 begin[4] = PRUnichar('y');
91 res += CHECK(s.Equals(NS_LITERAL_STRING("xyzzy")));
92 s.SetLength(4);
93 res += CHECK(4 == s.Length()) +
94 CHECK(s.Equals(NS_LITERAL_STRING("xyzz"))) +
95 CHECK(!s.Equals(NS_LITERAL_STRING("xyzzy"))) +
96 CHECK(!s.IsEmpty());
97 s.Truncate();
98 res += CHECK(0 == s.Length()) +
99 CHECK(s.IsEmpty());
100 const PRUnichar sample[] = { 's', 'a', 'm', 'p', 'l', 'e', '\0' };
101 s.Assign(sample);
102 res += CHECK(s.EqualsLiteral("sample"));
103 s.Assign(sample, 4);
104 res += CHECK(s.EqualsLiteral("samp"));
105 s.Assign(PRUnichar('q'));
106 res += CHECK(s.EqualsLiteral("q"));
107 return res;
110 int testFind() {
111 nsString str_haystack;
112 nsString str_needle;
113 str_needle.AssignLiteral("world");
115 PRInt32 ret = 0;
116 ret += CHECK(-1 == str_haystack.Find("world"));
117 ret += CHECK(-1 == str_haystack.Find(str_needle));
119 str_haystack.AssignLiteral("hello world hello world hello");
120 ret += CHECK( 6 == str_haystack.Find("world"));
121 ret += CHECK( 6 == str_haystack.Find(str_needle));
122 ret += CHECK(-1 == str_haystack.Find("world", 20, PR_FALSE));
123 ret += CHECK(-1 == str_haystack.Find(str_needle, 20));
124 ret += CHECK(18 == str_haystack.Find("world", 12, PR_FALSE));
125 ret += CHECK(18 == str_haystack.Find(str_needle, 12));
127 nsCString cstr_haystack;
128 nsCString cstr_needle;
129 cstr_needle.AssignLiteral("world");
131 ret += CHECK(-1 == cstr_haystack.Find("world"));
132 ret += CHECK(-1 == cstr_haystack.Find(cstr_needle));
134 cstr_haystack.AssignLiteral("hello world hello world hello");
135 ret += CHECK( 6 == cstr_haystack.Find("world"));
136 ret += CHECK( 6 == cstr_haystack.Find(cstr_needle));
137 ret += CHECK(-1 == cstr_haystack.Find(cstr_needle, 20));
138 ret += CHECK(18 == cstr_haystack.Find(cstr_needle, 12));
139 ret += CHECK( 6 == cstr_haystack.Find("world", 5));
141 return ret;
144 int testVoid() {
145 nsString s;
146 int ret = CHECK(!s.IsVoid());
147 s.SetIsVoid(PR_FALSE);
148 ret += CHECK(!s.IsVoid());
149 s.SetIsVoid(PR_TRUE);
150 ret += CHECK(s.IsVoid());
151 s.SetIsVoid(PR_FALSE);
152 ret += CHECK(!s.IsVoid());
153 s.SetIsVoid(PR_TRUE);
154 s.AssignLiteral("hello");
155 ret += CHECK(!s.IsVoid());
156 return ret;
159 int testRFind() {
160 PRInt32 ret = 0;
162 // nsString.RFind
163 nsString str_haystack;
164 nsString str_needle;
165 str_needle.AssignLiteral("world");
167 ret += CHECK(-1 == str_haystack.RFind(str_needle));
168 ret += CHECK(-1 == str_haystack.RFind("world"));
170 str_haystack.AssignLiteral("hello world hElLo woRlD");
172 ret += CHECK( 6 == str_haystack.RFind(str_needle));
173 ret += CHECK( 6 == str_haystack.RFind(str_needle, -1));
174 ret += CHECK( 6 == str_haystack.RFind(str_needle, 17));
175 ret += CHECK( 6 == str_haystack.RFind("world", PR_FALSE));
176 ret += CHECK(18 == str_haystack.RFind("world", PR_TRUE));
177 ret += CHECK( 6 == str_haystack.RFind("world", -1, PR_FALSE));
178 ret += CHECK(18 == str_haystack.RFind("world", -1, PR_TRUE));
179 ret += CHECK( 6 == str_haystack.RFind("world", 17, PR_FALSE));
180 ret += CHECK( 0 == str_haystack.RFind("hello", 0, PR_FALSE));
181 ret += CHECK(18 == str_haystack.RFind("world", 19, PR_TRUE));
182 ret += CHECK(18 == str_haystack.RFind("world", 22, PR_TRUE));
183 ret += CHECK(18 == str_haystack.RFind("world", 23, PR_TRUE));
185 // nsCString.RFind
186 nsCString cstr_haystack;
187 nsCString cstr_needle;
188 cstr_needle.AssignLiteral("world");
190 ret += CHECK(-1 == cstr_haystack.RFind(cstr_needle));
191 ret += CHECK(-1 == cstr_haystack.RFind("world"));
193 cstr_haystack.AssignLiteral("hello world hElLo woRlD");
195 ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle));
196 ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, -1));
197 ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, 17));
198 ret += CHECK( 6 == cstr_haystack.RFind("world", 5));
199 ret += CHECK( 0 == cstr_haystack.RFind(nsDependentCString("hello"), 0));
201 return ret;
204 int testCompressWhitespace() {
205 PRInt32 ret = 0;
207 // CompressWhitespace utility function
208 nsString s;
210 s.AssignLiteral(" ");
211 CompressWhitespace(s);
212 ret += CHECK(s.EqualsLiteral(""));
214 s.AssignLiteral(" no more leading spaces");
215 CompressWhitespace(s);
216 ret += CHECK(s.EqualsLiteral("no more leading spaces"));
218 s.AssignLiteral("no more trailing spaces ");
219 CompressWhitespace(s);
220 ret += CHECK(s.EqualsLiteral("no more trailing spaces"));
222 s.AssignLiteral(" hello one 2 three 45 ");
223 CompressWhitespace(s);
224 ret += CHECK(s.EqualsLiteral("hello one 2 three 45"));
226 return ret;
229 int main() {
230 int rv = 0;
231 rv += testEmpty();
232 rv += testAccess();
233 rv += testWrite();
234 rv += testFind();
235 rv += testVoid();
236 rv += testRFind();
237 rv += testCompressWhitespace();
238 if (0 == rv) {
239 fprintf(stderr, "PASS: StringAPI tests\n");
241 return rv;