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 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,Remove)93 TEST(String16Test, Remove) {
94     String16 tmp("Verify me");
95     tmp.remove(2, 6);
96     EXPECT_EQ(2U, tmp.size());
97     EXPECT_STR16EQ(u" m", tmp);
98 }
99 
TEST(String16Test,MakeLower)100 TEST(String16Test, MakeLower) {
101     String16 tmp("Verify Me!");
102     tmp.makeLower();
103     EXPECT_EQ(10U, tmp.size());
104     EXPECT_STR16EQ(u"verify me!", tmp);
105 }
106 
TEST(String16Test,ReplaceAll)107 TEST(String16Test, ReplaceAll) {
108     String16 tmp("Verify verify Verify");
109     tmp.replaceAll(u'r', u'!');
110     EXPECT_STR16EQ(u"Ve!ify ve!ify Ve!ify", tmp);
111 }
112 
TEST(String16Test,Compare)113 TEST(String16Test, Compare) {
114     String16 tmp("Verify me");
115     EXPECT_EQ(String16(u"Verify me"), tmp);
116 }
117 
TEST(String16Test,StaticString)118 TEST(String16Test, StaticString) {
119     String16 nonStaticString("NonStatic");
120     StaticString16 staticString(u"Static");
121 
122     EXPECT_TRUE(staticString.isStaticString());
123     EXPECT_FALSE(nonStaticString.isStaticString());
124 }
125 
TEST(String16Test,StaticStringCopy)126 TEST(String16Test, StaticStringCopy) {
127     StaticString16 tmp(u"Verify me");
128     String16 another = tmp;
129     EXPECT_STR16EQ(u"Verify me", tmp);
130     EXPECT_STR16EQ(u"Verify me", another);
131     EXPECT_TRUE(tmp.isStaticString());
132     EXPECT_TRUE(another.isStaticString());
133 }
134 
TEST(String16Test,StaticStringMove)135 TEST(String16Test, StaticStringMove) {
136     StaticString16 tmp(u"Verify me");
137     String16 another(std::move(tmp));
138     EXPECT_STR16EQ(u"Verify me", another);
139     EXPECT_TRUE(another.isStaticString());
140 }
141 
TEST(String16Test,StaticStringSize)142 TEST(String16Test, StaticStringSize) {
143     StaticString16 tmp(u"Verify me");
144     EXPECT_EQ(9U, tmp.size());
145 }
146 
TEST(String16Test,StaticStringSetTo)147 TEST(String16Test, StaticStringSetTo) {
148     StaticString16 tmp(u"Verify me");
149     tmp.setTo(u"New content");
150     EXPECT_EQ(11U, tmp.size());
151     EXPECT_STR16EQ(u"New content", tmp);
152     EXPECT_FALSE(tmp.isStaticString());
153 }
154 
TEST(String16Test,StaticStringAppend)155 TEST(String16Test, StaticStringAppend) {
156     StaticString16 tmp(u"Verify me");
157     tmp.append(String16("Hello"));
158     EXPECT_EQ(14U, tmp.size());
159     EXPECT_STR16EQ(u"Verify meHello", tmp);
160     EXPECT_FALSE(tmp.isStaticString());
161 }
162 
TEST(String16Test,StaticStringInsert)163 TEST(String16Test, StaticStringInsert) {
164     StaticString16 tmp(u"Verify me");
165     tmp.insert(6, u"Insert");
166     EXPECT_EQ(15U, tmp.size());
167     EXPECT_STR16EQ(u"VerifyInsert me", tmp);
168     EXPECT_FALSE(tmp.isStaticString());
169 }
170 
TEST(String16Test,StaticStringRemove)171 TEST(String16Test, StaticStringRemove) {
172     StaticString16 tmp(u"Verify me");
173     tmp.remove(2, 6);
174     EXPECT_EQ(2U, tmp.size());
175     EXPECT_STR16EQ(u" m", tmp);
176     EXPECT_FALSE(tmp.isStaticString());
177 }
178 
TEST(String16Test,StaticStringMakeLower)179 TEST(String16Test, StaticStringMakeLower) {
180     StaticString16 tmp(u"Verify me!");
181     tmp.makeLower();
182     EXPECT_EQ(10U, tmp.size());
183     EXPECT_STR16EQ(u"verify me!", tmp);
184     EXPECT_FALSE(tmp.isStaticString());
185 }
186 
TEST(String16Test,StaticStringReplaceAll)187 TEST(String16Test, StaticStringReplaceAll) {
188     StaticString16 tmp(u"Verify verify Verify");
189     tmp.replaceAll(u'r', u'!');
190     EXPECT_STR16EQ(u"Ve!ify ve!ify Ve!ify", tmp);
191     EXPECT_FALSE(tmp.isStaticString());
192 }
193 
TEST(String16Test,StaticStringCompare)194 TEST(String16Test, StaticStringCompare) {
195     StaticString16 tmp(u"Verify me");
196     EXPECT_EQ(String16(u"Verify me"), tmp);
197 }
198 
TEST(String16Test,StringSetToStaticString)199 TEST(String16Test, StringSetToStaticString) {
200     StaticString16 tmp(u"Verify me");
201     String16 another(u"nonstatic");
202     another = tmp;
203     EXPECT_STR16EQ(u"Verify me", tmp);
204     EXPECT_STR16EQ(u"Verify me", another);
205 }
206 
TEST(String16Test,StringMoveFromStaticString)207 TEST(String16Test, StringMoveFromStaticString) {
208     StaticString16 tmp(u"Verify me");
209     String16 another(std::move(tmp));
210     EXPECT_STR16EQ(u"Verify me", another);
211 }
212 
TEST(String16Test,EmptyStringIsStatic)213 TEST(String16Test, EmptyStringIsStatic) {
214     String16 tmp("");
215     EXPECT_TRUE(tmp.isStaticString());
216 }
217 
218 }  // namespace android
219