OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <math.h> | 5 #include <math.h> |
6 #include <stdarg.h> | 6 #include <stdarg.h> |
7 | 7 |
8 #include <limits> | 8 #include <limits> |
9 #include <sstream> | 9 #include <sstream> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/utf_string_conversions.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 // Given a null-terminated string of wchar_t with each wchar_t representing | 20 // Given a null-terminated string of wchar_t with each wchar_t representing |
20 // a UTF-16 code unit, returns a string16 made up of wchar_t's in the input. | 21 // a UTF-16 code unit, returns a string16 made up of wchar_t's in the input. |
21 // Each wchar_t should be <= 0xFFFF and a non-BMP character (> U+FFFF) | 22 // Each wchar_t should be <= 0xFFFF and a non-BMP character (> U+FFFF) |
22 // should be represented as a surrogate pair (two UTF-16 units) | 23 // should be represented as a surrogate pair (two UTF-16 units) |
23 // *even* where wchar_t is 32-bit (Linux and Mac). | 24 // *even* where wchar_t is 32-bit (Linux and Mac). |
24 // | 25 // |
25 // This is to help write tests for functions with string16 params until | 26 // This is to help write tests for functions with string16 params until |
26 // the C++ 0x UTF-16 literal is well-supported by compilers. | 27 // the C++ 0x UTF-16 literal is well-supported by compilers. |
27 string16 BuildString16(const wchar_t* s) { | 28 string16 BuildString16(const wchar_t* s) { |
28 #if defined(WCHAR_T_IS_UTF16) | 29 #if defined(WCHAR_T_IS_UTF16) |
29 return string16(s); | 30 return string16(s); |
30 #elif defined(WCHAR_T_IS_UTF32) | 31 #elif defined(WCHAR_T_IS_UTF32) |
31 string16 u16; | 32 string16 u16; |
32 while (*s != 0) { | 33 while (*s != 0) { |
33 DCHECK(static_cast<unsigned int>(*s) <= 0xFFFFu); | 34 DCHECK_LE(static_cast<unsigned int>(*s), 0xFFFFu); |
34 u16.push_back(*s++); | 35 u16.push_back(*s++); |
35 } | 36 } |
36 return u16; | 37 return u16; |
37 #endif | 38 #endif |
38 } | 39 } |
39 | 40 |
40 } // namespace | 41 } // namespace |
41 | 42 |
42 static const struct trim_case { | 43 static const struct trim_case { |
43 const wchar_t* input; | 44 const wchar_t* input; |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 | 449 |
449 namespace { | 450 namespace { |
450 | 451 |
451 template <typename INT> | 452 template <typename INT> |
452 struct IntToStringTest { | 453 struct IntToStringTest { |
453 INT num; | 454 INT num; |
454 const char* sexpected; | 455 const char* sexpected; |
455 const char* uexpected; | 456 const char* uexpected; |
456 }; | 457 }; |
457 | 458 |
458 } | 459 } // namespace |
459 | 460 |
460 TEST(StringUtilTest, IntToString) { | 461 TEST(StringUtilTest, IntToString) { |
461 static const IntToStringTest<int> int_tests[] = { | 462 static const IntToStringTest<int> int_tests[] = { |
462 { 0, "0", "0" }, | 463 { 0, "0", "0" }, |
463 { -1, "-1", "4294967295" }, | 464 { -1, "-1", "4294967295" }, |
464 { std::numeric_limits<int>::max(), "2147483647", "2147483647" }, | 465 { std::numeric_limits<int>::max(), "2147483647", "2147483647" }, |
465 { std::numeric_limits<int>::min(), "-2147483648", "2147483648" }, | 466 { std::numeric_limits<int>::min(), "-2147483648", "2147483648" }, |
466 }; | 467 }; |
467 static const IntToStringTest<int64> int64_tests[] = { | 468 static const IntToStringTest<int64> int64_tests[] = { |
468 { 0, "0", "0" }, | 469 { 0, "0", "0" }, |
(...skipping 1015 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1484 EXPECT_TRUE(ContainsOnlyChars("", "")); | 1485 EXPECT_TRUE(ContainsOnlyChars("", "")); |
1485 EXPECT_FALSE(ContainsOnlyChars("Hello", "")); | 1486 EXPECT_FALSE(ContainsOnlyChars("Hello", "")); |
1486 | 1487 |
1487 EXPECT_TRUE(ContainsOnlyChars("", "1234")); | 1488 EXPECT_TRUE(ContainsOnlyChars("", "1234")); |
1488 EXPECT_TRUE(ContainsOnlyChars("1", "1234")); | 1489 EXPECT_TRUE(ContainsOnlyChars("1", "1234")); |
1489 EXPECT_TRUE(ContainsOnlyChars("1", "4321")); | 1490 EXPECT_TRUE(ContainsOnlyChars("1", "4321")); |
1490 EXPECT_TRUE(ContainsOnlyChars("123", "4321")); | 1491 EXPECT_TRUE(ContainsOnlyChars("123", "4321")); |
1491 EXPECT_FALSE(ContainsOnlyChars("123a", "4321")); | 1492 EXPECT_FALSE(ContainsOnlyChars("123a", "4321")); |
1492 } | 1493 } |
1493 | 1494 |
1494 } // namaspace base | 1495 } // base |
OLD | NEW |