OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 } trim_cases_ascii[] = { | 42 } trim_cases_ascii[] = { |
43 {" Google Video ", TRIM_LEADING, "Google Video ", TRIM_LEADING}, | 43 {" Google Video ", TRIM_LEADING, "Google Video ", TRIM_LEADING}, |
44 {" Google Video ", TRIM_TRAILING, " Google Video", TRIM_TRAILING}, | 44 {" Google Video ", TRIM_TRAILING, " Google Video", TRIM_TRAILING}, |
45 {" Google Video ", TRIM_ALL, "Google Video", TRIM_ALL}, | 45 {" Google Video ", TRIM_ALL, "Google Video", TRIM_ALL}, |
46 {"Google Video", TRIM_ALL, "Google Video", TRIM_NONE}, | 46 {"Google Video", TRIM_ALL, "Google Video", TRIM_NONE}, |
47 {"", TRIM_ALL, "", TRIM_NONE}, | 47 {"", TRIM_ALL, "", TRIM_NONE}, |
48 {" ", TRIM_LEADING, "", TRIM_LEADING}, | 48 {" ", TRIM_LEADING, "", TRIM_LEADING}, |
49 {" ", TRIM_TRAILING, "", TRIM_TRAILING}, | 49 {" ", TRIM_TRAILING, "", TRIM_TRAILING}, |
50 {" ", TRIM_ALL, "", TRIM_ALL}, | 50 {" ", TRIM_ALL, "", TRIM_ALL}, |
51 {"\t\rTest String\n", TRIM_ALL, "Test String", TRIM_ALL}, | 51 {"\t\rTest String\n", TRIM_ALL, "Test String", TRIM_ALL}, |
52 {"\x85Test String\xa0\x20", TRIM_ALL, "Test String", TRIM_ALL}, | |
53 }; | 52 }; |
54 | 53 |
55 TEST(StringUtilTest, TrimWhitespace) { | 54 TEST(StringUtilTest, TrimWhitespace) { |
56 std::wstring output; // Allow contents to carry over to next testcase | 55 std::wstring output; // Allow contents to carry over to next testcase |
57 for (size_t i = 0; i < arraysize(trim_cases); ++i) { | 56 for (size_t i = 0; i < arraysize(trim_cases); ++i) { |
58 const trim_case& value = trim_cases[i]; | 57 const trim_case& value = trim_cases[i]; |
59 EXPECT_EQ(value.return_value, | 58 EXPECT_EQ(value.return_value, |
60 TrimWhitespace(value.input, value.positions, &output)); | 59 TrimWhitespace(value.input, value.positions, &output)); |
61 EXPECT_EQ(value.output, output); | 60 EXPECT_EQ(value.output, output); |
62 } | 61 } |
(...skipping 10 matching lines...) Expand all Loading... |
73 | 72 |
74 std::string output_ascii; | 73 std::string output_ascii; |
75 for (size_t i = 0; i < arraysize(trim_cases_ascii); ++i) { | 74 for (size_t i = 0; i < arraysize(trim_cases_ascii); ++i) { |
76 const trim_case_ascii& value = trim_cases_ascii[i]; | 75 const trim_case_ascii& value = trim_cases_ascii[i]; |
77 EXPECT_EQ(value.return_value, | 76 EXPECT_EQ(value.return_value, |
78 TrimWhitespace(value.input, value.positions, &output_ascii)); | 77 TrimWhitespace(value.input, value.positions, &output_ascii)); |
79 EXPECT_EQ(value.output, output_ascii); | 78 EXPECT_EQ(value.output, output_ascii); |
80 } | 79 } |
81 } | 80 } |
82 | 81 |
| 82 static const struct trim_case_utf8 { |
| 83 const char* input; |
| 84 const TrimPositions positions; |
| 85 const char* output; |
| 86 const TrimPositions return_value; |
| 87 } trim_cases_utf8[] = { |
| 88 // UTF-8 strings that start (and end) with Unicode space characters |
| 89 // (including zero-width spaces). |
| 90 {"\xE2\x80\x80Test String\xE2\x80\x81", TRIM_ALL, "Test String", TRIM_ALL}, |
| 91 {"\xE2\x80\x82Test String\xE2\x80\x83", TRIM_ALL, "Test String", TRIM_ALL}, |
| 92 {"\xE2\x80\x84Test String\xE2\x80\x85", TRIM_ALL, "Test String", TRIM_ALL}, |
| 93 {"\xE2\x80\x86Test String\xE2\x80\x87", TRIM_ALL, "Test String", TRIM_ALL}, |
| 94 {"\xE2\x80\x88Test String\xE2\x80\x8A", TRIM_ALL, "Test String", TRIM_ALL}, |
| 95 {"\xE3\x80\x80Test String\xE3\x80\x80", TRIM_ALL, "Test String", TRIM_ALL}, |
| 96 // UTF-8 strings that end with 0x85 (NEL in ISO-8859). |
| 97 {"\xD0\x85", TRIM_TRAILING, "\xD0\x85", TRIM_NONE}, |
| 98 {"\xD9\x85", TRIM_TRAILING, "\xD9\x85", TRIM_NONE}, |
| 99 {"\xEC\x97\x85", TRIM_TRAILING, "\xEC\x97\x85", TRIM_NONE}, |
| 100 {"\xF0\x90\x80\x85", TRIM_TRAILING, "\xF0\x90\x80\x85", TRIM_NONE}, |
| 101 // UTF-8 strings that end with 0xA0 (non-break space in ISO-8859-1). |
| 102 {"\xD0\xA0", TRIM_TRAILING, "\xD0\xA0", TRIM_NONE}, |
| 103 {"\xD9\xA0", TRIM_TRAILING, "\xD9\xA0", TRIM_NONE}, |
| 104 {"\xEC\x97\xA0", TRIM_TRAILING, "\xEC\x97\xA0", TRIM_NONE}, |
| 105 {"\xF0\x90\x80\xA0", TRIM_TRAILING, "\xF0\x90\x80\xA0", TRIM_NONE}, |
| 106 }; |
| 107 |
| 108 TEST(StringUtilTest, TrimWhitespaceUTF8) { |
| 109 std::string output_ascii; |
| 110 for (size_t i = 0; i < arraysize(trim_cases_ascii); ++i) { |
| 111 const trim_case_ascii& value = trim_cases_ascii[i]; |
| 112 EXPECT_EQ(value.return_value, |
| 113 TrimWhitespaceASCII(value.input, value.positions, &output_ascii)); |
| 114 EXPECT_EQ(value.output, output_ascii); |
| 115 } |
| 116 |
| 117 // Test that TrimWhiteSpaceUTF8() can remove Unicode space characters and |
| 118 // prevent from removing UTF-8 characters that end with an ISO-8859 NEL. |
| 119 std::string output_utf8; |
| 120 for (size_t i = 0; i < arraysize(trim_cases_utf8); ++i) { |
| 121 const trim_case_utf8& value = trim_cases_utf8[i]; |
| 122 EXPECT_EQ(value.return_value, |
| 123 TrimWhitespaceUTF8(value.input, value.positions, &output_utf8)); |
| 124 EXPECT_EQ(value.output, output_utf8); |
| 125 } |
| 126 } |
| 127 |
83 static const struct collapse_case { | 128 static const struct collapse_case { |
84 const wchar_t* input; | 129 const wchar_t* input; |
85 const bool trim; | 130 const bool trim; |
86 const wchar_t* output; | 131 const wchar_t* output; |
87 } collapse_cases[] = { | 132 } collapse_cases[] = { |
88 {L" Google Video ", false, L"Google Video"}, | 133 {L" Google Video ", false, L"Google Video"}, |
89 {L"Google Video", false, L"Google Video"}, | 134 {L"Google Video", false, L"Google Video"}, |
90 {L"", false, L""}, | 135 {L"", false, L""}, |
91 {L" ", false, L""}, | 136 {L" ", false, L""}, |
92 {L"\t\rTest String\n", false, L"Test String"}, | 137 {L"\t\rTest String\n", false, L"Test String"}, |
(...skipping 1429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1522 } | 1567 } |
1523 } | 1568 } |
1524 | 1569 |
1525 TEST(StringUtilTest, HexEncode) { | 1570 TEST(StringUtilTest, HexEncode) { |
1526 std::string hex(HexEncode(NULL, 0)); | 1571 std::string hex(HexEncode(NULL, 0)); |
1527 EXPECT_EQ(hex.length(), 0U); | 1572 EXPECT_EQ(hex.length(), 0U); |
1528 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 1573 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
1529 hex = HexEncode(bytes, sizeof(bytes)); | 1574 hex = HexEncode(bytes, sizeof(bytes)); |
1530 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 1575 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
1531 } | 1576 } |
OLD | NEW |