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

Side by Side Diff: base/utf_offset_string_conversions_unittest.cc

Issue 7828092: Add UTF16ToUTF8AndAdjustOffset() to base/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use vector version in single-offset version. Created 9 years, 3 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
« no previous file with comments | « base/utf_offset_string_conversions.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 18 matching lines...) Expand all
29 {"\xed\xb0\x80z", 3, 1}, 29 {"\xed\xb0\x80z", 3, 1},
30 {"A\xF0\x90\x8C\x80z", 1, 1}, 30 {"A\xF0\x90\x8C\x80z", 1, 1},
31 {"A\xF0\x90\x8C\x80z", 2, kNpos}, 31 {"A\xF0\x90\x8C\x80z", 2, kNpos},
32 {"A\xF0\x90\x8C\x80z", 5, 3}, 32 {"A\xF0\x90\x8C\x80z", 5, 3},
33 }; 33 };
34 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf8_to_utf16_cases); ++i) { 34 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf8_to_utf16_cases); ++i) {
35 size_t offset = utf8_to_utf16_cases[i].input_offset; 35 size_t offset = utf8_to_utf16_cases[i].input_offset;
36 UTF8ToUTF16AndAdjustOffset(utf8_to_utf16_cases[i].utf8, &offset); 36 UTF8ToUTF16AndAdjustOffset(utf8_to_utf16_cases[i].utf8, &offset);
37 EXPECT_EQ(utf8_to_utf16_cases[i].output_offset, offset); 37 EXPECT_EQ(utf8_to_utf16_cases[i].output_offset, offset);
38 } 38 }
39
40 struct UTF16ToUTF8Case {
41 char16 utf16[10];
42 size_t input_offset;
43 size_t output_offset;
44 } utf16_to_utf8_cases[] = {
45 {{}, 0, kNpos},
46 // Converted to 3-byte utf-8 sequences
47 {{0x5909, 0x63DB}, 2, kNpos},
48 {{0x5909, 0x63DB}, 1, 3},
49 // Converted to 2-byte utf-8 sequences
50 {{'A', 0x00bc, 0x00be, 'z'}, 1, 1},
51 {{'A', 0x00bc, 0x00be, 'z'}, 2, 3},
52 {{'A', 0x00bc, 0x00be, 'z'}, 3, 5},
53 // Surrogate pair
54 {{'A', 0xd800, 0xdf00, 'z'}, 1, 1},
55 {{'A', 0xd800, 0xdf00, 'z'}, 2, kNpos},
56 {{'A', 0xd800, 0xdf00, 'z'}, 3, 5},
57 };
58 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf16_to_utf8_cases); ++i) {
59 size_t offset = utf16_to_utf8_cases[i].input_offset;
60 UTF16ToUTF8AndAdjustOffset(utf16_to_utf8_cases[i].utf16, &offset);
61 EXPECT_EQ(utf16_to_utf8_cases[i].output_offset, offset);
62 }
39 } 63 }
40 64
41 TEST(UTFOffsetStringConversionsTest, LimitOffsets) { 65 TEST(UTFOffsetStringConversionsTest, LimitOffsets) {
42 const size_t kLimit = 10; 66 const size_t kLimit = 10;
43 const size_t kItems = 20; 67 const size_t kItems = 20;
44 std::vector<size_t> size_ts; 68 std::vector<size_t> size_ts;
45 for (size_t t = 0; t < kItems; ++t) 69 for (size_t t = 0; t < kItems; ++t)
46 size_ts.push_back(t); 70 size_ts.push_back(t);
47 std::for_each(size_ts.begin(), size_ts.end(), 71 std::for_each(size_ts.begin(), size_ts.end(),
48 LimitOffset<string16>(kLimit)); 72 LimitOffset<string16>(kLimit));
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 } 145 }
122 size_t expected_3[] = {kNpos, kNpos, kNpos, 0, 1, kNpos, kNpos, kNpos, 5, 6, 146 size_t expected_3[] = {kNpos, kNpos, kNpos, 0, 1, kNpos, kNpos, kNpos, 5, 6,
123 7, 8, kNpos, kNpos, 11, kNpos, kNpos}; 147 7, 8, kNpos, kNpos, 11, kNpos, kNpos};
124 EXPECT_EQ(offsets.size(), arraysize(expected_3)); 148 EXPECT_EQ(offsets.size(), arraysize(expected_3));
125 for (size_t i = 0; i < arraysize(expected_3); ++i) 149 for (size_t i = 0; i < arraysize(expected_3); ++i)
126 EXPECT_EQ(expected_3[i], offsets[i]); 150 EXPECT_EQ(expected_3[i], offsets[i]);
127 } 151 }
128 } 152 }
129 153
130 } // namaspace base 154 } // namaspace base
OLDNEW
« no previous file with comments | « base/utf_offset_string_conversions.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698