1 // Copyright 2007, Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "gears/base/common/common.h"
30 #include "gears/base/common/string_utils.h"
32 // We don't use google3/testing/base/gunit because our tests depend of
33 // browser specific code that needs to run in the context of the browser.
35 static bool TestStartsWithAndEndsWith();
36 static bool TestStrUtilsReplaceAll();
37 static bool TestStringCompareIgnoreCase();
38 static bool TestStringUTF8FileToUrl();
40 bool TestStringUtils() {
42 ok
&= TestStringCompareIgnoreCase();
43 ok
&= TestStartsWithAndEndsWith();
44 ok
&= TestStrUtilsReplaceAll();
45 ok
&= TestStringUTF8FileToUrl();
51 #define strcasecmp _stricmp
54 static bool TestStringCompareIgnoreCase() {
56 #define TEST_ASSERT(b) \
59 LOG(("TestStringCompareIgnoreCase - failed (%d)\n", __LINE__)); \
64 std::string empty_str
;
65 std::string
aaa_str("aaa");
66 std::string
bbb_str("bbb");
67 std::string
aaaa_str("aaaa");
68 std::string aaa_upper_str
= MakeUpperString(aaa_str
);
70 // First test our assumptions about the proper results by invoking
71 // the CRT's impl for 8 bit characters
72 TEST_ASSERT(strcasecmp(aaa_str
.c_str(), aaa_str
.c_str()) == 0);
73 TEST_ASSERT(strcasecmp(aaa_str
.c_str(), bbb_str
.c_str()) < 0);
74 TEST_ASSERT(strcasecmp(bbb_str
.c_str(), aaa_str
.c_str()) > 0);
75 TEST_ASSERT(strcasecmp(aaa_str
.c_str(), aaaa_str
.c_str()) < 0);
76 TEST_ASSERT(strcasecmp(aaa_str
.c_str(), empty_str
.c_str()) > 0);
77 TEST_ASSERT(strcasecmp(aaa_str
.c_str(),
78 aaa_upper_str
.c_str()) == 0);
80 TEST_ASSERT(StringCompareIgnoreCase(
81 aaa_str
.c_str(), aaa_str
.c_str()) == 0);
82 TEST_ASSERT(StringCompareIgnoreCase(
83 aaa_str
.c_str(), bbb_str
.c_str()) < 0);
84 TEST_ASSERT(StringCompareIgnoreCase(
85 bbb_str
.c_str(), aaa_str
.c_str()) > 0);
86 TEST_ASSERT(StringCompareIgnoreCase(
87 aaa_str
.c_str(), aaaa_str
.c_str()) < 0);
88 TEST_ASSERT(StringCompareIgnoreCase(
89 aaa_str
.c_str(), empty_str
.c_str()) > 0);
90 TEST_ASSERT(StringCompareIgnoreCase(
91 aaa_str
.c_str(), aaa_upper_str
.c_str()) == 0);
94 std::string16 empty_str
;
95 std::string16
aaa_str(STRING16(L
"aaa"));
96 std::string16
bbb_str(STRING16(L
"bbb"));
97 std::string16
aaaa_str(STRING16(L
"aaaa"));
98 std::string16 aaa_upper_str
= MakeUpperString(aaa_str
);
100 TEST_ASSERT(StringCompareIgnoreCase(
101 aaa_str
.c_str(), aaa_str
.c_str()) == 0);
102 TEST_ASSERT(StringCompareIgnoreCase(
103 aaa_str
.c_str(), bbb_str
.c_str()) < 0);
104 TEST_ASSERT(StringCompareIgnoreCase(
105 bbb_str
.c_str(), aaa_str
.c_str()) > 0);
106 TEST_ASSERT(StringCompareIgnoreCase(
107 aaa_str
.c_str(), aaaa_str
.c_str()) < 0);
108 TEST_ASSERT(StringCompareIgnoreCase(
109 aaa_str
.c_str(), empty_str
.c_str()) > 0);
110 TEST_ASSERT(StringCompareIgnoreCase(
111 aaa_str
.c_str(), aaa_upper_str
.c_str()) == 0);
117 static bool TestStrUtilsReplaceAll() {
119 #define TEST_ASSERT(b) \
122 LOG(("TestStrUtilsReplaceAll - failed (%d)\n", __LINE__)); \
126 std::string
str("bbaaabbaaabb");
127 TEST_ASSERT(ReplaceAll(str
, std::string("bb"), std::string("cc")) == 3);
128 TEST_ASSERT(str
== "ccaaaccaaacc");
130 TEST_ASSERT(ReplaceAll(str
, std::string("cc"), std::string("d")) == 3);
131 TEST_ASSERT(str
== "daaadaaad");
133 TEST_ASSERT(ReplaceAll(str
, std::string("d"), std::string("ff")) == 3);
134 TEST_ASSERT(str
== "ffaaaffaaaff");
136 TEST_ASSERT(ReplaceAll(str
, std::string("ff"), std::string(1, 0)) == 3);
137 TEST_ASSERT(str
.length() == 9);
139 TEST_ASSERT(ReplaceAll(str
, std::string(1, 0), std::string("bb")) == 3);
140 TEST_ASSERT(str
== "bbaaabbaaabb");
143 TEST_ASSERT(ReplaceAll(str
, std::string("a"), std::string("aa")) == 4);
144 TEST_ASSERT(str
== "aaaaaaaa");
146 TEST_ASSERT(ReplaceAll(str
, std::string("aa"), std::string("a")) == 4);
147 TEST_ASSERT(str
== "aaaa");
149 TEST_ASSERT(ReplaceAll(str
, std::string("b"), std::string("c")) == 0);
150 TEST_ASSERT(str
== "aaaa");
155 static bool TestStartsWithAndEndsWith() {
157 #define TEST_ASSERT(b) \
160 LOG(("TestBeginsWithAndEndsWith - failed (%d)\n", __LINE__)); \
164 // std::string16 tests
166 const std::string16
prefix(STRING16(L
"prefix"));
167 const std::string16
suffix(STRING16(L
"suffix"));
168 const std::string16
test(STRING16(L
"prefix_string_suffix"));
169 TEST_ASSERT(StartsWith(test
, prefix
));
170 TEST_ASSERT(EndsWith(test
, suffix
));
171 TEST_ASSERT(!StartsWith(test
, suffix
));
172 TEST_ASSERT(!EndsWith(test
, prefix
));
173 TEST_ASSERT(!StartsWith(prefix
, test
));
174 TEST_ASSERT(!EndsWith(suffix
, test
));
178 const std::string
prefix("prefix");
179 const std::string
suffix("suffix");
180 const std::string
test("prefix_string_suffix");
181 TEST_ASSERT(StartsWith(test
, prefix
));
182 TEST_ASSERT(EndsWith(test
, suffix
));
183 TEST_ASSERT(!StartsWith(test
, suffix
));
184 TEST_ASSERT(!EndsWith(test
, prefix
));
185 TEST_ASSERT(!StartsWith(prefix
, test
));
186 TEST_ASSERT(!EndsWith(suffix
, test
));
188 LOG(("TestStartsWithAndEndsWith - passed\n"));
192 static bool TestStringUTF8FileToUrl() {
194 #define TEST_ASSERT(b,test_name) \
197 LOG(("TestStringUTF8FileToUrl: %s - failed (%d)\n", test_name, __LINE__)); \
203 const char *expected
;
204 const char *test_name
;
206 {"c:/Dead/Beef.txt", "file:///c:/Dead/Beef.txt", "No escapes"},
207 {"c:\\Dead\\Beef.txt", "file:///c:/Dead/Beef.txt", "Backslash"},
208 {"c:/Dead/Beef/42;.txt", "file:///c:/Dead/Beef/42%3B.txt", "Semicolon"},
209 {"c:/Dead/Beef/42#{}.txt", "file:///c:/Dead/Beef/42%23%7B%7D.txt",
210 "Disallowed Characters"},
211 {"c:/Dead/Beef/牛肉.txt",
212 "file:///c:/Dead/Beef/%E7%89%9B%E8%82%89.txt",
213 "Non-Ascii Characters"}
216 struct URLCase directory_cases
[] = {
217 {"c:/Dead/Beef/", "file:///c:/Dead/Beef/", "Trailing slash"},
218 {"c:\\Dead\\Beef\\", "file:///c:/Dead/Beef/", "Trailing backslash"},
219 {"c:/Dead/Beef", "file:///c:/Dead/Beef/", "No trailing slash"},
220 {"c:\\Dead\\Beef", "file:///c:/Dead/Beef/", "No trailing backslash"},
223 for (unsigned int i
= 0; i
< ARRAYSIZE(cases
); ++i
) {
224 std::string
input(cases
[i
].input
);
225 std::string
output(UTF8PathToUrl(input
, false));
226 TEST_ASSERT(output
== cases
[i
].expected
, cases
[i
].test_name
);
229 for (unsigned int i
= 0; i
< ARRAYSIZE(directory_cases
); ++i
) {
230 std::string
input(directory_cases
[i
].input
);
231 std::string
output(UTF8PathToUrl(input
, true));
232 TEST_ASSERT(output
== directory_cases
[i
].expected
,
233 directory_cases
[i
].test_name
);
236 LOG(("TestStringUTF8FileToUrl - passed\n"));