1 /*
2  * Copyright (C) 2019 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 <utils/String16.h>
18 #include <utils/String8.h>
19 
20 #include <gtest/gtest.h>
21 
22 using namespace android;
23 
Char16_tStringEquals(const char16_t * a,const char16_t * b)24 ::testing::AssertionResult Char16_tStringEquals(const char16_t* a, const char16_t* b) {
25     if (strcmp16(a, b) != 0) {
26         return ::testing::AssertionFailure()
27                << "\"" << String8(a).c_str() << "\" not equal to \"" << String8(b).c_str() << "\"";
28     }
29     return ::testing::AssertionSuccess();
30 }
31 
32 #define EXPECT_STR16EQ(a, b) EXPECT_TRUE(Char16_tStringEquals(a, b))
33 
TEST(String16Test,FromChar16_t)34 TEST(String16Test, FromChar16_t) {
35     String16 tmp(u"Verify me");
36     EXPECT_STR16EQ(u"Verify me", tmp);
37 }
38 
TEST(String16Test,FromChar16_tSized)39 TEST(String16Test, FromChar16_tSized) {
40     String16 tmp(u"Verify me", 7);
41     EXPECT_STR16EQ(u"Verify ", tmp);
42 }
43 
TEST(String16Test,FromChar)44 TEST(String16Test, FromChar) {
45     String16 tmp("Verify me");
46     EXPECT_STR16EQ(u"Verify me", tmp);
47 }
48 
TEST(String16Test,FromCharSized)49 TEST(String16Test, FromCharSized) {
50     String16 tmp("Verify me", 7);
51     EXPECT_STR16EQ(u"Verify ", tmp);
52 }
53 
TEST(String16Test,Copy)54 TEST(String16Test, Copy) {
55     String16 tmp("Verify me");
56     String16 another = tmp;
57     EXPECT_STR16EQ(u"Verify me", tmp);
58     EXPECT_STR16EQ(u"Verify me", another);
59 }
60 
TEST(String16Test,Move)61 TEST(String16Test, Move) {
62     String16 tmp("Verify me");
63     String16 another(std::move(tmp));
64     EXPECT_STR16EQ(u"Verify me", another);
65 }
66 
TEST(String16Test,Size)67 TEST(String16Test, Size) {
68     String16 tmp("Verify me");
69     EXPECT_EQ(9U, tmp.size());
70 }
71 
TEST(String16Test,setTo)72 TEST(String16Test, setTo) {
73     String16 tmp("Verify me");
74     tmp.setTo(u"New content");
75     EXPECT_EQ(11U, tmp.size());
76     EXPECT_STR16EQ(u"New content", tmp);
77 }
78 
TEST(String16Test,Append)79 TEST(String16Test, Append) {
80     String16 tmp("Verify me");
81     tmp.append(String16("Hello"));
82     EXPECT_EQ(14U, tmp.size());
83     EXPECT_STR16EQ(u"Verify meHello", tmp);
84 }
85 
TEST(String16Test,Insert)86 TEST(String16Test, Insert) {
87     String16 tmp("Verify me");
88     tmp.insert(6, u"Insert");
89     EXPECT_EQ(15U, tmp.size());
90     EXPECT_STR16EQ(u"VerifyInsert me", tmp);
91 }
92 
TEST(String16Test,ReplaceAll)93 TEST(String16Test, ReplaceAll) {
94     String16 tmp("Verify verify Verify");
95     tmp.replaceAll(u'r', u'!');
96     EXPECT_STR16EQ(u"Ve!ify ve!ify Ve!ify", tmp);
97 }
98 
TEST(String16Test,Compare)99 TEST(String16Test, Compare) {
100     String16 tmp("Verify me");
101     EXPECT_EQ(String16(u"Verify me"), tmp);
102 }
103 
TEST(String16Test,StaticString)104 TEST(String16Test, StaticString) {
105     String16 nonStaticString("NonStatic");
106     StaticString16 staticString(u"Static");
107 
108     EXPECT_TRUE(staticString.isStaticString());
109     EXPECT_FALSE(nonStaticString.isStaticString());
110 }
111 
TEST(String16Test,StaticStringCopy)112 TEST(String16Test, StaticStringCopy) {
113     StaticString16 tmp(u"Verify me");
114     String16 another = tmp;
115     EXPECT_STR16EQ(u"Verify me", tmp);
116     EXPECT_STR16EQ(u"Verify me", another);
117     EXPECT_TRUE(tmp.isStaticString());
118     EXPECT_TRUE(another.isStaticString());
119 }
120 
TEST(String16Test,StaticStringMove)121 TEST(String16Test, StaticStringMove) {
122     StaticString16 tmp(u"Verify me");
123     String16 another(std::move(tmp));
124     EXPECT_STR16EQ(u"Verify me", another);
125     EXPECT_TRUE(another.isStaticString());
126 }
127 
TEST(String16Test,StaticStringSize)128 TEST(String16Test, StaticStringSize) {
129     StaticString16 tmp(u"Verify me");
130     EXPECT_EQ(9U, tmp.size());
131 }
132 
TEST(String16Test,StaticStringSetTo)133 TEST(String16Test, StaticStringSetTo) {
134     StaticString16 tmp(u"Verify me");
135     tmp.setTo(u"New content");
136     EXPECT_EQ(11U, tmp.size());
137     EXPECT_STR16EQ(u"New content", tmp);
138     EXPECT_FALSE(tmp.isStaticString());
139 }
140 
TEST(String16Test,StaticStringAppend)141 TEST(String16Test, StaticStringAppend) {
142     StaticString16 tmp(u"Verify me");
143     tmp.append(String16("Hello"));
144     EXPECT_EQ(14U, tmp.size());
145     EXPECT_STR16EQ(u"Verify meHello", tmp);
146     EXPECT_FALSE(tmp.isStaticString());
147 }
148 
TEST(String16Test,StaticStringInsert)149 TEST(String16Test, StaticStringInsert) {
150     StaticString16 tmp(u"Verify me");
151     tmp.insert(6, u"Insert");
152     EXPECT_EQ(15U, tmp.size());
153     EXPECT_STR16EQ(u"VerifyInsert me", tmp);
154     EXPECT_FALSE(tmp.isStaticString());
155 }
156 
TEST(String16Test,StaticStringReplaceAll)157 TEST(String16Test, StaticStringReplaceAll) {
158     StaticString16 tmp(u"Verify verify Verify");
159     tmp.replaceAll(u'r', u'!');
160     EXPECT_STR16EQ(u"Ve!ify ve!ify Ve!ify", tmp);
161     EXPECT_FALSE(tmp.isStaticString());
162 }
163 
TEST(String16Test,StaticStringCompare)164 TEST(String16Test, StaticStringCompare) {
165     StaticString16 tmp(u"Verify me");
166     EXPECT_EQ(String16(u"Verify me"), tmp);
167 }
168 
TEST(String16Test,StringSetToStaticString)169 TEST(String16Test, StringSetToStaticString) {
170     StaticString16 tmp(u"Verify me");
171     String16 another(u"nonstatic");
172     another = tmp;
173     EXPECT_STR16EQ(u"Verify me", tmp);
174     EXPECT_STR16EQ(u"Verify me", another);
175 }
176 
TEST(String16Test,StringMoveFromStaticString)177 TEST(String16Test, StringMoveFromStaticString) {
178     StaticString16 tmp(u"Verify me");
179     String16 another(std::move(tmp));
180     EXPECT_STR16EQ(u"Verify me", another);
181 }
182 
TEST(String16Test,EmptyStringIsStatic)183 TEST(String16Test, EmptyStringIsStatic) {
184     String16 tmp("");
185     EXPECT_TRUE(tmp.isStaticString());
186 }
187 
TEST(String16Test,OverreadUtf8Conversion)188 TEST(String16Test, OverreadUtf8Conversion) {
189     char tmp[] = {'a', static_cast<char>(0xe0), '\0'};
190     String16 another(tmp);
191     EXPECT_TRUE(another.size() == 0);
192 }
193 
TEST(String16Test,ValidUtf8Conversion)194 TEST(String16Test, ValidUtf8Conversion) {
195     String16 another("abcdef");
196     EXPECT_EQ(6U, another.size());
197     EXPECT_STR16EQ(another, u"abcdef");
198 }
199 
TEST(String16Test,append)200 TEST(String16Test, append) {
201     String16 s;
202     EXPECT_EQ(OK, s.append(String16(u"foo")));
203     EXPECT_STR16EQ(u"foo", s);
204     EXPECT_EQ(OK, s.append(String16(u"bar")));
205     EXPECT_STR16EQ(u"foobar", s);
206     EXPECT_EQ(OK, s.append(u"baz", 0));
207     EXPECT_STR16EQ(u"foobar", s);
208     EXPECT_EQ(NO_MEMORY, s.append(u"baz", SIZE_MAX));
209     EXPECT_STR16EQ(u"foobar", s);
210 }
211 
TEST(String16Test,insert)212 TEST(String16Test, insert) {
213     String16 s;
214 
215     // Inserting into the empty string inserts at the start.
216     EXPECT_EQ(OK, s.insert(123, u"foo"));
217     EXPECT_STR16EQ(u"foo", s);
218 
219     // Inserting zero characters at any position is okay, but won't expand the string.
220     EXPECT_EQ(OK, s.insert(123, u"foo", 0));
221     EXPECT_STR16EQ(u"foo", s);
222 
223     // Inserting past the end of a non-empty string appends.
224     EXPECT_EQ(OK, s.insert(123, u"bar"));
225     EXPECT_STR16EQ(u"foobar", s);
226 
227     EXPECT_EQ(OK, s.insert(3, u"!"));
228     EXPECT_STR16EQ(u"foo!bar", s);
229 
230     EXPECT_EQ(NO_MEMORY, s.insert(3, u"", SIZE_MAX));
231     EXPECT_STR16EQ(u"foo!bar", s);
232 }
233