blob: 5786bdf4873c9ad593d3a07e7a0969239dfe81d5 [file] [log] [blame]
[email protected]421de2ab2011-04-13 18:43:051// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]b9f93832009-11-13 19:27:482// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]421de2ab2011-04-13 18:43:055#include <algorithm>
6
[email protected]b9f93832009-11-13 19:27:487#include "base/logging.h"
[email protected]eb62f7262013-03-30 14:29:008#include "base/strings/string_piece.h"
[email protected]a3f721892013-02-07 03:59:069#include "base/strings/utf_offset_string_conversions.h"
[email protected]b9f93832009-11-13 19:27:4810#include "testing/gtest/include/gtest/gtest.h"
11
12namespace base {
13
14namespace {
15
[email protected]04866c42011-05-03 20:03:5016static const size_t kNpos = string16::npos;
[email protected]b9f93832009-11-13 19:27:4817
18} // namespace
19
20TEST(UTFOffsetStringConversionsTest, AdjustOffset) {
[email protected]04866c42011-05-03 20:03:5021 struct UTF8ToUTF16Case {
[email protected]b9f93832009-11-13 19:27:4822 const char* utf8;
23 size_t input_offset;
24 size_t output_offset;
[email protected]04866c42011-05-03 20:03:5025 } utf8_to_utf16_cases[] = {
[email protected]cf7ca8a2013-09-11 00:42:2826 {"", 0, 0},
27 {"", kNpos, kNpos},
[email protected]421de2ab2011-04-13 18:43:0528 {"\xe4\xbd\xa0\xe5\xa5\xbd", 1, kNpos},
[email protected]b9f93832009-11-13 19:27:4829 {"\xe4\xbd\xa0\xe5\xa5\xbd", 3, 1},
[email protected]d7a3e8e2010-01-01 22:16:3830 {"\xed\xb0\x80z", 3, 1},
[email protected]b9f93832009-11-13 19:27:4831 {"A\xF0\x90\x8C\x80z", 1, 1},
[email protected]421de2ab2011-04-13 18:43:0532 {"A\xF0\x90\x8C\x80z", 2, kNpos},
[email protected]b9f93832009-11-13 19:27:4833 {"A\xF0\x90\x8C\x80z", 5, 3},
[email protected]cf7ca8a2013-09-11 00:42:2834 {"A\xF0\x90\x8C\x80z", 6, 4},
35 {"A\xF0\x90\x8C\x80z", kNpos, kNpos},
[email protected]b9f93832009-11-13 19:27:4836 };
[email protected]04866c42011-05-03 20:03:5037 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf8_to_utf16_cases); ++i) {
[email protected]a97376e2014-04-18 20:54:4438 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]b9f93832009-11-13 19:27:4843 }
[email protected]cbf35e172011-09-08 02:18:1044
45 struct UTF16ToUTF8Case {
46 char16 utf16[10];
47 size_t input_offset;
48 size_t output_offset;
49 } utf16_to_utf8_cases[] = {
[email protected]cf7ca8a2013-09-11 00:42:2850 {{}, 0, 0},
[email protected]cbf35e172011-09-08 02:18:1051 // Converted to 3-byte utf-8 sequences
[email protected]cf7ca8a2013-09-11 00:42:2852 {{0x5909, 0x63DB}, 3, kNpos},
53 {{0x5909, 0x63DB}, 2, 6},
[email protected]cbf35e172011-09-08 02:18:1054 {{0x5909, 0x63DB}, 1, 3},
[email protected]cf7ca8a2013-09-11 00:42:2855 {{0x5909, 0x63DB}, 0, 0},
[email protected]cbf35e172011-09-08 02:18:1056 // 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]cf7ca8a2013-09-11 00:42:2860 {{'A', 0x00bc, 0x00be, 'z'}, 4, 6},
[email protected]cbf35e172011-09-08 02:18:1061 // 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]cf7ca8a2013-09-11 00:42:2865 {{'A', 0xd800, 0xdf00, 'z'}, 4, 6},
[email protected]cbf35e172011-09-08 02:18:1066 };
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]a97376e2014-04-18 20:54:4469 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]cbf35e172011-09-08 02:18:1073 }
[email protected]b9f93832009-11-13 19:27:4874}
75
[email protected]421de2ab2011-04-13 18:43:0576TEST(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]04866c42011-05-03 20:03:5083 LimitOffset<string16>(kLimit));
[email protected]421de2ab2011-04-13 18:43:0584 size_t unlimited_count = 0;
85 for (std::vector<size_t>::iterator ti = size_ts.begin(); ti != size_ts.end();
86 ++ti) {
[email protected]cf7ca8a2013-09-11 00:42:2887 if (*ti != kNpos)
[email protected]421de2ab2011-04-13 18:43:0588 ++unlimited_count;
89 }
[email protected]cf7ca8a2013-09-11 00:42:2890 EXPECT_EQ(11U, unlimited_count);
[email protected]421de2ab2011-04-13 18:43:0591
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]04866c42011-05-03 20:03:5097 LimitOffset<string16>(kLimit));
[email protected]421de2ab2011-04-13 18:43:0598 unlimited_count = 0;
99 for (std::vector<size_t>::iterator ti = size_ts.begin(); ti != size_ts.end();
100 ++ti) {
[email protected]cf7ca8a2013-09-11 00:42:28101 if (*ti != kNpos)
[email protected]421de2ab2011-04-13 18:43:05102 ++unlimited_count;
103 }
[email protected]cf7ca8a2013-09-11 00:42:28104 EXPECT_EQ(11U, unlimited_count);
[email protected]421de2ab2011-04-13 18:43:05105}
106
107TEST(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]04866c42011-05-03 20:03:50111 {
112 std::vector<size_t> offsets;
[email protected]cf7ca8a2013-09-11 00:42:28113 for (size_t t = 0; t <= 9; ++t)
[email protected]04866c42011-05-03 20:03:50114 offsets.push_back(t);
[email protected]a97376e2014-04-18 20:54:44115 OffsetAdjuster::Adjustments adjustments;
116 adjustments.push_back(OffsetAdjuster::Adjustment(3, 3, 1));
117 OffsetAdjuster::AdjustOffsets(adjustments, &offsets);
[email protected]cf7ca8a2013-09-11 00:42:28118 size_t expected_1[] = {0, 1, 2, 3, kNpos, kNpos, 4, 5, 6, 7};
[email protected]04866c42011-05-03 20:03:50119 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]421de2ab2011-04-13 18:43:05123
124 // 2: XXXaXXXXbcXXXXXXXdefXXX ==> XaXXbcXXXXdefX
[email protected]04866c42011-05-03 20:03:50125 {
126 std::vector<size_t> offsets;
[email protected]cf7ca8a2013-09-11 00:42:28127 for (size_t t = 0; t <= 23; ++t)
[email protected]04866c42011-05-03 20:03:50128 offsets.push_back(t);
[email protected]a97376e2014-04-18 20:54:44129 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]cf7ca8a2013-09-11 00:42:28135 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]04866c42011-05-03 20:03:50139 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]421de2ab2011-04-13 18:43:05143
144 // 3: XXXaXXXXbcdXXXeXX ==> aXXXXbcdXXXe
[email protected]04866c42011-05-03 20:03:50145 {
146 std::vector<size_t> offsets;
[email protected]cf7ca8a2013-09-11 00:42:28147 for (size_t t = 0; t <= 17; ++t)
[email protected]04866c42011-05-03 20:03:50148 offsets.push_back(t);
[email protected]a97376e2014-04-18 20:54:44149 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]cf7ca8a2013-09-11 00:42:28155 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]04866c42011-05-03 20:03:50159 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]421de2ab2011-04-13 18:43:05163}
164
[email protected]a97376e2014-04-18 20:54:44165// 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.
169TEST(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]b9f93832009-11-13 19:27:48238} // namaspace base