Roll src/third_party/WebKit 06cb9e9:a978ee5 (svn 202558:202559)
[chromium-blink-merge.git] / base / i18n / char_iterator_unittest.cc
blob0cf8e6c07d4efa57c03199dce0f7652ed79c8a94
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
3 // found in the LICENSE file.
5 #include "base/i18n/char_iterator.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace base {
11 namespace i18n {
13 TEST(CharIteratorsTest, TestUTF8) {
14 std::string empty;
15 UTF8CharIterator empty_iter(&empty);
16 ASSERT_TRUE(empty_iter.end());
17 ASSERT_EQ(0, empty_iter.array_pos());
18 ASSERT_EQ(0, empty_iter.char_pos());
19 ASSERT_FALSE(empty_iter.Advance());
21 std::string str("s\303\273r"); // [u with circumflex]
22 UTF8CharIterator iter(&str);
23 ASSERT_FALSE(iter.end());
24 ASSERT_EQ(0, iter.array_pos());
25 ASSERT_EQ(0, iter.char_pos());
26 ASSERT_EQ('s', iter.get());
27 ASSERT_TRUE(iter.Advance());
29 ASSERT_FALSE(iter.end());
30 ASSERT_EQ(1, iter.array_pos());
31 ASSERT_EQ(1, iter.char_pos());
32 ASSERT_EQ(251, iter.get());
33 ASSERT_TRUE(iter.Advance());
35 ASSERT_FALSE(iter.end());
36 ASSERT_EQ(3, iter.array_pos());
37 ASSERT_EQ(2, iter.char_pos());
38 ASSERT_EQ('r', iter.get());
39 ASSERT_TRUE(iter.Advance());
41 ASSERT_TRUE(iter.end());
42 ASSERT_EQ(4, iter.array_pos());
43 ASSERT_EQ(3, iter.char_pos());
45 // Don't care what it returns, but this shouldn't crash
46 iter.get();
48 ASSERT_FALSE(iter.Advance());
51 TEST(CharIteratorsTest, TestUTF16) {
52 string16 empty = UTF8ToUTF16("");
53 UTF16CharIterator empty_iter(&empty);
54 ASSERT_TRUE(empty_iter.end());
55 ASSERT_EQ(0, empty_iter.array_pos());
56 ASSERT_EQ(0, empty_iter.char_pos());
57 ASSERT_FALSE(empty_iter.Advance());
59 // This test string contains 4 characters:
60 // x
61 // u with circumflex - 2 bytes in UTF8, 1 codeword in UTF16
62 // math double-struck A - 4 bytes in UTF8, 2 codewords in UTF16
63 // z
64 string16 str = UTF8ToUTF16("x\303\273\360\235\224\270z");
65 UTF16CharIterator iter(&str);
66 ASSERT_FALSE(iter.end());
67 ASSERT_EQ(0, iter.array_pos());
68 ASSERT_EQ(0, iter.char_pos());
69 ASSERT_EQ('x', iter.get());
70 ASSERT_TRUE(iter.Advance());
72 ASSERT_FALSE(iter.end());
73 ASSERT_EQ(1, iter.array_pos());
74 ASSERT_EQ(1, iter.char_pos());
75 ASSERT_EQ(251, iter.get());
76 ASSERT_TRUE(iter.Advance());
78 ASSERT_FALSE(iter.end());
79 ASSERT_EQ(2, iter.array_pos());
80 ASSERT_EQ(2, iter.char_pos());
81 ASSERT_EQ(120120, iter.get());
82 ASSERT_TRUE(iter.Advance());
84 ASSERT_FALSE(iter.end());
85 ASSERT_EQ(4, iter.array_pos());
86 ASSERT_EQ(3, iter.char_pos());
87 ASSERT_EQ('z', iter.get());
88 ASSERT_TRUE(iter.Advance());
90 ASSERT_TRUE(iter.end());
91 ASSERT_EQ(5, iter.array_pos());
92 ASSERT_EQ(4, iter.char_pos());
94 // Don't care what it returns, but this shouldn't crash
95 iter.get();
97 ASSERT_FALSE(iter.Advance());
100 } // namespace i18n
101 } // namespace base