Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: base/string_unittest.cc

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/string.cc ('k') | base/synchronized.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2003-2010 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 // ========================================================================
15
16 #include "base/basictypes.h"
17 #include "omaha/base/debug.h"
18 #include "omaha/base/localization.h"
19 #include "omaha/base/string.h"
20 #include "omaha/base/time.h"
21 #include "omaha/base/timer.h"
22 #include "omaha/base/tr_rand.h"
23 #include "omaha/testing/resource.h"
24 #include "omaha/testing/unit_test.h"
25
26 namespace omaha {
27
28 TEST(StringTest, IntToString) {
29 ASSERT_STREQ(String_Int64ToString(0, 10), L"0");
30 ASSERT_STREQ(String_Int64ToString(1, 10), L"1");
31 ASSERT_STREQ(String_Int64ToString(-1, 10), L"-1");
32 ASSERT_STREQ(String_Int64ToString(123456789, 10), L"123456789");
33 ASSERT_STREQ(String_Int64ToString(-123456789, 10), L"-123456789");
34 ASSERT_STREQ(String_Int64ToString(1234567890987654321, 10),
35 L"1234567890987654321");
36 ASSERT_STREQ(String_Int64ToString(-1234567890987654321, 10),
37 L"-1234567890987654321");
38 ASSERT_STREQ(String_Int64ToString(0xabcdef, 16), L"abcdef");
39 ASSERT_STREQ(String_Int64ToString(0x101fff, 16), L"101fff");
40 ASSERT_STREQ(String_Int64ToString(0x999999, 16), L"999999");
41 ASSERT_STREQ(String_Int64ToString(0x0, 16), L"0");
42
43 ASSERT_STREQ(String_Int64ToString(01234, 8), L"1234");
44 ASSERT_STREQ(String_Int64ToString(0, 8), L"0");
45 ASSERT_STREQ(String_Int64ToString(0777, 8), L"777");
46 ASSERT_STREQ(String_Int64ToString(0123456, 8), L"123456");
47
48 ASSERT_STREQ(String_Int64ToString(0, 2), L"0");
49 ASSERT_STREQ(String_Int64ToString(0xf, 2), L"1111");
50 ASSERT_STREQ(String_Int64ToString(0x5ad1, 2), L"101101011010001");
51 ASSERT_STREQ(String_Int64ToString(-1, 2), L"-1");
52 }
53
54 TEST(StringTest, UintToString) {
55 ASSERT_STREQ(String_Uint64ToString(0, 10), L"0");
56 ASSERT_STREQ(String_Uint64ToString(1, 10), L"1");
57 ASSERT_STREQ(String_Uint64ToString(123456789, 10), L"123456789");
58 ASSERT_STREQ(String_Uint64ToString(1234567890987654321, 10),
59 L"1234567890987654321");
60 ASSERT_STREQ(String_Uint64ToString(18446744073709551615, 10),
61 L"18446744073709551615");
62
63 ASSERT_STREQ(String_Uint64ToString(0xabcdef, 16), L"abcdef");
64 ASSERT_STREQ(String_Uint64ToString(0x101fff, 16), L"101fff");
65 ASSERT_STREQ(String_Uint64ToString(0x999999, 16), L"999999");
66 ASSERT_STREQ(String_Uint64ToString(0x0, 16), L"0");
67 ASSERT_STREQ(String_Uint64ToString(0xffffffffffffffff, 16), L"ffffffffffffffff ");
68
69 ASSERT_STREQ(String_Uint64ToString(01234, 8), L"1234");
70 ASSERT_STREQ(String_Uint64ToString(0, 8), L"0");
71 ASSERT_STREQ(String_Uint64ToString(0777, 8), L"777");
72 ASSERT_STREQ(String_Uint64ToString(0123456, 8), L"123456");
73
74 ASSERT_STREQ(String_Uint64ToString(0, 2), L"0");
75 ASSERT_STREQ(String_Uint64ToString(0xf, 2), L"1111");
76 ASSERT_STREQ(String_Uint64ToString(0x5ad1, 2), L"101101011010001");
77 }
78
79 TEST(StringTest, DoubleToString) {
80 ASSERT_STREQ(String_DoubleToString(1.234, 1), L"1.2");
81 ASSERT_STREQ(String_DoubleToString(0.0, 0), L"0");
82 ASSERT_STREQ(String_DoubleToString(0.0, 2), L"0.00");
83 ASSERT_STREQ(String_DoubleToString(199.234, 2), L"199.23");
84 ASSERT_STREQ(String_DoubleToString(-199.234, 2), L"-199.23");
85 ASSERT_STREQ(String_DoubleToString(199.23490776, 5), L"199.23490");
86 ASSERT_STREQ(String_DoubleToString(-1.0001, 1), L"-1.0");
87 ASSERT_STREQ(String_DoubleToString(123456789.987654321, 3), L"123456789.987");
88 }
89
90 TEST(StringTest, StrNCpy) {
91 TCHAR * str1 = L"test str 1234";
92 TCHAR * str2 = L"test str 12";
93 TCHAR * str3 = L"Test StR 1234";
94
95 // check case sensitive
96 ASSERT_TRUE(0 == String_StrNCmp(str1, str2, 10, false));
97 ASSERT_TRUE(0 == String_StrNCmp(str1, str2, 11, false));
98
99 // check case in-sensitive
100 ASSERT_TRUE(0 == String_StrNCmp(str2, str3, 10, true));
101 ASSERT_TRUE(0 == String_StrNCmp(str2, str3, 11, true));
102 }
103
104 TEST(StringTest, StartsWith) {
105 ASSERT_TRUE(String_StartsWith(L"", L"", false));
106 ASSERT_TRUE(String_StartsWith(L"Joe", L"", false));
107 ASSERT_TRUE(String_StartsWith(L"Joe", L"J", false));
108 ASSERT_TRUE(String_StartsWith(L"Joe\\", L"J", false));
109 ASSERT_TRUE(String_StartsWith(L"Joe", L"Joe", false));
110 ASSERT_TRUE(String_StartsWith(L"The quick brown fox", L"The quic", false));
111 ASSERT_FALSE(String_StartsWith(L"", L"J", false));
112 ASSERT_FALSE(String_StartsWith(L"Joe", L"Joe2", false));
113 ASSERT_FALSE(String_StartsWith(L"The quick brown fox", L"The quiC", false));
114
115 ASSERT_TRUE(String_StartsWith(L"", L"", true));
116 ASSERT_TRUE(String_StartsWith(L"Joe", L"j", true));
117 ASSERT_TRUE(String_StartsWith(L"The quick brown fox", L"The quiC", true));
118 }
119
120 TEST(StringTest, StartsWithA) {
121 ASSERT_TRUE(String_StartsWithA("", "", false));
122 ASSERT_TRUE(String_StartsWithA("Joe", "", false));
123 ASSERT_TRUE(String_StartsWithA("Joe", "J", false));
124 ASSERT_TRUE(String_StartsWithA("Joe\\", "J", false));
125 ASSERT_TRUE(String_StartsWithA("Joe", "Joe", false));
126 ASSERT_TRUE(String_StartsWithA("The quick brown fox", "The quic", false));
127 ASSERT_FALSE(String_StartsWithA("", "J", false));
128 ASSERT_FALSE(String_StartsWithA("Joe", "Joe2", false));
129 ASSERT_FALSE(String_StartsWithA("The quick brown fox", "The quiC", false));
130
131 ASSERT_TRUE(String_StartsWithA("", "", true));
132 ASSERT_TRUE(String_StartsWithA("Joe", "j", true));
133 ASSERT_TRUE(String_StartsWithA("The quick brown fox", "The quiC", true));
134 }
135
136 TEST(StringTest, EndsWith) {
137 // Case sensitive
138
139 // Empty suffix
140 ASSERT_TRUE(String_EndsWith(L"", L"", false));
141 ASSERT_TRUE(String_EndsWith(L"Joe", L"", false));
142
143 // Partial suffix
144 ASSERT_TRUE(String_EndsWith(L"Joe", L"e", false));
145 ASSERT_TRUE(String_EndsWith(L"Joe\\", L"\\", false));
146 ASSERT_TRUE(String_EndsWith(L"The quick brown fox", L"n fox", false));
147
148 // Suffix == String
149 ASSERT_TRUE(String_EndsWith(L"Joe", L"Joe", false));
150 ASSERT_TRUE(String_EndsWith(L"The quick brown fox",
151 L"The quick brown fox",
152 false));
153
154 // Fail cases
155 ASSERT_FALSE(String_EndsWith(L"", L"J", false));
156 ASSERT_FALSE(String_EndsWith(L"Joe", L"Joe2", false));
157 ASSERT_FALSE(String_EndsWith(L"Joe", L"2Joe", false));
158 ASSERT_FALSE(String_EndsWith(L"The quick brown fox", L"n foX", false));
159
160 // Check case insensitive
161
162 // Empty suffix
163 ASSERT_TRUE(String_EndsWith(L"", L"", true));
164 ASSERT_TRUE(String_EndsWith(L"Joe", L"", true));
165
166 // Partial suffix
167 ASSERT_TRUE(String_EndsWith(L"Joe", L"E", true));
168 ASSERT_TRUE(String_EndsWith(L"The quick brown fox", L"n FOX", true));
169
170 // Suffix == String
171 ASSERT_TRUE(String_EndsWith(L"Joe", L"JOE", true));
172 ASSERT_TRUE(String_EndsWith(L"The quick brown fox",
173 L"The quick brown FOX",
174 true));
175
176 // Fail cases
177 ASSERT_FALSE(String_EndsWith(L"The quick brown fox", L"s", true));
178 ASSERT_FALSE(String_EndsWith(L"The quick brown fox", L"Xs", true));
179 ASSERT_FALSE(String_EndsWith(L"The quick brown fox", L"the brown foX", true));
180 }
181
182 TEST(StringTest, Unencode) {
183 // Normal, correct usage.
184 // char 0x25 is '%'
185 ASSERT_STREQ(Unencode(L"?q=moon+doggy_%25%5E%26"), L"?q=moon doggy_%^&");
186 ASSERT_STREQ(Unencode(L"%54%68%69%73+%69%73%09%61%20%74%65%73%74%0A"),
187 L"This is\ta test\n");
188 ASSERT_STREQ(Unencode(L"This+is%09a+test%0a"), L"This is\ta test\n");
189
190 // NULL char.
191 ASSERT_STREQ(Unencode(L"Terminated%00before+this"), L"Terminated");
192 ASSERT_STREQ(Unencode(L"invalid+%a%25"), L"invalid %a%");
193 ASSERT_STREQ(Unencode(L"invalid+%25%41%37"), L"invalid %A7");
194 ASSERT_STREQ(Unencode(L"not a symbol %RA"), L"not a symbol %RA");
195 ASSERT_STREQ(Unencode(L"%ag"), L"%ag");
196 ASSERT_STREQ(Unencode(L"dontdecode%dont"), L"dontdecode%dont");
197 ASSERT_STREQ(Unencode(L""), L"");
198 ASSERT_STREQ(Unencode(L"%1"), L"%1");
199 ASSERT_STREQ(Unencode(L"\x100"), L"\x100");
200 ASSERT_STREQ(Unencode(L"this is%20a%20wide%20char%20\x345"),
201 L"this is a wide char \x345");
202 ASSERT_STREQ(Unencode(L"a utf8 string %E7%BC%9c %E4%B8%8a = 2"),
203 L"a utf8 string \x7f1c \x4e0a = 2");
204 }
205
206 #if 0
207 static const struct {
208 const char *ansi;
209 const TCHAR *wide;
210 UINT cp;
211 } kAnsi2WideTests[] = {
212 { "\xc8\xae\xc1\xbe", L"\x72ac\x8f86", CP_GB2312},
213 { "\xa5\x69\xb1\x4e\xc2\xb2\xc5\xe9",
214 L"\x53ef\x5c07\x7c21\x9ad4", CP_BIG5},
215 { "\xE7\xBC\x96\xE4\xB8\x8B", L"\x7f16\x4e0b", CP_UTF8},
216 { "ascii", L"ascii", CP_GB2312},
217 { "\x3C\x20\xE7\xBC\x96", L"\x003c\x0020\x00E7\x00BC\x0096", 0 },
218 };
219
220 bool TestAnsiToWideString() {
221 for (size_t i = 0; i < arraysize(kAnsi2WideTests); ++i) {
222 CStringW out;
223 if (kAnsi2WideTests[i].cp == 0) {
224 out = AnsiToWideString(kAnsi2WideTests[i].ansi,
225 strlen(kAnsi2WideTests[i].ansi));
226 } else {
227 AnsiToWideString(kAnsi2WideTests[i].ansi,
228 strlen(kAnsi2WideTests[i].ansi),
229 kAnsi2WideTests[i].cp, &out);
230 }
231 CHK(out == kAnsi2WideTests[i].wide);
232 }
233 return true;
234 }
235 #endif
236
237 TEST(StringTest, Show) {
238 ASSERT_STREQ(Show(0), _T("0"));
239 ASSERT_STREQ(Show(1), _T("1"));
240 ASSERT_STREQ(Show(-1), _T("-1"));
241 }
242
243
244 // Test international strings.
245 TEST(StringTest, International) {
246 CString tabs_by_lang[] = {
247 _T("Web Prente Groepe Gids "), // Afrikaans
248 _T("Web Fotografitë Grupet Drejtoriumi "), // Albanian
249 // Amharic is missing, that doesn't show in normal windows fonts
250 _T("ويب صور مجموعات الدليل "), // Arabic
251 _T("Web Şəkillər Qruplar Qovluq "), // Azerbaija ni
252 _T("Web Irudiak Taldeak Direktorioa "), // Basque
253 _T("Ўэб Малюнкі Групы Каталёг "), // Belarusia n
254 _T("Antorjal Chitraboli Gosthi Bishoy-Talika "), // Bengali
255 _T("MakarJal Chhaya Jerow Nirdeshika "), // Bihari
256 _T("Veb Imeges Gruoops Durectury "), // Bork
257 _T("Internet Slike Grupe Katalog "), // Bosnian
258 _T("Gwiad Skeudennoù Strolladoù Roll "), // Breton
259 _T("Мрежата Изображения Групи Директория "), // Bulgarian
260 _T("Web Imatges Grups Directori "), // Catalan
261 _T("所有网站 图像 网上论坛 网页目录 "), // Chines e Simplified
262 _T("所有網頁 圖片 網上論壇 網頁目錄 "), // Chine se Traditional
263 _T("Web Slike Grupe Imenik "), // Croatian
264 _T("Web Obrázky Skupiny Adresář "), // Czech
265 _T("Nettet Billeder Grupper Katalog "), // Danish
266 _T("Het Internet Afbeeldingen Discussiegroepen Gids "), // Du tch
267 _T("Web Images Gwoups Diwectowy "), // Elmer
268 _T("Web Images Groups News Froogle more »"), // English
269 _T("TTT Bildoj Grupoj Katalogo "), // Esperanto
270 _T("Veeb Pildid Grupid Kataloog "), // Estonian
271 _T("Netið Myndir Bólkar Øki "), // Faroese
272 _T("Web Mga Larawan Mga Grupo Direktoryo "), // Filipino
273 _T("Web Kuvat Keskusteluryhmät Hakemisto "), // Finnish
274 _T("Web Images Groupes Annuaire Actualités "), // French
275 _T("Web Printsjes Diskusjegroepen Directory "), // Frisian
276 _T("Web Imaxes Grupos Directorio "), // Galician
277 _T("ინტერნეტი სურათები ჯგუფები კატალოგი "), / / Georgian
278 _T("Web Bilder Groups Verzeichnis News "), // German
279 _T("Ιστός Eικόνες Ομάδες Κατάλογος "), // Greek
280 _T("Ñanduti Ta'anga Atypy Sãmbyhypy "), // Guarani
281 _T("jalu Chhabi Sangathan Shabdakosh "), // Gujarati
282 _T("n0rM4L s33rCh 1|\\/|4935 6r00pZ d1r3c70rY "), // Hacker
283 _T("אתרים ברשת תמונות קבוצות דיון מדריך האתרים "), // Hebrew
284 _T("वेब छवियाँ समूह निर्देशिका "), // Hindi
285 _T("Web Képek Csoportok Címtár "), // Hungarian
286 _T("Vefur Myndir Hópar Flokkar "), // Icelandic
287 _T("Web Gambar Grup Direktori "), // Indonesia n
288 _T("Web Imagines Gruppos Catalogo "), // Interling ua
289 _T("An Gréasán Íomhánna Grúpaí Eolaire "), // Irish
290 _T("Web Immagini Gruppi Directory News Novità! "), // Ital ian
291 _T("ウェブ イメージ グループ ディレクトリ "), // Japanes e
292 _T("Web Gambar - gambar Paguyuban Bagian "), // Javanese
293 _T("antharajAla chitragaLu gumpugaLu Huduku vibhaagagaLu "), // Kannada
294 _T("Daqmey pat naghmey beQ ghommey mem "), // Klingon
295 _T("웹 문서 이미지 뉴스그룹 디렉토리 "), // Kling on
296 _T("Желе Суроттор Группалар Тизме "), // Kyrgyz
297 _T("Tela Imagines Circuli Index "), // Latin
298 _T("Internets Attēli Vēstkopas Katalogs"), // Latvian
299 _T("Internetas Vaizdai Grupės Katalogas "), // Lithuania n
300 _T("Мрежа Слики Групи Директориум "), // Macedonia n
301 _T("Jaringan Imej Kumpulan Direktori "), // Malay
302 _T("വെബ് ചിത്രങ്ങള് സംഘങ്ങള് ഡയറക്ടറി "), // Malayalam
303 _T("Web Stampi Gruppi Direttorju "), // Maltese
304 _T("वेबशोध चित्रशोध ग्रूप्स डिरेक्टरी "), // Marathi
305 _T("वेब तस्वीर समूह डाइरेक्टरी "), // Nepali
306 _T("Nett Bilder Grupper Katalog "), // Norwegian
307 _T("Veven Bilete Grupper Katalog "), // Norwegian (Nynorsk)
308 _T("Ret Imatges Grops Directori "), // Occitan
309 _T("web chitra goSThi prasanga tAlikA "), // Oriya
310 _T("وب تصويرها گروهها فهرست "), // Persian
311 _T("ebway imagesyay oupsgray Irectoryday "), // P. Latin
312 _T("WWW Grafika Grupy dyskusyjne Katalog "), // Polish
313 _T("Web Imagens Grupos Diretório "), // Potrugues e (Brazil)
314 _T("Web Imagens Grupos Directório "), // Potrugues e (Portugal)
315 _T("Web/Zaal Tasveraan Gutt Directory "), // Punjabi
316 _T("Web Imagini Grupuri Director "), // Romanian
317 _T("Веб Картинки Группы Каталог "), // Russian
318 _T("Lìon Dealbhan Cuantail Eòlaire "), // Scots Gae lic
319 _T("Интернет Слике Групе Каталог "), // Serbian
320 _T("Internet Slike Grupe Spisak "), // Serbo-Cro atian
321 _T("Web Ponahalo Dihlopha Tshupetso "), // Sesotho
322 _T("WEB Roopa Kandayam Namawaliya "), // Sinhalese
323 _T("Web Obrázky Skupiny Katalóg "), // Slovak
324 _T("Internet Slike Skupine Imenik "), // Slovenian
325 _T("La Web Imágenes Grupos Directorio News ¡Nuevo! "), // Spanish
326 _T("Web Gambar Grup Direktori "), // Sudanese
327 _T("Mtandao Picha Vikundi Orodha "), // Swahili
328 _T("Nätet Bilder Grupper Kategori "), // Swedish
329 _T("வலை படங்கள் குழுக்கள் விபரக்கோவை "), // Tamil
330 _T("వెబ్ చిత్రాలు సమూహములు darshini "), // Telugu
331 _T("เว็บ รูปภาพ กลุ่มข่าว สารบบเว็บ "), // Thai
332 // Tigrinya is missing, that doesn't show in normal windows fonts
333 _T("Web Grafikler Gruplar Dizin "), // Turkish
334 _T("Web Suratlar Toparlar Düzine "), // Turkmen
335 _T("tintan Nfonyin Akuokuo Krataa nhwemu "), // Twi
336 _T("Веб Зображення Групи Каталог "), // Ukrainian
337 _T("ويب تصاوير گروہ فہرست ") // Urdu
338 _T("To'r Tasvirlar Gruppalar Papka "), // Uzbek
339 _T("Internet Hình Ảnh Nhóm Thư Mục "), // Vietnames e
340 _T("Y We Lluniau Grwpiau Cyfeiriadur "), // Welsh
341 _T("Web Imifanekiso Amaqela Isilawuli "), // Xhosa
342 _T("װעב בילדער גרופּעס פּאַפּקע "), // Yiddis h
343 _T("I-web Izithombe Amaqembu Uhlu lwamafayela "), // Zulu
344 };
345
346 int i = 0;
347 for(i = 0; i < arraysize(tabs_by_lang); ++i) {
348 // Get the cannonical lower version with ::CharLower
349 CString true_lower(tabs_by_lang[i]);
350 ::CharLower(true_lower.GetBuffer());
351 true_lower.ReleaseBuffer();
352
353 // Get the lower version with String_ToLower,
354 CString low_temp(tabs_by_lang[i]);
355 String_ToLower(low_temp.GetBuffer());
356 low_temp.ReleaseBuffer();
357
358 // make sure they match
359 ASSERT_STREQ(low_temp, true_lower);
360
361 // Now make sure they match letter by letter
362 for(int j = 0; j < tabs_by_lang[i].GetLength(); ++j) {
363 TCHAR cur_char = tabs_by_lang[i].GetAt(j);
364
365 TCHAR low1 = static_cast<TCHAR>(String_ToLowerChar(cur_char));
366
367 ASSERT_EQ(low1, true_lower.GetAt(j));
368 ASSERT_EQ(Char_ToLower(cur_char), true_lower.GetAt(j));
369
370 // Check the Ansi version if applicable
371 if (cur_char < 128)
372 ASSERT_EQ(String_ToLowerChar(static_cast<char>(cur_char)),
373 true_lower.GetAt(j));
374 }
375
376 // Test out the CString conversion
377 CString temp(tabs_by_lang[i]);
378 MakeLowerCString(temp);
379 ASSERT_STREQ(temp, true_lower);
380
381 // Test out the fast version
382 temp = tabs_by_lang[i];
383 String_FastToLower(temp.GetBuffer());
384 temp.ReleaseBuffer();
385
386 ASSERT_STREQ(temp, true_lower);
387
388 // Make sure that the normal CString::Trim works the same as our fast one
389 CString trim_normal(tabs_by_lang[i]);
390 trim_normal.Trim();
391
392 CString trim_fast(tabs_by_lang[i]);
393 TrimCString(trim_fast);
394
395 ASSERT_STREQ(trim_normal, trim_fast);
396 }
397 }
398
399 void TestReplaceString (TCHAR *src, TCHAR *from, TCHAR *to, TCHAR *expected) {
400 ASSERT_TRUE(expected);
401 ASSERT_TRUE(to);
402 ASSERT_TRUE(from);
403 ASSERT_TRUE(src);
404
405 size_t new_src_size = _tcslen(src) + 1;
406 TCHAR* new_src = new TCHAR[new_src_size];
407
408 _tcscpy_s(new_src, new_src_size, src);
409
410 Timer tchar (false);
411 Timer tchar2 (false);
412 Timer cstring (false);
413 Timer orig_cstring (false);
414
415 // int iterations = 10000;
416 int iterations = 10;
417
418 int out_len;
419 TCHAR *out;
420
421 for (int i = 0; i < iterations; i++) {
422 _tcscpy_s(new_src, new_src_size, src);
423 bool created_new_string = false;
424
425 tchar.Start();
426 ReplaceString (new_src, from, to, &out, &out_len);
427 tchar.Stop();
428
429 ASSERT_STREQ(out, expected);
430 delete [] out;
431 }
432
433 for (int i = 0; i < iterations; i++) {
434 _tcscpy_s(new_src, new_src_size, src);
435 bool created_new_string = false;
436
437 tchar2.Start();
438 ReplaceStringMaybeInPlace (new_src, from, to, &out,
439 &out_len, &created_new_string);
440 tchar2.Stop();
441
442 ASSERT_STREQ(out, expected);
443 if (out != new_src) { delete [] out; }
444 }
445
446 for (int i = 0; i < iterations; i++) {
447 CString src_string(src);
448
449 orig_cstring.Start();
450 src_string.Replace (from, to);
451 orig_cstring.Stop();
452
453 ASSERT_STREQ(src_string, CString(expected));
454 }
455
456 for (int i = 0; i < iterations; i++) {
457 CString src_string(src);
458
459 cstring.Start();
460 ReplaceCString (src_string, from, to);
461 cstring.Stop();
462
463 ASSERT_STREQ(src_string, CString(expected));
464 }
465
466 delete [] new_src;
467 }
468
469 TEST(StringTest, ReplaceCString) {
470 CString t;
471 t = _T("a a a b ");
472 ReplaceCString(t, _T("a"), 1, _T("d"), 1, 5);
473 ASSERT_STREQ(_T("d d d b "), t);
474
475 t = _T("a a a b ");
476 ReplaceCString(t, _T("b"), 1, _T("d"), 1, 5);
477 ASSERT_STREQ(_T("a a a d "), t);
478
479 t = _T("a a a b ");
480 ReplaceCString(t, _T("a"), 1, _T("d"), 1, 1);
481 ASSERT_STREQ(_T("d a a b "), t);
482
483 t = _T("a a a b ");
484 ReplaceCString(t, _T("a"), 1, _T("dd"), 2, 5);
485 ASSERT_STREQ(_T("dd dd dd b "), t);
486
487 ReplaceCString(t, _T("dd"), 2, _T("dddd"), 4, 5);
488 ASSERT_STREQ(_T("dddd dddd dddd b "), t);
489
490 ReplaceCString(t, _T("dd"), 2, _T("dddd"), 4, 5);
491 ASSERT_STREQ(_T("dddddddd dddddddd dddddd b "), t);
492
493 ReplaceCString(t, _T("dddddddd"), 8, _T("dddd"), 4, 2);
494 ASSERT_STREQ(_T("dddd dddd dddddd b "), t);
495
496 ReplaceCString(t, _T("d"), 1, _T("a"), 1, 2);
497 ASSERT_STREQ(_T("aadd dddd dddddd b "), t);
498
499 ReplaceCString(t, _T("d d"), 3, _T("c"), 1, 2);
500 ASSERT_STREQ(_T("aadcddcddddd b "), t);
501
502 ReplaceCString(t, _T("c"), 1, _T("1234567890"), 10, 2);
503 ASSERT_STREQ(_T("aad1234567890dd1234567890ddddd b "), t);
504
505 ReplaceCString(t, _T("1"), 1, _T("1234567890"), 10, 2);
506 ASSERT_STREQ(_T("aad1234567890234567890dd1234567890234567890ddddd b "), t);
507
508 ReplaceCString(t, _T("1234567890"), 10, _T(""), 0, 2);
509 ASSERT_STREQ(_T("aad234567890dd234567890ddddd b "), t);
510
511 t = _T("a aa aa b ");
512 ReplaceCString(t, _T("aa"), 2, _T("b"), 1, 5);
513 ASSERT_STREQ(_T("a b b b "), t);
514
515 t = _T("moo a aa aa b ");
516 ReplaceCString(t, _T("aa"), 2, _T("b"), 1, 5);
517 ASSERT_STREQ(_T("moo a b b b "), t);
518
519 // Time to test some big strings
520 int test_sizes[] = {200, 500, 900, 10000};
521
522 int i;
523 for(i = 0; i < arraysize(test_sizes); ++i) {
524 CString in, out;
525 for(int j = 0; j < test_sizes[i]; ++j) {
526 in += L'a';
527 out += _T("bb");
528 }
529 CString bak_in(in);
530
531 // Make it a bit bigger
532 int times = ReplaceCString(in, _T("a"), 1, _T("bb"), 2, kRepMax);
533 ASSERT_EQ(times, test_sizes[i]);
534 ASSERT_EQ(out, in);
535
536 // Make it bigger still
537 times = ReplaceCString(in, _T("bb"), 2, _T("ccc"), 3, kRepMax);
538 ASSERT_EQ(times, test_sizes[i]);
539
540 // Same size swap
541 times = ReplaceCString(in, _T("c"), 1, _T("d"), 1, kRepMax);
542 ASSERT_EQ(times, test_sizes[i] * 3);
543
544 // Make it smaller again
545 times = ReplaceCString(in, _T("ddd"), 3, _T("a"), 1, kRepMax);
546 ASSERT_EQ(times, test_sizes[i]);
547 ASSERT_EQ(bak_in, in);
548 }
549 }
550
551 TEST(StringTest, GetField) {
552 CString s(_T("<a>a</a><b>123</b><c>aa\ndd</c>"));
553
554 CString a(GetField (s, L"a"));
555 ASSERT_STREQ(a, L"a");
556
557 CString b(GetField (s, L"b"));
558 ASSERT_STREQ(b, L"123");
559
560 CString c(GetField (s, L"c"));
561 ASSERT_STREQ(c, L"aa\ndd");
562 }
563
564 TEST(StringTest, String_HasAlphabetLetters) {
565 ASSERT_TRUE(String_HasAlphabetLetters (L"abc"));
566 ASSERT_TRUE(String_HasAlphabetLetters (L"X"));
567 ASSERT_TRUE(String_HasAlphabetLetters (L" pie "));
568 ASSERT_FALSE(String_HasAlphabetLetters (L"1"));
569 ASSERT_FALSE(String_HasAlphabetLetters (L"0"));
570 ASSERT_FALSE(String_HasAlphabetLetters (L"010"));
571 ASSERT_FALSE(String_HasAlphabetLetters (L"314-159"));
572 ASSERT_TRUE(String_HasAlphabetLetters (L"pie0"));
573 }
574
575 TEST(StringTest, String_LargeIntToApproximateString) {
576 int power;
577 ASSERT_TRUE(String_LargeIntToApproximateString(10LL, true, &power) == _T("10") && power == 0);
578 ASSERT_TRUE(String_LargeIntToApproximateString(99LL, true, &power) == _T("99") && power == 0);
579 ASSERT_TRUE(String_LargeIntToApproximateString(990LL, true, &power) == _T("990 ") && power == 0);
580 ASSERT_TRUE(String_LargeIntToApproximateString(999LL, true, &power) == _T("999 ") && power == 0);
581
582 ASSERT_TRUE(String_LargeIntToApproximateString(1000LL, true, &power) == _T("1. 0") && power == 1);
583 ASSERT_TRUE(String_LargeIntToApproximateString(1200LL, true, &power) == _T("1. 2") && power == 1);
584 ASSERT_TRUE(String_LargeIntToApproximateString(7500LL, true, &power) == _T("7. 5") && power == 1);
585 ASSERT_TRUE(String_LargeIntToApproximateString(9900LL, true, &power) == _T("9. 9") && power == 1);
586 ASSERT_TRUE(String_LargeIntToApproximateString(10000LL, true, &power) == _T("1 0") && power == 1);
587 ASSERT_TRUE(String_LargeIntToApproximateString(11000LL, true, &power) == _T("1 1") && power == 1);
588 ASSERT_TRUE(String_LargeIntToApproximateString(987654LL, true, &power) == _T(" 987") && power == 1);
589
590 ASSERT_TRUE(String_LargeIntToApproximateString(1000000LL, true, &power) == _T( "1.0") && power == 2);
591 ASSERT_TRUE(String_LargeIntToApproximateString(1300000LL, true, &power) == _T( "1.3") && power == 2);
592 ASSERT_TRUE(String_LargeIntToApproximateString(987654321LL, true, &power) == _ T("987") && power == 2);
593
594 ASSERT_TRUE(String_LargeIntToApproximateString(1000000000LL, true, &power) == _T("1.0") && power == 3);
595 ASSERT_TRUE(String_LargeIntToApproximateString(1999999999LL, true, &power) == _T("1.9") && power == 3);
596 ASSERT_TRUE(String_LargeIntToApproximateString(20000000000LL, true, &power) == _T("20") && power == 3);
597 ASSERT_TRUE(String_LargeIntToApproximateString(1000000000000LL, true, &power) == _T("1000") && power == 3);
598 ASSERT_TRUE(String_LargeIntToApproximateString(12345678901234LL, true, &power) == _T("12345") && power == 3);
599
600 ASSERT_TRUE(String_LargeIntToApproximateString(1023LL, false, &power) == _T("1 023") && power == 0);
601
602 ASSERT_TRUE(String_LargeIntToApproximateString(1024LL, false, &power) == _T("1 .0") && power == 1);
603 ASSERT_TRUE(String_LargeIntToApproximateString(1134LL, false, &power) == _T("1 .1") && power == 1);
604 ASSERT_TRUE(String_LargeIntToApproximateString(10240LL, false, &power) == _T(" 10") && power == 1);
605
606 ASSERT_TRUE(String_LargeIntToApproximateString(5242880LL, false, &power) == _T ("5.0") && power == 2);
607
608 ASSERT_TRUE(String_LargeIntToApproximateString(1073741824LL, false, &power) == _T("1.0") && power == 3);
609 ASSERT_TRUE(String_LargeIntToApproximateString(17179869184LL, false, &power) = = _T("16") && power == 3);
610 }
611
612 TEST(StringTest, FindWholeWordMatch) {
613 // words with spaces before / after
614 ASSERT_EQ(0, FindWholeWordMatch (L"pi", L"pi", false, 0));
615 ASSERT_EQ(1, FindWholeWordMatch (L" pi", L"pi", false, 0));
616 ASSERT_EQ(1, FindWholeWordMatch (L" pi ", L"pi", false, 0));
617 ASSERT_EQ(0, FindWholeWordMatch (L"pi ", L"pi", false, 0));
618
619 // partial matches
620 ASSERT_EQ(-1, FindWholeWordMatch (L"pie ", L"pi", false, 0));
621 ASSERT_EQ(-1, FindWholeWordMatch (L" pie ", L"pi", false, 0));
622 ASSERT_EQ(-1, FindWholeWordMatch (L"pie", L"pi", false, 0));
623 ASSERT_EQ(-1, FindWholeWordMatch (L" pie", L"pi", false, 0));
624
625 // partial match with non-alphanumeric chars
626 ASSERT_EQ(-1, FindWholeWordMatch (L" pumpkin_pie ", L"pie", false, 0));
627 ASSERT_EQ(-1, FindWholeWordMatch (L" pie_crust ", L"pie", false, 0));
628 ASSERT_EQ(-1, FindWholeWordMatch (L"tartar", L"tar", false, 0));
629 ASSERT_EQ(-1, FindWholeWordMatch (L"pie!", L"pie", false, 0));
630 }
631
632 TEST(StringTest, ReplaceWholeWord) {
633 CString str (L"pie");
634 ReplaceWholeWord (L"ie", L"..", false, &str);
635 ASSERT_STREQ(str, L"pie");
636
637 ReplaceWholeWord (L"pie", L"..", false, &str);
638 ASSERT_STREQ(str, L"..");
639
640 str = L"banana pie";
641 ReplaceWholeWord (L"pie", L"..", false, &str);
642 ASSERT_STREQ(str, L"banana ..");
643
644 str = L"banana pie";
645 ReplaceWholeWord (L"banana", L"..", false, &str);
646 ASSERT_STREQ(str, L".. pie");
647
648 str = L"banana pie";
649 ReplaceWholeWord (L"banana pie", L" .. ", false, &str);
650 ASSERT_STREQ(str, L" .. ");
651
652 str = L"banana pie";
653 ReplaceWholeWord (L"pi", L" .. ", false, &str);
654 ASSERT_STREQ(str, L"banana pie");
655
656 str = L"ishniferatsu";
657 ReplaceWholeWord (L"era", L" .. ", false, &str);
658 ASSERT_STREQ(str, L"ishniferatsu");
659
660 str = L"i i i hi ii i";
661 ReplaceWholeWord (L"i", L"you", false, &str);
662 ASSERT_STREQ(str, L"you you you hi ii you");
663
664 str = L"a nice cream cheese pie";
665 ReplaceWholeWord (L"cream cheese", L"..", false, &str);
666 ASSERT_STREQ(str, L"a nice .. pie");
667
668 // ---
669 // Test replacement with whitespace trimming
670
671 // Replace in the middle of the string.
672 str = L"a nice cream cheese pie";
673 ReplaceWholeWord (L"cream cheese", L"..", true, &str);
674 ASSERT_STREQ(str, L"a nice..pie");
675
676 // Replace in the beginning of the string.
677 str = L"a nice cream cheese pie";
678 ReplaceWholeWord (L"a nice", L"..", true, &str);
679 ASSERT_STREQ(str, L"..cream cheese pie");
680
681 // Replace in the end of the string.
682 str = L"a nice cream cheese pie";
683 ReplaceWholeWord (L"pie", L"..", true, &str);
684 ASSERT_STREQ(str, L"a nice cream cheese..");
685 }
686
687
688 TEST(StringTest, TestReplaceString) {
689 // timing for replace string, for the specific tests below shows:
690 //
691 // the TCHAR version is always faster than CRT CString::Replace
692 //
693 // the CString version is faster than CRT CString::Replace:
694 // - always if the replacement is shorter
695 // - if the source string is longer than ~60 characters if the replacement is
696 // longer
697 //
698 // based on our current usage of CString::Replace, I expect the new CString
699 // version is faster on average than CRT CString::Replace
700 //
701 // non-CRT CString::Replace is much slower, so all of these should be much
702 // faster than that
703
704 TestReplaceString(L"that's what i changed -it was propagating the error code b ut i ..", L" .. ", L"<b> .. </b>", L"that's what i changed -it was propagating t he error code but i ..");
705 TestReplaceString(L"news.com.url", L".url", L"", L"news.com");
706 TestReplaceString(L"news.com..url", L".url", L"", L"news.com.");
707 TestReplaceString(L"news.com.u.url", L".url", L"", L"news.com.u");
708 TestReplaceString(L"abanana pie banana", L"banana", L"c", L"ac pie c");
709 TestReplaceString(L"bananabananabanana", L"banana", L"c", L"ccc");
710 TestReplaceString(L"abanana pie banana", L"banana", L"cabanapie", L"acabanapie pie cabanapie");
711 TestReplaceString(L"bananabananabanana", L"banana", L"cabanapie", L"cabanapiec abanapiecabanapie");
712 TestReplaceString(L"banana pie banana pie", L"banana", L"c", L"c pie c pie");
713 TestReplaceString(L"banana pie banana pie", L"pie", L"z", L"banana z banana z" );
714 TestReplaceString(L"banana pie banana pie", L"banana", L"bananacabana", L"bana nacabana pie bananacabana pie");
715 TestReplaceString(L"banana pie banana pie", L"pie", L"pietie", L"banana pietie banana pietie");
716 TestReplaceString(L"banana pie banana pie", L"tie", L"pietie", L"banana pie ba nana pie");
717 TestReplaceString(L"banana pie banana pie banana pie banana pie banana pie", L "banana", L"bananacab", L"bananacab pie bananacab pie bananacab pie bananacab pi e bananacab pie");
718 TestReplaceString(L"banana pie banana pie banana pie banana pie banana pie ban ana pie banana pie", L"banana", L"bananacab", L"bananacab pie bananacab pie bana nacab pie bananacab pie bananacab pie bananacab pie bananacab pie");
719 TestReplaceString(L"banana pie banana pie banana pie banana pie banana pie ban ana pie banana pie banana pie banana pie banana pie banana pie banana pie", L"ba nana", L"bananacab", L"bananacab pie bananacab pie bananacab pie bananacab pie b ananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab p ie bananacab pie bananacab pie");
720 TestReplaceString(L"banana pie banana pie banana pie banana pie banana pie ban ana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pi e banana pie banana pie banana pie banana pie banana pie banana pie banana pie b anana pie banana pie banana pie banana pie banana pie banana pie banana pie bana na pie banana pie banana pie", L"banana", L"bananacab", L"bananacab pie bananaca b pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bana nacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie banana cab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pie ba nanacab pie bananacab pie bananacab pie bananacab pie bananacab pie bananacab pi e bananacab pie");
721 TestReplaceString(L"banana pie banana pie banana pie banana pie banana pie ban ana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pi e banana pie banana pie banana pie banana pie banana pie banana pie banana pie b anana pie banana pie banana pie banana pie banana pie banana pie banana pie bana na pie banana pie banana pie", L"banana", L"cab", L"cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie cab pie");
722 TestReplaceString(L"news", L"news", L"", L"");
723 TestReplaceString(L"&nbsp;", L"&nbsp;", L"", L"");
724 TestReplaceString(L"&nbsp;&nbsp;&nbsp;", L"&nbsp;", L"", L"");
725 TestReplaceString(L"&nbsp; &nbsp;&nbsp;", L"&nbsp;", L"", L" ");
726 }
727
728
729 TEST(StringTest, GetAbsoluteUri) {
730 ASSERT_STREQ(GetAbsoluteUri(L"http://www.google.com"),
731 L"http://www.google.com/");
732 ASSERT_STREQ(GetAbsoluteUri(L"http://www.google.com/"),
733 L"http://www.google.com/");
734 ASSERT_STREQ(GetAbsoluteUri(L"http://www.google.com//"),
735 L"http://www.google.com/");
736 ASSERT_STREQ(GetAbsoluteUri(L"http://www.google.com/test"),
737 L"http://www.google.com/test");
738 }
739
740 void TestTrim(const TCHAR *str, const TCHAR *result) {
741 ASSERT_TRUE(result);
742 ASSERT_TRUE(str);
743
744 size_t ptr_size = _tcslen(str) + 1;
745 TCHAR* ptr = new TCHAR[ptr_size];
746 _tcscpy_s(ptr, ptr_size, str);
747
748 int len = Trim(ptr);
749 ASSERT_STREQ(ptr, result);
750 ASSERT_EQ(len, lstrlen(result));
751
752 delete [] ptr;
753 }
754
755 TEST(StringTest, Trim) {
756 TestTrim(L"", L"");
757 TestTrim(L" ", L"");
758 TestTrim(L"\t", L"");
759 TestTrim(L"\n", L"");
760 TestTrim(L"\n\t \t \n", L"");
761 TestTrim(L" joe", L"joe");
762 TestTrim(L"joe ", L"joe");
763 TestTrim(L" joe ", L"joe");
764 TestTrim(L"joe smith ", L"joe smith");
765 TestTrim(L" joe smith ", L"joe smith");
766 TestTrim(L" joe smith ", L"joe smith");
767 TestTrim(L" The quick brown fox,\tblah", L"The quick brown fox,\tblah");
768 TestTrim(L" \tblah\n joe smith ", L"blah\n joe smith");
769 }
770
771 // IsSpaceA1 is much faster without the cache clearing (which is what happends
772 // in release mode)
773 // IsSpaceA1 is roughly the same speed as IsSpaceA2 with cache clearing (in
774 // debug mode)
775 // IsSpaceA3 is always much slower
776
777 static const byte spacesA[256] = {
778 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // 0-9
779 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 10-19
780 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-29
781 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, // 30-39
782 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-49
783 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50-59
784 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60-69
785 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70-79
786 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-89
787 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90-99
788 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 100-109
789 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 110-119
790 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 120-129
791 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 130-139
792 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 140-149
793 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 150-159
794 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 160-169
795 };
796
797 bool IsSpaceA1(char c) {
798 return spacesA[c] == 1;
799 }
800
801 bool IsSpaceA2(char c) {
802 // return (c==32);
803 // if (c>32) { return 0; }
804 // most characters >32, check first for that case
805 if (c>32 && c!=160) { return 0; }
806 if (c==32) { return 1; }
807 if (c>=9&&c<=13) return 1; else return 0;
808 }
809
810 bool IsSpaceA3(char c) {
811 WORD result;
812 if (GetStringTypeA(0, CT_CTYPE1, &c, 1, &result)) {
813 return (0 != (result & C1_SPACE));
814 }
815 return false;
816 }
817
818 void TestIsSpace (char *s) {
819 ASSERT_TRUE(s);
820
821 Timer t1 (false);
822 Timer t2 (false);
823 Timer t3 (false);
824
825 // int iterations = 10000;
826 int iterations = 100;
827 int len = strlen (s);
828
829 // used to try to clear the processor cache
830 int dlen = 100000;
831 char *dummy = new char [dlen];
832 for (int i = 0; i < dlen; i++) {
833 dummy[i] = static_cast<char>(tr_rand() % 256);
834 }
835
836 int num_spaces = 0;
837 int n = iterations * len;
838 for (int i = 0; i < iterations; i++) {
839 t1.Start();
840 for (int j = 0; j < len; j++) {
841 num_spaces += IsSpaceA1 (s[j]);
842 }
843 t1.Stop();
844 // this cache clearing code gets optimized out in release mode
845 int d2 = 0;
846 for (int i = 0; i < dlen; i++) { d2 += dummy[i]; }
847 }
848
849 num_spaces = 0;
850 for (int i = 0; i < iterations; i++) {
851 t2.Start();
852 for (int j = 0; j < len; j++) {
853 num_spaces += IsSpaceA2 (s[j]);
854 }
855 t2.Stop();
856 int d2 = 0;
857 for (int i = 0; i < dlen; i++) { d2 += dummy[i]; }
858 }
859
860 num_spaces = 0;
861 for (int i = 0; i < iterations; i++) {
862 t3.Start();
863 for (int j = 0; j < len; j++) {
864 num_spaces += IsSpaceA3 (s[j]);
865 }
866 t3.Stop();
867 int d2 = 0;
868 for (int i = 0; i < dlen; i++) { d2 += dummy[i]; }
869 }
870 }
871
872 TEST(StringTest, IsSpace) {
873 TestIsSpace("banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie ba nana pie banana pie banana pie banana pie banana pie banana pie banana pie banan a pie banana pie banana pie banana pie banana pie banana pie banana pie banana p ie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie banana pie");
874 TestIsSpace("sdlfhdkgheorutsgj sdlj aoi oaj gldjg opre gdsfjng oate yhdnv ;zsj fpoe v;kjae hgpaieh dajlgn aegh avn WEIf h9243y 9814cu 902t7 9[-32 [O8W759 RC90 817 V9pDAHc n( ny(7LKFJAOISF *&^*^%$$%#*&^(*_*)_^& 67% 796%&$*^$ 8)6 (^ 08&^ )*^ 9-7=90z& +(^ )^* %9%4386 $& (& &+ 7- &(_* ");
875 }
876
877 void TestCleanupWhitespace(const TCHAR *str, const TCHAR *result) {
878 ASSERT_TRUE(result);
879 ASSERT_TRUE(str);
880
881 size_t ptr_size = _tcslen(str) + 1;
882 TCHAR* ptr = new TCHAR[ptr_size];
883 _tcscpy_s(ptr, ptr_size, str);
884
885 int len = CleanupWhitespace(ptr);
886 ASSERT_STREQ(ptr, result);
887 ASSERT_EQ(len, lstrlen(result));
888
889 delete [] ptr;
890 }
891
892 TEST(StringTest, CleanupWhitespace) {
893 TestCleanupWhitespace(L"", L"");
894 TestCleanupWhitespace(L"a ", L"a");
895 TestCleanupWhitespace(L" a", L"a");
896 TestCleanupWhitespace(L" a ", L"a");
897 TestCleanupWhitespace(L"\t\n\r a ", L"a");
898 TestCleanupWhitespace(L" \n a \t \r ", L"a");
899 TestCleanupWhitespace(L"a b", L"a b");
900 TestCleanupWhitespace(L" a \t\n\r b", L"a b");
901 TestCleanupWhitespace(L" vool voop", L"vool voop");
902 TestCleanupWhitespace(L"thisisaverylongstringwithsometext",
903 L"thisisaverylongstringwithsometext");
904 TestCleanupWhitespace(L"thisisavery longstringwithsometext",
905 L"thisisavery longstringwithsometext");
906 }
907
908 void TestWcstoul (TCHAR *string, int radix, unsigned long expected) {
909 ASSERT_TRUE(string);
910
911 wchar_t *ptr;
912 int v = Wcstoul (string, &ptr, radix);
913 ASSERT_EQ(v, expected);
914
915 #ifdef DEBUG
916 int v2 = wcstoul (string, &ptr, radix);
917 ASSERT_EQ(v, v2);
918 #endif
919 }
920
921 TEST(StringTest, Wcstoul) {
922 TestWcstoul(L"625", 16, 1573);
923 TestWcstoul(L" 625", 16, 1573);
924 TestWcstoul(L"a3", 16, 163);
925 TestWcstoul(L"A3", 16, 163);
926 TestWcstoul(L" A3", 16, 163);
927 TestWcstoul(L" 12445", 10, 12445);
928 TestWcstoul(L"12445778", 10, 12445778);
929 }
930
931 TEST(StringTest, IsDigit) {
932 ASSERT_TRUE(String_IsDigit('0'));
933 ASSERT_TRUE(String_IsDigit('1'));
934 ASSERT_TRUE(String_IsDigit('2'));
935 ASSERT_TRUE(String_IsDigit('3'));
936 ASSERT_TRUE(String_IsDigit('4'));
937 ASSERT_TRUE(String_IsDigit('5'));
938 ASSERT_TRUE(String_IsDigit('6'));
939 ASSERT_TRUE(String_IsDigit('7'));
940 ASSERT_TRUE(String_IsDigit('8'));
941 ASSERT_TRUE(String_IsDigit('9'));
942 ASSERT_FALSE(String_IsDigit('a'));
943 ASSERT_FALSE(String_IsDigit('b'));
944 ASSERT_FALSE(String_IsDigit('z'));
945 ASSERT_FALSE(String_IsDigit('A'));
946 ASSERT_FALSE(String_IsDigit(' '));
947 ASSERT_FALSE(String_IsDigit('#'));
948 }
949
950 TEST(StringTest, IsUpper) {
951 ASSERT_FALSE(String_IsUpper('0'));
952 ASSERT_FALSE(String_IsUpper(' '));
953 ASSERT_FALSE(String_IsUpper('#'));
954 ASSERT_FALSE(String_IsUpper('a'));
955 ASSERT_FALSE(String_IsUpper('z'));
956 ASSERT_TRUE(String_IsUpper('A'));
957 ASSERT_TRUE(String_IsUpper('B'));
958 ASSERT_TRUE(String_IsUpper('C'));
959 ASSERT_TRUE(String_IsUpper('D'));
960 ASSERT_TRUE(String_IsUpper('H'));
961 ASSERT_TRUE(String_IsUpper('Y'));
962 ASSERT_TRUE(String_IsUpper('Z'));
963 }
964
965 TEST(StringTest, StringToDouble) {
966 ASSERT_DOUBLE_EQ(String_StringToDouble(L"625"), 625);
967 ASSERT_DOUBLE_EQ(String_StringToDouble(L"-625"), -625);
968 ASSERT_DOUBLE_EQ(String_StringToDouble(L"-6.25"), -6.25);
969 ASSERT_DOUBLE_EQ(String_StringToDouble(L"6.25"), 6.25);
970 ASSERT_DOUBLE_EQ(String_StringToDouble(L"0.00"), 0);
971 ASSERT_DOUBLE_EQ(String_StringToDouble(L" 55.1"), 55.1);
972 ASSERT_DOUBLE_EQ(String_StringToDouble(L" 55.001"), 55.001);
973 ASSERT_DOUBLE_EQ(String_StringToDouble(L" 1.001"), 1.001);
974 }
975
976 TEST(StringTest, StringToInt) {
977 ASSERT_EQ(String_StringToInt(L"625"), 625);
978 ASSERT_EQ(String_StringToInt(L"6"), 6);
979 ASSERT_EQ(String_StringToInt(L"0"), 0);
980 ASSERT_EQ(String_StringToInt(L" 122"), 122);
981 ASSERT_EQ(String_StringToInt(L"a"), 0);
982 ASSERT_EQ(String_StringToInt(L" a"), 0);
983 }
984
985 TEST(StringTest, StringToInt64) {
986 ASSERT_EQ(String_StringToInt64(L"119600064000000000"),
987 119600064000000000uI64);
988 ASSERT_EQ(String_StringToInt64(L" 119600064000000000"),
989 119600064000000000uI64);
990 ASSERT_EQ(String_StringToInt64(L"625"), 625);
991 ASSERT_EQ(String_StringToInt64(L"6"), 6);
992 ASSERT_EQ(String_StringToInt64(L"0"), 0);
993 ASSERT_EQ(String_StringToInt64(L" 122"), 122);
994 ASSERT_EQ(String_StringToInt64(L"a"), 0);
995 ASSERT_EQ(String_StringToInt64(L" a"), 0);
996 }
997
998 void TestEndWithChar(const TCHAR *s, char c, const TCHAR *expected) {
999 ASSERT_TRUE(expected);
1000 ASSERT_TRUE(s);
1001
1002 TCHAR buf[5000];
1003 _tcscpy(buf, s);
1004 String_EndWithChar(buf, c);
1005 ASSERT_STREQ(buf, expected);
1006 }
1007
1008 TEST(StringTest, EndWithChar) {
1009 TestEndWithChar(L"", L'a', L"a");
1010 TestEndWithChar(L"", L'\\', L"\\");
1011 TestEndWithChar(L"a", L'a', L"a");
1012 TestEndWithChar(L"a", L'b', L"ab");
1013 TestEndWithChar(L"abcdefghij", L'a', L"abcdefghija");
1014 TestEndWithChar(L"abcdefghij", L'\\', L"abcdefghij\\");
1015 }
1016
1017 TEST(StringTest, HexDigitToInt) {
1018 ASSERT_EQ(HexDigitToInt(L'0'), 0);
1019 ASSERT_EQ(HexDigitToInt(L'1'), 1);
1020 ASSERT_EQ(HexDigitToInt(L'2'), 2);
1021 ASSERT_EQ(HexDigitToInt(L'3'), 3);
1022 ASSERT_EQ(HexDigitToInt(L'4'), 4);
1023 ASSERT_EQ(HexDigitToInt(L'5'), 5);
1024 ASSERT_EQ(HexDigitToInt(L'6'), 6);
1025 ASSERT_EQ(HexDigitToInt(L'7'), 7);
1026 ASSERT_EQ(HexDigitToInt(L'8'), 8);
1027 ASSERT_EQ(HexDigitToInt(L'9'), 9);
1028 ASSERT_EQ(HexDigitToInt(L'A'), 10);
1029 ASSERT_EQ(HexDigitToInt(L'a'), 10);
1030 ASSERT_EQ(HexDigitToInt(L'B'), 11);
1031 ASSERT_EQ(HexDigitToInt(L'b'), 11);
1032 ASSERT_EQ(HexDigitToInt(L'C'), 12);
1033 ASSERT_EQ(HexDigitToInt(L'c'), 12);
1034 ASSERT_EQ(HexDigitToInt(L'D'), 13);
1035 ASSERT_EQ(HexDigitToInt(L'd'), 13);
1036 ASSERT_EQ(HexDigitToInt(L'E'), 14);
1037 ASSERT_EQ(HexDigitToInt(L'e'), 14);
1038 ASSERT_EQ(HexDigitToInt(L'F'), 15);
1039 ASSERT_EQ(HexDigitToInt(L'f'), 15);
1040 }
1041
1042 TEST(StringTest, IsHexDigit) {
1043 ASSERT_TRUE(IsHexDigit(L'0'));
1044 ASSERT_TRUE(IsHexDigit(L'1'));
1045 ASSERT_TRUE(IsHexDigit(L'2'));
1046 ASSERT_TRUE(IsHexDigit(L'3'));
1047 ASSERT_TRUE(IsHexDigit(L'4'));
1048 ASSERT_TRUE(IsHexDigit(L'5'));
1049 ASSERT_TRUE(IsHexDigit(L'6'));
1050 ASSERT_TRUE(IsHexDigit(L'7'));
1051 ASSERT_TRUE(IsHexDigit(L'8'));
1052 ASSERT_TRUE(IsHexDigit(L'9'));
1053 ASSERT_TRUE(IsHexDigit(L'a'));
1054 ASSERT_TRUE(IsHexDigit(L'A'));
1055 ASSERT_TRUE(IsHexDigit(L'b'));
1056 ASSERT_TRUE(IsHexDigit(L'B'));
1057 ASSERT_TRUE(IsHexDigit(L'c'));
1058 ASSERT_TRUE(IsHexDigit(L'C'));
1059 ASSERT_TRUE(IsHexDigit(L'd'));
1060 ASSERT_TRUE(IsHexDigit(L'D'));
1061 ASSERT_TRUE(IsHexDigit(L'e'));
1062 ASSERT_TRUE(IsHexDigit(L'E'));
1063 ASSERT_TRUE(IsHexDigit(L'f'));
1064 ASSERT_TRUE(IsHexDigit(L'F'));
1065
1066 for(TCHAR digit = static_cast<TCHAR>(127); digit < 10000; ++digit) {
1067 ASSERT_FALSE(IsHexDigit(digit));
1068 }
1069 }
1070
1071 TEST(StringTest, Remove) {
1072 CString temp_remove;
1073
1074 // Remove everything
1075 temp_remove = _T("ftp://");
1076 RemoveFromStart (temp_remove, _T("ftp://"), false);
1077 ASSERT_STREQ(temp_remove, _T(""));
1078
1079 // Remove all but 1 letter
1080 temp_remove = _T("ftp://a");
1081 RemoveFromStart (temp_remove, _T("ftp://"), false);
1082 ASSERT_STREQ(temp_remove, _T("a"));
1083
1084 // Remove the first instance
1085 temp_remove = _T("ftp://ftp://");
1086 RemoveFromStart (temp_remove, _T("ftp://"), false);
1087 ASSERT_STREQ(temp_remove, _T("ftp://"));
1088
1089 // Remove normal
1090 temp_remove = _T("ftp://taz the tiger");
1091 RemoveFromStart (temp_remove, _T("ftp://"), false);
1092 ASSERT_STREQ(temp_remove, _T("taz the tiger"));
1093
1094 // Wrong prefix
1095 temp_remove = _T("ftp:/taz the tiger");
1096 RemoveFromStart (temp_remove, _T("ftp://"), false);
1097 ASSERT_STREQ(temp_remove, _T("ftp:/taz the tiger"));
1098
1099 // Not long enough
1100 temp_remove = _T("ftp:/");
1101 RemoveFromStart (temp_remove, _T("ftp://"), false);
1102 ASSERT_STREQ(temp_remove, _T("ftp:/"));
1103
1104 // Remove nothing
1105 temp_remove = _T("ftp:/");
1106 RemoveFromStart (temp_remove, _T(""), false);
1107 ASSERT_STREQ(temp_remove, _T("ftp:/"));
1108
1109 // Remove 1 character
1110 temp_remove = _T("ftp:/");
1111 RemoveFromStart (temp_remove, _T("f"), false);
1112 ASSERT_STREQ(temp_remove, _T("tp:/"));
1113
1114 // Wrong case
1115 temp_remove = _T("ftp:/");
1116 RemoveFromStart (temp_remove, _T("F"), false);
1117 ASSERT_STREQ(temp_remove, _T("ftp:/"));
1118
1119 // Remove everything
1120 temp_remove = _T(".edu");
1121 RemoveFromEnd (temp_remove, _T(".edu"));
1122 ASSERT_STREQ(temp_remove, _T(""));
1123
1124 // Remove all but 1 letter
1125 temp_remove = _T("a.edu");
1126 RemoveFromEnd(temp_remove, _T(".edu"));
1127 ASSERT_STREQ(temp_remove, _T("a"));
1128
1129 // Remove the first instance
1130 temp_remove = _T(".edu.edu");
1131 RemoveFromEnd(temp_remove, _T(".edu"));
1132 ASSERT_STREQ(temp_remove, _T(".edu"));
1133
1134 // Remove normal
1135 temp_remove = _T("ftp://taz the tiger.edu");
1136 RemoveFromEnd(temp_remove, _T(".edu"));
1137 ASSERT_STREQ(temp_remove, _T("ftp://taz the tiger"));
1138
1139 // Wrong suffix
1140 temp_remove = _T("ftp:/taz the tiger.edu");
1141 RemoveFromEnd(temp_remove, _T("/edu"));
1142 ASSERT_STREQ(temp_remove, _T("ftp:/taz the tiger.edu"));
1143
1144 // Not long enough
1145 temp_remove = _T("edu");
1146 RemoveFromEnd(temp_remove, _T(".edu"));
1147 ASSERT_STREQ(temp_remove, _T("edu"));
1148
1149 // Remove nothing
1150 temp_remove = _T(".edu");
1151 RemoveFromEnd(temp_remove, _T(""));
1152 ASSERT_STREQ(temp_remove, _T(".edu"));
1153
1154 // Remove 1 character
1155 temp_remove = _T(".edu");
1156 RemoveFromEnd(temp_remove, _T("u"));
1157 ASSERT_STREQ(temp_remove, _T(".ed"));
1158
1159 // Wrong case
1160 temp_remove = _T(".edu");
1161 RemoveFromEnd(temp_remove, _T("U"));
1162 ASSERT_STREQ(temp_remove, _T(".edu"));
1163 }
1164
1165 TEST(StringTest, WideToAnsiDirect) {
1166 CString temp_convert;
1167 ASSERT_STREQ("", WideToAnsiDirect(_T("")));
1168 ASSERT_STREQ("a", WideToAnsiDirect(_T("a")));
1169 ASSERT_STREQ("moon doggy", WideToAnsiDirect(_T("moon doggy")));
1170
1171 // Generate a string of all characters 0-255.
1172 const int kNumChars = 256;
1173 TCHAR nasty_chars[kNumChars];
1174 for (int i = 0; i < kNumChars; ++i) {
1175 nasty_chars[i] = static_cast<TCHAR>(i);
1176 }
1177 CString temp(nasty_chars, kNumChars);
1178
1179 // Convert it and make sure it matches.
1180 CStringA out = WideToAnsiDirect(temp);
1181 ASSERT_EQ(out.GetLength(), kNumChars);
1182 for (int i = 0; i < kNumChars; ++i) {
1183 ASSERT_EQ(static_cast<unsigned char>(nasty_chars[i]),
1184 static_cast<unsigned char>(out.GetAt(i)));
1185 }
1186 }
1187
1188 TEST(StringTest, FindStringASpaceStringB) {
1189 ASSERT_TRUE(FindStringASpaceStringB(L"content-type: text/html", L"content-type :", L"text/html"));
1190 ASSERT_TRUE(FindStringASpaceStringB(L"content-TYPE: text/html", L"content-type :", L"text/html"));
1191 ASSERT_TRUE(FindStringASpaceStringB(L"content-TYPE: text/HTML", L"content-type :", L"text/html"));
1192 ASSERT_TRUE(FindStringASpaceStringB(L"content-TYPE: text/HTML", L"content-type :", L"text/HTML"));
1193 ASSERT_TRUE(FindStringASpaceStringB(L"content-TYPE: text/HTML", L"content-TYPE :", L"text/HTML"));
1194 ASSERT_TRUE(FindStringASpaceStringB(L"content-type:text/html", L"content-type: ", L"text/html"));
1195 ASSERT_TRUE(FindStringASpaceStringB(L"content-type: text/html", L"content-typ e:", L"text/html"));
1196 ASSERT_TRUE(FindStringASpaceStringB(L"content-type: text/html", L"content-ty pe:", L"text/html"));
1197 ASSERT_TRUE(FindStringASpaceStringB(L"content-type: sdfjsldkgjsdg content-type : text/html", L"content-type:", L"text/html"));
1198 ASSERT_TRUE(FindStringASpaceStringB(L"content-type: content-type: sdfjsldkgjsd g content-type: text/html", L"content-type:", L"text/html"));
1199 ASSERT_TRUE(FindStringASpaceStringB(L"content-type:content-type: sdfjsldkgjsdg content-type: text/html", L"content-type:", L"text/html"));
1200 ASSERT_TRUE(FindStringASpaceStringB(L"content-type:content-type: text/html" , L"content-type:", L"text/html"));
1201 ASSERT_TRUE(FindStringASpaceStringB(L"test/html content-type:content-type: text/html", L"content-type:", L"text/html"));
1202 ASSERT_TRUE(FindStringASpaceStringB(L"content-type: text/html", L"content-t ype:", L"text/html"));
1203 ASSERT_TRUE(FindStringASpaceStringB(L"content-type:\ttext/html", L"content-typ e:", L"text/html"));
1204 ASSERT_TRUE(FindStringASpaceStringB(L"content-type:\t text/html", L"content-ty pe:", L"text/html"));
1205 ASSERT_TRUE(FindStringASpaceStringB(L"Content-Type:\t text/html", L"content-ty pe:", L"text/html"));
1206 ASSERT_TRUE(FindStringASpaceStringB(L"aasd content-type: text/html", L"content -type:", L"text/html"));
1207 ASSERT_TRUE(FindStringASpaceStringB(L"aa content-TYPE: text/html", L"content-t ype:", L"text/html"));
1208 ASSERT_TRUE(FindStringASpaceStringB(L"text.html content-TYPE: text/HTML", L"c ontent-type:", L"text/html"));
1209 ASSERT_TRUE(FindStringASpaceStringB(L"text/html content-TYPE: text/HTML", L"co ntent-type:", L"text/HTML"));
1210 ASSERT_TRUE(FindStringASpaceStringB(L"AAAA content-TYPE: text/HTML", L"content -TYPE:", L"text/HTML"));
1211 ASSERT_TRUE(FindStringASpaceStringB(L"content-type:text/html AAAAA", L"content -type:", L"text/html"));
1212 ASSERT_TRUE(FindStringASpaceStringB(L"content-type: text/html", L"content-typ e:", L"text/html"));
1213 ASSERT_TRUE(FindStringASpaceStringB(L"content-type: text/htmlaaa", L"content -type:", L"text/html"));
1214 ASSERT_TRUE(FindStringASpaceStringB(L"content-type: text/html asdsdg conte nt-type", L"content-type:", L"text/html"));
1215 ASSERT_TRUE(FindStringASpaceStringB(L"content-type:\ttext/htmlconttent-type:te ", L"content-type:", L"text/html"));
1216
1217 ASSERT_FALSE(FindStringASpaceStringB(L"content-type: a text/html", L"content -type:", L"text/html"));
1218 ASSERT_FALSE(FindStringASpaceStringB(L"content-type: content-type: a text/h tml", L"content-type:", L"text/html"));
1219 ASSERT_FALSE(FindStringASpaceStringB(L"content-type: b text/html content-type : a text/html", L"content-type:", L"text/html"));
1220 ASSERT_FALSE(FindStringASpaceStringB(L"content-type:-text/html", L"content-typ e:", L"text/html"));
1221 ASSERT_FALSE(FindStringASpaceStringB(L"content-type:\ntext/html", L"content-ty pe:", L"text/html"));
1222 ASSERT_FALSE(FindStringASpaceStringB(L"content-type: a TEXT/html", L"content -type:", L"text/html"));
1223 ASSERT_FALSE(FindStringASpaceStringB(L"content-type: a html/text", L"content -type:", L"text/html"));
1224 ASSERT_FALSE(FindStringASpaceStringB(L"a dss content-type: a text/html", L"c ontent-type:", L"text/html"));
1225 ASSERT_FALSE(FindStringASpaceStringB(L"text/html content-type:-text/html", L"c ontent-type:", L"text/html"));
1226 ASSERT_FALSE(FindStringASpaceStringB(L"text/html sdfsd fcontent-type:\ntext/ht ml", L"content-type:", L"text/html"));
1227 ASSERT_FALSE(FindStringASpaceStringB(L"AAAA content-type: a TEXT/html", L"co ntent-type:", L"text/html"));
1228 ASSERT_FALSE(FindStringASpaceStringB(L"content-type: a html/text AAA", L"con tent-type:", L"text/html"));
1229 ASSERT_FALSE(FindStringASpaceStringB(L"content-type:", L"content-type:", L"tex t/html"));
1230 ASSERT_FALSE(FindStringASpaceStringB(L"content-type:content-type:", L"content- type:", L"text/html"));
1231 ASSERT_FALSE(FindStringASpaceStringB(L"content-type:content-type: content-type :", L"content-type:", L"text/html"));
1232 }
1233
1234 TEST(StringTest, ElideIfNeeded) {
1235 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 3, 3), L"1..");
1236 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 4, 3), L"12..");
1237 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 5, 3), L"123..");
1238 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 6, 3), L"1234..");
1239 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 7, 3), L"1234..");
1240 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 8, 3), L"1234..");
1241 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 9, 3), L"1234..");
1242 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 10, 3), L"1234..");
1243 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 11, 3), L"1234 6789..");
1244 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 12, 3), L"1234 6789..");
1245 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 13, 3), L"1234 6789..");
1246 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 14, 3), L"1234 6789 1234");
1247 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 15, 3), L"1234 6789 1234");
1248 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 16, 3), L"1234 6789 1234");
1249 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 17, 3), L"1234 6789 1234");
1250
1251 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 7, 6), L"1234..");
1252 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 8, 6), L"1234 6..");
1253 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 9, 6), L"1234 67..");
1254 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 10, 6), L"1234 678..");
1255 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 11, 6), L"1234 6789..");
1256 ASSERT_STREQ(ElideIfNeeded(L"1234 6789 1234", 12, 6), L"1234 6789..");
1257 }
1258
1259 TEST(StringTest, SafeStrCat) {
1260 const int kDestLen = 7;
1261 TCHAR dest[kDestLen];
1262 lstrcpyn(dest, L"short", kDestLen);
1263 ASSERT_LT(lstrlen(dest), kDestLen);
1264
1265 dest[kDestLen-1] = 'a';
1266 lstrcpyn(dest, L"medium123", kDestLen);
1267 ASSERT_EQ(dest[kDestLen - 1], '\0');
1268 ASSERT_LT(lstrlen(dest), kDestLen);
1269
1270 lstrcpyn(dest, L"longerlonger", kDestLen);
1271 ASSERT_EQ(dest[kDestLen - 1], '\0');
1272 ASSERT_LT(lstrlen(dest), kDestLen);
1273
1274 lstrcpyn(dest, L"12", kDestLen);
1275 SafeStrCat(dest, L"3456", kDestLen);
1276 ASSERT_EQ(dest[kDestLen - 1], '\0');
1277 ASSERT_LT(lstrlen(dest), kDestLen);
1278 }
1279
1280 void TestPathFindExtension(const TCHAR *s) {
1281 ASSERT_STREQ(String_PathFindExtension(s), PathFindExtension(s));
1282 }
1283
1284 TEST(StringTest, TestPathFindExtension) {
1285 TestPathFindExtension(L"c:\\test.tmp");
1286 TestPathFindExtension(L"c:\\test.temp");
1287 TestPathFindExtension(L"c:\\t\\e\\st.temp");
1288 TestPathFindExtension(L"c:\\a.temp");
1289 TestPathFindExtension(L"\\aaa\\a.temp");
1290 TestPathFindExtension(L"\\a\\a.temp");
1291 TestPathFindExtension(L"\\a\\a.temp");
1292 TestPathFindExtension(L"\\a\\a.t....emp");
1293 TestPathFindExtension(L"\\a.a.a...a\\a.t....emp");
1294 TestPathFindExtension(L"\\a\\a\\bbbb\\ddddddddddddddd.temp");
1295 TestPathFindExtension(L"\\a\\a\\bbbb\\ddddddddddddddd.te___124567mp");
1296 TestPathFindExtension(L"\\a\\a\\bbbb\\ddddddd.dddddddd.te___124567mp");
1297 }
1298
1299 TEST(StringTest, TextToLinesAndBack) {
1300 const TCHAR sample_input[] = L"Now is the time\r\nfor all good men\r\nto come to the aid of their country";
1301 const TCHAR* sample_lines[] = { L"Now is the time", L"for all good men", L"to come to the aid of their country" };
1302 const TCHAR sample_output1[] = L"Now is the time\nfor all good men\nto come to the aid of their country\n";
1303 const TCHAR sample_output2[] = L"Now is the timefor all good mento come to the aid of their country";
1304
1305 CString text_in(sample_input);
1306 std::vector<CString> lines;
1307 CString text_out;
1308
1309 TextToLines(text_in, L"\r\n", &lines);
1310 ASSERT_EQ(lines.size(), 3);
1311 for (size_t i = 0; i < arraysize(sample_lines); ++i) {
1312 ASSERT_TRUE(0 == lines[i].Compare(sample_lines[i]));
1313 }
1314 LinesToText(lines, L"\n", &text_out);
1315 ASSERT_TRUE(0 == text_out.Compare(sample_output1));
1316 LinesToText(lines, L"", &text_out);
1317 ASSERT_TRUE(0 == text_out.Compare(sample_output2));
1318 }
1319
1320 CString TrimStdString(const TCHAR* str) {
1321 CString s(str);
1322 TrimString(s, L" \t");
1323 return s;
1324 }
1325
1326 TEST(StringTest, TrimString) {
1327 ASSERT_STREQ(L"abc", TrimStdString(L"abc"));
1328 ASSERT_STREQ(L"abc", TrimStdString(L" abc "));
1329 ASSERT_STREQ(L"a c", TrimStdString(L" a c "));
1330 ASSERT_STREQ(L"abc", TrimStdString(L" \tabc\t "));
1331 ASSERT_STREQ(L"", TrimStdString(L""));
1332 ASSERT_STREQ(L"", TrimStdString(L" "));
1333 }
1334
1335 TEST(StringTest, StripFirstQuotedToken) {
1336 ASSERT_STREQ(StripFirstQuotedToken(L""), L"");
1337 ASSERT_STREQ(StripFirstQuotedToken(L"a" ), L"");
1338 ASSERT_STREQ(StripFirstQuotedToken(L" a b "), L"b");
1339 ASSERT_STREQ(StripFirstQuotedToken(L"\"abc\" def"), L" def");
1340 ASSERT_STREQ(StripFirstQuotedToken(L" \"abc def\" ghi "), L" ghi");
1341 ASSERT_STREQ(StripFirstQuotedToken(L"\"abc\" \"def\" "), L" \"def\"");
1342 }
1343
1344 TEST(StringTest, EscapeUnescape) {
1345 CString original_str(_T("test <>\"#{}|\\^[]?%&/"));
1346 CString escaped_str;
1347 ASSERT_SUCCEEDED(StringEscape(original_str, true, &escaped_str));
1348 ASSERT_STREQ(escaped_str,
1349 _T("test%20%3C%3E%22%23%7B%7D%7C%5C%5E%5B%5D%3F%25%26%2F"));
1350 CString unescaped_str;
1351 ASSERT_SUCCEEDED(StringUnescape(escaped_str, &unescaped_str));
1352 ASSERT_STREQ(original_str, unescaped_str);
1353
1354 original_str = _T("foo.test path?app=1");
1355 ASSERT_SUCCEEDED(StringEscape(original_str, false, &escaped_str));
1356 ASSERT_STREQ(escaped_str,
1357 _T("foo.test%20path?app=1"));
1358 ASSERT_SUCCEEDED(StringUnescape(escaped_str, &unescaped_str));
1359 ASSERT_STREQ(original_str, unescaped_str);
1360 }
1361
1362 TEST(StringTest, ReplaceIgnoreCase) {
1363 EXPECT_STREQ(_T("AddddAddddAB"),
1364 String_ReplaceIgnoreCase(_T("ABCABCAB"), _T("bc"), _T("dddd")));
1365 EXPECT_STREQ(_T("AdAdAd"),
1366 String_ReplaceIgnoreCase(_T("ABCABCABC"), _T("bc"), _T("d")));
1367 }
1368
1369 TEST(StringTest, String_StringToDecimalIntChecked) {
1370 int value = 0;
1371
1372 // This code before the first valid case verifies that errno is properly
1373 // cleared and there are no dependencies on prior code.
1374 EXPECT_EQ(0, _set_errno(ERANGE));
1375
1376 // Valid Cases
1377 EXPECT_TRUE(String_StringToDecimalIntChecked(_T("935"), &value));
1378 EXPECT_EQ(value, 935);
1379 EXPECT_TRUE(String_StringToDecimalIntChecked(_T("-935"), &value));
1380 EXPECT_EQ(value, -935);
1381 EXPECT_TRUE(String_StringToDecimalIntChecked(_T("0"), &value));
1382 EXPECT_EQ(value, 0);
1383 EXPECT_TRUE(String_StringToDecimalIntChecked(_T("2147483647"), &value));
1384 EXPECT_EQ(value, LONG_MAX);
1385 EXPECT_TRUE(String_StringToDecimalIntChecked(_T("-2147483648"), &value));
1386 EXPECT_EQ(value, LONG_MIN);
1387 EXPECT_TRUE(String_StringToDecimalIntChecked(_T(" 0"), &value));
1388 EXPECT_EQ(value, 0);
1389
1390 // Failing Cases
1391 EXPECT_FALSE(String_StringToDecimalIntChecked(_T(""), &value));
1392 EXPECT_FALSE(String_StringToDecimalIntChecked(_T("2147483648"), &value));
1393 EXPECT_EQ(value, LONG_MAX);
1394 EXPECT_FALSE(String_StringToDecimalIntChecked(_T("-2147483649"), &value));
1395 EXPECT_EQ(value, LONG_MIN);
1396 EXPECT_FALSE(String_StringToDecimalIntChecked(_T("0x935"), &value));
1397 EXPECT_FALSE(String_StringToDecimalIntChecked(_T("nine"), &value));
1398 EXPECT_FALSE(String_StringToDecimalIntChecked(_T("9nine"), &value));
1399 EXPECT_FALSE(String_StringToDecimalIntChecked(_T("nine9"), &value));
1400
1401 // A valid case after an overflow verifies that this method clears errno.
1402 EXPECT_FALSE(String_StringToDecimalIntChecked(_T("2147483648"), &value));
1403 EXPECT_TRUE(String_StringToDecimalIntChecked(_T("935"), &value));
1404 }
1405 TEST(StringTest, String_StringToTristate) {
1406 Tristate value = TRISTATE_NONE;
1407
1408 // Valid Cases
1409 EXPECT_TRUE(String_StringToTristate(_T("0"), &value));
1410 EXPECT_EQ(value, TRISTATE_FALSE);
1411 EXPECT_TRUE(String_StringToTristate(_T("1"), &value));
1412 EXPECT_EQ(value, TRISTATE_TRUE);
1413 EXPECT_TRUE(String_StringToTristate(_T("2"), &value));
1414 EXPECT_EQ(value, TRISTATE_NONE);
1415
1416 // Invalid Cases
1417 EXPECT_FALSE(String_StringToTristate(_T("-1"), &value));
1418 EXPECT_FALSE(String_StringToTristate(_T("3"), &value));
1419 EXPECT_FALSE(String_StringToTristate(_T(""), &value));
1420 }
1421
1422 TEST(StringTest, ParseNameValuePair) {
1423 CString name;
1424 CString value;
1425
1426 // Valid Cases
1427 EXPECT_TRUE(ParseNameValuePair(_T("xx=yyzz"), _T('='), &name, &value));
1428 EXPECT_EQ(name, _T("xx"));
1429 EXPECT_EQ(value, _T("yyzz"));
1430 EXPECT_TRUE(ParseNameValuePair(_T("x=3?\\/\r\n "), _T('='), &name, &value));
1431 EXPECT_EQ(name, _T("x"));
1432 EXPECT_EQ(value, _T("3?\\/\r\n "));
1433 EXPECT_TRUE(ParseNameValuePair(_T("3?google"), _T('?'), &name, &value));
1434 EXPECT_EQ(name, _T("3"));
1435 EXPECT_EQ(value, _T("google"));
1436
1437 // Invalid Cases
1438 EXPECT_FALSE(ParseNameValuePair(_T(""), _T('='), &name, &value));
1439 EXPECT_FALSE(ParseNameValuePair(_T(" "), _T('='), &name, &value));
1440 EXPECT_FALSE(ParseNameValuePair(_T("="), _T('='), &name, &value));
1441 EXPECT_FALSE(ParseNameValuePair(_T("x="), _T('='), &name, &value));
1442 EXPECT_FALSE(ParseNameValuePair(_T("=y"), _T('='), &name, &value));
1443 EXPECT_FALSE(ParseNameValuePair(_T("="), _T('='), &name, &value));
1444 EXPECT_FALSE(ParseNameValuePair(_T("xxyyzz"), _T('='), &name, &value));
1445 EXPECT_FALSE(ParseNameValuePair(_T("xx yyzz"), _T('='), &name, &value));
1446 EXPECT_FALSE(ParseNameValuePair(_T("xx==yyzz"), _T('='), &name, &value));
1447 EXPECT_FALSE(ParseNameValuePair(_T("xx=yy=zz"), _T('='), &name, &value));
1448 }
1449
1450 TEST(StringTest, SplitCommandLineInPlace) {
1451 const TCHAR * const test_long_paths[] = {
1452 _T("c:\\Program Files\\Google\\App\\App.exe"),
1453 _T("c:\\Program Files\\Google\\App\\Long Name App.exe"),
1454 };
1455 const TCHAR * const test_short_paths[] = {
1456 _T("notepad.exe"),
1457 };
1458 const TCHAR * const test_arguments[] = {
1459 _T("/a=\"some text\""),
1460 _T("/a /b /c"),
1461 _T(""),
1462 };
1463 TCHAR command_line[1024] = {};
1464 TCHAR* path = NULL;
1465 TCHAR* arguments = NULL;
1466 for (int ii = 0; ii < ARRAYSIZE(test_arguments) ; ++ii) {
1467 for (int jj = 0; jj < ARRAYSIZE(test_long_paths) ; ++jj) {
1468 _snwprintf_s(command_line, ARRAYSIZE(command_line), _TRUNCATE,
1469 _T("\"%s\" %s"), test_long_paths[jj], test_arguments[ii]);
1470 EXPECT_EQ(true, SplitCommandLineInPlace(command_line, &path, &arguments));
1471 EXPECT_STREQ(test_long_paths[jj], path);
1472 EXPECT_STREQ(test_arguments[ii], arguments);
1473 }
1474 for (int kk = 0; kk < ARRAYSIZE(test_short_paths) ; ++kk) {
1475 _snwprintf_s(command_line, ARRAYSIZE(command_line), _TRUNCATE,
1476 _T("%s %s"), test_short_paths[kk], test_arguments[ii]);
1477 EXPECT_EQ(true, SplitCommandLineInPlace(command_line, &path, &arguments));
1478 EXPECT_STREQ(test_short_paths[kk], path);
1479 EXPECT_STREQ(test_arguments[ii], arguments);
1480 }
1481 }
1482 }
1483
1484 TEST(StringTest, ContainsOnlyAsciiChars) {
1485 CString test(_T("hello worlr"));
1486 ASSERT_TRUE(ContainsOnlyAsciiChars(test));
1487
1488 TCHAR non_ascii[] = {0x4345, 0x1234, 0x2000};
1489 ASSERT_FALSE(ContainsOnlyAsciiChars(non_ascii));
1490 }
1491 TEST(StringTest, BytesToHex) {
1492 EXPECT_STREQ(BytesToHex(NULL, 0), _T(""));
1493 uint8 i = 0;
1494 EXPECT_STREQ(BytesToHex(&i, sizeof(i)), _T("00"));
1495 i = 0x7f;
1496 EXPECT_STREQ(BytesToHex(&i, sizeof(i)), _T("7f"));
1497 i = 0xff;
1498 EXPECT_STREQ(BytesToHex(&i, sizeof(i)), _T("ff"));
1499
1500 // Assumes little-endian representation of integers.
1501 const uint32 array[] = {0x67452301, 0xefcdab89};
1502 EXPECT_STREQ(BytesToHex(reinterpret_cast<const uint8*>(array), sizeof(array)),
1503 _T("0123456789abcdef"));
1504
1505 const uint8* first = reinterpret_cast<const uint8*>(array);
1506 const uint8* last = first + sizeof(array);
1507 EXPECT_STREQ(BytesToHex(std::vector<uint8>(first, last)),
1508 _T("0123456789abcdef"));
1509 }
1510
1511 TEST(StringTest, JoinStrings) {
1512 std::vector<CString> components;
1513 const TCHAR* delim = _T("-");
1514 CString result;
1515
1516 JoinStrings(components, delim, &result);
1517 EXPECT_TRUE(result.IsEmpty());
1518 JoinStrings(components, NULL, &result);
1519 EXPECT_TRUE(result.IsEmpty());
1520
1521 components.push_back(CString(_T("foo")));
1522 JoinStrings(components, delim, &result);
1523 EXPECT_STREQ(result, (_T("foo")));
1524 JoinStrings(components, NULL, &result);
1525 EXPECT_STREQ(result, (_T("foo")));
1526
1527 components.push_back(CString(_T("bar")));
1528 JoinStrings(components, delim, &result);
1529 EXPECT_STREQ(result, (_T("foo-bar")));
1530 JoinStrings(components, NULL, &result);
1531 EXPECT_STREQ(result, (_T("foobar")));
1532
1533 components.push_back(CString(_T("baz")));
1534 JoinStrings(components, delim, &result);
1535 EXPECT_STREQ(result, (_T("foo-bar-baz")));
1536 JoinStrings(components, NULL, &result);
1537 EXPECT_STREQ(result, (_T("foobarbaz")));
1538
1539
1540 JoinStringsInArray(NULL, 0, delim, &result);
1541 EXPECT_TRUE(result.IsEmpty());
1542 JoinStringsInArray(NULL, 0, NULL, &result);
1543 EXPECT_TRUE(result.IsEmpty());
1544
1545 const TCHAR* array1[] = {_T("foo")};
1546 JoinStringsInArray(array1, arraysize(array1), delim, &result);
1547 EXPECT_STREQ(result, (_T("foo")));
1548 JoinStringsInArray(array1, arraysize(array1), NULL, &result);
1549 EXPECT_STREQ(result, (_T("foo")));
1550
1551 const TCHAR* array2[] = {_T("foo"), _T("bar")};
1552 JoinStringsInArray(array2, arraysize(array2), delim, &result);
1553 EXPECT_STREQ(result, (_T("foo-bar")));
1554 JoinStringsInArray(array2, arraysize(array2), NULL, &result);
1555 EXPECT_STREQ(result, (_T("foobar")));
1556
1557 const TCHAR* array3[] = {_T("foo"), _T("bar"), _T("baz")};
1558 JoinStringsInArray(array3, arraysize(array3), delim, &result);
1559 EXPECT_STREQ(result, (_T("foo-bar-baz")));
1560 JoinStringsInArray(array3, arraysize(array3), NULL, &result);
1561 EXPECT_STREQ(result, (_T("foobarbaz")));
1562
1563 const TCHAR* array_null_1[] = {NULL};
1564 JoinStringsInArray(array_null_1, arraysize(array_null_1), delim, &result);
1565 EXPECT_STREQ(result, (_T("")));
1566
1567 const TCHAR* array_null_2[] = {NULL, NULL};
1568 JoinStringsInArray(array_null_2, arraysize(array_null_2), delim, &result);
1569 EXPECT_STREQ(result, (_T("-")));
1570 }
1571
1572 TEST(StringTest, String_ToUpper) {
1573 // String_ToUpper is a wrapper over ::CharUpper.
1574 TCHAR s[] = _T("foo");
1575 String_ToUpper(s);
1576 EXPECT_STREQ(s, _T("FOO"));
1577 }
1578
1579 TEST(StringTest, FormatResourceMessage_Valid) {
1580 EXPECT_STREQ(
1581 _T("Thanks for installing Gears."),
1582 FormatResourceMessage(IDS_BUNDLE_INSTALLED_SUCCESSFULLY, _T("Gears")));
1583
1584 EXPECT_STREQ(
1585 _T("The installer encountered error 12345: Action failed."),
1586 FormatResourceMessage(IDS_INSTALLER_FAILED_WITH_MESSAGE,
1587 _T("12345"),
1588 _T("Action failed.")));
1589 }
1590
1591 TEST(StringTest, FormatResourceMessage_IdNotFound) {
1592 EXPECT_STREQ(_T(""), FormatResourceMessage(100000, "foo", 9));
1593 }
1594
1595 TEST(StringTest, FormatErrorCode) {
1596 EXPECT_STREQ(_T("0xffffffff"), FormatErrorCode(static_cast<DWORD>(-1)));
1597 EXPECT_STREQ(_T("0"), FormatErrorCode(0));
1598 EXPECT_STREQ(_T("567"), FormatErrorCode(567));
1599 EXPECT_STREQ(_T("2147483647"), FormatErrorCode(0x7fffffff));
1600 EXPECT_STREQ(_T("0x80000000"), FormatErrorCode(0x80000000));
1601 EXPECT_STREQ(_T("0x80000001"), FormatErrorCode(0x80000001));
1602 EXPECT_STREQ(_T("0x8fffffff"), FormatErrorCode(0x8fffffff));
1603 }
1604
1605 TEST(StringTest, Utf8BufferToWideChar) {
1606 // Unicode Greek capital letters.
1607 const TCHAR expected_string[] = {913, 914, 915, 916, 917, 918, 919, 920,
1608 921, 922, 923, 924, 925, 926, 927, 928,
1609 929, 931, 932, 933, 934, 935, 936, 937, 0};
1610 // Greek capital letters UTF-8 encoded.
1611 const char buffer[] = "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ";
1612 CString actual_string = Utf8BufferToWideChar(
1613 std::vector<uint8>(buffer, buffer + arraysize(buffer)));
1614 EXPECT_STREQ(expected_string, actual_string);
1615
1616 EXPECT_STREQ(_T(""), Utf8BufferToWideChar(std::vector<uint8>()));
1617 }
1618
1619 TEST(StringTest, WideStringToUtf8UrlEncodedStringRoundTrip) {
1620 CString unicode_string;
1621 ASSERT_TRUE(unicode_string.LoadString(IDS_ESCAPE_TEST));
1622
1623 // Convert from unicode to a wide representation of utf8,url encoded string.
1624 CString utf8encoded_str;
1625 ASSERT_HRESULT_SUCCEEDED(WideStringToUtf8UrlEncodedString(unicode_string,
1626 &utf8encoded_str));
1627 ASSERT_FALSE(utf8encoded_str.IsEmpty());
1628
1629 // Reconvert from the utf8, url encoded string to the wide version.
1630 CString out;
1631 ASSERT_HRESULT_SUCCEEDED(Utf8UrlEncodedStringToWideString(utf8encoded_str,
1632 &out));
1633 ASSERT_FALSE(out.IsEmpty());
1634 ASSERT_STREQ(unicode_string, out);
1635 }
1636
1637 TEST(StringTest, WideStringToUtf8UrlEncodedStringEmptyString) {
1638 CString unicode_string;
1639
1640 // Convert from unicode to a wide representation of utf8,url encoded string.
1641 CString utf8encoded_str;
1642 ASSERT_HRESULT_SUCCEEDED(WideStringToUtf8UrlEncodedString(unicode_string,
1643 &utf8encoded_str));
1644 ASSERT_TRUE(utf8encoded_str.IsEmpty());
1645 }
1646
1647 TEST(StringTest, WideStringToUtf8UrlEncodedStringSpaces) {
1648 CString unicode_string(_T(" "));
1649 CStringA expected("%20%20%20");
1650 CString exp(expected);
1651
1652 // Convert from unicode to a wide representation of utf8,url encoded string.
1653 CString utf8encoded_str;
1654 ASSERT_HRESULT_SUCCEEDED(WideStringToUtf8UrlEncodedString(unicode_string,
1655 &utf8encoded_str));
1656 ASSERT_STREQ(exp, utf8encoded_str);
1657 }
1658
1659 TEST(StringTest, WideStringToUtf8UrlEncodedStringTestString) {
1660 CString unicode_string(_T("Test Str/ing&values=&*^%$#"));
1661 CStringA ansi_exp("Test%20Str/ing%26values=%26*%5E%$#");
1662 CString exp(ansi_exp);
1663
1664 // Convert from unicode to a wide representation of utf8,url encoded string.
1665 CString utf8encoded_str;
1666 ASSERT_HRESULT_SUCCEEDED(WideStringToUtf8UrlEncodedString(unicode_string,
1667 &utf8encoded_str));
1668 ASSERT_STREQ(exp, utf8encoded_str);
1669 }
1670
1671 TEST(StringTest, WideStringToUtf8UrlEncodedStringSimpleTestString) {
1672 CString unicode_string(_T("TestStr"));
1673 CStringA ansi_exp("TestStr");
1674 CString exp(ansi_exp);
1675
1676 // Convert from unicode to a wide representation of utf8,url encoded string.
1677 CString utf8encoded_str;
1678 ASSERT_HRESULT_SUCCEEDED(WideStringToUtf8UrlEncodedString(unicode_string,
1679 &utf8encoded_str));
1680 ASSERT_STREQ(exp, utf8encoded_str);
1681 }
1682
1683 TEST(StringTest, Utf8UrlEncodedStringToWideStringEmpty) {
1684 // Convert from wide representation of utf8,url encoded string to unicode.
1685 CString unicode_string;
1686 CString utf8encoded_str;
1687 ASSERT_HRESULT_SUCCEEDED(Utf8UrlEncodedStringToWideString(utf8encoded_str,
1688 &unicode_string));
1689 ASSERT_TRUE(unicode_string.IsEmpty());
1690 }
1691
1692 TEST(StringTest, Utf8UrlEncodedStringToWideStringSpaces) {
1693 CString exp(_T(" "));
1694 CStringA utf8("%20%20%20");
1695 CString utf8encoded_str(utf8);
1696
1697 // Convert from wide representation of utf8,url encoded string to unicode.
1698 CString unicode_string;
1699 ASSERT_HRESULT_SUCCEEDED(Utf8UrlEncodedStringToWideString(utf8encoded_str,
1700 &unicode_string));
1701 ASSERT_STREQ(exp, unicode_string);
1702 }
1703
1704 TEST(StringTest, Utf8UrlEncodedStringToWideStringSimpleString) {
1705 CString exp(_T("TestStr"));
1706 CStringA utf8("TestStr");
1707 CString utf8encoded_str(utf8);
1708
1709 // Convert from wide representation of utf8,url encoded string to unicode.
1710 CString unicode_string;
1711 ASSERT_HRESULT_SUCCEEDED(Utf8UrlEncodedStringToWideString(utf8encoded_str,
1712 &unicode_string));
1713 ASSERT_STREQ(exp, unicode_string);
1714 }
1715
1716 TEST(StringTest, ConvertFileUriToLocalPath) {
1717 CString path_out;
1718
1719 EXPECT_HRESULT_SUCCEEDED(ConvertFileUriToLocalPath(
1720 _T("file:///c:/test/path/one/test.txt"), &path_out));
1721 EXPECT_STREQ(_T("c:\\test\\path\\one\\test.txt"), path_out);
1722
1723 EXPECT_HRESULT_SUCCEEDED(ConvertFileUriToLocalPath(
1724 _T("file:///d:/path%20with%20spaces/test.txt"), &path_out));
1725 EXPECT_STREQ(_T("d:\\path with spaces\\test.txt"), path_out);
1726
1727 EXPECT_HRESULT_SUCCEEDED(ConvertFileUriToLocalPath(
1728 _T("file://e|/pipe_for_colon/test.txt"), &path_out));
1729 EXPECT_STREQ(_T("e:\\pipe_for_colon\\test.txt"), path_out);
1730
1731 // The unescaped & is illegal in both URIs and Win32 paths, and the escaped
1732 // bracket is illegal in a Win32 path. However, we should pass them through
1733 // and allow the file system layer to reject it at an appropriate spot.
1734 EXPECT_HRESULT_SUCCEEDED(ConvertFileUriToLocalPath(
1735 _T("file://f:/path_with_illegal_characters/te&st%60.txt"), &path_out));
1736 EXPECT_STREQ(_T("f:\\path_with_illegal_characters\\te&st%60.txt"), path_out);
1737
1738 EXPECT_HRESULT_SUCCEEDED(ConvertFileUriToLocalPath(
1739 _T("file://g:/url_has_get_arguments/test.txt?q=abcde"), &path_out));
1740 EXPECT_STREQ(_T("g:\\url_has_get_arguments\\test.txt"), path_out);
1741
1742 EXPECT_HRESULT_FAILED(ConvertFileUriToLocalPath(
1743 _T("i:\\actual_path_instead_of_url\\test.txt"), &path_out));
1744 EXPECT_HRESULT_FAILED(ConvertFileUriToLocalPath(
1745 _T("http://uri_uses_another_scheme.com/test.txt"), &path_out));
1746 EXPECT_HRESULT_FAILED(ConvertFileUriToLocalPath(_T("not_a_uri"), &path_out));
1747 EXPECT_HRESULT_FAILED(ConvertFileUriToLocalPath(_T(""), &path_out));
1748 }
1749
1750 } // namespace omaha
1751
OLDNEW
« no previous file with comments | « base/string.cc ('k') | base/synchronized.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698