xpcom/tests/TestStringAPI.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:a66aa80c88d2
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include <stdio.h>
6 #include "nsStringAPI.h"
7
8 #define CHECK(x) \
9 _doCheck(x, #x, __LINE__)
10
11 int _doCheck(bool cond, const char* msg, int line) {
12 if (cond) return 0;
13 fprintf(stderr, "FAIL: line %d: %s\n", line, msg);
14 return 1;
15 }
16
17 int testEmpty() {
18 nsString s;
19 return CHECK(0 == s.Length()) +
20 CHECK(s.IsEmpty());
21 }
22
23 int testAccess() {
24 nsString s;
25 s.Assign(NS_LITERAL_STRING("hello"));
26 int res = CHECK(5 == s.Length()) +
27 CHECK(s.EqualsLiteral("hello"));
28 const char16_t *it, *end;
29 int len = s.BeginReading(&it, &end);
30 res += CHECK(5 == len);
31 res += CHECK(char16_t('h') == it[0]) +
32 CHECK(char16_t('e') == it[1]) +
33 CHECK(char16_t('l') == it[2]) +
34 CHECK(char16_t('l') == it[3]) +
35 CHECK(char16_t('o') == it[4]) +
36 CHECK(it + len == end);
37 res += CHECK(s[0] == s.First());
38 for (int i = 0; i < len; ++i) {
39 res += CHECK(s[i] == it[i]);
40 res += CHECK(s[i] == s.CharAt(i));
41 }
42 res += CHECK(it == s.BeginReading());
43 res += CHECK(end == s.EndReading());
44 return res;
45 }
46
47 int testWrite() {
48 nsString s(NS_LITERAL_STRING("xyzz"));
49 char16_t *begin, *end;
50 int res = CHECK(4 == s.Length());
51 uint32_t len = s.BeginWriting(&begin, &end, 5);
52 res += CHECK(5 == s.Length()) +
53 CHECK(5 == len) +
54 CHECK(end == begin + 5) +
55 CHECK(begin == s.BeginWriting()) +
56 CHECK(end == s.EndWriting());
57 begin[4] = char16_t('y');
58 res += CHECK(s.Equals(NS_LITERAL_STRING("xyzzy")));
59 s.SetLength(4);
60 res += CHECK(4 == s.Length()) +
61 CHECK(s.Equals(NS_LITERAL_STRING("xyzz"))) +
62 CHECK(!s.Equals(NS_LITERAL_STRING("xyzzy"))) +
63 CHECK(!s.IsEmpty());
64 s.Truncate();
65 res += CHECK(0 == s.Length()) +
66 CHECK(s.IsEmpty());
67 const char16_t sample[] = { 's', 'a', 'm', 'p', 'l', 'e', '\0' };
68 s.Assign(sample);
69 res += CHECK(s.EqualsLiteral("sample"));
70 s.Assign(sample, 4);
71 res += CHECK(s.EqualsLiteral("samp"));
72 s.Assign(char16_t('q'));
73 res += CHECK(s.EqualsLiteral("q"));
74 return res;
75 }
76
77 int testFind() {
78 nsString str_haystack;
79 nsString str_needle;
80 str_needle.AssignLiteral("world");
81
82 int32_t ret = 0;
83 ret += CHECK(-1 == str_haystack.Find("world"));
84 ret += CHECK(-1 == str_haystack.Find(str_needle));
85
86 str_haystack.AssignLiteral("hello world hello world hello");
87 ret += CHECK( 6 == str_haystack.Find("world"));
88 ret += CHECK( 6 == str_haystack.Find(str_needle));
89 ret += CHECK(-1 == str_haystack.Find("world", 20, false));
90 ret += CHECK(-1 == str_haystack.Find(str_needle, 20));
91 ret += CHECK(18 == str_haystack.Find("world", 12, false));
92 ret += CHECK(18 == str_haystack.Find(str_needle, 12));
93
94 nsCString cstr_haystack;
95 nsCString cstr_needle;
96 cstr_needle.AssignLiteral("world");
97
98 ret += CHECK(-1 == cstr_haystack.Find("world"));
99 ret += CHECK(-1 == cstr_haystack.Find(cstr_needle));
100
101 cstr_haystack.AssignLiteral("hello world hello world hello");
102 ret += CHECK( 6 == cstr_haystack.Find("world"));
103 ret += CHECK( 6 == cstr_haystack.Find(cstr_needle));
104 ret += CHECK(-1 == cstr_haystack.Find(cstr_needle, 20));
105 ret += CHECK(18 == cstr_haystack.Find(cstr_needle, 12));
106 ret += CHECK( 6 == cstr_haystack.Find("world", 5));
107
108 return ret;
109 }
110
111 int testVoid() {
112 nsString s;
113 int ret = CHECK(!s.IsVoid());
114 s.SetIsVoid(false);
115 ret += CHECK(!s.IsVoid());
116 s.SetIsVoid(true);
117 ret += CHECK(s.IsVoid());
118 s.SetIsVoid(false);
119 ret += CHECK(!s.IsVoid());
120 s.SetIsVoid(true);
121 s.AssignLiteral("hello");
122 ret += CHECK(!s.IsVoid());
123 return ret;
124 }
125
126 int testRFind() {
127 int32_t ret = 0;
128
129 // nsString.RFind
130 nsString str_haystack;
131 nsString str_needle;
132 str_needle.AssignLiteral("world");
133
134 ret += CHECK(-1 == str_haystack.RFind(str_needle));
135 ret += CHECK(-1 == str_haystack.RFind("world"));
136
137 str_haystack.AssignLiteral("hello world hElLo woRlD");
138
139 ret += CHECK( 6 == str_haystack.RFind(str_needle));
140 ret += CHECK( 6 == str_haystack.RFind(str_needle, -1));
141 ret += CHECK( 6 == str_haystack.RFind(str_needle, 17));
142 ret += CHECK( 6 == str_haystack.RFind("world", false));
143 ret += CHECK(18 == str_haystack.RFind("world", true));
144 ret += CHECK( 6 == str_haystack.RFind("world", -1, false));
145 ret += CHECK(18 == str_haystack.RFind("world", -1, true));
146 ret += CHECK( 6 == str_haystack.RFind("world", 17, false));
147 ret += CHECK( 0 == str_haystack.RFind("hello", 0, false));
148 ret += CHECK(18 == str_haystack.RFind("world", 19, true));
149 ret += CHECK(18 == str_haystack.RFind("world", 22, true));
150 ret += CHECK(18 == str_haystack.RFind("world", 23, true));
151
152 // nsCString.RFind
153 nsCString cstr_haystack;
154 nsCString cstr_needle;
155 cstr_needle.AssignLiteral("world");
156
157 ret += CHECK(-1 == cstr_haystack.RFind(cstr_needle));
158 ret += CHECK(-1 == cstr_haystack.RFind("world"));
159
160 cstr_haystack.AssignLiteral("hello world hElLo woRlD");
161
162 ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle));
163 ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, -1));
164 ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, 17));
165 ret += CHECK( 6 == cstr_haystack.RFind("world", 5));
166 ret += CHECK( 0 == cstr_haystack.RFind(nsDependentCString("hello"), 0));
167
168 return ret;
169 }
170
171 int testCompressWhitespace() {
172 int32_t ret = 0;
173
174 // CompressWhitespace utility function
175 nsString s;
176
177 s.AssignLiteral(" ");
178 CompressWhitespace(s);
179 ret += CHECK(s.EqualsLiteral(""));
180
181 s.AssignLiteral(" no more leading spaces");
182 CompressWhitespace(s);
183 ret += CHECK(s.EqualsLiteral("no more leading spaces"));
184
185 s.AssignLiteral("no more trailing spaces ");
186 CompressWhitespace(s);
187 ret += CHECK(s.EqualsLiteral("no more trailing spaces"));
188
189 s.AssignLiteral(" hello one 2 three 45 ");
190 CompressWhitespace(s);
191 ret += CHECK(s.EqualsLiteral("hello one 2 three 45"));
192
193 return ret;
194 }
195
196 int main() {
197 int rv = 0;
198 rv += testEmpty();
199 rv += testAccess();
200 rv += testWrite();
201 rv += testFind();
202 rv += testVoid();
203 rv += testRFind();
204 rv += testCompressWhitespace();
205 if (0 == rv) {
206 fprintf(stderr, "PASS: StringAPI tests\n");
207 }
208 return rv;
209 }

mercurial