OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
9 #include "base/strings/utf_offset_string_conversions.h" | 9 #include "base/strings/utf_offset_string_conversions.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 static const size_t kNpos = string16::npos; | 16 static const size_t kNpos = string16::npos; |
17 | 17 |
18 } // namespace | 18 } // namespace |
19 | 19 |
20 TEST(UTFOffsetStringConversionsTest, AdjustOffset) { | 20 TEST(UTFOffsetStringConversionsTest, AdjustOffset) { |
21 struct UTF8ToUTF16Case { | 21 struct UTF8ToUTF16Case { |
22 const char* utf8; | 22 const char* utf8; |
23 size_t input_offset; | 23 size_t input_offset; |
24 size_t output_offset; | 24 size_t output_offset; |
25 } utf8_to_utf16_cases[] = { | 25 } utf8_to_utf16_cases[] = { |
26 {"", 0, kNpos}, | 26 {"", 0, 0}, |
| 27 {"", kNpos, kNpos}, |
27 {"\xe4\xbd\xa0\xe5\xa5\xbd", 1, kNpos}, | 28 {"\xe4\xbd\xa0\xe5\xa5\xbd", 1, kNpos}, |
28 {"\xe4\xbd\xa0\xe5\xa5\xbd", 3, 1}, | 29 {"\xe4\xbd\xa0\xe5\xa5\xbd", 3, 1}, |
29 {"\xed\xb0\x80z", 3, 1}, | 30 {"\xed\xb0\x80z", 3, 1}, |
30 {"A\xF0\x90\x8C\x80z", 1, 1}, | 31 {"A\xF0\x90\x8C\x80z", 1, 1}, |
31 {"A\xF0\x90\x8C\x80z", 2, kNpos}, | 32 {"A\xF0\x90\x8C\x80z", 2, kNpos}, |
32 {"A\xF0\x90\x8C\x80z", 5, 3}, | 33 {"A\xF0\x90\x8C\x80z", 5, 3}, |
| 34 {"A\xF0\x90\x8C\x80z", 6, 4}, |
| 35 {"A\xF0\x90\x8C\x80z", kNpos, kNpos}, |
33 }; | 36 }; |
34 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf8_to_utf16_cases); ++i) { | 37 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf8_to_utf16_cases); ++i) { |
35 size_t offset = utf8_to_utf16_cases[i].input_offset; | 38 size_t offset = utf8_to_utf16_cases[i].input_offset; |
36 UTF8ToUTF16AndAdjustOffset(utf8_to_utf16_cases[i].utf8, &offset); | 39 UTF8ToUTF16AndAdjustOffset(utf8_to_utf16_cases[i].utf8, &offset); |
37 EXPECT_EQ(utf8_to_utf16_cases[i].output_offset, offset); | 40 EXPECT_EQ(utf8_to_utf16_cases[i].output_offset, offset); |
38 } | 41 } |
39 | 42 |
40 struct UTF16ToUTF8Case { | 43 struct UTF16ToUTF8Case { |
41 char16 utf16[10]; | 44 char16 utf16[10]; |
42 size_t input_offset; | 45 size_t input_offset; |
43 size_t output_offset; | 46 size_t output_offset; |
44 } utf16_to_utf8_cases[] = { | 47 } utf16_to_utf8_cases[] = { |
45 {{}, 0, kNpos}, | 48 {{}, 0, 0}, |
46 // Converted to 3-byte utf-8 sequences | 49 // Converted to 3-byte utf-8 sequences |
47 {{0x5909, 0x63DB}, 2, kNpos}, | 50 {{0x5909, 0x63DB}, 3, kNpos}, |
| 51 {{0x5909, 0x63DB}, 2, 6}, |
48 {{0x5909, 0x63DB}, 1, 3}, | 52 {{0x5909, 0x63DB}, 1, 3}, |
| 53 {{0x5909, 0x63DB}, 0, 0}, |
49 // Converted to 2-byte utf-8 sequences | 54 // Converted to 2-byte utf-8 sequences |
50 {{'A', 0x00bc, 0x00be, 'z'}, 1, 1}, | 55 {{'A', 0x00bc, 0x00be, 'z'}, 1, 1}, |
51 {{'A', 0x00bc, 0x00be, 'z'}, 2, 3}, | 56 {{'A', 0x00bc, 0x00be, 'z'}, 2, 3}, |
52 {{'A', 0x00bc, 0x00be, 'z'}, 3, 5}, | 57 {{'A', 0x00bc, 0x00be, 'z'}, 3, 5}, |
| 58 {{'A', 0x00bc, 0x00be, 'z'}, 4, 6}, |
53 // Surrogate pair | 59 // Surrogate pair |
54 {{'A', 0xd800, 0xdf00, 'z'}, 1, 1}, | 60 {{'A', 0xd800, 0xdf00, 'z'}, 1, 1}, |
55 {{'A', 0xd800, 0xdf00, 'z'}, 2, kNpos}, | 61 {{'A', 0xd800, 0xdf00, 'z'}, 2, kNpos}, |
56 {{'A', 0xd800, 0xdf00, 'z'}, 3, 5}, | 62 {{'A', 0xd800, 0xdf00, 'z'}, 3, 5}, |
| 63 {{'A', 0xd800, 0xdf00, 'z'}, 4, 6}, |
57 }; | 64 }; |
58 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf16_to_utf8_cases); ++i) { | 65 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf16_to_utf8_cases); ++i) { |
59 size_t offset = utf16_to_utf8_cases[i].input_offset; | 66 size_t offset = utf16_to_utf8_cases[i].input_offset; |
60 UTF16ToUTF8AndAdjustOffset(utf16_to_utf8_cases[i].utf16, &offset); | 67 UTF16ToUTF8AndAdjustOffset(utf16_to_utf8_cases[i].utf16, &offset); |
61 EXPECT_EQ(utf16_to_utf8_cases[i].output_offset, offset); | 68 EXPECT_EQ(utf16_to_utf8_cases[i].output_offset, offset); |
62 } | 69 } |
63 } | 70 } |
64 | 71 |
65 TEST(UTFOffsetStringConversionsTest, LimitOffsets) { | 72 TEST(UTFOffsetStringConversionsTest, LimitOffsets) { |
66 const size_t kLimit = 10; | 73 const size_t kLimit = 10; |
67 const size_t kItems = 20; | 74 const size_t kItems = 20; |
68 std::vector<size_t> size_ts; | 75 std::vector<size_t> size_ts; |
69 for (size_t t = 0; t < kItems; ++t) | 76 for (size_t t = 0; t < kItems; ++t) |
70 size_ts.push_back(t); | 77 size_ts.push_back(t); |
71 std::for_each(size_ts.begin(), size_ts.end(), | 78 std::for_each(size_ts.begin(), size_ts.end(), |
72 LimitOffset<string16>(kLimit)); | 79 LimitOffset<string16>(kLimit)); |
73 size_t unlimited_count = 0; | 80 size_t unlimited_count = 0; |
74 for (std::vector<size_t>::iterator ti = size_ts.begin(); ti != size_ts.end(); | 81 for (std::vector<size_t>::iterator ti = size_ts.begin(); ti != size_ts.end(); |
75 ++ti) { | 82 ++ti) { |
76 if (*ti < kLimit && *ti != kNpos) | 83 if (*ti != kNpos) |
77 ++unlimited_count; | 84 ++unlimited_count; |
78 } | 85 } |
79 EXPECT_EQ(10U, unlimited_count); | 86 EXPECT_EQ(11U, unlimited_count); |
80 | 87 |
81 // Reverse the values in the vector and try again. | 88 // Reverse the values in the vector and try again. |
82 size_ts.clear(); | 89 size_ts.clear(); |
83 for (size_t t = kItems; t > 0; --t) | 90 for (size_t t = kItems; t > 0; --t) |
84 size_ts.push_back(t - 1); | 91 size_ts.push_back(t - 1); |
85 std::for_each(size_ts.begin(), size_ts.end(), | 92 std::for_each(size_ts.begin(), size_ts.end(), |
86 LimitOffset<string16>(kLimit)); | 93 LimitOffset<string16>(kLimit)); |
87 unlimited_count = 0; | 94 unlimited_count = 0; |
88 for (std::vector<size_t>::iterator ti = size_ts.begin(); ti != size_ts.end(); | 95 for (std::vector<size_t>::iterator ti = size_ts.begin(); ti != size_ts.end(); |
89 ++ti) { | 96 ++ti) { |
90 if (*ti < kLimit && *ti != kNpos) | 97 if (*ti != kNpos) |
91 ++unlimited_count; | 98 ++unlimited_count; |
92 } | 99 } |
93 EXPECT_EQ(10U, unlimited_count); | 100 EXPECT_EQ(11U, unlimited_count); |
94 } | 101 } |
95 | 102 |
96 TEST(UTFOffsetStringConversionsTest, AdjustOffsets) { | 103 TEST(UTFOffsetStringConversionsTest, AdjustOffsets) { |
97 // Imagine we have strings as shown in the following cases where the | 104 // Imagine we have strings as shown in the following cases where the |
98 // X's represent encoded characters. | 105 // X's represent encoded characters. |
99 // 1: abcXXXdef ==> abcXdef | 106 // 1: abcXXXdef ==> abcXdef |
100 { | 107 { |
101 std::vector<size_t> offsets; | 108 std::vector<size_t> offsets; |
102 for (size_t t = 0; t < 9; ++t) | 109 for (size_t t = 0; t <= 9; ++t) |
103 offsets.push_back(t); | 110 offsets.push_back(t); |
104 { | 111 { |
105 OffsetAdjuster offset_adjuster(&offsets); | 112 OffsetAdjuster offset_adjuster(&offsets); |
106 offset_adjuster.Add(OffsetAdjuster::Adjustment(3, 3, 1)); | 113 offset_adjuster.Add(OffsetAdjuster::Adjustment(3, 3, 1)); |
107 } | 114 } |
108 size_t expected_1[] = {0, 1, 2, 3, kNpos, kNpos, 4, 5, 6}; | 115 size_t expected_1[] = {0, 1, 2, 3, kNpos, kNpos, 4, 5, 6, 7}; |
109 EXPECT_EQ(offsets.size(), arraysize(expected_1)); | 116 EXPECT_EQ(offsets.size(), arraysize(expected_1)); |
110 for (size_t i = 0; i < arraysize(expected_1); ++i) | 117 for (size_t i = 0; i < arraysize(expected_1); ++i) |
111 EXPECT_EQ(expected_1[i], offsets[i]); | 118 EXPECT_EQ(expected_1[i], offsets[i]); |
112 } | 119 } |
113 | 120 |
114 // 2: XXXaXXXXbcXXXXXXXdefXXX ==> XaXXbcXXXXdefX | 121 // 2: XXXaXXXXbcXXXXXXXdefXXX ==> XaXXbcXXXXdefX |
115 { | 122 { |
116 std::vector<size_t> offsets; | 123 std::vector<size_t> offsets; |
117 for (size_t t = 0; t < 23; ++t) | 124 for (size_t t = 0; t <= 23; ++t) |
118 offsets.push_back(t); | 125 offsets.push_back(t); |
119 { | 126 { |
120 OffsetAdjuster offset_adjuster(&offsets); | 127 OffsetAdjuster offset_adjuster(&offsets); |
121 offset_adjuster.Add(OffsetAdjuster::Adjustment(0, 3, 1)); | 128 offset_adjuster.Add(OffsetAdjuster::Adjustment(0, 3, 1)); |
122 offset_adjuster.Add(OffsetAdjuster::Adjustment(4, 4, 2)); | 129 offset_adjuster.Add(OffsetAdjuster::Adjustment(4, 4, 2)); |
123 offset_adjuster.Add(OffsetAdjuster::Adjustment(10, 7, 4)); | 130 offset_adjuster.Add(OffsetAdjuster::Adjustment(10, 7, 4)); |
124 offset_adjuster.Add(OffsetAdjuster::Adjustment(20, 3, 1)); | 131 offset_adjuster.Add(OffsetAdjuster::Adjustment(20, 3, 1)); |
125 } | 132 } |
126 size_t expected_2[] = {0, kNpos, kNpos, 1, 2, kNpos, kNpos, kNpos, 4, 5, 6, | 133 size_t expected_2[] = { |
127 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 10, 11, 12, | 134 0, kNpos, kNpos, 1, 2, kNpos, kNpos, kNpos, 4, 5, 6, kNpos, kNpos, kNpos, |
128 13, kNpos, kNpos}; | 135 kNpos, kNpos, kNpos, 10, 11, 12, 13, kNpos, kNpos, 14 |
| 136 }; |
129 EXPECT_EQ(offsets.size(), arraysize(expected_2)); | 137 EXPECT_EQ(offsets.size(), arraysize(expected_2)); |
130 for (size_t i = 0; i < arraysize(expected_2); ++i) | 138 for (size_t i = 0; i < arraysize(expected_2); ++i) |
131 EXPECT_EQ(expected_2[i], offsets[i]); | 139 EXPECT_EQ(expected_2[i], offsets[i]); |
132 } | 140 } |
133 | 141 |
134 // 3: XXXaXXXXbcdXXXeXX ==> aXXXXbcdXXXe | 142 // 3: XXXaXXXXbcdXXXeXX ==> aXXXXbcdXXXe |
135 { | 143 { |
136 std::vector<size_t> offsets; | 144 std::vector<size_t> offsets; |
137 for (size_t t = 0; t < 17; ++t) | 145 for (size_t t = 0; t <= 17; ++t) |
138 offsets.push_back(t); | 146 offsets.push_back(t); |
139 { | 147 { |
140 OffsetAdjuster offset_adjuster(&offsets); | 148 OffsetAdjuster offset_adjuster(&offsets); |
141 offset_adjuster.Add(OffsetAdjuster::Adjustment(0, 3, 0)); | 149 offset_adjuster.Add(OffsetAdjuster::Adjustment(0, 3, 0)); |
142 offset_adjuster.Add(OffsetAdjuster::Adjustment(4, 4, 4)); | 150 offset_adjuster.Add(OffsetAdjuster::Adjustment(4, 4, 4)); |
143 offset_adjuster.Add(OffsetAdjuster::Adjustment(11, 3, 3)); | 151 offset_adjuster.Add(OffsetAdjuster::Adjustment(11, 3, 3)); |
144 offset_adjuster.Add(OffsetAdjuster::Adjustment(15, 2, 0)); | 152 offset_adjuster.Add(OffsetAdjuster::Adjustment(15, 2, 0)); |
145 } | 153 } |
146 size_t expected_3[] = {kNpos, kNpos, kNpos, 0, 1, kNpos, kNpos, kNpos, 5, 6, | 154 size_t expected_3[] = { |
147 7, 8, kNpos, kNpos, 11, kNpos, kNpos}; | 155 0, kNpos, kNpos, 0, 1, kNpos, kNpos, kNpos, 5, 6, 7, 8, kNpos, kNpos, 11, |
| 156 12, kNpos, 12 |
| 157 }; |
148 EXPECT_EQ(offsets.size(), arraysize(expected_3)); | 158 EXPECT_EQ(offsets.size(), arraysize(expected_3)); |
149 for (size_t i = 0; i < arraysize(expected_3); ++i) | 159 for (size_t i = 0; i < arraysize(expected_3); ++i) |
150 EXPECT_EQ(expected_3[i], offsets[i]); | 160 EXPECT_EQ(expected_3[i], offsets[i]); |
151 } | 161 } |
152 } | 162 } |
153 | 163 |
154 } // namaspace base | 164 } // namaspace base |
OLD | NEW |