[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 5 | #include <algorithm> |
| 6 | |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 8 | #include "base/strings/string_piece.h" |
[email protected] | a3f72189 | 2013-02-07 03:59:06 | [diff] [blame] | 9 | #include "base/strings/utf_offset_string_conversions.h" |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace base { |
| 13 | |
| 14 | namespace { |
| 15 | |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 16 | static const size_t kNpos = string16::npos; |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 17 | |
| 18 | } // namespace |
| 19 | |
| 20 | TEST(UTFOffsetStringConversionsTest, AdjustOffset) { |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 21 | struct UTF8ToUTF16Case { |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 22 | const char* utf8; |
| 23 | size_t input_offset; |
| 24 | size_t output_offset; |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 25 | } utf8_to_utf16_cases[] = { |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 26 | {"", 0, 0}, |
| 27 | {"", kNpos, kNpos}, |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 28 | {"\xe4\xbd\xa0\xe5\xa5\xbd", 1, kNpos}, |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 29 | {"\xe4\xbd\xa0\xe5\xa5\xbd", 3, 1}, |
[email protected] | d7a3e8e | 2010-01-01 22:16:38 | [diff] [blame] | 30 | {"\xed\xb0\x80z", 3, 1}, |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 31 | {"A\xF0\x90\x8C\x80z", 1, 1}, |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 32 | {"A\xF0\x90\x8C\x80z", 2, kNpos}, |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 33 | {"A\xF0\x90\x8C\x80z", 5, 3}, |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 34 | {"A\xF0\x90\x8C\x80z", 6, 4}, |
| 35 | {"A\xF0\x90\x8C\x80z", kNpos, kNpos}, |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 36 | }; |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 37 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf8_to_utf16_cases); ++i) { |
[email protected] | a97376e | 2014-04-18 20:54:44 | [diff] [blame^] | 38 | const size_t offset = utf8_to_utf16_cases[i].input_offset; |
| 39 | std::vector<size_t> offsets; |
| 40 | offsets.push_back(offset); |
| 41 | UTF8ToUTF16AndAdjustOffsets(utf8_to_utf16_cases[i].utf8, &offsets); |
| 42 | EXPECT_EQ(utf8_to_utf16_cases[i].output_offset, offsets[0]); |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 43 | } |
[email protected] | cbf35e17 | 2011-09-08 02:18:10 | [diff] [blame] | 44 | |
| 45 | struct UTF16ToUTF8Case { |
| 46 | char16 utf16[10]; |
| 47 | size_t input_offset; |
| 48 | size_t output_offset; |
| 49 | } utf16_to_utf8_cases[] = { |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 50 | {{}, 0, 0}, |
[email protected] | cbf35e17 | 2011-09-08 02:18:10 | [diff] [blame] | 51 | // Converted to 3-byte utf-8 sequences |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 52 | {{0x5909, 0x63DB}, 3, kNpos}, |
| 53 | {{0x5909, 0x63DB}, 2, 6}, |
[email protected] | cbf35e17 | 2011-09-08 02:18:10 | [diff] [blame] | 54 | {{0x5909, 0x63DB}, 1, 3}, |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 55 | {{0x5909, 0x63DB}, 0, 0}, |
[email protected] | cbf35e17 | 2011-09-08 02:18:10 | [diff] [blame] | 56 | // Converted to 2-byte utf-8 sequences |
| 57 | {{'A', 0x00bc, 0x00be, 'z'}, 1, 1}, |
| 58 | {{'A', 0x00bc, 0x00be, 'z'}, 2, 3}, |
| 59 | {{'A', 0x00bc, 0x00be, 'z'}, 3, 5}, |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 60 | {{'A', 0x00bc, 0x00be, 'z'}, 4, 6}, |
[email protected] | cbf35e17 | 2011-09-08 02:18:10 | [diff] [blame] | 61 | // Surrogate pair |
| 62 | {{'A', 0xd800, 0xdf00, 'z'}, 1, 1}, |
| 63 | {{'A', 0xd800, 0xdf00, 'z'}, 2, kNpos}, |
| 64 | {{'A', 0xd800, 0xdf00, 'z'}, 3, 5}, |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 65 | {{'A', 0xd800, 0xdf00, 'z'}, 4, 6}, |
[email protected] | cbf35e17 | 2011-09-08 02:18:10 | [diff] [blame] | 66 | }; |
| 67 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf16_to_utf8_cases); ++i) { |
| 68 | size_t offset = utf16_to_utf8_cases[i].input_offset; |
[email protected] | a97376e | 2014-04-18 20:54:44 | [diff] [blame^] | 69 | std::vector<size_t> offsets; |
| 70 | offsets.push_back(offset); |
| 71 | UTF16ToUTF8AndAdjustOffsets(utf16_to_utf8_cases[i].utf16, &offsets); |
| 72 | EXPECT_EQ(utf16_to_utf8_cases[i].output_offset, offsets[0]) << i; |
[email protected] | cbf35e17 | 2011-09-08 02:18:10 | [diff] [blame] | 73 | } |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 76 | TEST(UTFOffsetStringConversionsTest, LimitOffsets) { |
| 77 | const size_t kLimit = 10; |
| 78 | const size_t kItems = 20; |
| 79 | std::vector<size_t> size_ts; |
| 80 | for (size_t t = 0; t < kItems; ++t) |
| 81 | size_ts.push_back(t); |
| 82 | std::for_each(size_ts.begin(), size_ts.end(), |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 83 | LimitOffset<string16>(kLimit)); |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 84 | size_t unlimited_count = 0; |
| 85 | for (std::vector<size_t>::iterator ti = size_ts.begin(); ti != size_ts.end(); |
| 86 | ++ti) { |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 87 | if (*ti != kNpos) |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 88 | ++unlimited_count; |
| 89 | } |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 90 | EXPECT_EQ(11U, unlimited_count); |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 91 | |
| 92 | // Reverse the values in the vector and try again. |
| 93 | size_ts.clear(); |
| 94 | for (size_t t = kItems; t > 0; --t) |
| 95 | size_ts.push_back(t - 1); |
| 96 | std::for_each(size_ts.begin(), size_ts.end(), |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 97 | LimitOffset<string16>(kLimit)); |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 98 | unlimited_count = 0; |
| 99 | for (std::vector<size_t>::iterator ti = size_ts.begin(); ti != size_ts.end(); |
| 100 | ++ti) { |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 101 | if (*ti != kNpos) |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 102 | ++unlimited_count; |
| 103 | } |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 104 | EXPECT_EQ(11U, unlimited_count); |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | TEST(UTFOffsetStringConversionsTest, AdjustOffsets) { |
| 108 | // Imagine we have strings as shown in the following cases where the |
| 109 | // X's represent encoded characters. |
| 110 | // 1: abcXXXdef ==> abcXdef |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 111 | { |
| 112 | std::vector<size_t> offsets; |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 113 | for (size_t t = 0; t <= 9; ++t) |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 114 | offsets.push_back(t); |
[email protected] | a97376e | 2014-04-18 20:54:44 | [diff] [blame^] | 115 | OffsetAdjuster::Adjustments adjustments; |
| 116 | adjustments.push_back(OffsetAdjuster::Adjustment(3, 3, 1)); |
| 117 | OffsetAdjuster::AdjustOffsets(adjustments, &offsets); |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 118 | size_t expected_1[] = {0, 1, 2, 3, kNpos, kNpos, 4, 5, 6, 7}; |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 119 | EXPECT_EQ(offsets.size(), arraysize(expected_1)); |
| 120 | for (size_t i = 0; i < arraysize(expected_1); ++i) |
| 121 | EXPECT_EQ(expected_1[i], offsets[i]); |
| 122 | } |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 123 | |
| 124 | // 2: XXXaXXXXbcXXXXXXXdefXXX ==> XaXXbcXXXXdefX |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 125 | { |
| 126 | std::vector<size_t> offsets; |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 127 | for (size_t t = 0; t <= 23; ++t) |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 128 | offsets.push_back(t); |
[email protected] | a97376e | 2014-04-18 20:54:44 | [diff] [blame^] | 129 | OffsetAdjuster::Adjustments adjustments; |
| 130 | adjustments.push_back(OffsetAdjuster::Adjustment(0, 3, 1)); |
| 131 | adjustments.push_back(OffsetAdjuster::Adjustment(4, 4, 2)); |
| 132 | adjustments.push_back(OffsetAdjuster::Adjustment(10, 7, 4)); |
| 133 | adjustments.push_back(OffsetAdjuster::Adjustment(20, 3, 1)); |
| 134 | OffsetAdjuster::AdjustOffsets(adjustments, &offsets); |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 135 | size_t expected_2[] = { |
| 136 | 0, kNpos, kNpos, 1, 2, kNpos, kNpos, kNpos, 4, 5, 6, kNpos, kNpos, kNpos, |
| 137 | kNpos, kNpos, kNpos, 10, 11, 12, 13, kNpos, kNpos, 14 |
| 138 | }; |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 139 | EXPECT_EQ(offsets.size(), arraysize(expected_2)); |
| 140 | for (size_t i = 0; i < arraysize(expected_2); ++i) |
| 141 | EXPECT_EQ(expected_2[i], offsets[i]); |
| 142 | } |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 143 | |
| 144 | // 3: XXXaXXXXbcdXXXeXX ==> aXXXXbcdXXXe |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 145 | { |
| 146 | std::vector<size_t> offsets; |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 147 | for (size_t t = 0; t <= 17; ++t) |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 148 | offsets.push_back(t); |
[email protected] | a97376e | 2014-04-18 20:54:44 | [diff] [blame^] | 149 | OffsetAdjuster::Adjustments adjustments; |
| 150 | adjustments.push_back(OffsetAdjuster::Adjustment(0, 3, 0)); |
| 151 | adjustments.push_back(OffsetAdjuster::Adjustment(4, 4, 4)); |
| 152 | adjustments.push_back(OffsetAdjuster::Adjustment(11, 3, 3)); |
| 153 | adjustments.push_back(OffsetAdjuster::Adjustment(15, 2, 0)); |
| 154 | OffsetAdjuster::AdjustOffsets(adjustments, &offsets); |
[email protected] | cf7ca8a | 2013-09-11 00:42:28 | [diff] [blame] | 155 | size_t expected_3[] = { |
| 156 | 0, kNpos, kNpos, 0, 1, kNpos, kNpos, kNpos, 5, 6, 7, 8, kNpos, kNpos, 11, |
| 157 | 12, kNpos, 12 |
| 158 | }; |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 159 | EXPECT_EQ(offsets.size(), arraysize(expected_3)); |
| 160 | for (size_t i = 0; i < arraysize(expected_3); ++i) |
| 161 | EXPECT_EQ(expected_3[i], offsets[i]); |
| 162 | } |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | a97376e | 2014-04-18 20:54:44 | [diff] [blame^] | 165 | // MergeSequentialAdjustments is used by net/base/escape.{h,cc} and |
| 166 | // net/base/net_util.{h,cc}. The two tests EscapeTest.AdjustOffset and |
| 167 | // NetUtilTest.FormatUrlWithOffsets test its behavior extensively. This |
| 168 | // is simply a short, additional test. |
| 169 | TEST(UTFOffsetStringConversionsTest, MergeSequentialAdjustments) { |
| 170 | // Pretend the input string is "abcdefghijklmnopqrstuvwxyz". |
| 171 | |
| 172 | // Set up |first_adjustments| to |
| 173 | // - remove the leading "a" |
| 174 | // - combine the "bc" into one character (call it ".") |
| 175 | // - remove the "f" |
| 176 | // - remove the "tuv" |
| 177 | // The resulting string should be ".deghijklmnopqrswxyz". |
| 178 | OffsetAdjuster::Adjustments first_adjustments; |
| 179 | first_adjustments.push_back(OffsetAdjuster::Adjustment(0, 1, 0)); |
| 180 | first_adjustments.push_back(OffsetAdjuster::Adjustment(1, 2, 1)); |
| 181 | first_adjustments.push_back(OffsetAdjuster::Adjustment(5, 1, 0)); |
| 182 | first_adjustments.push_back(OffsetAdjuster::Adjustment(19, 3, 0)); |
| 183 | |
| 184 | // Set up |adjustments_on_adjusted_string| to |
| 185 | // - combine the "." character that replaced "bc" with "d" into one character |
| 186 | // (call it "?") |
| 187 | // - remove the "egh" |
| 188 | // - expand the "i" into two characters (call them "12") |
| 189 | // - combine the "jkl" into one character (call it "@") |
| 190 | // - expand the "z" into two characters (call it "34") |
| 191 | // The resulting string should be "?12@mnopqrswxy34". |
| 192 | OffsetAdjuster::Adjustments adjustments_on_adjusted_string; |
| 193 | adjustments_on_adjusted_string.push_back(OffsetAdjuster::Adjustment( |
| 194 | 0, 2, 1)); |
| 195 | adjustments_on_adjusted_string.push_back(OffsetAdjuster::Adjustment( |
| 196 | 2, 3, 0)); |
| 197 | adjustments_on_adjusted_string.push_back(OffsetAdjuster::Adjustment( |
| 198 | 5, 1, 2)); |
| 199 | adjustments_on_adjusted_string.push_back(OffsetAdjuster::Adjustment( |
| 200 | 6, 3, 1)); |
| 201 | adjustments_on_adjusted_string.push_back(OffsetAdjuster::Adjustment( |
| 202 | 19, 1, 2)); |
| 203 | |
| 204 | // Now merge the adjustments and check the results. |
| 205 | OffsetAdjuster::MergeSequentialAdjustments(first_adjustments, |
| 206 | &adjustments_on_adjusted_string); |
| 207 | // The merged adjustments should look like |
| 208 | // - combine abcd into "?" |
| 209 | // - note: it's also reasonable for the Merge function to instead produce |
| 210 | // two adjustments instead of this, one to remove a and another to |
| 211 | // combine bcd into "?". This test verifies the current behavior. |
| 212 | // - remove efgh |
| 213 | // - expand i into "12" |
| 214 | // - combine jkl into "@" |
| 215 | // - remove tuv |
| 216 | // - expand z into "34" |
| 217 | ASSERT_EQ(6u, adjustments_on_adjusted_string.size()); |
| 218 | EXPECT_EQ(0u, adjustments_on_adjusted_string[0].original_offset); |
| 219 | EXPECT_EQ(4u, adjustments_on_adjusted_string[0].original_length); |
| 220 | EXPECT_EQ(1u, adjustments_on_adjusted_string[0].output_length); |
| 221 | EXPECT_EQ(4u, adjustments_on_adjusted_string[1].original_offset); |
| 222 | EXPECT_EQ(4u, adjustments_on_adjusted_string[1].original_length); |
| 223 | EXPECT_EQ(0u, adjustments_on_adjusted_string[1].output_length); |
| 224 | EXPECT_EQ(8u, adjustments_on_adjusted_string[2].original_offset); |
| 225 | EXPECT_EQ(1u, adjustments_on_adjusted_string[2].original_length); |
| 226 | EXPECT_EQ(2u, adjustments_on_adjusted_string[2].output_length); |
| 227 | EXPECT_EQ(9u, adjustments_on_adjusted_string[3].original_offset); |
| 228 | EXPECT_EQ(3u, adjustments_on_adjusted_string[3].original_length); |
| 229 | EXPECT_EQ(1u, adjustments_on_adjusted_string[3].output_length); |
| 230 | EXPECT_EQ(19u, adjustments_on_adjusted_string[4].original_offset); |
| 231 | EXPECT_EQ(3u, adjustments_on_adjusted_string[4].original_length); |
| 232 | EXPECT_EQ(0u, adjustments_on_adjusted_string[4].output_length); |
| 233 | EXPECT_EQ(25u, adjustments_on_adjusted_string[5].original_offset); |
| 234 | EXPECT_EQ(1u, adjustments_on_adjusted_string[5].original_length); |
| 235 | EXPECT_EQ(2u, adjustments_on_adjusted_string[5].output_length); |
| 236 | } |
| 237 | |
[email protected] | b9f9383 | 2009-11-13 19:27:48 | [diff] [blame] | 238 | } // namaspace base |