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 "StringPiece.h"
18
19 #include <gtest/gtest.h>
20
21 namespace minikin {
22
TEST(StringPieceTest,basics)23 TEST(StringPieceTest, basics) {
24 {
25 StringPiece s(nullptr);
26 EXPECT_EQ(nullptr, s.data());
27 EXPECT_EQ(0u, s.size());
28 EXPECT_TRUE(s.empty());
29 }
30 {
31 StringPiece s("");
32 EXPECT_NE(nullptr, s.data());
33 EXPECT_EQ(0u, s.size());
34 EXPECT_TRUE(s.empty());
35 }
36 {
37 StringPiece s("", 0);
38 EXPECT_NE(nullptr, s.data());
39 EXPECT_EQ(0u, s.size());
40 EXPECT_TRUE(s.empty());
41 }
42 {
43 StringPiece s("abcde");
44 EXPECT_NE(nullptr, s.data());
45 EXPECT_EQ(5u, s.size());
46 EXPECT_FALSE(s.empty());
47 EXPECT_EQ("abcde", s);
48 EXPECT_NE("abc", s);
49 }
50 {
51 StringPiece s("abcde", 5);
52 EXPECT_NE(nullptr, s.data());
53 EXPECT_EQ(5u, s.size());
54 EXPECT_FALSE(s.empty());
55 EXPECT_EQ("abcde", s);
56 EXPECT_NE("abc", s);
57 }
58 {
59 StringPiece s("abcde", 3);
60 EXPECT_NE(nullptr, s.data());
61 EXPECT_EQ(3u, s.size());
62 EXPECT_FALSE(s.empty());
63 EXPECT_EQ("abc", s);
64 EXPECT_NE("abcde", s);
65 }
66 {
67 const char* kText = "abcde";
68 StringPiece s(kText + 2, 3);
69 EXPECT_NE(nullptr, s.data());
70 EXPECT_EQ(3u, s.size());
71 EXPECT_FALSE(s.empty());
72 EXPECT_EQ("cde", s);
73 EXPECT_NE("abcde", s);
74 }
75 {
76 const char* kText = "abcde";
77 StringPiece s(kText + 2);
78 EXPECT_NE(nullptr, s.data());
79 EXPECT_EQ(3u, s.size());
80 EXPECT_FALSE(s.empty());
81 EXPECT_EQ("cde", s);
82 EXPECT_NE("abcde", s);
83 }
84 }
85
TEST(StringPieceTest,substr)86 TEST(StringPieceTest, substr) {
87 StringPiece s("abcde");
88 EXPECT_EQ("", s.substr(0, 0));
89 EXPECT_EQ("a", s.substr(0, 1));
90 EXPECT_EQ("abc", s.substr(0, 3));
91 EXPECT_EQ("cde", s.substr(2, 3));
92 EXPECT_EQ("", s.substr(2, 0));
93 EXPECT_EQ("", s.substr(5, 0));
94 }
95
TEST(StringPieceTest,find)96 TEST(StringPieceTest, find) {
97 StringPiece s("mississippi");
98 EXPECT_EQ(1u, s.find(0, 'i'));
99 EXPECT_EQ(1u, s.find(1, 'i'));
100 EXPECT_EQ(4u, s.find(2, 'i'));
101 EXPECT_EQ(4u, s.find(3, 'i'));
102 EXPECT_EQ(4u, s.find(4, 'i'));
103 EXPECT_EQ(7u, s.find(5, 'i'));
104 EXPECT_EQ(7u, s.find(6, 'i'));
105 EXPECT_EQ(7u, s.find(7, 'i'));
106 EXPECT_EQ(10u, s.find(8, 'i'));
107 EXPECT_EQ(10u, s.find(9, 'i'));
108 EXPECT_EQ(10u, s.find(10, 'i'));
109 EXPECT_EQ(11u, s.find(11, 'i'));
110
111 EXPECT_EQ(11u, s.find(12, 'i')); // search index is out of bounds.
112 }
113
TEST(StringPieceTest,find_empty)114 TEST(StringPieceTest, find_empty) {
115 StringPiece s("");
116 EXPECT_EQ(0u, s.find(0, 'a'));
117 }
118
TEST(SplitIteratorTest,split)119 TEST(SplitIteratorTest, split) {
120 {
121 StringPiece s("");
122 SplitIterator it(s, ',');
123 EXPECT_FALSE(it.hasNext());
124 }
125 {
126 StringPiece s("abcde");
127 SplitIterator it(s, ',');
128 EXPECT_TRUE(it.hasNext());
129 EXPECT_EQ("abcde", it.next());
130 EXPECT_FALSE(it.hasNext());
131 }
132 {
133 StringPiece s("a,bb,ccc,dddd,eeeee");
134 SplitIterator it(s, ',');
135 EXPECT_TRUE(it.hasNext());
136 EXPECT_EQ("a", it.next());
137 EXPECT_TRUE(it.hasNext());
138 EXPECT_EQ("bb", it.next());
139 EXPECT_TRUE(it.hasNext());
140 EXPECT_EQ("ccc", it.next());
141 EXPECT_TRUE(it.hasNext());
142 EXPECT_EQ("dddd", it.next());
143 EXPECT_TRUE(it.hasNext());
144 EXPECT_EQ("eeeee", it.next());
145 EXPECT_FALSE(it.hasNext());
146 }
147 {
148 StringPiece s(",,,,");
149 SplitIterator it(s, ',');
150 EXPECT_TRUE(it.hasNext());
151 EXPECT_EQ("", it.next());
152 EXPECT_TRUE(it.hasNext());
153 EXPECT_EQ("", it.next());
154 EXPECT_TRUE(it.hasNext());
155 EXPECT_EQ("", it.next());
156 EXPECT_TRUE(it.hasNext());
157 EXPECT_EQ("", it.next());
158 EXPECT_TRUE(it.hasNext());
159 EXPECT_EQ("", it.next());
160 EXPECT_FALSE(it.hasNext());
161 }
162 {
163 StringPiece s(",a,,b,");
164 SplitIterator it(s, ',');
165 EXPECT_TRUE(it.hasNext());
166 EXPECT_EQ("", it.next());
167 EXPECT_TRUE(it.hasNext());
168 EXPECT_EQ("a", it.next());
169 EXPECT_TRUE(it.hasNext());
170 EXPECT_EQ("", it.next());
171 EXPECT_TRUE(it.hasNext());
172 EXPECT_EQ("b", it.next());
173 EXPECT_TRUE(it.hasNext());
174 EXPECT_EQ("", it.next());
175 EXPECT_FALSE(it.hasNext());
176 }
177 }
178
179 } // namespace minikin
180