Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: base/utf_offset_string_conversions_unittest.cc

Issue 6898026: Eliminate wstring from base/utf_offset_string_conversions.h, net/base/escape.h, and net/base/net_... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/string_piece.h" 8 #include "base/string_piece.h"
9 #include "base/utf_offset_string_conversions.h" 9 #include "base/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 = std::wstring::npos; 16 static const size_t kNpos = string16::npos;
17
18 // Given a null-terminated string of wchar_t with each wchar_t representing
19 // a UTF-16 code unit, returns a string16 made up of wchar_t's in the input.
20 // Each wchar_t should be <= 0xFFFF and a non-BMP character (> U+FFFF)
21 // should be represented as a surrogate pair (two UTF-16 units)
22 // *even* where wchar_t is 32-bit (Linux and Mac).
23 //
24 // This is to help write tests for functions with string16 params until
25 // the C++ 0x UTF-16 literal is well-supported by compilers.
26 string16 BuildString16(const wchar_t* s) {
27 #if defined(WCHAR_T_IS_UTF16)
28 return string16(s);
29 #elif defined(WCHAR_T_IS_UTF32)
30 string16 u16;
31 while (*s != 0) {
32 DCHECK(static_cast<unsigned int>(*s) <= 0xFFFFu);
33 u16.push_back(*s++);
34 }
35 return u16;
36 #endif
37 }
38 17
39 } // namespace 18 } // namespace
40 19
41 TEST(UTFOffsetStringConversionsTest, AdjustOffset) { 20 TEST(UTFOffsetStringConversionsTest, AdjustOffset) {
42 struct UTF8ToWideCase { 21 struct UTF8ToUTF16Case {
43 const char* utf8; 22 const char* utf8;
44 size_t input_offset; 23 size_t input_offset;
45 size_t output_offset; 24 size_t output_offset;
46 } utf8_to_wide_cases[] = { 25 } utf8_to_utf16_cases[] = {
47 {"", 0, kNpos}, 26 {"", 0, kNpos},
48 {"\xe4\xbd\xa0\xe5\xa5\xbd", 1, kNpos}, 27 {"\xe4\xbd\xa0\xe5\xa5\xbd", 1, kNpos},
49 {"\xe4\xbd\xa0\xe5\xa5\xbd", 3, 1}, 28 {"\xe4\xbd\xa0\xe5\xa5\xbd", 3, 1},
50 {"\xed\xb0\x80z", 3, 1}, 29 {"\xed\xb0\x80z", 3, 1},
51 {"A\xF0\x90\x8C\x80z", 1, 1}, 30 {"A\xF0\x90\x8C\x80z", 1, 1},
52 {"A\xF0\x90\x8C\x80z", 2, kNpos}, 31 {"A\xF0\x90\x8C\x80z", 2, kNpos},
53 #if defined(WCHAR_T_IS_UTF16) 32 #if defined(WCHAR_T_IS_UTF16)
54 {"A\xF0\x90\x8C\x80z", 5, 3}, 33 {"A\xF0\x90\x8C\x80z", 5, 3},
55 #elif defined(WCHAR_T_IS_UTF32) 34 #elif defined(WCHAR_T_IS_UTF32)
56 {"A\xF0\x90\x8C\x80z", 5, 2}, 35 {"A\xF0\x90\x8C\x80z", 5, 2},
57 #endif 36 #endif
58 }; 37 };
59 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf8_to_wide_cases); ++i) { 38 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf8_to_utf16_cases); ++i) {
60 size_t offset = utf8_to_wide_cases[i].input_offset; 39 size_t offset = utf8_to_utf16_cases[i].input_offset;
61 UTF8ToWideAndAdjustOffset(utf8_to_wide_cases[i].utf8, &offset); 40 UTF8ToUTF16AndAdjustOffset(utf8_to_utf16_cases[i].utf8, &offset);
62 EXPECT_EQ(utf8_to_wide_cases[i].output_offset, offset); 41 EXPECT_EQ(utf8_to_utf16_cases[i].output_offset, offset);
63 } 42 }
64
65 #if defined(WCHAR_T_IS_UTF32)
66 struct UTF16ToWideCase {
67 const wchar_t* wide;
68 size_t input_offset;
69 size_t output_offset;
70 } utf16_to_wide_cases[] = {
71 {L"\xD840\xDC00\x4E00", 0, 0},
72 {L"\xD840\xDC00\x4E00", 1, kNpos},
73 {L"\xD840\xDC00\x4E00", 2, 1},
74 };
75 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf16_to_wide_cases); ++i) {
76 size_t offset = utf16_to_wide_cases[i].input_offset;
77 UTF16ToWideAndAdjustOffset(BuildString16(utf16_to_wide_cases[i].wide),
78 &offset);
79 EXPECT_EQ(utf16_to_wide_cases[i].output_offset, offset);
80 }
81 #endif
82 } 43 }
83 44
84 TEST(UTFOffsetStringConversionsTest, LimitOffsets) { 45 TEST(UTFOffsetStringConversionsTest, LimitOffsets) {
85 const size_t kLimit = 10; 46 const size_t kLimit = 10;
86 const size_t kItems = 20; 47 const size_t kItems = 20;
87 std::vector<size_t> size_ts; 48 std::vector<size_t> size_ts;
88 for (size_t t = 0; t < kItems; ++t) 49 for (size_t t = 0; t < kItems; ++t)
89 size_ts.push_back(t); 50 size_ts.push_back(t);
90 std::for_each(size_ts.begin(), size_ts.end(), 51 std::for_each(size_ts.begin(), size_ts.end(),
91 LimitOffset<std::wstring>(kLimit)); 52 LimitOffset<string16>(kLimit));
92 size_t unlimited_count = 0; 53 size_t unlimited_count = 0;
93 for (std::vector<size_t>::iterator ti = size_ts.begin(); ti != size_ts.end(); 54 for (std::vector<size_t>::iterator ti = size_ts.begin(); ti != size_ts.end();
94 ++ti) { 55 ++ti) {
95 if (*ti < kLimit && *ti != kNpos) 56 if (*ti < kLimit && *ti != kNpos)
96 ++unlimited_count; 57 ++unlimited_count;
97 } 58 }
98 EXPECT_EQ(10U, unlimited_count); 59 EXPECT_EQ(10U, unlimited_count);
99 60
100 // Reverse the values in the vector and try again. 61 // Reverse the values in the vector and try again.
101 size_ts.clear(); 62 size_ts.clear();
102 for (size_t t = kItems; t > 0; --t) 63 for (size_t t = kItems; t > 0; --t)
103 size_ts.push_back(t - 1); 64 size_ts.push_back(t - 1);
104 std::for_each(size_ts.begin(), size_ts.end(), 65 std::for_each(size_ts.begin(), size_ts.end(),
105 LimitOffset<std::wstring>(kLimit)); 66 LimitOffset<string16>(kLimit));
106 unlimited_count = 0; 67 unlimited_count = 0;
107 for (std::vector<size_t>::iterator ti = size_ts.begin(); ti != size_ts.end(); 68 for (std::vector<size_t>::iterator ti = size_ts.begin(); ti != size_ts.end();
108 ++ti) { 69 ++ti) {
109 if (*ti < kLimit && *ti != kNpos) 70 if (*ti < kLimit && *ti != kNpos)
110 ++unlimited_count; 71 ++unlimited_count;
111 } 72 }
112 EXPECT_EQ(10U, unlimited_count); 73 EXPECT_EQ(10U, unlimited_count);
113 } 74 }
114 75
115 TEST(UTFOffsetStringConversionsTest, AdjustOffsets) { 76 TEST(UTFOffsetStringConversionsTest, AdjustOffsets) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 adjustments.push_back(AdjustOffset::Adjustment(15, 2, 0)); 116 adjustments.push_back(AdjustOffset::Adjustment(15, 2, 0));
156 std::for_each(offsets.begin(), offsets.end(), AdjustOffset(adjustments)); 117 std::for_each(offsets.begin(), offsets.end(), AdjustOffset(adjustments));
157 size_t expected_3[] = {kNpos, kNpos, kNpos, 0, 1, kNpos, kNpos, kNpos, 5, 6, 118 size_t expected_3[] = {kNpos, kNpos, kNpos, 0, 1, kNpos, kNpos, kNpos, 5, 6,
158 7, 8, kNpos, kNpos, 11, kNpos, kNpos}; 119 7, 8, kNpos, kNpos, 11, kNpos, kNpos};
159 EXPECT_EQ(offsets.size(), arraysize(expected_3)); 120 EXPECT_EQ(offsets.size(), arraysize(expected_3));
160 for (size_t i = 0; i < arraysize(expected_3); ++i) 121 for (size_t i = 0; i < arraysize(expected_3); ++i)
161 EXPECT_EQ(expected_3[i], offsets[i]); 122 EXPECT_EQ(expected_3[i], offsets[i]);
162 } 123 }
163 124
164 } // namaspace base 125 } // namaspace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698