1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "text/Utf8Iterator.h"
18
19 #include "test/Test.h"
20
21 using ::android::StringPiece;
22 using ::testing::Eq;
23
24 namespace aapt {
25 namespace text {
26
TEST(Utf8IteratorTest,IteratesOverAscii)27 TEST(Utf8IteratorTest, IteratesOverAscii) {
28 Utf8Iterator iter("hello");
29
30 ASSERT_TRUE(iter.HasNext());
31 EXPECT_THAT(iter.Next(), Eq(U'h'));
32
33 ASSERT_TRUE(iter.HasNext());
34 EXPECT_THAT(iter.Next(), Eq(U'e'));
35
36 ASSERT_TRUE(iter.HasNext());
37 EXPECT_THAT(iter.Next(), Eq(U'l'));
38
39 ASSERT_TRUE(iter.HasNext());
40 EXPECT_THAT(iter.Next(), Eq(U'l'));
41
42 ASSERT_TRUE(iter.HasNext());
43 EXPECT_THAT(iter.Next(), Eq(U'o'));
44
45 EXPECT_FALSE(iter.HasNext());
46 }
47
TEST(Utf8IteratorTest,IteratesOverUnicode)48 TEST(Utf8IteratorTest, IteratesOverUnicode) {
49 Utf8Iterator iter("Hi there 華勵蓮");
50 iter.Skip(9);
51
52 ASSERT_TRUE(iter.HasNext());
53 EXPECT_THAT(iter.Next(), Eq(U'華'));
54
55 ASSERT_TRUE(iter.HasNext());
56 EXPECT_THAT(iter.Next(), Eq(U'勵'));
57
58 ASSERT_TRUE(iter.HasNext());
59 EXPECT_THAT(iter.Next(), Eq(U'蓮'));
60
61 ASSERT_TRUE(iter.HasNext());
62 EXPECT_THAT(iter.Next(), Eq(U''));
63
64 EXPECT_FALSE(iter.HasNext());
65 }
66
TEST(Utf8IteratorTest,PositionPointsToTheCorrectPlace)67 TEST(Utf8IteratorTest, PositionPointsToTheCorrectPlace) {
68 const StringPiece expected("Mm");
69 Utf8Iterator iter(expected);
70
71 // Before any character, the position should be 0.
72 EXPECT_THAT(iter.Position(), Eq(0u));
73
74 // The 'M' character, one byte.
75 ASSERT_TRUE(iter.HasNext());
76 iter.Next();
77 EXPECT_THAT(iter.Position(), Eq(1u));
78
79 // The 'm' character, one byte.
80 ASSERT_TRUE(iter.HasNext());
81 iter.Next();
82 EXPECT_THAT(iter.Position(), Eq(2u));
83
84 // The doughnut character, 4 bytes.
85 ASSERT_TRUE(iter.HasNext());
86 iter.Next();
87 EXPECT_THAT(iter.Position(), Eq(6u));
88
89 // There should be nothing left.
90 EXPECT_FALSE(iter.HasNext());
91 EXPECT_THAT(iter.Position(), Eq(expected.size()));
92 }
93
94 } // namespace text
95 } // namespace aapt
96