1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/i18n/char_iterator.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace i18n {
TEST(CharIteratorsTest, TestUTF8) {
std::string empty;
UTF8CharIterator empty_iter(&empty);
ASSERT_TRUE(empty_iter.end());
ASSERT_EQ(0, empty_iter.array_pos());
ASSERT_EQ(0, empty_iter.char_pos());
ASSERT_FALSE(empty_iter.Advance());
std::string str("s\303\273r"); // [u with circumflex]
UTF8CharIterator iter(&str);
ASSERT_FALSE(iter.end());
ASSERT_EQ(0, iter.array_pos());
ASSERT_EQ(0, iter.char_pos());
ASSERT_EQ('s', iter.get());
ASSERT_TRUE(iter.Advance());
ASSERT_FALSE(iter.end());
ASSERT_EQ(1, iter.array_pos());
ASSERT_EQ(1, iter.char_pos());
ASSERT_EQ(251, iter.get());
ASSERT_TRUE(iter.Advance());
ASSERT_FALSE(iter.end());
ASSERT_EQ(3, iter.array_pos());
ASSERT_EQ(2, iter.char_pos());
ASSERT_EQ('r', iter.get());
ASSERT_TRUE(iter.Advance());
ASSERT_TRUE(iter.end());
ASSERT_EQ(4, iter.array_pos());
ASSERT_EQ(3, iter.char_pos());
// Don't care what it returns, but this shouldn't crash
iter.get();
ASSERT_FALSE(iter.Advance());
}
TEST(CharIteratorsTest, TestUTF16) {
string16 empty = UTF8ToUTF16("");
UTF16CharIterator empty_iter(&empty);
ASSERT_TRUE(empty_iter.end());
ASSERT_EQ(0, empty_iter.array_pos());
ASSERT_EQ(0, empty_iter.char_pos());
ASSERT_FALSE(empty_iter.Advance());
// This test string contains 4 characters:
// x
// u with circumflex - 2 bytes in UTF8, 1 codeword in UTF16
// math double-struck A - 4 bytes in UTF8, 2 codewords in UTF16
// z
string16 str = UTF8ToUTF16("x\303\273\360\235\224\270z");
UTF16CharIterator iter(&str);
ASSERT_FALSE(iter.end());
ASSERT_EQ(0, iter.array_pos());
ASSERT_EQ(0, iter.char_pos());
ASSERT_EQ('x', iter.get());
ASSERT_TRUE(iter.Advance());
ASSERT_FALSE(iter.end());
ASSERT_EQ(1, iter.array_pos());
ASSERT_EQ(1, iter.char_pos());
ASSERT_EQ(251, iter.get());
ASSERT_TRUE(iter.Advance());
ASSERT_FALSE(iter.end());
ASSERT_EQ(2, iter.array_pos());
ASSERT_EQ(2, iter.char_pos());
ASSERT_EQ(120120, iter.get());
ASSERT_TRUE(iter.Advance());
ASSERT_FALSE(iter.end());
ASSERT_EQ(4, iter.array_pos());
ASSERT_EQ(3, iter.char_pos());
ASSERT_EQ('z', iter.get());
ASSERT_TRUE(iter.Advance());
ASSERT_TRUE(iter.end());
ASSERT_EQ(5, iter.array_pos());
ASSERT_EQ(4, iter.char_pos());
// Don't care what it returns, but this shouldn't crash
iter.get();
ASSERT_FALSE(iter.Advance());
}
} // namespace i18n
} // namespace base
|