1 /* Copyright (c) 2016 Michael Hansen
2
3 Permission is hereby granted, free of charge, to any person obtaining a
4 copy of this software and associated documentation files (the "Software"),
5 to deal in the Software without restriction, including without limitation
6 the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 and/or sell copies of the Software, and to permit persons to whom the
8 Software is furnished to do so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in
11 all copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 DEALINGS IN THE SOFTWARE. */
20
21 #include "st_string.h"
22 #include "st_assert.h"
23
24 #include <gtest/gtest.h>
25 #include <wchar.h>
26 #include <cmath>
27 #include <limits>
28 #include <iostream>
29
30 #ifdef _MSC_VER
31 # pragma warning(disable: 4996)
32 #endif
33
34 namespace ST
35 {
36 // Teach GTest how to print an ST::string
PrintTo(const ST::string & str,std::ostream * os)37 static void PrintTo(const ST::string &str, std::ostream *os)
38 {
39 *os << "ST::string{\"" << str.c_str() << "\"}";
40 }
41 }
42
43 template <typename T, size_t Size>
text_size(const T (&)[Size])44 constexpr size_t text_size(const T (&)[Size]) { return Size - 1; }
45
46 static const char32_t test_data[] = {
47 0x20, 0x7f, /* Normal ASCII chars */
48 0xff, 0x100, /* 8-bit boundary chars */
49 0x7fff, /* UTF-8 2-byte boundary */
50 0xffff, 0x10000, /* 16-bit boundary chars */
51 0x10020, 0x40000, /* Non-edge UTF-16 surrogate pairs */
52 0x10ffff, /* Highest Unicode character */
53 0 /* Null terminator */
54 };
55
56 /* UTF-8 version of above test data */
57 static const char utf8_test_data[] =
58 "\x20" "\x7f"
59 "\xc3\xbf" "\xc4\x80"
60 "\xe7\xbf\xbf"
61 "\xef\xbf\xbf" "\xf0\x90\x80\x80"
62 "\xf0\x90\x80\xa0" "\xf1\x80\x80\x80"
63 "\xf4\x8f\xbf\xbf";
64
65 /* UTF-16 version of test data */
66 static const char16_t utf16_test_data[] = {
67 0x20, 0x7f,
68 0xff, 0x100,
69 0x7fff,
70 0xffff,
71 /* surrogate pairs for chars >0xffff */
72 0xd800, 0xdc00,
73 0xd800, 0xdc20,
74 0xd8c0, 0xdc00,
75 0xdbff, 0xdfff,
76 0
77 };
78
79 /* wide version of test data */
80 static const wchar_t wide_test_data[] =
81 L"\x20" L"\x7f"
82 L"\xff" L"\u0100"
83 L"\u7fff"
84 L"\uffff" L"\U00010000"
85 L"\U00010020" L"\U00040000"
86 L"\U0010FFFF";
87
88 /* Latin-1 test data */
89 static const char latin1_data[] = "\x20\x7e\xa0\xff";
90 static const char latin1_utf8[] = "\x20\x7e\xc2\xa0\xc3\xbf";
91 static const char16_t latin1_utf16[] = { 0x20, 0x7e, 0xa0, 0xff, 0 };
92 static const char32_t latin1_utf32[] = { 0x20, 0x7e, 0xa0, 0xff, 0 };
93 static const wchar_t latin1_wide[] = L"\x20\x7e\u00a0\u00ff";
94
95 /* Utility for comparing char16_t/char32_t buffers */
96 template <typename char_T>
T_strcmp(const char_T * left,const char_T * right)97 static int T_strcmp(const char_T *left, const char_T *right)
98 {
99 for ( ;; ) {
100 if (*left != *right)
101 return *left - *right;
102 if (*left == 0)
103 return 0;
104
105 ++left;
106 ++right;
107 }
108 }
109
TEST(string,helpers)110 TEST(string, helpers)
111 {
112 /* Ensure the utilities for testing the module function properly */
113 EXPECT_EQ(0, T_strcmp("abc", "abc"));
114 EXPECT_LT(0, T_strcmp("abc", "aba"));
115 EXPECT_GT(0, T_strcmp("abc", "abe"));
116 EXPECT_LT(0, T_strcmp("abc", "ab"));
117 EXPECT_GT(0, T_strcmp("abc", "abcd"));
118 EXPECT_EQ(0, T_strcmp("", ""));
119 EXPECT_GT(0, T_strcmp("", "a"));
120 EXPECT_LT(0, T_strcmp("a", ""));
121 }
122
TEST(string,utility)123 TEST(string, utility)
124 {
125 // Literal constructors
126 EXPECT_EQ(ST::string(), ST_LITERAL(""));
127 EXPECT_EQ(ST::string("abc"), ST_LITERAL("abc"));
128
129 EXPECT_EQ(0U, ST::string().size());
130 EXPECT_TRUE(ST::string().empty());
131
132 // Short and Long string length
133 EXPECT_EQ(4U, ST_LITERAL("1234").size());
134 EXPECT_EQ(15U, ST_LITERAL("123456789012345").size());
135 EXPECT_EQ(16U, ST_LITERAL("1234567890123456").size());
136 EXPECT_EQ(32U, ST_LITERAL("12345678901234567890123456789012").size());
137
138 // ST::string stores data as UTF-8 internally
139 EXPECT_EQ(text_size(utf8_test_data), ST::string(utf8_test_data).size());
140 }
141
TEST(string,stack_construction)142 TEST(string, stack_construction)
143 {
144 char stack_buf[256];
145 strcpy(stack_buf, "Test");
146 ST::string test(stack_buf);
147
148 EXPECT_EQ(ST_LITERAL("Test"), test);
149 EXPECT_EQ(strlen("Test"), test.size());
150
151 wchar_t wstack_buf[256];
152 wcscpy(wstack_buf, L"Test");
153 ST::string wtest(wstack_buf);
154
155 EXPECT_EQ(ST::string(L"Test"), wtest);
156 EXPECT_EQ(strlen("Test"), wtest.size());
157
158 strcpy(stack_buf, "operator=");
159 test = stack_buf;
160
161 EXPECT_EQ(ST_LITERAL("operator="), test);
162 EXPECT_EQ(strlen("operator="), test.size());
163
164 wcscpy(wstack_buf, L"operator=");
165 wtest = wstack_buf;
166
167 EXPECT_EQ(ST::string(L"operator="), wtest);
168 EXPECT_EQ(strlen("operator="), wtest.size());
169 }
170
TEST(string,copy)171 TEST(string, copy)
172 {
173 // If this changes, this test may need to be updated to match
174 ASSERT_EQ(16, ST_MAX_SSO_LENGTH);
175
176 ST::string s1("Test");
177 ST::string dest(s1);
178 EXPECT_EQ(ST_LITERAL("Test"), dest);
179 EXPECT_EQ(4U, dest.size());
180
181 ST::string s2("operator=");
182 dest = s2;
183 EXPECT_EQ(ST_LITERAL("operator="), dest);
184 EXPECT_EQ(9U, dest.size());
185
186 ST::string s3("0123456789abcdefghij");
187 ST::string dest2(s3);
188 EXPECT_EQ(ST_LITERAL("0123456789abcdefghij"), dest2);
189 EXPECT_EQ(20U, dest2.size());
190
191 ST::string s4("9876543210zyxwvutsrqponm");
192 dest2 = s4;
193 EXPECT_EQ(ST_LITERAL("9876543210zyxwvutsrqponm"), dest2);
194 EXPECT_EQ(24U, dest2.size());
195 }
196
TEST(string,move)197 TEST(string, move)
198 {
199 // If this changes, this test may need to be updated to match
200 ASSERT_EQ(16, ST_MAX_SSO_LENGTH);
201
202 ST::string s1("Test");
203 ST::string dest(std::move(s1));
204 EXPECT_EQ(ST_LITERAL("Test"), dest);
205 EXPECT_EQ(4U, dest.size());
206
207 ST::string s2("operator=");
208 dest = std::move(s2);
209 EXPECT_EQ(ST_LITERAL("operator="), dest);
210 EXPECT_EQ(9U, dest.size());
211
212 ST::string s3("0123456789abcdefghij");
213 ST::string dest2(std::move(s3));
214 EXPECT_EQ(ST_LITERAL("0123456789abcdefghij"), dest2);
215 EXPECT_EQ(20U, dest2.size());
216
217 ST::string s4("9876543210zyxwvutsrqponm");
218 dest2 = std::move(s4);
219 EXPECT_EQ(ST_LITERAL("9876543210zyxwvutsrqponm"), dest2);
220 EXPECT_EQ(24U, dest2.size());
221 }
222
TEST(string,conv_utf8)223 TEST(string, conv_utf8)
224 {
225 // From UTF-16 to UTF-8
226 ST::char_buffer from_utf16 = ST::utf16_to_utf8(utf16_test_data, text_size(utf16_test_data),
227 ST::check_validity);
228 EXPECT_EQ(text_size(utf8_test_data), from_utf16.size());
229 EXPECT_EQ(0, T_strcmp(utf8_test_data, from_utf16.data()));
230
231 // From UTF-32 to UTF-8
232 ST::char_buffer from_utf32 = ST::utf32_to_utf8(test_data, text_size(test_data),
233 ST::check_validity);
234 EXPECT_EQ(text_size(utf8_test_data), from_utf32.size());
235 EXPECT_EQ(0, T_strcmp(utf8_test_data, from_utf32.data()));
236
237 // From Wide to UTF-8
238 ST::char_buffer from_wide = ST::wchar_to_utf8(wide_test_data, text_size(wide_test_data),
239 ST::check_validity);
240 EXPECT_EQ(text_size(utf8_test_data), from_wide.size());
241 EXPECT_EQ(0, T_strcmp(utf8_test_data, from_wide.data()));
242
243 // From Latin-1 to UTF-8
244 ST::char_buffer from_latin_1 = ST::latin_1_to_utf8(latin1_data, text_size(latin1_data));
245 EXPECT_EQ(text_size(latin1_utf8), from_latin_1.size());
246 EXPECT_EQ(0, T_strcmp(latin1_utf8, from_latin_1.data()));
247 }
248
TEST(string,string_utf8)249 TEST(string, string_utf8)
250 {
251 // From UTF-8 to ST::string
252 ST::string from_utf8 = ST::string::from_utf8(utf8_test_data);
253 EXPECT_STREQ(utf8_test_data, from_utf8.c_str());
254 EXPECT_EQ(text_size(utf8_test_data), from_utf8.size());
255 ST::utf32_buffer unicode = from_utf8.to_utf32();
256 EXPECT_EQ(0, T_strcmp(test_data, unicode.data()));
257
258 // From ST::string to UTF-8
259 ST::string to_utf8 = ST::string::from_utf32(test_data);
260 EXPECT_STREQ(utf8_test_data, to_utf8.c_str());
261
262 // Empty strings
263 ST::string empty = ST::string::from_utf8("");
264 EXPECT_EQ(0U, empty.size());
265 EXPECT_EQ(0, T_strcmp(empty.c_str(), ""));
266
267 const char32_t empty_data[] = { 0 };
268 empty = ST::string::from_utf32(empty_data);
269 EXPECT_EQ(0U, empty.size());
270 EXPECT_EQ(0, T_strcmp(empty.c_str(), ""));
271 }
272
TEST(string,conv_utf16)273 TEST(string, conv_utf16)
274 {
275 // From UTF-8 to UTF-16
276 ST::utf16_buffer from_utf8 = ST::utf8_to_utf16(utf8_test_data, text_size(utf8_test_data),
277 ST::check_validity);
278 EXPECT_EQ(text_size(utf16_test_data), from_utf8.size());
279 EXPECT_EQ(0, T_strcmp(utf16_test_data, from_utf8.data()));
280
281 // From UTF-32 to UTF-16
282 ST::utf16_buffer from_utf32 = ST::utf32_to_utf16(test_data, text_size(test_data),
283 ST::check_validity);
284 EXPECT_EQ(text_size(utf16_test_data), from_utf32.size());
285 EXPECT_EQ(0, T_strcmp(utf16_test_data, from_utf32.data()));
286
287 // From Wide to UTF-16
288 ST::utf16_buffer from_wide = ST::wchar_to_utf16(wide_test_data, text_size(wide_test_data),
289 ST::check_validity);
290 EXPECT_EQ(text_size(utf16_test_data), from_wide.size());
291 EXPECT_EQ(0, T_strcmp(utf16_test_data, from_wide.data()));
292
293 // From Latin-1 to UTF-16
294 ST::utf16_buffer from_latin_1 = ST::latin_1_to_utf16(latin1_data, text_size(latin1_data));
295 EXPECT_EQ(text_size(latin1_utf16), from_latin_1.size());
296 EXPECT_EQ(0, T_strcmp(latin1_utf16, from_latin_1.data()));
297 }
298
TEST(string,string_utf16)299 TEST(string, string_utf16)
300 {
301 // From UTF-16 to ST::string
302 ST::string from_utf16 = ST::string::from_utf16(utf16_test_data);
303 EXPECT_EQ(text_size(utf8_test_data), from_utf16.size());
304 ST::utf32_buffer unicode = from_utf16.to_utf32();
305 EXPECT_EQ(0, T_strcmp(test_data, unicode.data()));
306
307 // From ST::string to UTF-16
308 ST::utf16_buffer to_utf16 = ST::string::from_utf32(test_data).to_utf16();
309 EXPECT_EQ(0, T_strcmp(utf16_test_data, to_utf16.data()));
310
311 // Empty string
312 const char16_t empty_data[] = { 0 };
313 ST::string empty = ST::string::from_utf16(empty_data);
314 EXPECT_EQ(0U, empty.size());
315 EXPECT_EQ(0, T_strcmp(empty.c_str(), ""));
316 }
317
TEST(string,conv_utf32)318 TEST(string, conv_utf32)
319 {
320 // From UTF-8 to UTF-32
321 ST::utf32_buffer from_utf8 = ST::utf8_to_utf32(utf8_test_data, text_size(utf8_test_data),
322 ST::check_validity);
323 EXPECT_EQ(text_size(test_data), from_utf8.size());
324 EXPECT_EQ(0, T_strcmp(test_data, from_utf8.data()));
325
326 // From UTF-16 to UTF-32
327 ST::utf32_buffer from_utf32 = ST::utf16_to_utf32(utf16_test_data, text_size(utf16_test_data),
328 ST::check_validity);
329 EXPECT_EQ(text_size(test_data), from_utf32.size());
330 EXPECT_EQ(0, T_strcmp(test_data, from_utf32.data()));
331
332 // From Wide to UTF-32
333 ST::utf32_buffer from_wide = ST::wchar_to_utf32(wide_test_data, text_size(wide_test_data),
334 ST::check_validity);
335 EXPECT_EQ(text_size(test_data), from_wide.size());
336 EXPECT_EQ(0, T_strcmp(test_data, from_wide.data()));
337
338 // From Latin-1 to UTF-32
339 ST::utf32_buffer from_latin_1 = ST::latin_1_to_utf32(latin1_data, text_size(latin1_data));
340 EXPECT_EQ(text_size(latin1_utf32), from_latin_1.size());
341 EXPECT_EQ(0, T_strcmp(latin1_utf32, from_latin_1.data()));
342 }
343
TEST(string,conv_latin_1)344 TEST(string, conv_latin_1)
345 {
346 // From UTF-8 to Latin-1
347 ST::char_buffer from_utf8 = ST::utf8_to_latin_1(latin1_utf8, text_size(latin1_utf8),
348 ST::check_validity);
349 EXPECT_EQ(text_size(latin1_data), from_utf8.size());
350 EXPECT_EQ(0, T_strcmp(latin1_data, from_utf8.data()));
351
352 // From UTF-16 to Latin-1
353 ST::char_buffer from_utf16 = ST::utf16_to_latin_1(latin1_utf16, text_size(latin1_utf16),
354 ST::check_validity);
355 EXPECT_EQ(text_size(latin1_data), from_utf16.size());
356 EXPECT_EQ(0, T_strcmp(latin1_data, from_utf16.data()));
357
358 // From UTF-32 to Latin-1
359 ST::char_buffer from_latin_1 = ST::utf32_to_latin_1(latin1_utf32, text_size(latin1_utf32),
360 ST::check_validity);
361 EXPECT_EQ(text_size(latin1_data), from_latin_1.size());
362 EXPECT_EQ(0, T_strcmp(latin1_data, from_latin_1.data()));
363
364 // From Wide to Latin-1
365 ST::char_buffer from_wide = ST::wchar_to_latin_1(latin1_wide, text_size(latin1_wide),
366 ST::check_validity);
367 EXPECT_EQ(text_size(latin1_data), from_wide.size());
368 EXPECT_EQ(0, T_strcmp(latin1_data, from_wide.data()));
369 }
370
TEST(string,string_latin_1)371 TEST(string, string_latin_1)
372 {
373 // From Latin-1 to ST::string
374 const char latin1[] = "\x20\x7e\xa0\xff";
375 const char32_t unicode_cp0[] = { 0x20, 0x7e, 0xa0, 0xff, 0 };
376 static const size_t latin1_utf8_length = 6;
377 ST::string from_latin1 = ST::string::from_latin_1(latin1);
378 EXPECT_EQ(latin1_utf8_length, from_latin1.size());
379 ST::utf32_buffer unicode = from_latin1.to_utf32();
380 EXPECT_EQ(0, T_strcmp(unicode_cp0, unicode.data()));
381
382 // From ST::string to Latin-1
383 ST::char_buffer to_latin1 = ST::string::from_utf32(unicode_cp0).to_latin_1();
384 EXPECT_STREQ(latin1, to_latin1.data());
385
386 // Empty string
387 ST::string empty = ST::string::from_latin_1("");
388 EXPECT_EQ(0U, empty.size());
389 EXPECT_EQ(0, T_strcmp(empty.c_str(), ""));
390 }
391
TEST(string,conv_wchar)392 TEST(string, conv_wchar)
393 {
394 // From UTF-8 to Wide
395 ST::wchar_buffer from_utf8 = ST::utf8_to_wchar(utf8_test_data, text_size(utf8_test_data),
396 ST::check_validity);
397 EXPECT_EQ(text_size(wide_test_data), from_utf8.size());
398 EXPECT_EQ(0, T_strcmp(wide_test_data, from_utf8.data()));
399
400 // From UTF-16 to Wide
401 ST::wchar_buffer from_utf16 = ST::utf16_to_wchar(utf16_test_data, text_size(utf16_test_data),
402 ST::check_validity);
403 EXPECT_EQ(text_size(wide_test_data), from_utf16.size());
404 EXPECT_EQ(0, T_strcmp(wide_test_data, from_utf16.data()));
405
406 // From UTF-32 to Wide
407 ST::wchar_buffer from_utf32 = ST::utf32_to_wchar(test_data, text_size(test_data),
408 ST::check_validity);
409 EXPECT_EQ(text_size(wide_test_data), from_utf32.size());
410 EXPECT_EQ(0, T_strcmp(wide_test_data, from_utf32.data()));
411
412 // From Latin-1 to Wide
413 ST::wchar_buffer from_latin_1 = ST::latin_1_to_wchar(latin1_data, text_size(latin1_data));
414 EXPECT_EQ(text_size(latin1_wide), from_latin_1.size());
415 EXPECT_EQ(0, T_strcmp(latin1_wide, from_latin_1.data()));
416 }
417
TEST(string,string_wchar)418 TEST(string, string_wchar)
419 {
420 // UTF-8 and UTF-16 are already tested, so just make sure we test
421 // wchar_t and L"" conversions
422
423 const wchar_t wtext[] = L"\x20\x7f\xff\u0100\ufffe";
424 const char32_t unicode_text[] = { 0x20, 0x7f, 0xff, 0x100, 0xfffe, 0 };
425 static const size_t wtext_utf8_length = 9;
426 ST::string from_wchar = ST::string::from_wchar(wtext);
427 EXPECT_EQ(wtext_utf8_length, from_wchar.size());
428 ST::utf32_buffer unicode = from_wchar.to_utf32();
429 EXPECT_EQ(0, T_strcmp(unicode_text, unicode.data()));
430
431 // From ST::string to wchar_t
432 ST::wchar_buffer to_wchar = ST::string::from_utf32(unicode_text).to_wchar();
433 EXPECT_STREQ(wtext, to_wchar.data());
434
435 // Empty string
436 ST::string empty = ST::string::from_wchar(L"");
437 EXPECT_EQ(0U, empty.size());
438 EXPECT_EQ(0, T_strcmp(empty.c_str(), ""));
439 }
440
TEST(string,validation)441 TEST(string, validation)
442 {
443 // Truncated sequences
444 EXPECT_THROW({ (void)ST::string::from_utf8("\xC0", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
445 EXPECT_THROW({ (void)ST::string::from_utf8("\xE0", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
446 EXPECT_THROW({ (void)ST::string::from_utf8("\xE0\x80", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
447 EXPECT_THROW({ (void)ST::string::from_utf8("\xF0", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
448 EXPECT_THROW({ (void)ST::string::from_utf8("\xF0\x80", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
449 EXPECT_THROW({ (void)ST::string::from_utf8("\xF0\x80\x80", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
450
451 const auto replacement = ST_LITERAL("\xef\xbf\xbdx");
452 const auto replacement2 = ST_LITERAL("\xef\xbf\xbd\xef\xbf\xbdx");
453 const auto replacement3 = ST_LITERAL("\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdx");
454 EXPECT_EQ(replacement, ST::string::from_utf8("\xC0x", ST_AUTO_SIZE, ST::substitute_invalid));
455 EXPECT_EQ(replacement, ST::string::from_utf8("\xE0x", ST_AUTO_SIZE, ST::substitute_invalid));
456 EXPECT_EQ(replacement2, ST::string::from_utf8("\xE0\x80x", ST_AUTO_SIZE, ST::substitute_invalid));
457 EXPECT_EQ(replacement, ST::string::from_utf8("\xF0x", ST_AUTO_SIZE, ST::substitute_invalid));
458 EXPECT_EQ(replacement2, ST::string::from_utf8("\xF0\x80x", ST_AUTO_SIZE, ST::substitute_invalid));
459 EXPECT_EQ(replacement3, ST::string::from_utf8("\xF0\x80\x80x", ST_AUTO_SIZE, ST::substitute_invalid));
460
461 // Invalid sequences
462 EXPECT_THROW({ (void)ST::string::from_utf8("\x80", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
463 EXPECT_THROW({ (void)ST::string::from_utf8("\xC0x", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
464 EXPECT_THROW({ (void)ST::string::from_utf8("\xE0xx", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
465 EXPECT_THROW({ (void)ST::string::from_utf8("\xF0xxx", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
466 EXPECT_THROW({ (void)ST::string::from_utf8("\xF8\x80\x80\x80\x80", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
467 EXPECT_THROW({ (void)ST::string::from_utf8("\xFC\x80\x80\x80\x80\x80", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
468 EXPECT_THROW({ (void)ST::string::from_utf8("\xFE\x80\x80\x80\x80\x80\x80", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
469 EXPECT_THROW({ (void)ST::string::from_utf8("\xFF\x80\x80\x80\x80\x80\x80\x80", ST_AUTO_SIZE, ST::check_validity); }, ST::unicode_error);
470
471 const auto replacement2x = ST_LITERAL("\xef\xbf\xbdxx");
472 const auto replacement3x = ST_LITERAL("\xef\xbf\xbdxxx");
473 const auto replacement4 = ST_LITERAL("\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdx");
474 const auto replacement4x = ST_LITERAL("\xef\xbf\xbdxxxx");
475 EXPECT_EQ(replacement, ST::string::from_utf8("\x80x", ST_AUTO_SIZE, ST::substitute_invalid));
476 EXPECT_EQ(replacement, ST::string::from_utf8("\xC0x", ST_AUTO_SIZE, ST::substitute_invalid));
477 EXPECT_EQ(replacement2x, ST::string::from_utf8("\xE0xx", ST_AUTO_SIZE, ST::substitute_invalid));
478 EXPECT_EQ(replacement3x, ST::string::from_utf8("\xF0xxx", ST_AUTO_SIZE, ST::substitute_invalid));
479 EXPECT_EQ(replacement4, ST::string::from_utf8("\xF8\x80\x80\x80\x80x", ST_AUTO_SIZE, ST::substitute_invalid));
480 EXPECT_EQ(replacement4x, ST::string::from_utf8("\xF8xxxx", ST_AUTO_SIZE, ST::substitute_invalid));
481
482 // Pass through bad data from ST_LITERAL and ST::assume_valid
483 const char junk[] = "\xFCxx\x80xx";
484 EXPECT_EQ(0, T_strcmp(junk, ST_LITERAL("\xFCxx\x80xx").c_str()));
485 EXPECT_EQ(0, T_strcmp(junk, ST::string::from_utf8(junk, ST_AUTO_SIZE, ST::assume_valid).c_str()));
486 }
487
TEST(string,conv_utf8_validation)488 TEST(string, conv_utf8_validation)
489 {
490 const char16_t truncL[] = { 0xd800, 'x', 0 };
491 const char16_t truncH[] = { 0xdc00, 'x', 0 };
492 EXPECT_THROW({
493 (void)ST::utf16_to_utf8(truncL, text_size(truncL),
494 ST::check_validity);
495 }, ST::unicode_error);
496 EXPECT_THROW({
497 (void)ST::utf16_to_utf8(truncH, text_size(truncH),
498 ST::check_validity);
499 }, ST::unicode_error);
500 EXPECT_EQ(0, T_strcmp("\xef\xbf\xbdx", ST::utf16_to_utf8(truncL, text_size(truncL),
501 ST::substitute_invalid).c_str()));
502 EXPECT_EQ(0, T_strcmp("\xef\xbf\xbdx", ST::utf16_to_utf8(truncH, text_size(truncH),
503 ST::substitute_invalid).c_str()));
504
505 const char16_t incompleteL[] = { 0xd800, 'x', 'x', 0 };
506 const char16_t incompleteH[] = { 0xdc00, 'x', 'x', 0 };
507 EXPECT_THROW({
508 (void)ST::utf16_to_utf8(incompleteL, text_size(incompleteL),
509 ST::check_validity);
510 }, ST::unicode_error);
511 EXPECT_THROW({
512 (void)ST::utf16_to_utf8(incompleteH, text_size(incompleteH),
513 ST::check_validity);
514 }, ST::unicode_error);
515 EXPECT_EQ(0, T_strcmp("\xef\xbf\xbdxx", ST::utf16_to_utf8(incompleteL, text_size(incompleteL),
516 ST::substitute_invalid).c_str()));
517 EXPECT_EQ(0, T_strcmp("\xef\xbf\xbdxx", ST::utf16_to_utf8(incompleteH, text_size(incompleteH),
518 ST::substitute_invalid).c_str()));
519
520 const char16_t doubleL[] = { 0xd800, 0xdbff, 'x', 0 };
521 const char16_t doubleH[] = { 0xdc00, 0xdfff, 'x', 0 };
522 EXPECT_THROW({
523 (void)ST::utf16_to_utf8(doubleL, text_size(doubleL),
524 ST::check_validity);
525 }, ST::unicode_error);
526 EXPECT_THROW({
527 (void)ST::utf16_to_utf8(doubleH, text_size(doubleH),
528 ST::check_validity);
529 }, ST::unicode_error);
530 EXPECT_EQ(0, T_strcmp("\xef\xbf\xbd\xef\xbf\xbdx",
531 ST::utf16_to_utf8(doubleL, text_size(doubleL),
532 ST::substitute_invalid).c_str()));
533 EXPECT_EQ(0, T_strcmp("\xef\xbf\xbd\xef\xbf\xbdx",
534 ST::utf16_to_utf8(doubleH, text_size(doubleH),
535 ST::substitute_invalid).c_str()));
536
537 // Out of range UTF-32
538 const char32_t range32[] = { 0x110000, 'x', 0 };
539 EXPECT_THROW({
540 (void)ST::utf32_to_utf8(range32, text_size(range32),
541 ST::check_validity);
542 }, ST::unicode_error);
543 EXPECT_EQ(0, T_strcmp("\xef\xbf\xbdx", ST::utf32_to_utf8(range32, text_size(range32),
544 ST::substitute_invalid).c_str()));
545 }
546
TEST(string,conv_utf16_validation)547 TEST(string, conv_utf16_validation)
548 {
549 // Truncated UTF-8 sequences
550 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xC0", 1, ST::check_validity); }, ST::unicode_error);
551 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xE0", 1, ST::check_validity); }, ST::unicode_error);
552 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xE0\x80", 2, ST::check_validity); }, ST::unicode_error);
553 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xF0", 1, ST::check_validity); }, ST::unicode_error);
554 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xF0\x80", 2, ST::check_validity); }, ST::unicode_error);
555 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xF0\x80\x80", 3, ST::check_validity); }, ST::unicode_error);
556
557 EXPECT_EQ(0, T_strcmp(u"\ufffdx", ST::utf8_to_utf16("\xC0x", 2, ST::substitute_invalid).c_str()));
558 EXPECT_EQ(0, T_strcmp(u"\ufffdx", ST::utf8_to_utf16("\xE0x", 2, ST::substitute_invalid).c_str()));
559 EXPECT_EQ(0, T_strcmp(u"\ufffd\ufffdx", ST::utf8_to_utf16("\xE0\x80x", 3, ST::substitute_invalid).c_str()));
560 EXPECT_EQ(0, T_strcmp(u"\ufffdx", ST::utf8_to_utf16("\xF0x", 2, ST::substitute_invalid).c_str()));
561 EXPECT_EQ(0, T_strcmp(u"\ufffd\ufffdx", ST::utf8_to_utf16("\xF0\x80x", 3, ST::substitute_invalid).c_str()));
562 EXPECT_EQ(0, T_strcmp(u"\ufffd\ufffd\ufffdx", ST::utf8_to_utf16("\xF0\x80\x80x", 4, ST::substitute_invalid).c_str()));
563
564 // Invalid UTF-8 sequences
565 EXPECT_THROW({ (void)ST::utf8_to_utf16("\x80", 1, ST::check_validity); }, ST::unicode_error);
566 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xC0x", 2, ST::check_validity); }, ST::unicode_error);
567 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xE0xx", 3, ST::check_validity); }, ST::unicode_error);
568 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xF0xxx", 4, ST::check_validity); }, ST::unicode_error);
569 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xF8\x80\x80\x80\x80", 5, ST::check_validity); }, ST::unicode_error);
570 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xFC\x80\x80\x80\x80\x80", 6, ST::check_validity); }, ST::unicode_error);
571 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xFE\x80\x80\x80\x80\x80\x80", 7, ST::check_validity); }, ST::unicode_error);
572 EXPECT_THROW({ (void)ST::utf8_to_utf16("\xFF\x80\x80\x80\x80\x80\x80\x80", 8, ST::check_validity); }, ST::unicode_error);
573
574 EXPECT_EQ(0, T_strcmp(u"\ufffdx", ST::utf8_to_utf16("\x80x", 2, ST::substitute_invalid).c_str()));
575 EXPECT_EQ(0, T_strcmp(u"\ufffdx", ST::utf8_to_utf16("\xC0x", 2, ST::substitute_invalid).c_str()));
576 EXPECT_EQ(0, T_strcmp(u"\ufffdxx", ST::utf8_to_utf16("\xE0xx", 3, ST::substitute_invalid).c_str()));
577 EXPECT_EQ(0, T_strcmp(u"\ufffdxxx", ST::utf8_to_utf16("\xF0xxx", 4, ST::substitute_invalid).c_str()));
578 EXPECT_EQ(0, T_strcmp(u"\ufffd\ufffd\ufffd\ufffd\ufffdx",
579 ST::utf8_to_utf16("\xF8\x80\x80\x80\x80x", 6, ST::substitute_invalid).c_str()));
580 EXPECT_EQ(0, T_strcmp(u"\ufffdxxxx", ST::utf8_to_utf16("\xF8xxxx", 5, ST::substitute_invalid).c_str()));
581
582 // Out of range UTF-32
583 const char32_t range32[] = { 0x110000, 'x', 0 };
584 EXPECT_THROW({
585 (void)ST::utf32_to_utf16(range32, text_size(range32),
586 ST::check_validity);
587 }, ST::unicode_error);
588 EXPECT_EQ(0, T_strcmp(u"\ufffdx", ST::utf32_to_utf16(range32, text_size(range32),
589 ST::substitute_invalid).c_str()));
590 }
591
TEST(string,conv_utf32_validation)592 TEST(string, conv_utf32_validation)
593 {
594 // Truncated UTF-8 sequences
595 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xC0", 1, ST::check_validity); }, ST::unicode_error);
596 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xE0", 1, ST::check_validity); }, ST::unicode_error);
597 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xE0\x80", 2, ST::check_validity); }, ST::unicode_error);
598 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xF0", 1, ST::check_validity); }, ST::unicode_error);
599 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xF0\x80", 2, ST::check_validity); }, ST::unicode_error);
600 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xF0\x80\x80", 3, ST::check_validity); }, ST::unicode_error);
601
602 EXPECT_EQ(0, T_strcmp(U"\ufffdx", ST::utf8_to_utf32("\xC0x", 2, ST::substitute_invalid).c_str()));
603 EXPECT_EQ(0, T_strcmp(U"\ufffdx", ST::utf8_to_utf32("\xE0x", 2, ST::substitute_invalid).c_str()));
604 EXPECT_EQ(0, T_strcmp(U"\ufffd\ufffdx", ST::utf8_to_utf32("\xE0\x80x", 3, ST::substitute_invalid).c_str()));
605 EXPECT_EQ(0, T_strcmp(U"\ufffdx", ST::utf8_to_utf32("\xF0x", 2, ST::substitute_invalid).c_str()));
606 EXPECT_EQ(0, T_strcmp(U"\ufffd\ufffdx", ST::utf8_to_utf32("\xF0\x80x", 3, ST::substitute_invalid).c_str()));
607 EXPECT_EQ(0, T_strcmp(U"\ufffd\ufffd\ufffdx", ST::utf8_to_utf32("\xF0\x80\x80x", 4, ST::substitute_invalid).c_str()));
608
609 // Invalid UTF-8 sequences
610 EXPECT_THROW({ (void)ST::utf8_to_utf32("\x80", 1, ST::check_validity); }, ST::unicode_error);
611 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xC0x", 2, ST::check_validity); }, ST::unicode_error);
612 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xE0xx", 3, ST::check_validity); }, ST::unicode_error);
613 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xF0xxx", 4, ST::check_validity); }, ST::unicode_error);
614 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xF8\x80\x80\x80\x80", 5, ST::check_validity); }, ST::unicode_error);
615 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xFC\x80\x80\x80\x80\x80", 6, ST::check_validity); }, ST::unicode_error);
616 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xFE\x80\x80\x80\x80\x80\x80", 7, ST::check_validity); }, ST::unicode_error);
617 EXPECT_THROW({ (void)ST::utf8_to_utf32("\xFF\x80\x80\x80\x80\x80\x80\x80", 8, ST::check_validity); }, ST::unicode_error);
618
619 EXPECT_EQ(0, T_strcmp(U"\ufffdx", ST::utf8_to_utf32("\x80x", 2, ST::substitute_invalid).c_str()));
620 EXPECT_EQ(0, T_strcmp(U"\ufffdx", ST::utf8_to_utf32("\xC0x", 2, ST::substitute_invalid).c_str()));
621 EXPECT_EQ(0, T_strcmp(U"\ufffdxx", ST::utf8_to_utf32("\xE0xx", 3, ST::substitute_invalid).c_str()));
622 EXPECT_EQ(0, T_strcmp(U"\ufffdxxx", ST::utf8_to_utf32("\xF0xxx", 4, ST::substitute_invalid).c_str()));
623 EXPECT_EQ(0, T_strcmp(U"\ufffd\ufffd\ufffd\ufffd\ufffdx",
624 ST::utf8_to_utf32("\xF8\x80\x80\x80\x80x", 6, ST::substitute_invalid).c_str()));
625 EXPECT_EQ(0, T_strcmp(U"\ufffdxxxx", ST::utf8_to_utf32("\xF8xxxx", 5, ST::substitute_invalid).c_str()));
626
627 // Bad UTF-16 sequences
628 const char16_t truncL[] = { 0xd800, 'x', 0 };
629 const char16_t truncH[] = { 0xdc00, 'x', 0 };
630 EXPECT_THROW({
631 (void)ST::utf16_to_utf32(truncL, text_size(truncL),
632 ST::check_validity);
633 }, ST::unicode_error);
634 EXPECT_THROW({
635 (void)ST::utf16_to_utf32(truncH, text_size(truncH),
636 ST::check_validity);
637 }, ST::unicode_error);
638 EXPECT_EQ(0, T_strcmp(U"\ufffdx", ST::utf16_to_utf32(truncL, text_size(truncL),
639 ST::substitute_invalid).c_str()));
640 EXPECT_EQ(0, T_strcmp(U"\ufffdx", ST::utf16_to_utf32(truncH, text_size(truncH),
641 ST::substitute_invalid).c_str()));
642
643 const char16_t incompleteL[] = { 0xd800, 'x', 'x', 0 };
644 const char16_t incompleteH[] = { 0xdc00, 'x', 'x', 0 };
645 EXPECT_THROW({
646 (void)ST::utf16_to_utf32(incompleteL, text_size(incompleteL),
647 ST::check_validity);
648 }, ST::unicode_error);
649 EXPECT_THROW({
650 (void)ST::utf16_to_utf32(incompleteH, text_size(incompleteH),
651 ST::check_validity);
652 }, ST::unicode_error);
653 EXPECT_EQ(0, T_strcmp(U"\ufffdxx", ST::utf16_to_utf32(incompleteL, text_size(incompleteL),
654 ST::substitute_invalid).c_str()));
655 EXPECT_EQ(0, T_strcmp(U"\ufffdxx", ST::utf16_to_utf32(incompleteH, text_size(incompleteH),
656 ST::substitute_invalid).c_str()));
657
658 const char16_t doubleL[] = { 0xd800, 0xdbff, 'x', 0 };
659 const char16_t doubleH[] = { 0xdc00, 0xdfff, 'x', 0 };
660 EXPECT_THROW({
661 (void)ST::utf16_to_utf32(doubleL, text_size(doubleL),
662 ST::check_validity);
663 }, ST::unicode_error);
664 EXPECT_THROW({
665 (void)ST::utf16_to_utf32(doubleH, text_size(doubleH),
666 ST::check_validity);
667 }, ST::unicode_error);
668 EXPECT_EQ(0, T_strcmp(U"\ufffd\ufffdx", ST::utf16_to_utf32(doubleL, text_size(doubleL),
669 ST::substitute_invalid).c_str()));
670 EXPECT_EQ(0, T_strcmp(U"\ufffd\ufffdx", ST::utf16_to_utf32(doubleH, text_size(doubleH),
671 ST::substitute_invalid).c_str()));
672 }
673
TEST(string,conv_latin_1_validation)674 TEST(string, conv_latin_1_validation)
675 {
676 // Truncated UTF-8 sequences
677 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xC0", 1, ST::check_validity); }, ST::unicode_error);
678 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xE0", 1, ST::check_validity); }, ST::unicode_error);
679 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xE0\x80", 2, ST::check_validity); }, ST::unicode_error);
680 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xF0", 1, ST::check_validity); }, ST::unicode_error);
681 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xF0\x80", 2, ST::check_validity); }, ST::unicode_error);
682 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xF0\x80\x80", 3, ST::check_validity); }, ST::unicode_error);
683
684 EXPECT_EQ(0, T_strcmp("?x", ST::utf8_to_latin_1("\xC0x", 2, ST::substitute_invalid).c_str()));
685 EXPECT_EQ(0, T_strcmp("?x", ST::utf8_to_latin_1("\xE0x", 2, ST::substitute_invalid).c_str()));
686 EXPECT_EQ(0, T_strcmp("??x", ST::utf8_to_latin_1("\xE0\x80x", 3, ST::substitute_invalid).c_str()));
687 EXPECT_EQ(0, T_strcmp("?x", ST::utf8_to_latin_1("\xF0x", 2, ST::substitute_invalid).c_str()));
688 EXPECT_EQ(0, T_strcmp("??x", ST::utf8_to_latin_1("\xF0\x80x", 3, ST::substitute_invalid).c_str()));
689 EXPECT_EQ(0, T_strcmp("???x", ST::utf8_to_latin_1("\xF0\x80\x80x", 4, ST::substitute_invalid).c_str()));
690
691 // Invalid UTF-8 sequences
692 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\x80", 1, ST::check_validity); }, ST::unicode_error);
693 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xC0x", 2, ST::check_validity); }, ST::unicode_error);
694 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xE0xx", 3, ST::check_validity); }, ST::unicode_error);
695 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xF0xxx", 4, ST::check_validity); }, ST::unicode_error);
696 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xF8\x80\x80\x80\x80", 5, ST::check_validity); }, ST::unicode_error);
697 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xFC\x80\x80\x80\x80\x80", 6, ST::check_validity); }, ST::unicode_error);
698 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xFE\x80\x80\x80\x80\x80\x80", 7, ST::check_validity); }, ST::unicode_error);
699 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xFF\x80\x80\x80\x80\x80\x80\x80", 8, ST::check_validity); }, ST::unicode_error);
700
701 EXPECT_EQ(0, T_strcmp("?x", ST::utf8_to_latin_1("\x80x", 2, ST::substitute_invalid).c_str()));
702 EXPECT_EQ(0, T_strcmp("?x", ST::utf8_to_latin_1("\xC0x", 2, ST::substitute_invalid).c_str()));
703 EXPECT_EQ(0, T_strcmp("?xx", ST::utf8_to_latin_1("\xE0xx", 3, ST::substitute_invalid).c_str()));
704 EXPECT_EQ(0, T_strcmp("?xxx", ST::utf8_to_latin_1("\xF0xxx", 4, ST::substitute_invalid).c_str()));
705 EXPECT_EQ(0, T_strcmp("?????x", ST::utf8_to_latin_1("\xF8\x80\x80\x80\x80x", 6, ST::substitute_invalid).c_str()));
706 EXPECT_EQ(0, T_strcmp("?xxxx", ST::utf8_to_latin_1("\xF8xxxx", 5, ST::substitute_invalid).c_str()));
707
708 // Out of Latin-1 range
709 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xc4\x80", 2, ST::check_validity, false); }, ST::unicode_error);
710 EXPECT_THROW({ (void)ST::utf8_to_latin_1("\xf4\x8f\xbf\xbf", 4, ST::check_validity, false); }, ST::unicode_error);
711 EXPECT_EQ(0, T_strcmp("?x", ST::utf8_to_latin_1("\xc4\x80x", 3, ST::check_validity, true).c_str()));
712 EXPECT_EQ(0, T_strcmp("?x", ST::utf8_to_latin_1("\xf4\x8f\xbf\xbfx", 5, ST::check_validity, true).c_str()));
713
714 // Bad UTF-16 sequences
715 const char16_t truncL[] = { 0xd800, 'x', 0 };
716 const char16_t truncH[] = { 0xdc00, 'x', 0 };
717 EXPECT_THROW({
718 (void)ST::utf16_to_latin_1(truncL, text_size(truncL),
719 ST::check_validity);
720 }, ST::unicode_error);
721 EXPECT_THROW({
722 (void)ST::utf16_to_latin_1(truncH, text_size(truncH),
723 ST::check_validity);
724 }, ST::unicode_error);
725 EXPECT_EQ(0, T_strcmp("?x", ST::utf16_to_latin_1(truncL, text_size(truncL),
726 ST::substitute_invalid).c_str()));
727 EXPECT_EQ(0, T_strcmp("?x", ST::utf16_to_latin_1(truncH, text_size(truncH),
728 ST::substitute_invalid).c_str()));
729
730 const char16_t incompleteL[] = { 0xd800, 'x', 'x', 0 };
731 const char16_t incompleteH[] = { 0xdc00, 'x', 'x', 0 };
732 EXPECT_THROW({
733 (void)ST::utf16_to_latin_1(incompleteL, text_size(incompleteL),
734 ST::check_validity);
735 }, ST::unicode_error);
736 EXPECT_THROW({
737 (void)ST::utf16_to_latin_1(incompleteH, text_size(incompleteH),
738 ST::check_validity);
739 }, ST::unicode_error);
740 EXPECT_EQ(0, T_strcmp("?xx", ST::utf16_to_latin_1(incompleteL, text_size(incompleteL),
741 ST::substitute_invalid).c_str()));
742 EXPECT_EQ(0, T_strcmp("?xx", ST::utf16_to_latin_1(incompleteH, text_size(incompleteH),
743 ST::substitute_invalid).c_str()));
744
745 const char16_t doubleL[] = { 0xd800, 0xdbff, 'x', 0 };
746 const char16_t doubleH[] = { 0xdc00, 0xdfff, 'x', 0 };
747 EXPECT_THROW({
748 (void)ST::utf16_to_latin_1(doubleL, text_size(doubleL),
749 ST::check_validity);
750 }, ST::unicode_error);
751 EXPECT_THROW({
752 (void)ST::utf16_to_latin_1(doubleH, text_size(doubleH),
753 ST::check_validity);
754 }, ST::unicode_error);
755 EXPECT_EQ(0, T_strcmp("??x", ST::utf16_to_latin_1(doubleL, text_size(doubleL),
756 ST::substitute_invalid).c_str()));
757 EXPECT_EQ(0, T_strcmp("??x", ST::utf16_to_latin_1(doubleH, text_size(doubleH),
758 ST::substitute_invalid).c_str()));
759
760 // Out of Latin-1 range
761 EXPECT_THROW({ (void)ST::utf16_to_latin_1(u"\u0100", 1, ST::check_validity, false); }, ST::unicode_error);
762 EXPECT_THROW({ (void)ST::utf16_to_latin_1(u"\U0010ffff", 2, ST::check_validity, false); }, ST::unicode_error);
763 EXPECT_EQ(0, T_strcmp("?x", ST::utf16_to_latin_1(u"\u0100x", 2, ST::check_validity, true).c_str()));
764 EXPECT_EQ(0, T_strcmp("?x", ST::utf16_to_latin_1(u"\U0010ffffx", 3, ST::check_validity, true).c_str()));
765
766 // Out of range UTF-32
767 const char32_t range32[] = { 0x110000, 'x', 0 };
768 EXPECT_THROW({
769 (void)ST::utf32_to_latin_1(range32, text_size(range32),
770 ST::check_validity);
771 }, ST::unicode_error);
772 EXPECT_EQ(0, T_strcmp("?x", ST::utf32_to_latin_1(range32, text_size(range32),
773 ST::substitute_invalid).c_str()));
774
775 // Out of Latin-1 range
776 EXPECT_THROW({ (void)ST::utf32_to_latin_1(U"\u0100", 1, ST::check_validity, false); }, ST::unicode_error);
777 EXPECT_THROW({ (void)ST::utf32_to_latin_1(U"\U0010ffff", 1, ST::check_validity, false); }, ST::unicode_error);
778 EXPECT_EQ(0, T_strcmp("?x", ST::utf32_to_latin_1(U"\u0100x", 2, ST::check_validity, true).c_str()));
779 EXPECT_EQ(0, T_strcmp("?x", ST::utf32_to_latin_1(U"\U0010ffffx", 2, ST::check_validity, true).c_str()));
780 }
781
TEST(string,conversion_errors)782 TEST(string, conversion_errors)
783 {
784 // The following should encode replacement characters for invalid chars
785 const char32_t unicode_replacement[] = { 0xfffd, 0 };
786 const char latin1_replacement[] = "?";
787
788 // Character outside of Unicode specification range
789 const char32_t too_big_c[] = { 0xffffff, 0 };
790 EXPECT_THROW({ (void)ST::string::from_utf32(too_big_c, ST_AUTO_SIZE, ST::check_validity); },
791 ST::unicode_error);
792 ST::utf32_buffer too_big = ST::string::from_utf32(too_big_c, ST_AUTO_SIZE,
793 ST::substitute_invalid).to_utf32();
794 EXPECT_EQ(0, T_strcmp(unicode_replacement, too_big.data()));
795
796 // Invalid surrogate pairs
797 const char16_t incomplete_surr_c[] = { 0xd800, 0 };
798 EXPECT_THROW({ (void)ST::string::from_utf16(incomplete_surr_c, ST_AUTO_SIZE, ST::check_validity); },
799 ST::unicode_error);
800 ST::string incomplete_surr = ST::string::from_utf16(incomplete_surr_c, ST_AUTO_SIZE,
801 ST::substitute_invalid);
802 EXPECT_EQ(0, T_strcmp(unicode_replacement, incomplete_surr.to_utf32().data()));
803
804 const char32_t unicode_replacement2[] = { 0xfffd, 0xfffd, 0 };
805 const char16_t double_low_c[] = { 0xd800, 0xd801, 0 };
806 EXPECT_THROW({ (void)ST::string::from_utf16(double_low_c, ST_AUTO_SIZE, ST::check_validity); },
807 ST::unicode_error);
808 ST::string double_low = ST::string::from_utf16(double_low_c, ST_AUTO_SIZE,
809 ST::substitute_invalid);
810 EXPECT_EQ(0, T_strcmp(unicode_replacement2, double_low.to_utf32().data()));
811
812 const char16_t double_high_c[] = { 0xdc00, 0xdc01, 0 };
813 EXPECT_THROW({ (void)ST::string::from_utf16(double_high_c, ST_AUTO_SIZE, ST::check_validity); },
814 ST::unicode_error);
815 ST::string double_high = ST::string::from_utf16(double_high_c, ST_AUTO_SIZE,
816 ST::substitute_invalid);
817 EXPECT_EQ(0, T_strcmp(unicode_replacement2, double_high.to_utf32().data()));
818
819 const char32_t unicode_replacement3[] = { 0xfffd, 0x20, 0 };
820 const char16_t bad_combo_c[] = { 0xdc00, 0x20, 0 };
821 EXPECT_THROW({ (void)ST::string::from_utf16(bad_combo_c, ST_AUTO_SIZE, ST::check_validity); },
822 ST::unicode_error);
823 ST::string bad_combo = ST::string::from_utf16(bad_combo_c, ST_AUTO_SIZE,
824 ST::substitute_invalid);
825 EXPECT_EQ(0, T_strcmp(unicode_replacement3, bad_combo.to_utf32().data()));
826
827 // Latin-1 doesn't have \ufffd, so it uses '?' instead
828 const char32_t non_latin1_c[] = { 0x1ff, 0 };
829 EXPECT_THROW({ (void)ST::string::from_utf32(non_latin1_c).to_latin_1(false); }, ST::unicode_error);
830 ST::char_buffer non_latin1 = ST::string::from_utf32(non_latin1_c).to_latin_1();
831 EXPECT_STREQ(latin1_replacement, non_latin1.data());
832 }
833
TEST(string,concatenation)834 TEST(string, concatenation)
835 {
836 // If this changes, this test may need to be updated to match
837 ASSERT_EQ(16, ST_MAX_SSO_LENGTH);
838
839 ST::string expected_short = "xxxxyyy";
840 ST::string input1 = "xxxx";
841 ST::string input2 = "yyy";
842
843 ST::string expected_med = "xxxxxxxxxxyyyyyyyyy";
844 ST::string input3 = "xxxxxxxxxx";
845 ST::string input4 = "yyyyyyyyy";
846
847 ST::string expected_long = "xxxxxxxxxxxxxxxyyyyyyyyyyyyyyyy";
848 ST::string expected_long2 = "yyyyyyyyyyyyyyyyxxxxxxxxxxxxxxx";
849 ST::string input5 = "xxxxxxxxxxxxxxx";
850 ST::string input6 = "yyyyyyyyyyyyyyyy";
851
852 // ST::string + ST::string
853 EXPECT_EQ(expected_short, input1 + input2);
854 EXPECT_EQ(expected_med, input3 + input4);
855 EXPECT_EQ(expected_long, input5 + input6);
856 EXPECT_EQ(expected_long2, input6 + input5);
857 EXPECT_EQ(input1, input1 + ST::string());
858 EXPECT_EQ(input1, ST::string() + input1);
859 EXPECT_EQ(input6, input6 + ST::string());
860 EXPECT_EQ(input6, ST::string() + input6);
861
862 // ST::string + const char*
863 EXPECT_EQ(expected_short, input1 + input2.c_str());
864 EXPECT_EQ(expected_short, input1.c_str() + input2);
865 EXPECT_EQ(expected_med, input3 + input4.c_str());
866 EXPECT_EQ(expected_med, input3.c_str() + input4);
867 EXPECT_EQ(expected_long, input5 + input6.c_str());
868 EXPECT_EQ(expected_long, input5.c_str() + input6);
869 EXPECT_EQ(expected_long2, input6 + input5.c_str());
870 EXPECT_EQ(expected_long2, input6.c_str() + input5);
871 EXPECT_EQ(input1, input1 + "");
872 EXPECT_EQ(input1, "" + input1);
873 EXPECT_EQ(input6, input6 + "");
874 EXPECT_EQ(input6, "" + input6);
875 }
876
TEST(string,char_concatenation)877 TEST(string, char_concatenation)
878 {
879 // If this changes, this test may need to be updated to match
880 ASSERT_EQ(16, ST_MAX_SSO_LENGTH);
881
882 ST::string input1 = "xxxx";
883 ST::string input2 = "xxxxxxxxxxxxxxx";
884 ST::string input3 = "xxxxxxxxxxxxxxxx";
885
886 // ST::string + char
887 EXPECT_EQ(ST_LITERAL("xxxxy"), input1 + 'y');
888 EXPECT_EQ(ST_LITERAL("xxxxxxxxxxxxxxxy"), input2 + 'y');
889 EXPECT_EQ(ST_LITERAL("xxxxxxxxxxxxxxxxy"), input3 + 'y');
890 EXPECT_EQ(ST_LITERAL("yxxxx"), 'y' + input1);
891 EXPECT_EQ(ST_LITERAL("yxxxxxxxxxxxxxxx"), 'y' + input2);
892 EXPECT_EQ(ST_LITERAL("yxxxxxxxxxxxxxxxx"), 'y' + input3);
893
894 EXPECT_EQ(ST::string(L"xxxx\u00ff"), input1 + char(0xff));
895 EXPECT_EQ(ST::string(L"xxxxxxxxxxxxxxx\u00ff"), input2 + char(0xff));
896 EXPECT_EQ(ST::string(L"xxxxxxxxxxxxxxxx\u00ff"), input3 + char(0xff));
897 EXPECT_EQ(ST::string(L"\u00ffxxxx"), char(0xff) + input1);
898 EXPECT_EQ(ST::string(L"\u00ffxxxxxxxxxxxxxxx"), char(0xff) + input2);
899 EXPECT_EQ(ST::string(L"\u00ffxxxxxxxxxxxxxxxx"), char(0xff) + input3);
900
901 // ST::string + char16_t
902 EXPECT_EQ(ST::string(L"xxxx\u00ff"), input1 + char16_t(0xff));
903 EXPECT_EQ(ST::string(L"xxxxxxxxxxxxxxx\u00ff"), input2 + char16_t(0xff));
904 EXPECT_EQ(ST::string(L"xxxxxxxxxxxxxxxx\u00ff"), input3 + char16_t(0xff));
905 EXPECT_EQ(ST::string(L"\u00ffxxxx"), char16_t(0xff) + input1);
906 EXPECT_EQ(ST::string(L"\u00ffxxxxxxxxxxxxxxx"), char16_t(0xff) + input2);
907 EXPECT_EQ(ST::string(L"\u00ffxxxxxxxxxxxxxxxx"), char16_t(0xff) + input3);
908
909 EXPECT_EQ(ST::string(L"xxxx\u0100"), input1 + char16_t(0x100));
910 EXPECT_EQ(ST::string(L"xxxxxxxxxxxxxxx\u0100"), input2 + char16_t(0x100));
911 EXPECT_EQ(ST::string(L"xxxxxxxxxxxxxxxx\u0100"), input3 + char16_t(0x100));
912 EXPECT_EQ(ST::string(L"\u0100xxxx"), char16_t(0x100) + input1);
913 EXPECT_EQ(ST::string(L"\u0100xxxxxxxxxxxxxxx"), char16_t(0x100) + input2);
914 EXPECT_EQ(ST::string(L"\u0100xxxxxxxxxxxxxxxx"), char16_t(0x100) + input3);
915
916 const char32_t expect_wide1[] = { 0x78, 0x78, 0x78, 0x78, 0x10FFFF, 0 };
917 const char32_t expect_wide2[] = {
918 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
919 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
920 0x10FFFF, 0
921 };
922 const char32_t expect_wide3[] = {
923 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
924 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
925 0x10FFFF, 0
926 };
927
928 const char32_t expect_wide4[] = { 0x10FFFF, 0x78, 0x78, 0x78, 0x78, 0 };
929 const char32_t expect_wide5[] = {
930 0x10FFFF,
931 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
932 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0
933 };
934 const char32_t expect_wide6[] = {
935 0x10FFFF,
936 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
937 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0
938 };
939
940 // ST::string + char32_t
941 EXPECT_EQ(ST::string::from_utf32(expect_wide1), input1 + char32_t(0x10ffff));
942 EXPECT_EQ(ST::string::from_utf32(expect_wide2), input2 + char32_t(0x10ffff));
943 EXPECT_EQ(ST::string::from_utf32(expect_wide3), input3 + char32_t(0x10ffff));
944 EXPECT_EQ(ST::string::from_utf32(expect_wide4), char32_t(0x10ffff) + input1);
945 EXPECT_EQ(ST::string::from_utf32(expect_wide5), char32_t(0x10ffff) + input2);
946 EXPECT_EQ(ST::string::from_utf32(expect_wide6), char32_t(0x10ffff) + input3);
947
948 // UTF-16 and UTF-32 are already tested, so just check the conversion from wchar_t
949 EXPECT_EQ(ST::string(L"xxxx\u0100"), input1 + wchar_t(0x100));
950 EXPECT_EQ(ST::string(L"xxxxxxxxxxxxxxx\u0100"), input2 + wchar_t(0x100));
951 EXPECT_EQ(ST::string(L"xxxxxxxxxxxxxxxx\u0100"), input3 + wchar_t(0x100));
952 EXPECT_EQ(ST::string(L"\u0100xxxx"), wchar_t(0x100) + input1);
953 EXPECT_EQ(ST::string(L"\u0100xxxxxxxxxxxxxxx"), wchar_t(0x100) + input2);
954 EXPECT_EQ(ST::string(L"\u0100xxxxxxxxxxxxxxxx"), wchar_t(0x100) + input3);
955 }
956
TEST(string,from_int)957 TEST(string, from_int)
958 {
959 EXPECT_EQ(ST_LITERAL("0"), ST::string::from_int(0));
960 EXPECT_EQ(ST_LITERAL("-80000"), ST::string::from_int(-80000));
961 EXPECT_EQ(ST_LITERAL("80000"), ST::string::from_int(80000));
962 EXPECT_EQ(ST_LITERAL("-13880"), ST::string::from_int(-80000, 16));
963 EXPECT_EQ(ST_LITERAL("13880"), ST::string::from_int(80000, 16));
964 EXPECT_EQ(ST_LITERAL("-234200"), ST::string::from_int(-80000, 8));
965 EXPECT_EQ(ST_LITERAL("234200"), ST::string::from_int(80000, 8));
966
967 EXPECT_EQ(ST_LITERAL("0"), ST::string::from_int(0LL));
968 EXPECT_EQ(ST_LITERAL("-1000000000000"), ST::string::from_int(-1000000000000LL));
969 EXPECT_EQ(ST_LITERAL("1000000000000"), ST::string::from_int(1000000000000LL));
970 EXPECT_EQ(ST_LITERAL("-e8d4a51000"), ST::string::from_int(-1000000000000LL, 16));
971 EXPECT_EQ(ST_LITERAL("e8d4a51000"), ST::string::from_int(1000000000000LL, 16));
972 EXPECT_EQ(ST_LITERAL("-16432451210000"), ST::string::from_int(-1000000000000LL, 8));
973 EXPECT_EQ(ST_LITERAL("16432451210000"), ST::string::from_int(1000000000000LL, 8));
974
975 static const int int32_min = std::numeric_limits<int>::min();
976 static const int int32_max = std::numeric_limits<int>::max();
977 EXPECT_EQ(ST_LITERAL("-2147483648"), ST::string::from_int(int32_min));
978 EXPECT_EQ(ST_LITERAL("2147483647"), ST::string::from_int(int32_max));
979 EXPECT_EQ(ST_LITERAL("-80000000"), ST::string::from_int(int32_min, 16));
980 EXPECT_EQ(ST_LITERAL("7fffffff"), ST::string::from_int(int32_max, 16, false));
981 EXPECT_EQ(ST_LITERAL("7FFFFFFF"), ST::string::from_int(int32_max, 16, true));
982 EXPECT_EQ(ST_LITERAL("-20000000000"), ST::string::from_int(int32_min, 8));
983 EXPECT_EQ(ST_LITERAL("17777777777"), ST::string::from_int(int32_max, 8));
984 EXPECT_EQ(ST_LITERAL("-10000000000000000000000000000000"), ST::string::from_int(int32_min, 2));
985 EXPECT_EQ(ST_LITERAL("1111111111111111111111111111111"), ST::string::from_int(int32_max, 2));
986
987 static const long long int64_min = std::numeric_limits<long long>::min();
988 static const long long int64_max = std::numeric_limits<long long>::max();
989 EXPECT_EQ(ST_LITERAL("-9223372036854775808"), ST::string::from_int(int64_min));
990 EXPECT_EQ(ST_LITERAL("9223372036854775807"), ST::string::from_int(int64_max));
991 EXPECT_EQ(ST_LITERAL("-8000000000000000"), ST::string::from_int(int64_min, 16));
992 EXPECT_EQ(ST_LITERAL("7fffffffffffffff"), ST::string::from_int(int64_max, 16, false));
993 EXPECT_EQ(ST_LITERAL("7FFFFFFFFFFFFFFF"), ST::string::from_int(int64_max, 16, true));
994 EXPECT_EQ(ST_LITERAL("-1000000000000000000000"), ST::string::from_int(int64_min, 8));
995 EXPECT_EQ(ST_LITERAL("777777777777777777777"), ST::string::from_int(int64_max, 8));
996 EXPECT_EQ(ST_LITERAL("-1000000000000000000000000000000000000000000000000000000000000000"),
997 ST::string::from_int(int64_min, 2));
998 EXPECT_EQ(ST_LITERAL("111111111111111111111111111111111111111111111111111111111111111"),
999 ST::string::from_int(int64_max, 2));
1000 }
1001
TEST(string,from_uint)1002 TEST(string, from_uint)
1003 {
1004 EXPECT_EQ(ST_LITERAL("0"), ST::string::from_uint(0U));
1005 EXPECT_EQ(ST_LITERAL("80000"), ST::string::from_uint(80000U));
1006 EXPECT_EQ(ST_LITERAL("13880"), ST::string::from_uint(80000U, 16));
1007 EXPECT_EQ(ST_LITERAL("234200"), ST::string::from_uint(80000U, 8));
1008
1009 EXPECT_EQ(ST_LITERAL("0"), ST::string::from_uint(0ULL));
1010 EXPECT_EQ(ST_LITERAL("1000000000000"), ST::string::from_uint(1000000000000ULL));
1011 EXPECT_EQ(ST_LITERAL("e8d4a51000"), ST::string::from_uint(1000000000000ULL, 16));
1012 EXPECT_EQ(ST_LITERAL("16432451210000"), ST::string::from_uint(1000000000000ULL, 8));
1013
1014 static const unsigned int uint32_max = std::numeric_limits<unsigned int>::max();
1015 EXPECT_EQ(ST_LITERAL("4294967295"), ST::string::from_uint(uint32_max));
1016 EXPECT_EQ(ST_LITERAL("ffffffff"), ST::string::from_uint(uint32_max, 16, false));
1017 EXPECT_EQ(ST_LITERAL("FFFFFFFF"), ST::string::from_uint(uint32_max, 16, true));
1018 EXPECT_EQ(ST_LITERAL("37777777777"), ST::string::from_uint(uint32_max, 8));
1019 EXPECT_EQ(ST_LITERAL("11111111111111111111111111111111"), ST::string::from_uint(uint32_max, 2));
1020
1021 static const unsigned long long uint64_max = std::numeric_limits<unsigned long long>::max();
1022 EXPECT_EQ(ST_LITERAL("18446744073709551615"), ST::string::from_uint(uint64_max));
1023 EXPECT_EQ(ST_LITERAL("ffffffffffffffff"), ST::string::from_uint(uint64_max, 16, false));
1024 EXPECT_EQ(ST_LITERAL("FFFFFFFFFFFFFFFF"), ST::string::from_uint(uint64_max, 16, true));
1025 EXPECT_EQ(ST_LITERAL("1777777777777777777777"), ST::string::from_uint(uint64_max, 8));
1026 EXPECT_EQ(ST_LITERAL("1111111111111111111111111111111111111111111111111111111111111111"),
1027 ST::string::from_uint(uint64_max, 2));
1028 }
1029
TEST(string,from_float)1030 TEST(string, from_float)
1031 {
1032 EXPECT_EQ(ST_LITERAL("0"), ST::string::from_float(0.0f));
1033 EXPECT_EQ(ST_LITERAL("0"), ST::string::from_double(0.0));
1034
1035 EXPECT_EQ(ST_LITERAL("-16"), ST::string::from_float(-16.0f));
1036 EXPECT_EQ(ST_LITERAL("16"), ST::string::from_float(16.0f));
1037 EXPECT_EQ(ST_LITERAL("1.6"), ST::string::from_float(1.6f));
1038 EXPECT_EQ(ST_LITERAL("16384.5"), ST::string::from_float(16384.5f));
1039 EXPECT_EQ(ST_LITERAL("0.0078"), ST::string::from_float(0.0078f));
1040
1041 EXPECT_EQ(ST_LITERAL("-16"), ST::string::from_double(-16.0));
1042 EXPECT_EQ(ST_LITERAL("16"), ST::string::from_double(16.0));
1043 EXPECT_EQ(ST_LITERAL("1.6"), ST::string::from_double(1.6));
1044 EXPECT_EQ(ST_LITERAL("16384.5"), ST::string::from_double(16384.5));
1045 EXPECT_EQ(ST_LITERAL("0.0078"), ST::string::from_double(0.0078));
1046
1047 // Special values (Different CRTs have very different ways of representing
1048 // infinity and NaN textually :( )
1049 EXPECT_TRUE(ST::string::from_float(std::numeric_limits<float>::infinity())
1050 .find("inf", ST::case_insensitive) >= 0);
1051 EXPECT_TRUE(ST::string::from_double(std::numeric_limits<double>::infinity())
1052 .find("inf", ST::case_insensitive) >= 0);
1053 EXPECT_TRUE(ST::string::from_float(std::numeric_limits<float>::quiet_NaN())
1054 .find("nan", ST::case_insensitive) >= 0);
1055 EXPECT_TRUE(ST::string::from_double(std::numeric_limits<float>::quiet_NaN())
1056 .find("nan", ST::case_insensitive) >= 0);
1057 }
1058
TEST(string,from_bool)1059 TEST(string, from_bool)
1060 {
1061 EXPECT_EQ(ST_LITERAL("false"), ST::string::from_bool(false));
1062 EXPECT_EQ(ST_LITERAL("true"), ST::string::from_bool(true));
1063 EXPECT_EQ(ST_LITERAL("true"), ST::string::from_bool((bool)16));
1064 }
1065
TEST(string,to_int)1066 TEST(string, to_int)
1067 {
1068 EXPECT_EQ(0, ST_LITERAL("0").to_int());
1069 EXPECT_EQ(0, ST_LITERAL("+0").to_int());
1070 EXPECT_EQ(0, ST_LITERAL("-0").to_int());
1071
1072 EXPECT_EQ(0, ST_LITERAL("0").to_long_long());
1073 EXPECT_EQ(0, ST_LITERAL("+0").to_long_long());
1074 EXPECT_EQ(0, ST_LITERAL("-0").to_long_long());
1075
1076 EXPECT_EQ(-80000, ST_LITERAL("-80000").to_int());
1077 EXPECT_EQ(80000, ST_LITERAL("80000").to_int());
1078 EXPECT_EQ(80000, ST_LITERAL("+80000").to_int());
1079 EXPECT_EQ(-80000, ST_LITERAL("-0x13880").to_int());
1080 EXPECT_EQ(80000, ST_LITERAL("0x13880").to_int());
1081 EXPECT_EQ(80000, ST_LITERAL("+0x13880").to_int());
1082 EXPECT_EQ(-80000, ST_LITERAL("-0234200").to_int());
1083 EXPECT_EQ(80000, ST_LITERAL("0234200").to_int());
1084 EXPECT_EQ(80000, ST_LITERAL("+0234200").to_int());
1085 EXPECT_EQ(-80000, ST_LITERAL("-13880").to_int(16));
1086 EXPECT_EQ(80000, ST_LITERAL("13880").to_int(16));
1087 EXPECT_EQ(80000, ST_LITERAL("+13880").to_int(16));
1088 EXPECT_EQ(-80000, ST_LITERAL("-234200").to_int(8));
1089 EXPECT_EQ(80000, ST_LITERAL("234200").to_int(8));
1090 EXPECT_EQ(80000, ST_LITERAL("+234200").to_int(8));
1091
1092 EXPECT_EQ(-1000000000000LL, ST_LITERAL("-1000000000000").to_long_long());
1093 EXPECT_EQ(1000000000000LL, ST_LITERAL("1000000000000").to_long_long());
1094 EXPECT_EQ(1000000000000LL, ST_LITERAL("+1000000000000").to_long_long());
1095 EXPECT_EQ(-1000000000000LL, ST_LITERAL("-0xe8d4a51000").to_long_long());
1096 EXPECT_EQ(1000000000000LL, ST_LITERAL("0xe8d4a51000").to_long_long());
1097 EXPECT_EQ(1000000000000LL, ST_LITERAL("+0xe8d4a51000").to_long_long());
1098 EXPECT_EQ(-1000000000000LL, ST_LITERAL("-016432451210000").to_long_long());
1099 EXPECT_EQ(1000000000000LL, ST_LITERAL("016432451210000").to_long_long());
1100 EXPECT_EQ(1000000000000LL, ST_LITERAL("+016432451210000").to_long_long());
1101 EXPECT_EQ(-1000000000000LL, ST_LITERAL("-e8d4a51000").to_long_long(16));
1102 EXPECT_EQ(1000000000000LL, ST_LITERAL("e8d4a51000").to_long_long(16));
1103 EXPECT_EQ(1000000000000LL, ST_LITERAL("+e8d4a51000").to_long_long(16));
1104 EXPECT_EQ(-1000000000000LL, ST_LITERAL("-16432451210000").to_long_long(8));
1105 EXPECT_EQ(1000000000000LL, ST_LITERAL("16432451210000").to_long_long(8));
1106 EXPECT_EQ(1000000000000LL, ST_LITERAL("+16432451210000").to_long_long(8));
1107
1108 static const int int32_min = std::numeric_limits<int>::min();
1109 static const int int32_max = std::numeric_limits<int>::max();
1110 EXPECT_EQ(int32_min, ST_LITERAL("-2147483648").to_int());
1111 EXPECT_EQ(int32_max, ST_LITERAL("2147483647").to_int());
1112 EXPECT_EQ(int32_max, ST_LITERAL("+2147483647").to_int());
1113 EXPECT_EQ(int32_min, ST_LITERAL("-0x80000000").to_int());
1114 EXPECT_EQ(int32_max, ST_LITERAL("0x7FFFFFFF").to_int());
1115 EXPECT_EQ(int32_max, ST_LITERAL("+0x7FFFFFFF").to_int());
1116 EXPECT_EQ(int32_min, ST_LITERAL("-020000000000").to_int());
1117 EXPECT_EQ(int32_max, ST_LITERAL("017777777777").to_int());
1118 EXPECT_EQ(int32_max, ST_LITERAL("+017777777777").to_int());
1119 EXPECT_EQ(int32_min, ST_LITERAL("-80000000").to_int(16));
1120 EXPECT_EQ(int32_max, ST_LITERAL("7FFFFFFF").to_int(16));
1121 EXPECT_EQ(int32_max, ST_LITERAL("+7FFFFFFF").to_int(16));
1122 EXPECT_EQ(int32_min, ST_LITERAL("-20000000000").to_int(8));
1123 EXPECT_EQ(int32_max, ST_LITERAL("17777777777").to_int(8));
1124 EXPECT_EQ(int32_max, ST_LITERAL("+17777777777").to_int(8));
1125
1126 static const long long int64_min = std::numeric_limits<long long>::min();
1127 static const long long int64_max = std::numeric_limits<long long>::max();
1128 EXPECT_EQ(int64_min, ST_LITERAL("-9223372036854775808").to_long_long());
1129 EXPECT_EQ(int64_max, ST_LITERAL("9223372036854775807").to_long_long());
1130 EXPECT_EQ(int64_max, ST_LITERAL("+9223372036854775807").to_long_long());
1131 EXPECT_EQ(int64_min, ST_LITERAL("-0x8000000000000000").to_long_long());
1132 EXPECT_EQ(int64_max, ST_LITERAL("0x7FFFFFFFFFFFFFFF").to_long_long());
1133 EXPECT_EQ(int64_max, ST_LITERAL("+0x7FFFFFFFFFFFFFFF").to_long_long());
1134 EXPECT_EQ(int64_min, ST_LITERAL("-01000000000000000000000").to_long_long());
1135 EXPECT_EQ(int64_max, ST_LITERAL("0777777777777777777777").to_long_long());
1136 EXPECT_EQ(int64_max, ST_LITERAL("+0777777777777777777777").to_long_long());
1137 EXPECT_EQ(int64_min, ST_LITERAL("-8000000000000000").to_long_long(16));
1138 EXPECT_EQ(int64_max, ST_LITERAL("7FFFFFFFFFFFFFFF").to_long_long(16));
1139 EXPECT_EQ(int64_max, ST_LITERAL("+7FFFFFFFFFFFFFFF").to_long_long(16));
1140 EXPECT_EQ(int64_min, ST_LITERAL("-1000000000000000000000").to_long_long(8));
1141 EXPECT_EQ(int64_max, ST_LITERAL("777777777777777777777").to_long_long(8));
1142 EXPECT_EQ(int64_max, ST_LITERAL("+777777777777777777777").to_long_long(8));
1143
1144 // Empty string is treated as zero for compatibility with strtol
1145 EXPECT_EQ(0, ST::string().to_int());
1146 EXPECT_EQ(0LL, ST::string().to_long_long());
1147 }
1148
TEST(string,to_int_check)1149 TEST(string, to_int_check)
1150 {
1151 ST::conversion_result result;
1152 (void) ST_LITERAL("0").to_int(result);
1153 EXPECT_TRUE(result.ok());
1154 EXPECT_TRUE(result.full_match());
1155 (void) ST_LITERAL("100").to_int(result);
1156 EXPECT_TRUE(result.ok());
1157 EXPECT_TRUE(result.full_match());
1158 (void) ST_LITERAL("+100").to_int(result);
1159 EXPECT_TRUE(result.ok());
1160 EXPECT_TRUE(result.full_match());
1161 (void) ST_LITERAL("-100").to_int(result);
1162 EXPECT_TRUE(result.ok());
1163 EXPECT_TRUE(result.full_match());
1164 (void) ST_LITERAL("0x1FF").to_int(result);
1165 EXPECT_TRUE(result.ok());
1166 EXPECT_TRUE(result.full_match());
1167 (void) ST_LITERAL("+0x1FF").to_int(result);
1168 EXPECT_TRUE(result.ok());
1169 EXPECT_TRUE(result.full_match());
1170 (void) ST_LITERAL("-0x1FF").to_int(result);
1171 EXPECT_TRUE(result.ok());
1172 EXPECT_TRUE(result.full_match());
1173 (void) ST_LITERAL("1FF").to_int(result, 16);
1174 EXPECT_TRUE(result.ok());
1175 EXPECT_TRUE(result.full_match());
1176 (void) ST_LITERAL("+1FF").to_int(result, 16);
1177 EXPECT_TRUE(result.ok());
1178 EXPECT_TRUE(result.full_match());
1179 (void) ST_LITERAL("-1FF").to_int(result, 16);
1180 EXPECT_TRUE(result.ok());
1181 EXPECT_TRUE(result.full_match());
1182 (void) ST_LITERAL("0100").to_int(result);
1183 EXPECT_TRUE(result.ok());
1184 EXPECT_TRUE(result.full_match());
1185 (void) ST_LITERAL("+0100").to_int(result);
1186 EXPECT_TRUE(result.ok());
1187 EXPECT_TRUE(result.full_match());
1188 (void) ST_LITERAL("-0100").to_int(result);
1189 EXPECT_TRUE(result.ok());
1190 EXPECT_TRUE(result.full_match());
1191
1192 (void) ST_LITERAL("1FF").to_int(result);
1193 EXPECT_TRUE(result.ok());
1194 EXPECT_FALSE(result.full_match());
1195 (void) ST_LITERAL("+1FF").to_int(result);
1196 EXPECT_TRUE(result.ok());
1197 EXPECT_FALSE(result.full_match());
1198 (void) ST_LITERAL("-1FF").to_int(result);
1199 EXPECT_TRUE(result.ok());
1200 EXPECT_FALSE(result.full_match());
1201 (void) ST_LITERAL("FF").to_int(result);
1202 EXPECT_FALSE(result.ok());
1203 EXPECT_FALSE(result.full_match());
1204 (void) ST_LITERAL("+FF").to_int(result);
1205 EXPECT_FALSE(result.ok());
1206 EXPECT_FALSE(result.full_match());
1207 (void) ST_LITERAL("-FF").to_int(result);
1208 EXPECT_FALSE(result.ok());
1209 EXPECT_FALSE(result.full_match());
1210 (void) ST::string().to_int(result);
1211 EXPECT_FALSE(result.ok());
1212 EXPECT_TRUE(result.full_match());
1213 }
1214
TEST(string,to_uint)1215 TEST(string, to_uint)
1216 {
1217 EXPECT_EQ(0U, ST_LITERAL("0").to_uint());
1218 EXPECT_EQ(0U, ST_LITERAL("+0").to_uint());
1219 EXPECT_EQ(0U, ST_LITERAL("-0").to_uint());
1220
1221 EXPECT_EQ(0ULL, ST_LITERAL("0").to_ulong_long());
1222 EXPECT_EQ(0ULL, ST_LITERAL("+0").to_ulong_long());
1223 EXPECT_EQ(0ULL, ST_LITERAL("-0").to_ulong_long());
1224
1225 EXPECT_EQ(80000U, ST_LITERAL("80000").to_uint());
1226 EXPECT_EQ(80000U, ST_LITERAL("+80000").to_uint());
1227 EXPECT_EQ(80000U, ST_LITERAL("0x13880").to_uint());
1228 EXPECT_EQ(80000U, ST_LITERAL("+0x13880").to_uint());
1229 EXPECT_EQ(80000U, ST_LITERAL("0234200").to_uint());
1230 EXPECT_EQ(80000U, ST_LITERAL("+0234200").to_uint());
1231 EXPECT_EQ(80000U, ST_LITERAL("13880").to_uint(16));
1232 EXPECT_EQ(80000U, ST_LITERAL("+13880").to_uint(16));
1233 EXPECT_EQ(80000U, ST_LITERAL("234200").to_uint(8));
1234 EXPECT_EQ(80000U, ST_LITERAL("+234200").to_uint(8));
1235
1236 EXPECT_EQ(1000000000000ULL, ST_LITERAL("1000000000000").to_ulong_long());
1237 EXPECT_EQ(1000000000000ULL, ST_LITERAL("+1000000000000").to_ulong_long());
1238 EXPECT_EQ(1000000000000ULL, ST_LITERAL("0xe8d4a51000").to_ulong_long());
1239 EXPECT_EQ(1000000000000ULL, ST_LITERAL("+0xe8d4a51000").to_ulong_long());
1240 EXPECT_EQ(1000000000000ULL, ST_LITERAL("016432451210000").to_ulong_long());
1241 EXPECT_EQ(1000000000000ULL, ST_LITERAL("+016432451210000").to_ulong_long());
1242 EXPECT_EQ(1000000000000ULL, ST_LITERAL("e8d4a51000").to_ulong_long(16));
1243 EXPECT_EQ(1000000000000ULL, ST_LITERAL("+e8d4a51000").to_ulong_long(16));
1244 EXPECT_EQ(1000000000000ULL, ST_LITERAL("16432451210000").to_ulong_long(8));
1245 EXPECT_EQ(1000000000000ULL, ST_LITERAL("+16432451210000").to_ulong_long(8));
1246
1247 static const unsigned int uint32_max = std::numeric_limits<unsigned int>::max();
1248 EXPECT_EQ(uint32_max, ST_LITERAL("4294967295").to_uint());
1249 EXPECT_EQ(uint32_max, ST_LITERAL("+4294967295").to_uint());
1250 EXPECT_EQ(uint32_max, ST_LITERAL("0xFFFFFFFF").to_uint());
1251 EXPECT_EQ(uint32_max, ST_LITERAL("+0xFFFFFFFF").to_uint());
1252 EXPECT_EQ(uint32_max, ST_LITERAL("037777777777").to_uint());
1253 EXPECT_EQ(uint32_max, ST_LITERAL("+037777777777").to_uint());
1254
1255 static const unsigned long long uint64_max = std::numeric_limits<unsigned long long>::max();
1256 EXPECT_EQ(uint64_max, ST_LITERAL("18446744073709551615").to_ulong_long());
1257 EXPECT_EQ(uint64_max, ST_LITERAL("+18446744073709551615").to_ulong_long());
1258 EXPECT_EQ(uint64_max, ST_LITERAL("0xFFFFFFFFFFFFFFFF").to_ulong_long());
1259 EXPECT_EQ(uint64_max, ST_LITERAL("+0xFFFFFFFFFFFFFFFF").to_ulong_long());
1260 EXPECT_EQ(uint64_max, ST_LITERAL("01777777777777777777777").to_ulong_long());
1261 EXPECT_EQ(uint64_max, ST_LITERAL("+01777777777777777777777").to_ulong_long());
1262
1263 // Empty string is treated as zero for compatibility with strtoul
1264 EXPECT_EQ(0U, ST::string().to_uint());
1265 EXPECT_EQ(0ULL, ST::string().to_ulong_long());
1266 }
1267
TEST(string,to_uint_check)1268 TEST(string, to_uint_check)
1269 {
1270 ST::conversion_result result;
1271 (void) ST_LITERAL("0").to_uint(result);
1272 EXPECT_TRUE(result.ok());
1273 EXPECT_TRUE(result.full_match());
1274 (void) ST_LITERAL("100").to_uint(result);
1275 EXPECT_TRUE(result.ok());
1276 EXPECT_TRUE(result.full_match());
1277 (void) ST_LITERAL("0x1FF").to_uint(result);
1278 EXPECT_TRUE(result.ok());
1279 EXPECT_TRUE(result.full_match());
1280 (void) ST_LITERAL("1FF").to_uint(result, 16);
1281 EXPECT_TRUE(result.ok());
1282 EXPECT_TRUE(result.full_match());
1283 (void) ST_LITERAL("0100").to_uint(result);
1284 EXPECT_TRUE(result.ok());
1285 EXPECT_TRUE(result.full_match());
1286
1287 (void) ST_LITERAL("1FF").to_uint(result);
1288 EXPECT_TRUE(result.ok());
1289 EXPECT_FALSE(result.full_match());
1290 (void) ST_LITERAL("FF").to_uint(result);
1291 EXPECT_FALSE(result.ok());
1292 EXPECT_FALSE(result.full_match());
1293 (void) ST::string().to_uint(result);
1294 EXPECT_FALSE(result.ok());
1295 EXPECT_TRUE(result.full_match());
1296 }
1297
TEST(string,to_float)1298 TEST(string, to_float)
1299 {
1300 EXPECT_EQ(0.0f, ST_LITERAL("0").to_float());
1301 EXPECT_EQ(0.0f, ST_LITERAL("+0").to_float());
1302 EXPECT_EQ(0.0f, ST_LITERAL("-0").to_float());
1303
1304 EXPECT_EQ(0.0, ST_LITERAL("0").to_double());
1305 EXPECT_EQ(0.0, ST_LITERAL("+0").to_double());
1306 EXPECT_EQ(0.0, ST_LITERAL("-0").to_double());
1307
1308 EXPECT_EQ(-16.0f, ST_LITERAL("-16").to_float());
1309 EXPECT_EQ(16.0f, ST_LITERAL("16").to_float());
1310 EXPECT_EQ(16.0f, ST_LITERAL("+16").to_float());
1311 EXPECT_EQ(-16.0f, ST_LITERAL("-16.0").to_float());
1312 EXPECT_EQ(16.0f, ST_LITERAL("16.0").to_float());
1313 EXPECT_EQ(16.0f, ST_LITERAL("+16.0").to_float());
1314 EXPECT_EQ(-16.0f, ST_LITERAL("-1.6e1").to_float());
1315 EXPECT_EQ(16.0f, ST_LITERAL("1.6e1").to_float());
1316 EXPECT_EQ(16.0f, ST_LITERAL("+1.6e1").to_float());
1317
1318 EXPECT_EQ(-16.0, ST_LITERAL("-16").to_double());
1319 EXPECT_EQ(16.0, ST_LITERAL("16").to_double());
1320 EXPECT_EQ(16.0, ST_LITERAL("+16").to_double());
1321 EXPECT_EQ(-16.0, ST_LITERAL("-16.0").to_double());
1322 EXPECT_EQ(16.0, ST_LITERAL("16.0").to_double());
1323 EXPECT_EQ(16.0, ST_LITERAL("+16.0").to_double());
1324 EXPECT_EQ(-16.0, ST_LITERAL("-1.6e1").to_double());
1325 EXPECT_EQ(16.0, ST_LITERAL("1.6e1").to_double());
1326 EXPECT_EQ(16.0, ST_LITERAL("+1.6e1").to_double());
1327
1328 // Empty string is treated as zero for compatibility with strtod
1329 EXPECT_EQ(0.0f, ST::string().to_float());
1330 EXPECT_EQ(0.0, ST::string().to_double());
1331 }
1332
TEST(string,to_float_check)1333 TEST(string, to_float_check)
1334 {
1335 ST::conversion_result result;
1336 (void) ST_LITERAL("0").to_float(result);
1337 EXPECT_TRUE(result.ok());
1338 EXPECT_TRUE(result.full_match());
1339 (void) ST_LITERAL("0").to_double(result);
1340 EXPECT_TRUE(result.ok());
1341 EXPECT_TRUE(result.full_match());
1342
1343 (void) ST_LITERAL("16").to_float(result);
1344 EXPECT_TRUE(result.ok());
1345 EXPECT_TRUE(result.full_match());
1346 (void) ST_LITERAL("+16").to_float(result);
1347 EXPECT_TRUE(result.ok());
1348 EXPECT_TRUE(result.full_match());
1349 (void) ST_LITERAL("-16").to_float(result);
1350 EXPECT_TRUE(result.ok());
1351 EXPECT_TRUE(result.full_match());
1352 (void) ST_LITERAL("16").to_double(result);
1353 EXPECT_TRUE(result.ok());
1354 EXPECT_TRUE(result.full_match());
1355 (void) ST_LITERAL("+16").to_double(result);
1356 EXPECT_TRUE(result.ok());
1357 EXPECT_TRUE(result.full_match());
1358 (void) ST_LITERAL("-16").to_double(result);
1359 EXPECT_TRUE(result.ok());
1360 EXPECT_TRUE(result.full_match());
1361
1362 (void) ST_LITERAL("16.0").to_float(result);
1363 EXPECT_TRUE(result.ok());
1364 EXPECT_TRUE(result.full_match());
1365 (void) ST_LITERAL("+16.0").to_float(result);
1366 EXPECT_TRUE(result.ok());
1367 EXPECT_TRUE(result.full_match());
1368 (void) ST_LITERAL("-16.0").to_float(result);
1369 EXPECT_TRUE(result.ok());
1370 EXPECT_TRUE(result.full_match());
1371 (void) ST_LITERAL("16.0").to_double(result);
1372 EXPECT_TRUE(result.ok());
1373 EXPECT_TRUE(result.full_match());
1374 (void) ST_LITERAL("+16.0").to_double(result);
1375 EXPECT_TRUE(result.ok());
1376 EXPECT_TRUE(result.full_match());
1377 (void) ST_LITERAL("-16.0").to_double(result);
1378 EXPECT_TRUE(result.ok());
1379 EXPECT_TRUE(result.full_match());
1380
1381 (void) ST_LITERAL("1.6e1").to_float(result);
1382 EXPECT_TRUE(result.ok());
1383 EXPECT_TRUE(result.full_match());
1384 (void) ST_LITERAL("+1.6e1").to_float(result);
1385 EXPECT_TRUE(result.ok());
1386 EXPECT_TRUE(result.full_match());
1387 (void) ST_LITERAL("-1.6e1").to_float(result);
1388 EXPECT_TRUE(result.ok());
1389 EXPECT_TRUE(result.full_match());
1390 (void) ST_LITERAL("1.6e1").to_double(result);
1391 EXPECT_TRUE(result.ok());
1392 EXPECT_TRUE(result.full_match());
1393 (void) ST_LITERAL("+1.6e1").to_double(result);
1394 EXPECT_TRUE(result.ok());
1395 EXPECT_TRUE(result.full_match());
1396 (void) ST_LITERAL("-1.6e1").to_double(result);
1397 EXPECT_TRUE(result.ok());
1398 EXPECT_TRUE(result.full_match());
1399
1400 (void) ST_LITERAL("16xx").to_float(result);
1401 EXPECT_TRUE(result.ok());
1402 EXPECT_FALSE(result.full_match());
1403 (void) ST_LITERAL("16xx").to_double(result);
1404 EXPECT_TRUE(result.ok());
1405 EXPECT_FALSE(result.full_match());
1406 (void) ST_LITERAL("xx").to_float(result);
1407 EXPECT_FALSE(result.ok());
1408 EXPECT_FALSE(result.full_match());
1409 (void) ST_LITERAL("xx").to_double(result);
1410 EXPECT_FALSE(result.ok());
1411 EXPECT_FALSE(result.full_match());
1412
1413 (void) ST::string().to_float(result);
1414 EXPECT_FALSE(result.ok());
1415 EXPECT_TRUE(result.full_match());
1416 (void) ST::string().to_double(result);
1417 EXPECT_FALSE(result.ok());
1418 EXPECT_TRUE(result.full_match());
1419 }
1420
TEST(string,to_bool)1421 TEST(string, to_bool)
1422 {
1423 EXPECT_TRUE(ST_LITERAL("true").to_bool());
1424 EXPECT_TRUE(ST_LITERAL("TRUE").to_bool());
1425 EXPECT_FALSE(ST_LITERAL("false").to_bool());
1426 EXPECT_FALSE(ST_LITERAL("FALSE").to_bool());
1427
1428 EXPECT_FALSE(ST_LITERAL("0").to_bool());
1429 EXPECT_TRUE(ST_LITERAL("1").to_bool());
1430 EXPECT_TRUE(ST_LITERAL("-1").to_bool());
1431 EXPECT_TRUE(ST_LITERAL("1000").to_bool());
1432 EXPECT_TRUE(ST_LITERAL("0x1000").to_bool());
1433
1434 EXPECT_FALSE(ST_LITERAL("T").to_bool());
1435 EXPECT_FALSE(ST_LITERAL("trueXX").to_bool());
1436
1437 EXPECT_FALSE(ST::string().to_bool());
1438 }
1439
TEST(string,to_bool_check)1440 TEST(string, to_bool_check)
1441 {
1442 ST::conversion_result result;
1443 (void) ST_LITERAL("true").to_bool(result);
1444 EXPECT_TRUE(result.ok());
1445 EXPECT_TRUE(result.full_match());
1446 (void) ST_LITERAL("TRUE").to_bool(result);
1447 EXPECT_TRUE(result.ok());
1448 EXPECT_TRUE(result.full_match());
1449 (void) ST_LITERAL("false").to_bool(result);
1450 EXPECT_TRUE(result.ok());
1451 EXPECT_TRUE(result.full_match());
1452 (void) ST_LITERAL("FALSE").to_bool(result);
1453 EXPECT_TRUE(result.ok());
1454 EXPECT_TRUE(result.full_match());
1455
1456 (void) ST_LITERAL("0").to_bool(result);
1457 EXPECT_TRUE(result.ok());
1458 EXPECT_TRUE(result.full_match());
1459 (void) ST_LITERAL("1").to_bool(result);
1460 EXPECT_TRUE(result.ok());
1461 EXPECT_TRUE(result.full_match());
1462 (void) ST_LITERAL("-1").to_bool(result);
1463 EXPECT_TRUE(result.ok());
1464 EXPECT_TRUE(result.full_match());
1465 (void) ST_LITERAL("1000").to_bool(result);
1466 EXPECT_TRUE(result.ok());
1467 EXPECT_TRUE(result.full_match());
1468 (void) ST_LITERAL("0x1000").to_bool(result);
1469 EXPECT_TRUE(result.ok());
1470 EXPECT_TRUE(result.full_match());
1471
1472 (void) ST_LITERAL("T").to_bool(result);
1473 EXPECT_FALSE(result.ok());
1474 EXPECT_FALSE(result.full_match());
1475 (void) ST_LITERAL("trueXX").to_bool(result);
1476 EXPECT_FALSE(result.ok());
1477 EXPECT_FALSE(result.full_match());
1478
1479 (void) ST::string().to_bool(result);
1480 EXPECT_FALSE(result.ok());
1481 EXPECT_TRUE(result.full_match());
1482 }
1483
TEST(string,compare)1484 TEST(string, compare)
1485 {
1486 // Same length, case sensitive
1487 EXPECT_EQ(0, ST_LITERAL("abc").compare("abc", ST::case_sensitive));
1488 EXPECT_GT(0, ST_LITERAL("abc").compare("abd", ST::case_sensitive));
1489 EXPECT_LT(0, ST_LITERAL("abc").compare("abb", ST::case_sensitive));
1490 EXPECT_GT(0, ST_LITERAL("abC").compare("abc", ST::case_sensitive));
1491 EXPECT_GT(0, ST_LITERAL("Abc").compare("abc", ST::case_sensitive));
1492 EXPECT_EQ(0, ST::string().compare("", ST::case_sensitive));
1493
1494 // Same length, case insensitive
1495 EXPECT_EQ(0, ST_LITERAL("abc").compare("abc", ST::case_insensitive));
1496 EXPECT_EQ(0, ST_LITERAL("abc").compare("ABC", ST::case_insensitive));
1497 EXPECT_GT(0, ST_LITERAL("abc").compare("abD", ST::case_insensitive));
1498 EXPECT_LT(0, ST_LITERAL("abc").compare("abB", ST::case_insensitive));
1499 EXPECT_EQ(0, ST::string().compare("", ST::case_insensitive));
1500
1501 // Mismatched length, case sensitive
1502 EXPECT_LT(0, ST_LITERAL("abc").compare("ab", ST::case_sensitive));
1503 EXPECT_GT(0, ST_LITERAL("abc").compare("abcd", ST::case_sensitive));
1504 EXPECT_LT(0, ST_LITERAL("abc").compare("", ST::case_sensitive));
1505 EXPECT_GT(0, ST_LITERAL("").compare("abc", ST::case_sensitive));
1506
1507 // Mismatched length, case insensitive
1508 EXPECT_LT(0, ST_LITERAL("abc").compare("Ab", ST::case_insensitive));
1509 EXPECT_GT(0, ST_LITERAL("abc").compare("Abcd", ST::case_insensitive));
1510 EXPECT_LT(0, ST_LITERAL("abc").compare("", ST::case_insensitive));
1511 EXPECT_GT(0, ST::string().compare("abc", ST::case_insensitive));
1512 }
1513
TEST(string,compare_n)1514 TEST(string, compare_n)
1515 {
1516 // Same length, case sensitive
1517 EXPECT_EQ(0, ST_LITERAL("abcXX").compare_n("abcYY", 3, ST::case_sensitive));
1518 EXPECT_GT(0, ST_LITERAL("abcXX").compare_n("abdYY", 3, ST::case_sensitive));
1519 EXPECT_LT(0, ST_LITERAL("abcXX").compare_n("abbYY", 3, ST::case_sensitive));
1520 EXPECT_GT(0, ST_LITERAL("abCXX").compare_n("abcYY", 3, ST::case_sensitive));
1521 EXPECT_GT(0, ST_LITERAL("AbcXX").compare_n("abcYY", 3, ST::case_sensitive));
1522
1523 // Same length, case insensitive
1524 EXPECT_EQ(0, ST_LITERAL("abcXX").compare_n("abcYY", 3, ST::case_insensitive));
1525 EXPECT_EQ(0, ST_LITERAL("abcXX").compare_n("ABCYY", 3, ST::case_insensitive));
1526 EXPECT_GT(0, ST_LITERAL("abcXX").compare_n("abDYY", 3, ST::case_insensitive));
1527 EXPECT_LT(0, ST_LITERAL("abcXX").compare_n("abBYY", 3, ST::case_insensitive));
1528
1529 // Mismatched length, case sensitive
1530 EXPECT_LT(0, ST_LITERAL("abc").compare_n("ab", 3, ST::case_sensitive));
1531 EXPECT_GT(0, ST_LITERAL("abc").compare_n("abcd", 4, ST::case_sensitive));
1532 EXPECT_LT(0, ST_LITERAL("abc").compare_n("", 3, ST::case_sensitive));
1533 EXPECT_GT(0, ST_LITERAL("").compare_n("abc", 3, ST::case_sensitive));
1534
1535 // Mismatched length, case insensitive
1536 EXPECT_LT(0, ST_LITERAL("abc").compare_n("Ab", 3, ST::case_insensitive));
1537 EXPECT_GT(0, ST_LITERAL("abc").compare_n("Abcd", 4, ST::case_insensitive));
1538 EXPECT_LT(0, ST_LITERAL("abc").compare_n("", 3, ST::case_insensitive));
1539 EXPECT_GT(0, ST::string().compare_n("abc", 3, ST::case_insensitive));
1540 }
1541
TEST(string,find_char)1542 TEST(string, find_char)
1543 {
1544 // Available char, case sensitive
1545 EXPECT_EQ(0, ST_LITERAL("Aaaaaaaa").find('A', ST::case_sensitive));
1546 EXPECT_EQ(0, ST_LITERAL("AaaaAaaa").find('A', ST::case_sensitive));
1547 EXPECT_EQ(4, ST_LITERAL("aaaaAaaa").find('A', ST::case_sensitive));
1548 EXPECT_EQ(7, ST_LITERAL("aaaaaaaA").find('A', ST::case_sensitive));
1549
1550 // Available char, case insensitive
1551 EXPECT_EQ(0, ST_LITERAL("Abbbbbbb").find('A', ST::case_insensitive));
1552 EXPECT_EQ(0, ST_LITERAL("AbbbAbbb").find('A', ST::case_insensitive));
1553 EXPECT_EQ(4, ST_LITERAL("bbbbAbbb").find('A', ST::case_insensitive));
1554 EXPECT_EQ(7, ST_LITERAL("bbbbbbbA").find('A', ST::case_insensitive));
1555 EXPECT_EQ(0, ST_LITERAL("abbbbbbb").find('A', ST::case_insensitive));
1556 EXPECT_EQ(0, ST_LITERAL("abbbabbb").find('A', ST::case_insensitive));
1557 EXPECT_EQ(4, ST_LITERAL("bbbbabbb").find('A', ST::case_insensitive));
1558 EXPECT_EQ(7, ST_LITERAL("bbbbbbba").find('A', ST::case_insensitive));
1559 EXPECT_EQ(0, ST_LITERAL("Abbbbbbb").find('a', ST::case_insensitive));
1560 EXPECT_EQ(0, ST_LITERAL("AbbbAbbb").find('a', ST::case_insensitive));
1561 EXPECT_EQ(4, ST_LITERAL("bbbbAbbb").find('a', ST::case_insensitive));
1562 EXPECT_EQ(7, ST_LITERAL("bbbbbbbA").find('a', ST::case_insensitive));
1563
1564 // Unavailable char
1565 EXPECT_EQ(-1, ST_LITERAL("AaaaAaaa").find('C', ST::case_sensitive));
1566 EXPECT_EQ(-1, ST_LITERAL("caaacaaa").find('C', ST::case_sensitive));
1567 EXPECT_EQ(-1, ST_LITERAL("AaaaAaaa").find('C', ST::case_insensitive));
1568
1569 // Empty string
1570 EXPECT_EQ(-1, ST::string().find('A', ST::case_sensitive));
1571 EXPECT_EQ(-1, ST::string().find('A', ST::case_insensitive));
1572
1573 // Starting position, case senstive
1574 EXPECT_EQ( 4, ST_LITERAL("AaaaAaaa").find(1, 'A', ST::case_sensitive));
1575 EXPECT_EQ( 4, ST_LITERAL("AaaaAaaa").find(4, 'A', ST::case_sensitive));
1576 EXPECT_EQ(-1, ST_LITERAL("AaaaAaaa").find(5, 'A', ST::case_sensitive));
1577 EXPECT_EQ( 7, ST_LITERAL("AaaaAaaA").find(5, 'A', ST::case_sensitive));
1578 EXPECT_EQ(-1, ST_LITERAL("AaaaAaaa").find(7, 'A', ST::case_sensitive));
1579 EXPECT_EQ(-1, ST_LITERAL("AaaaAaaA").find(8, 'A', ST::case_sensitive));
1580 EXPECT_EQ(-1, ST_LITERAL("AaaaAaaA").find(100, 'A', ST::case_sensitive));
1581
1582 // Starting position, case insenstive
1583 EXPECT_EQ( 4, ST_LITERAL("abbbabbb").find(1, 'A', ST::case_insensitive));
1584 EXPECT_EQ( 4, ST_LITERAL("abbbabbb").find(4, 'A', ST::case_insensitive));
1585 EXPECT_EQ(-1, ST_LITERAL("abbbabbb").find(5, 'A', ST::case_insensitive));
1586 EXPECT_EQ( 7, ST_LITERAL("abbbabba").find(5, 'A', ST::case_insensitive));
1587 EXPECT_EQ(-1, ST_LITERAL("abbbabbb").find(7, 'A', ST::case_insensitive));
1588 EXPECT_EQ(-1, ST_LITERAL("abbbabba").find(8, 'A', ST::case_insensitive));
1589 EXPECT_EQ(-1, ST_LITERAL("abbbabba").find(100, 'A', ST::case_insensitive));
1590 }
1591
TEST(string,find_last_char)1592 TEST(string, find_last_char)
1593 {
1594 // Available char, case sensitive
1595 EXPECT_EQ(0, ST_LITERAL("Aaaaaaaa").find_last('A', ST::case_sensitive));
1596 EXPECT_EQ(4, ST_LITERAL("AaaaAaaa").find_last('A', ST::case_sensitive));
1597 EXPECT_EQ(4, ST_LITERAL("aaaaAaaa").find_last('A', ST::case_sensitive));
1598 EXPECT_EQ(7, ST_LITERAL("aaaaaaaA").find_last('A', ST::case_sensitive));
1599
1600 // Available char, case insensitive
1601 EXPECT_EQ(0, ST_LITERAL("Abbbbbbb").find_last('A', ST::case_insensitive));
1602 EXPECT_EQ(4, ST_LITERAL("AbbbAbbb").find_last('A', ST::case_insensitive));
1603 EXPECT_EQ(4, ST_LITERAL("bbbbAbbb").find_last('A', ST::case_insensitive));
1604 EXPECT_EQ(7, ST_LITERAL("bbbbbbbA").find_last('A', ST::case_insensitive));
1605 EXPECT_EQ(0, ST_LITERAL("abbbbbbb").find_last('A', ST::case_insensitive));
1606 EXPECT_EQ(4, ST_LITERAL("abbbabbb").find_last('A', ST::case_insensitive));
1607 EXPECT_EQ(4, ST_LITERAL("bbbbabbb").find_last('A', ST::case_insensitive));
1608 EXPECT_EQ(7, ST_LITERAL("bbbbbbba").find_last('A', ST::case_insensitive));
1609 EXPECT_EQ(0, ST_LITERAL("Abbbbbbb").find_last('a', ST::case_insensitive));
1610 EXPECT_EQ(4, ST_LITERAL("AbbbAbbb").find_last('a', ST::case_insensitive));
1611 EXPECT_EQ(4, ST_LITERAL("bbbbAbbb").find_last('a', ST::case_insensitive));
1612 EXPECT_EQ(7, ST_LITERAL("bbbbbbbA").find_last('a', ST::case_insensitive));
1613
1614 // Unavailable char
1615 EXPECT_EQ(-1, ST_LITERAL("AaaaAaaa").find_last('C', ST::case_sensitive));
1616 EXPECT_EQ(-1, ST_LITERAL("caaacaaa").find_last('C', ST::case_sensitive));
1617 EXPECT_EQ(-1, ST_LITERAL("AaaaAaaa").find_last('C', ST::case_insensitive));
1618
1619 // Empty string
1620 EXPECT_EQ(-1, ST::string().find_last('A', ST::case_sensitive));
1621 EXPECT_EQ(-1, ST::string().find_last('A', ST::case_insensitive));
1622
1623 // End position, case senstive
1624 EXPECT_EQ( 0, ST_LITERAL("AaaaAaaa").find_last(4, 'A', ST::case_sensitive));
1625 EXPECT_EQ( 4, ST_LITERAL("AaaaAaaa").find_last(5, 'A', ST::case_sensitive));
1626 EXPECT_EQ( 4, ST_LITERAL("AaaaAaaA").find_last(7, 'A', ST::case_sensitive));
1627 EXPECT_EQ( 7, ST_LITERAL("AaaaAaaA").find_last(8, 'A', ST::case_sensitive));
1628 EXPECT_EQ( 7, ST_LITERAL("AaaaAaaA").find_last(100, 'A', ST::case_sensitive));
1629 EXPECT_EQ(-1, ST_LITERAL("aaaaAaaA").find_last(4, 'A', ST::case_sensitive));
1630 EXPECT_EQ(-1, ST_LITERAL("AaaaAaaA").find_last(0, 'A', ST::case_sensitive));
1631
1632 // End position, case insenstive
1633 EXPECT_EQ( 0, ST_LITERAL("abbbabbb").find_last(4, 'A', ST::case_insensitive));
1634 EXPECT_EQ( 4, ST_LITERAL("abbbabbb").find_last(5, 'A', ST::case_insensitive));
1635 EXPECT_EQ( 4, ST_LITERAL("abbbabba").find_last(7, 'A', ST::case_insensitive));
1636 EXPECT_EQ( 7, ST_LITERAL("abbbabba").find_last(8, 'A', ST::case_insensitive));
1637 EXPECT_EQ( 7, ST_LITERAL("abbbabba").find_last(100, 'A', ST::case_insensitive));
1638 EXPECT_EQ(-1, ST_LITERAL("bbbbabba").find_last(4, 'A', ST::case_insensitive));
1639 EXPECT_EQ(-1, ST_LITERAL("abbbabba").find_last(0, 'A', ST::case_insensitive));
1640 }
1641
TEST(string,find)1642 TEST(string, find)
1643 {
1644 // Available string, case sensitive
1645 EXPECT_EQ(0, ST_LITERAL("ABCDabcd").find("ABCD", ST::case_sensitive));
1646 EXPECT_EQ(4, ST_LITERAL("abcdABCDABCDabcd").find("ABCD", ST::case_sensitive));
1647 EXPECT_EQ(4, ST_LITERAL("abcdABCDabcd").find("ABCD", ST::case_sensitive));
1648 EXPECT_EQ(4, ST_LITERAL("abcdABCD").find("ABCD", ST::case_sensitive));
1649
1650 // Available string, case insensitive
1651 EXPECT_EQ(0, ST_LITERAL("ABCDxxxx").find("ABCD", ST::case_insensitive));
1652 EXPECT_EQ(4, ST_LITERAL("xxxxABCDABCDxxxx").find("ABCD", ST::case_insensitive));
1653 EXPECT_EQ(4, ST_LITERAL("xxxxABCDxxxx").find("ABCD", ST::case_insensitive));
1654 EXPECT_EQ(4, ST_LITERAL("xxxxABCD").find("ABCD", ST::case_insensitive));
1655 EXPECT_EQ(0, ST_LITERAL("abcdxxxx").find("ABCD", ST::case_insensitive));
1656 EXPECT_EQ(4, ST_LITERAL("xxxxabcdABCDxxxx").find("ABCD", ST::case_insensitive));
1657 EXPECT_EQ(4, ST_LITERAL("xxxxabcdxxxx").find("ABCD", ST::case_insensitive));
1658 EXPECT_EQ(4, ST_LITERAL("xxxxabcd").find("ABCD", ST::case_insensitive));
1659 EXPECT_EQ(0, ST_LITERAL("ABCDxxxx").find("abcd", ST::case_insensitive));
1660 EXPECT_EQ(4, ST_LITERAL("xxxxABCDabcdxxxx").find("abcd", ST::case_insensitive));
1661 EXPECT_EQ(4, ST_LITERAL("xxxxABCDxxxx").find("abcd", ST::case_insensitive));
1662 EXPECT_EQ(4, ST_LITERAL("xxxxABCD").find("abcd", ST::case_insensitive));
1663
1664 // Unavailable string
1665 EXPECT_EQ(-1, ST_LITERAL("xxxx").find("ABCD", ST::case_sensitive));
1666 EXPECT_EQ(-1, ST_LITERAL("xxxx").find("ABCD", ST::case_insensitive));
1667 EXPECT_EQ(-1, ST_LITERAL("xxxxABC").find("ABCD", ST::case_sensitive));
1668 EXPECT_EQ(-1, ST_LITERAL("xxxxABC").find("ABCD", ST::case_insensitive));
1669 EXPECT_EQ(-1, ST_LITERAL("xxxxABCxxxx").find("ABCD", ST::case_sensitive));
1670 EXPECT_EQ(-1, ST_LITERAL("xxxxABCxxxx").find("ABCD", ST::case_insensitive));
1671
1672 // Empty string
1673 EXPECT_EQ(-1, ST::string().find("AAAA", ST::case_sensitive));
1674 EXPECT_EQ(-1, ST::string().find("AAAA", ST::case_insensitive));
1675 EXPECT_EQ(-1, ST_LITERAL("xxxx").find("", ST::case_sensitive));
1676 EXPECT_EQ(-1, ST_LITERAL("xxxx").find((const char *)nullptr, ST::case_sensitive));
1677 EXPECT_EQ(-1, ST::string().find("", ST::case_sensitive));
1678 EXPECT_EQ(-1, ST::string().find((const char *)nullptr, ST::case_sensitive));
1679
1680 // Unicode substring
1681 ST::string haystack;
1682 haystack = ST_LITERAL("xxxx") + ST::string::from_utf32(test_data);
1683 EXPECT_EQ(4, haystack.find(utf8_test_data, ST::case_sensitive));
1684 EXPECT_EQ(4, haystack.find(utf8_test_data, ST::case_insensitive));
1685
1686 haystack = ST::string::from_utf32(test_data) + ST_LITERAL("xxxx");
1687 EXPECT_EQ(0, haystack.find(utf8_test_data, ST::case_sensitive));
1688 EXPECT_EQ(0, haystack.find(utf8_test_data, ST::case_insensitive));
1689
1690 haystack = ST_LITERAL("xxxx") + ST::string::from_utf32(test_data) + ST_LITERAL("xxxx");
1691 EXPECT_EQ(4, haystack.find(utf8_test_data, ST::case_sensitive));
1692 EXPECT_EQ(4, haystack.find(utf8_test_data, ST::case_insensitive));
1693
1694 // Starting position, case senstive
1695 EXPECT_EQ(-1, ST_LITERAL("ABCDabcd").find(1, "ABCD", ST::case_sensitive));
1696 EXPECT_EQ( 4, ST_LITERAL("abcdABCDABCDabcd").find(1, "ABCD", ST::case_sensitive));
1697 EXPECT_EQ( 4, ST_LITERAL("abcdABCDABCDabcd").find(4, "ABCD", ST::case_sensitive));
1698 EXPECT_EQ( 8, ST_LITERAL("abcdABCDABCDabcd").find(5, "ABCD", ST::case_sensitive));
1699 EXPECT_EQ(-1, ST_LITERAL("abcdABCDabcd").find(5, "ABCD", ST::case_sensitive));
1700 EXPECT_EQ(-1, ST_LITERAL("abcdABCDabcd").find(100, "ABCD", ST::case_sensitive));
1701
1702 // Starting position, case insenstive
1703 EXPECT_EQ(-1, ST_LITERAL("abcdxxxx").find(1, "ABCD", ST::case_insensitive));
1704 EXPECT_EQ( 4, ST_LITERAL("xxxxabcdabcdxxxx").find(1, "ABCD", ST::case_insensitive));
1705 EXPECT_EQ( 4, ST_LITERAL("xxxxabcdabcdxxxx").find(4, "ABCD", ST::case_insensitive));
1706 EXPECT_EQ( 8, ST_LITERAL("xxxxabcdabcdxxxx").find(5, "ABCD", ST::case_insensitive));
1707 EXPECT_EQ(-1, ST_LITERAL("xxxxabcdxxxx").find(5, "ABCD", ST::case_insensitive));
1708 EXPECT_EQ(-1, ST_LITERAL("xxxxabcdxxxx").find(100, "ABCD", ST::case_insensitive));
1709
1710 // Empty search string
1711 EXPECT_EQ(-1, ST_LITERAL("xxxx").find("", ST::case_sensitive));
1712 EXPECT_EQ(-1, ST_LITERAL("xxxx").find("", ST::case_insensitive));
1713 EXPECT_EQ(-1, ST_LITERAL("xxxx").find(4, "", ST::case_sensitive));
1714 EXPECT_EQ(-1, ST_LITERAL("xxxx").find(4, "", ST::case_insensitive));
1715 }
1716
TEST(string,find_last)1717 TEST(string, find_last)
1718 {
1719 // Available string, case sensitive
1720 EXPECT_EQ(0, ST_LITERAL("ABCDabcd").find_last("ABCD", ST::case_sensitive));
1721 EXPECT_EQ(8, ST_LITERAL("abcdABCDABCDabcd").find_last("ABCD", ST::case_sensitive));
1722 EXPECT_EQ(4, ST_LITERAL("abcdABCDabcd").find_last("ABCD", ST::case_sensitive));
1723 EXPECT_EQ(4, ST_LITERAL("abcdABCD").find_last("ABCD", ST::case_sensitive));
1724
1725 // Available string, case insensitive
1726 EXPECT_EQ(0, ST_LITERAL("ABCDxxxx").find_last("ABCD", ST::case_insensitive));
1727 EXPECT_EQ(8, ST_LITERAL("xxxxABCDABCDxxxx").find_last("ABCD", ST::case_insensitive));
1728 EXPECT_EQ(4, ST_LITERAL("xxxxABCDxxxx").find_last("ABCD", ST::case_insensitive));
1729 EXPECT_EQ(4, ST_LITERAL("xxxxABCD").find_last("ABCD", ST::case_insensitive));
1730 EXPECT_EQ(0, ST_LITERAL("abcdxxxx").find_last("ABCD", ST::case_insensitive));
1731 EXPECT_EQ(8, ST_LITERAL("xxxxabcdABCDxxxx").find_last("ABCD", ST::case_insensitive));
1732 EXPECT_EQ(4, ST_LITERAL("xxxxabcdxxxx").find_last("ABCD", ST::case_insensitive));
1733 EXPECT_EQ(4, ST_LITERAL("xxxxabcd").find_last("ABCD", ST::case_insensitive));
1734 EXPECT_EQ(0, ST_LITERAL("ABCDxxxx").find_last("abcd", ST::case_insensitive));
1735 EXPECT_EQ(8, ST_LITERAL("xxxxABCDabcdxxxx").find_last("abcd", ST::case_insensitive));
1736 EXPECT_EQ(4, ST_LITERAL("xxxxABCDxxxx").find_last("abcd", ST::case_insensitive));
1737 EXPECT_EQ(4, ST_LITERAL("xxxxABCD").find_last("abcd", ST::case_insensitive));
1738
1739 // Unavailable string
1740 EXPECT_EQ(-1, ST_LITERAL("xxxx").find_last("ABCD", ST::case_sensitive));
1741 EXPECT_EQ(-1, ST_LITERAL("xxxx").find_last("ABCD", ST::case_insensitive));
1742 EXPECT_EQ(-1, ST_LITERAL("xxxxABC").find_last("ABCD", ST::case_sensitive));
1743 EXPECT_EQ(-1, ST_LITERAL("xxxxABC").find_last("ABCD", ST::case_insensitive));
1744 EXPECT_EQ(-1, ST_LITERAL("xxxxABCxxxx").find_last("ABCD", ST::case_sensitive));
1745 EXPECT_EQ(-1, ST_LITERAL("xxxxABCxxxx").find_last("ABCD", ST::case_insensitive));
1746
1747 // Empty string
1748 EXPECT_EQ(-1, ST::string().find_last("AAAA", ST::case_sensitive));
1749 EXPECT_EQ(-1, ST::string().find_last("AAAA", ST::case_insensitive));
1750 EXPECT_EQ(-1, ST_LITERAL("xxxx").find_last("", ST::case_sensitive));
1751 EXPECT_EQ(-1, ST_LITERAL("xxxx").find_last((const char *)nullptr, ST::case_sensitive));
1752 EXPECT_EQ(-1, ST::string().find_last("", ST::case_sensitive));
1753 EXPECT_EQ(-1, ST::string().find_last((const char *)nullptr, ST::case_sensitive));
1754
1755 // Unicode substring
1756 ST::string haystack;
1757 haystack = ST_LITERAL("xxxx") + ST::string::from_utf32(test_data);
1758 EXPECT_EQ(4, haystack.find_last(utf8_test_data, ST::case_sensitive));
1759 EXPECT_EQ(4, haystack.find_last(utf8_test_data, ST::case_insensitive));
1760
1761 haystack = ST::string::from_utf32(test_data) + ST_LITERAL("xxxx");
1762 EXPECT_EQ(0, haystack.find_last(utf8_test_data, ST::case_sensitive));
1763 EXPECT_EQ(0, haystack.find_last(utf8_test_data, ST::case_insensitive));
1764
1765 haystack = ST_LITERAL("xxxx") + ST::string::from_utf32(test_data) + ST_LITERAL("xxxx");
1766 EXPECT_EQ(4, haystack.find_last(utf8_test_data, ST::case_sensitive));
1767 EXPECT_EQ(4, haystack.find_last(utf8_test_data, ST::case_insensitive));
1768
1769 // Starting position, case senstive
1770 EXPECT_EQ(-1, ST_LITERAL("abcdABCD").find_last(4, "ABCD", ST::case_sensitive));
1771 EXPECT_EQ( 4, ST_LITERAL("abcdABCDABCDabcd").find_last(5, "ABCD", ST::case_sensitive));
1772 EXPECT_EQ( 4, ST_LITERAL("abcdABCDABCDabcd").find_last(8, "ABCD", ST::case_sensitive));
1773 EXPECT_EQ( 8, ST_LITERAL("abcdABCDABCDabcd").find_last(9, "ABCD", ST::case_sensitive));
1774 EXPECT_EQ(-1, ST_LITERAL("abcdABCDabcd").find_last(4, "ABCD", ST::case_sensitive));
1775 EXPECT_EQ(-1, ST_LITERAL("ABCDabcd").find_last(0, "ABCD", ST::case_sensitive));
1776
1777 // Starting position, case insenstive
1778 EXPECT_EQ(-1, ST_LITERAL("xxxxabcd").find_last(4, "ABCD", ST::case_insensitive));
1779 EXPECT_EQ( 4, ST_LITERAL("xxxxabcdabcdxxxx").find_last(5, "ABCD", ST::case_insensitive));
1780 EXPECT_EQ( 4, ST_LITERAL("xxxxabcdabcdxxxx").find_last(8, "ABCD", ST::case_insensitive));
1781 EXPECT_EQ( 8, ST_LITERAL("xxxxabcdabcdxxxx").find_last(9, "ABCD", ST::case_insensitive));
1782 EXPECT_EQ(-1, ST_LITERAL("xxxxabcdxxxx").find_last(4, "ABCD", ST::case_insensitive));
1783 EXPECT_EQ(-1, ST_LITERAL("abcdxxxx").find_last(0, "ABCD", ST::case_insensitive));
1784
1785 // Empty search string
1786 EXPECT_EQ(-1, ST_LITERAL("xxxx").find_last("", ST::case_sensitive));
1787 EXPECT_EQ(-1, ST_LITERAL("xxxx").find_last("", ST::case_insensitive));
1788 EXPECT_EQ(-1, ST_LITERAL("xxxx").find_last(0, "", ST::case_sensitive));
1789 EXPECT_EQ(-1, ST_LITERAL("xxxx").find_last(0, "", ST::case_insensitive));
1790 }
1791
TEST(string,trim)1792 TEST(string, trim)
1793 {
1794 EXPECT_EQ(ST_LITERAL("xxx "), ST_LITERAL(" xxx ").trim_left(" \t\r\n"));
1795 EXPECT_EQ(ST_LITERAL("xxx\t"), ST_LITERAL("\txxx\t").trim_left(" \t\r\n"));
1796 EXPECT_EQ(ST_LITERAL("xxx\r\n"), ST_LITERAL("\r\nxxx\r\n").trim_left(" \t\r\n"));
1797 EXPECT_EQ(ST_LITERAL(" xxx "), ST_LITERAL(" xxx ").trim_left("abc"));
1798 EXPECT_EQ(ST_LITERAL(" xxx "), ST_LITERAL(" xxx ").trim_left("x"));
1799
1800 EXPECT_EQ(ST_LITERAL(" xxx"), ST_LITERAL(" xxx ").trim_right(" \t\r\n"));
1801 EXPECT_EQ(ST_LITERAL("\txxx"), ST_LITERAL("\txxx\t").trim_right(" \t\r\n"));
1802 EXPECT_EQ(ST_LITERAL("\r\nxxx"), ST_LITERAL("\r\nxxx\r\n").trim_right(" \t\r\n"));
1803 EXPECT_EQ(ST_LITERAL(" xxx "), ST_LITERAL(" xxx ").trim_right("abc"));
1804 EXPECT_EQ(ST_LITERAL(" xxx "), ST_LITERAL(" xxx ").trim_right("x"));
1805
1806 EXPECT_EQ(ST_LITERAL("xxx"), ST_LITERAL(" xxx ").trim(" \t\r\n"));
1807 EXPECT_EQ(ST_LITERAL("xxx"), ST_LITERAL("\txxx\t").trim(" \t\r\n"));
1808 EXPECT_EQ(ST_LITERAL("xxx"), ST_LITERAL("\r\nxxx\r\n").trim(" \t\r\n"));
1809 EXPECT_EQ(ST_LITERAL(" xxx "), ST_LITERAL(" xxx ").trim("abc"));
1810 EXPECT_EQ(ST_LITERAL(" xxx "), ST_LITERAL(" xxx ").trim("x"));
1811 }
1812
TEST(string,substrings)1813 TEST(string, substrings)
1814 {
1815 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA").left(3));
1816 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAAxxxx").left(3));
1817 EXPECT_EQ(ST_LITERAL("A"), ST_LITERAL("A").left(3));
1818 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").left(3));
1819
1820 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA").right(3));
1821 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("xxxxAAA").right(3));
1822 EXPECT_EQ(ST_LITERAL("A"), ST_LITERAL("A").right(3));
1823 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").right(3));
1824
1825 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAAxxxx").substr(0, 3));
1826 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("xxxxAAA").substr(4, 3));
1827 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("xxAAAxx").substr(2, 3));
1828
1829 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("AAAA").substr(2, 0));
1830 EXPECT_EQ(ST_LITERAL("AA"), ST_LITERAL("AAAA").substr(2, 4));
1831 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("AAAA").substr(6, 4));
1832 EXPECT_EQ(ST_LITERAL("AAAA"), ST_LITERAL("AAAA").substr(0, 4));
1833 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").substr(0, 4));
1834
1835 // Negative indexes start from the right
1836 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("xxxxAAA").substr(-3, 3));
1837 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("xxAAAxx").substr(-5, 3));
1838 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("xxxxAAA").substr(-3, 6));
1839 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAAxxxx").substr(-10, 3));
1840 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA").substr(-10, 10));
1841 }
1842
TEST(string,starts_with)1843 TEST(string, starts_with)
1844 {
1845 EXPECT_TRUE(ST_LITERAL("AAA").starts_with("AAA"));
1846 EXPECT_TRUE(ST_LITERAL("AAAxxx").starts_with("AAA"));
1847 EXPECT_TRUE(ST_LITERAL("AAAAA").starts_with("AAA"));
1848 EXPECT_FALSE(ST_LITERAL("xxx").starts_with("AAA"));
1849 EXPECT_FALSE(ST_LITERAL("AAxxx").starts_with("AAA"));
1850 EXPECT_FALSE(ST_LITERAL("xAAAxxx").starts_with("AAA"));
1851 EXPECT_FALSE(ST_LITERAL("").starts_with("AAA"));
1852 EXPECT_TRUE(ST_LITERAL("xxx").starts_with(""));
1853 }
1854
TEST(string,ends_with)1855 TEST(string, ends_with)
1856 {
1857 EXPECT_TRUE(ST_LITERAL("AAA").ends_with("AAA"));
1858 EXPECT_TRUE(ST_LITERAL("xxxAAA").ends_with("AAA"));
1859 EXPECT_TRUE(ST_LITERAL("AAAAA").ends_with("AAA"));
1860 EXPECT_FALSE(ST_LITERAL("xxx").ends_with("AAA"));
1861 EXPECT_FALSE(ST_LITERAL("xxxAA").ends_with("AAA"));
1862 EXPECT_FALSE(ST_LITERAL("xxxAAAx").ends_with("AAA"));
1863 EXPECT_FALSE(ST_LITERAL("").ends_with("AAA"));
1864 EXPECT_TRUE(ST_LITERAL("xxx").ends_with(""));
1865 }
1866
TEST(string,before_after)1867 TEST(string, before_after)
1868 {
1869 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA;BBB").before_first(';'));
1870 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA##SEP##BBB").before_first("##SEP##"));
1871 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA;BBB;CCC").before_first(';'));
1872 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA##SEP##BBB##SEP##CCC").before_first("##SEP##"));
1873 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA").before_first(';'));
1874 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA").before_first("##SEP##"));
1875 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL(";").before_first(';'));
1876 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("##SEP##").before_first("##SEP##"));
1877 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").before_first(';'));
1878 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").before_first("##SEP##"));
1879
1880 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA;BBB").before_last(';'));
1881 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA##SEP##BBB").before_last("##SEP##"));
1882 EXPECT_EQ(ST_LITERAL("AAA;BBB"), ST_LITERAL("AAA;BBB;CCC").before_last(';'));
1883 EXPECT_EQ(ST_LITERAL("AAA##SEP##BBB"), ST_LITERAL("AAA##SEP##BBB##SEP##CCC").before_last("##SEP##"));
1884 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("AAA").before_last(';'));
1885 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("AAA").before_last("##SEP##"));
1886 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL(";").before_last(';'));
1887 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("##SEP##").before_last("##SEP##"));
1888 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").before_last(';'));
1889 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").before_last("##SEP##"));
1890
1891 EXPECT_EQ(ST_LITERAL("BBB"), ST_LITERAL("AAA;BBB").after_first(';'));
1892 EXPECT_EQ(ST_LITERAL("BBB"), ST_LITERAL("AAA##SEP##BBB").after_first("##SEP##"));
1893 EXPECT_EQ(ST_LITERAL("BBB;CCC"), ST_LITERAL("AAA;BBB;CCC").after_first(';'));
1894 EXPECT_EQ(ST_LITERAL("BBB##SEP##CCC"), ST_LITERAL("AAA##SEP##BBB##SEP##CCC").after_first("##SEP##"));
1895 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("AAA").after_first(';'));
1896 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("AAA").after_first("##SEP##"));
1897 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL(";").after_first(';'));
1898 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("##SEP##").after_first("##SEP##"));
1899 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").after_first(';'));
1900 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").after_first("##SEP##"));
1901
1902 EXPECT_EQ(ST_LITERAL("BBB"), ST_LITERAL("AAA;BBB").after_last(';'));
1903 EXPECT_EQ(ST_LITERAL("BBB"), ST_LITERAL("AAA##SEP##BBB").after_last("##SEP##"));
1904 EXPECT_EQ(ST_LITERAL("CCC"), ST_LITERAL("AAA;BBB;CCC").after_last(';'));
1905 EXPECT_EQ(ST_LITERAL("CCC"), ST_LITERAL("AAA##SEP##BBB##SEP##CCC").after_last("##SEP##"));
1906 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA").after_last(';'));
1907 EXPECT_EQ(ST_LITERAL("AAA"), ST_LITERAL("AAA").after_last("##SEP##"));
1908 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL(";").after_last(';'));
1909 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("##SEP##").after_last("##SEP##"));
1910 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").after_last(';'));
1911 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").after_last("##SEP##"));
1912 }
1913
TEST(string,replace)1914 TEST(string, replace)
1915 {
1916 EXPECT_EQ(ST_LITERAL("xxYYxx"), ST_LITERAL("xxAAxx").replace("A", "Y"));
1917 EXPECT_EQ(ST_LITERAL("xxAAxx"), ST_LITERAL("xxAAxx").replace("XX", "Y"));
1918 EXPECT_EQ(ST_LITERAL("xxxx"), ST_LITERAL("xxAAxx").replace("A", ""));
1919 EXPECT_EQ(ST_LITERAL("xxREPLACExx"), ST_LITERAL("xxFINDxx").replace("FIND", "REPLACE"));
1920 EXPECT_EQ(ST_LITERAL("xxREPLACExxREPLACExx"), ST_LITERAL("xxFINDxxFINDxx").replace("FIND", "REPLACE"));
1921
1922 EXPECT_EQ(ST_LITERAL("YYxx"), ST_LITERAL("AAxx").replace("A", "Y"));
1923 EXPECT_EQ(ST_LITERAL("AAxx"), ST_LITERAL("AAxx").replace("XX", "Y"));
1924 EXPECT_EQ(ST_LITERAL("xx"), ST_LITERAL("AAxx").replace("A", ""));
1925 EXPECT_EQ(ST_LITERAL("REPLACExx"), ST_LITERAL("FINDxx").replace("FIND", "REPLACE"));
1926 EXPECT_EQ(ST_LITERAL("REPLACExxREPLACExx"), ST_LITERAL("FINDxxFINDxx").replace("FIND", "REPLACE"));
1927
1928 EXPECT_EQ(ST_LITERAL("xxYY"), ST_LITERAL("xxAA").replace("A", "Y"));
1929 EXPECT_EQ(ST_LITERAL("xxAA"), ST_LITERAL("xxAA").replace("XX", "Y"));
1930 EXPECT_EQ(ST_LITERAL("xx"), ST_LITERAL("xxAA").replace("A", ""));
1931 EXPECT_EQ(ST_LITERAL("xxREPLACE"), ST_LITERAL("xxFIND").replace("FIND", "REPLACE"));
1932 EXPECT_EQ(ST_LITERAL("xxREPLACExxREPLACE"), ST_LITERAL("xxFINDxxFIND").replace("FIND", "REPLACE"));
1933
1934 EXPECT_EQ(ST_LITERAL("YY"), ST_LITERAL("AA").replace("A", "Y"));
1935 EXPECT_EQ(ST_LITERAL("AA"), ST_LITERAL("AA").replace("XX", "Y"));
1936 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("AA").replace("A", ""));
1937 EXPECT_EQ(ST_LITERAL("REPLACE"), ST_LITERAL("FIND").replace("FIND", "REPLACE"));
1938 EXPECT_EQ(ST_LITERAL("REPLACExxREPLACE"), ST_LITERAL("FINDxxFIND").replace("FIND", "REPLACE"));
1939
1940 // Empty search string
1941 EXPECT_EQ(ST_LITERAL("AA"), ST_LITERAL("AA").replace("", "Y"));
1942 }
1943
TEST(string,case_conversion)1944 TEST(string, case_conversion)
1945 {
1946 /* Edge cases:
1947 * '@' = 'A' - 1
1948 * '[' = 'Z' + 1
1949 * '`' = 'a' - 1
1950 * '{' = 'z' + 1
1951 */
1952
1953 EXPECT_EQ(ST_LITERAL("AAZZ"), ST_LITERAL("aazz").to_upper());
1954 EXPECT_EQ(ST_LITERAL("AAZZ"), ST_LITERAL("AAZZ").to_upper());
1955 EXPECT_EQ(ST_LITERAL("@AZ[`AZ{"), ST_LITERAL("@AZ[`az{").to_upper());
1956 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").to_upper());
1957
1958 EXPECT_EQ(ST_LITERAL("aazz"), ST_LITERAL("aazz").to_lower());
1959 EXPECT_EQ(ST_LITERAL("aazz"), ST_LITERAL("AAZZ").to_lower());
1960 EXPECT_EQ(ST_LITERAL("@az[`az{"), ST_LITERAL("@AZ[`az{").to_lower());
1961 EXPECT_EQ(ST_LITERAL(""), ST_LITERAL("").to_lower());
1962 }
1963
TEST(string,tokenize)1964 TEST(string, tokenize)
1965 {
1966 std::vector<ST::string> expected1;
1967 expected1.push_back("aaa");
1968 expected1.push_back("b");
1969 expected1.push_back("ccc");
1970 expected1.push_back("d");
1971 expected1.push_back("èèè");
1972 const ST::string input1("aaa\t\tb\n;ccc-d;èèè");
1973 EXPECT_EQ(expected1, input1.tokenize("\t\n-;"));
1974
1975 std::vector<ST::string> expected2;
1976 expected2.push_back("aaa\t\tb\n");
1977 expected2.push_back("ccc-d");
1978 expected2.push_back("èèè");
1979 EXPECT_EQ(expected2, input1.tokenize(";"));
1980
1981 std::vector<ST::string> expected3;
1982 expected3.push_back(input1);
1983 EXPECT_EQ(expected3, input1.tokenize("x"));
1984
1985 const ST::string input2("\t;aaa\t\tb\n;ccc-d;èèè--");
1986 EXPECT_EQ(expected1, input2.tokenize("\t\n-;"));
1987
1988 // tokenize will return an empty vector if there are no tokens in the input
1989 EXPECT_EQ(std::vector<ST::string>(), ST_LITERAL("\t;\n;").tokenize("\t\n-;"));
1990 EXPECT_EQ(std::vector<ST::string>(), ST_LITERAL("").tokenize("\t\n-;"));
1991 }
1992
TEST(string,split)1993 TEST(string, split)
1994 {
1995 std::vector<ST::string> expected1;
1996 expected1.push_back("aaa");
1997 expected1.push_back("b");
1998 expected1.push_back("ccc");
1999 expected1.push_back("d");
2000 expected1.push_back("èèè");
2001 const ST::string input1("aaa-b-ccc-d-èèè");
2002 EXPECT_EQ(expected1, input1.split("-"));
2003 EXPECT_EQ(expected1, input1.split("-", 4));
2004 EXPECT_EQ(expected1, input1.split("-", 10));
2005
2006 const ST::string input2("aaa#SEP#b#SEP#ccc#SEP#d#SEP#èèè");
2007 EXPECT_EQ(expected1, input2.split("#SEP#"));
2008 EXPECT_EQ(expected1, input2.split("#SEP#", 4));
2009 EXPECT_EQ(expected1, input2.split("#SEP#", 10));
2010
2011 std::vector<ST::string> expected2;
2012 expected2.push_back("aaa");
2013 expected2.push_back("b");
2014 expected2.push_back("ccc-d-èèè");
2015 EXPECT_EQ(expected2, input1.split("-", 2));
2016
2017 std::vector<ST::string> expected3;
2018 expected3.push_back(input1);
2019 EXPECT_EQ(expected3, input1.split("-", 0));
2020 EXPECT_EQ(expected3, input1.split("x"));
2021 EXPECT_EQ(expected3, input1.split("x", 4));
2022
2023 std::vector<ST::string> expected4;
2024 expected4.push_back("");
2025 expected4.push_back("aaa");
2026 expected4.push_back("b");
2027 expected4.push_back("ccc");
2028 expected4.push_back("d");
2029 expected4.push_back("èèè");
2030 expected4.push_back("");
2031 const ST::string input3("-aaa-b-ccc-d-èèè-");
2032 EXPECT_EQ(expected4, input3.split("-"));
2033 EXPECT_EQ(expected4, input3.split("-", 6));
2034 EXPECT_EQ(expected4, input3.split("-", 10));
2035
2036 std::vector<ST::string> expected5;
2037 expected5.push_back("");
2038 expected5.push_back("");
2039 expected5.push_back("");
2040 expected5.push_back("");
2041 expected5.push_back("");
2042 const ST::string input4("----");
2043 EXPECT_EQ(expected5, input4.split("-"));
2044 EXPECT_EQ(expected5, input4.split("-", 4));
2045 EXPECT_EQ(expected5, input4.split("-", 10));
2046
2047 std::vector<ST::string> expected6;
2048 expected6.push_back("");
2049 expected6.push_back("");
2050 expected6.push_back("--");
2051 EXPECT_EQ(expected6, input4.split("-", 2));
2052
2053 std::vector<ST::string> expected7;
2054 expected7.push_back("");
2055 expected7.push_back("");
2056 expected7.push_back("");
2057 EXPECT_EQ(expected7, input4.split("--"));
2058
2059 std::vector<ST::string> expected8;
2060 expected8.push_back("");
2061 expected8.push_back("--");
2062 EXPECT_EQ(expected8, input4.split("--", 1));
2063
2064 // split never provides an empty vector, even for empty input
2065 std::vector<ST::string> expected9;
2066 expected9.push_back(ST::string());
2067 EXPECT_EQ(expected9, ST_LITERAL("").split("-"));
2068 EXPECT_EQ(expected9, ST_LITERAL("").split("-", 4));
2069 }
2070
TEST(string,split_char)2071 TEST(string, split_char)
2072 {
2073 std::vector<ST::string> expected1;
2074 expected1.push_back("aaa");
2075 expected1.push_back("b");
2076 expected1.push_back("ccc");
2077 expected1.push_back("d");
2078 expected1.push_back("èèè");
2079 const ST::string input1("aaa-b-ccc-d-èèè");
2080 EXPECT_EQ(expected1, input1.split('-'));
2081 EXPECT_EQ(expected1, input1.split('-', 4));
2082 EXPECT_EQ(expected1, input1.split('-', 10));
2083
2084 std::vector<ST::string> expected2;
2085 expected2.push_back("aaa");
2086 expected2.push_back("b");
2087 expected2.push_back("ccc-d-èèè");
2088 EXPECT_EQ(expected2, input1.split('-', 2));
2089
2090 std::vector<ST::string> expected3;
2091 expected3.push_back(input1);
2092 EXPECT_EQ(expected3, input1.split('-', 0));
2093 EXPECT_EQ(expected3, input1.split('x'));
2094 EXPECT_EQ(expected3, input1.split('x', 4));
2095
2096 std::vector<ST::string> expected4;
2097 expected4.push_back("");
2098 expected4.push_back("aaa");
2099 expected4.push_back("b");
2100 expected4.push_back("ccc");
2101 expected4.push_back("d");
2102 expected4.push_back("èèè");
2103 expected4.push_back("");
2104 const ST::string input3("-aaa-b-ccc-d-èèè-");
2105 EXPECT_EQ(expected4, input3.split('-'));
2106 EXPECT_EQ(expected4, input3.split('-', 6));
2107 EXPECT_EQ(expected4, input3.split('-', 10));
2108
2109 std::vector<ST::string> expected5;
2110 expected5.push_back("");
2111 expected5.push_back("");
2112 expected5.push_back("");
2113 expected5.push_back("");
2114 expected5.push_back("");
2115 const ST::string input4("----");
2116 EXPECT_EQ(expected5, input4.split('-'));
2117 EXPECT_EQ(expected5, input4.split('-', 4));
2118 EXPECT_EQ(expected5, input4.split('-', 10));
2119
2120 std::vector<ST::string> expected6;
2121 expected6.push_back("");
2122 expected6.push_back("");
2123 expected6.push_back("--");
2124 EXPECT_EQ(expected6, input4.split('-', 2));
2125
2126 // split never provides an empty vector, even for empty input
2127 std::vector<ST::string> expected9;
2128 expected9.push_back(ST::string());
2129 EXPECT_EQ(expected9, ST_LITERAL("").split('-'));
2130 EXPECT_EQ(expected9, ST_LITERAL("").split('-', 4));
2131 }
2132
TEST(string,fill)2133 TEST(string, fill)
2134 {
2135 EXPECT_EQ(ST_LITERAL(""), ST::string::fill(0, 'a'));
2136 EXPECT_EQ(ST_LITERAL("aaaaa"), ST::string::fill(5, 'a'));
2137 EXPECT_EQ(ST_LITERAL("aaaaaaaaaaaaaaaaaaaa"), ST::string::fill(20, 'a'));
2138 }
2139
TEST(string,iterators)2140 TEST(string, iterators)
2141 {
2142 ST::string source_short("X");
2143 ST::string source_long("The quick brown fox jumps over the lazy dog.");
2144 ST::string empty;
2145
2146 EXPECT_EQ('X', source_short.front());
2147 EXPECT_EQ('X', source_short.back());
2148 EXPECT_EQ('T', source_long.front());
2149 EXPECT_EQ('.', source_long.back());
2150 EXPECT_EQ('\0', empty.front());
2151 EXPECT_EQ('\0', empty.back());
2152
2153 EXPECT_NE(source_short.begin(), source_short.end());
2154 EXPECT_NE(source_short.rbegin(), source_short.rend());
2155 EXPECT_NE(source_long.begin(), source_long.end());
2156 EXPECT_NE(source_long.rbegin(), source_long.rend());
2157 EXPECT_EQ(empty.begin(), empty.end());
2158 EXPECT_EQ(empty.rbegin(), empty.rend());
2159
2160 std::string result;
2161 std::string forward = "The quick brown fox jumps over the lazy dog.";
2162 std::copy(source_long.begin(), source_long.end(), std::back_inserter(result));
2163 EXPECT_EQ(forward, result);
2164
2165 result.clear();
2166 std::string reverse = ".god yzal eht revo spmuj xof nworb kciuq ehT";
2167 std::copy(source_long.rbegin(), source_long.rend(), std::back_inserter(result));
2168 EXPECT_EQ(reverse, result);
2169
2170 result.clear();
2171 std::string x = "X";
2172 std::copy(source_short.begin(), source_short.end(), std::back_inserter(result));
2173 EXPECT_EQ(x, result);
2174
2175 result.clear();
2176 std::copy(source_short.rbegin(), source_short.rend(), std::back_inserter(result));
2177 EXPECT_EQ(x, result);
2178 }
2179
TEST(string,udls)2180 TEST(string, udls)
2181 {
2182 using namespace ST::literals;
2183
2184 // Only need to test the UDL usage -- the rest is covered above
2185 EXPECT_EQ(ST_LITERAL(""), ""_st);
2186 EXPECT_EQ(ST_LITERAL("Test"), "Test"_st);
2187 EXPECT_EQ(ST_LITERAL("Test"), L"Test"_st);
2188 EXPECT_EQ(ST_LITERAL("Test"), u"Test"_st);
2189 EXPECT_EQ(ST_LITERAL("Test"), U"Test"_st);
2190 #ifdef ST_HAVE_CXX20_CHAR8_TYPES
2191 EXPECT_EQ(ST_LITERAL("Test"), u8"Test"_st);
2192 #endif
2193 }
2194