1 // Copyright (C) 2011 The Libphonenumber Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Author: Philippe Liard
16
17 #include <iostream>
18
19 #include <gtest/gtest.h>
20
21 #include "phonenumbers/unicodestring.h"
22
23 using std::ostream;
24
25 namespace i18n {
26 namespace phonenumbers {
27
28 // Used by GTest to print the expected and actual results in case of failure.
operator <<(ostream & out,const UnicodeString & s)29 ostream& operator<<(ostream& out, const UnicodeString& s) {
30 string utf8;
31 s.toUTF8String(utf8);
32 out << utf8;
33 return out;
34 }
35
TEST(UnicodeString,ToUTF8StringWithEmptyString)36 TEST(UnicodeString, ToUTF8StringWithEmptyString) {
37 UnicodeString s;
38 string utf8;
39 s.toUTF8String(utf8);
40 EXPECT_EQ("", utf8);
41 }
42
TEST(UnicodeString,ToUTF8String)43 TEST(UnicodeString, ToUTF8String) {
44 UnicodeString s("hello");
45 string utf8;
46 s.toUTF8String(utf8);
47 EXPECT_EQ("hello", utf8);
48 }
49
TEST(UnicodeString,ToUTF8StringWithNonAscii)50 TEST(UnicodeString, ToUTF8StringWithNonAscii) {
51 UnicodeString s("\xEF\xBC\x95\xEF\xBC\x93" /* "53" */);
52 string utf8;
53 s.toUTF8String(utf8);
54 EXPECT_EQ("\xEF\xBC\x95\xEF\xBC\x93", utf8);
55 }
56
TEST(UnicodeString,AppendCodepoint)57 TEST(UnicodeString, AppendCodepoint) {
58 UnicodeString s;
59 s.append('h');
60 ASSERT_EQ(UnicodeString("h"), s);
61 s.append('e');
62 EXPECT_EQ(UnicodeString("he"), s);
63 }
64
TEST(UnicodeString,AppendCodepointWithNonAscii)65 TEST(UnicodeString, AppendCodepointWithNonAscii) {
66 UnicodeString s;
67 s.append(0xFF15 /* 5 */);
68 ASSERT_EQ(UnicodeString("\xEF\xBC\x95" /* 5 */), s);
69 s.append(0xFF13 /* 3 */);
70 EXPECT_EQ(UnicodeString("\xEF\xBC\x95\xEF\xBC\x93" /* 53 */), s);
71 }
72
TEST(UnicodeString,AppendUnicodeString)73 TEST(UnicodeString, AppendUnicodeString) {
74 UnicodeString s;
75 s.append(UnicodeString("he"));
76 ASSERT_EQ(UnicodeString("he"), s);
77 s.append(UnicodeString("llo"));
78 EXPECT_EQ(UnicodeString("hello"), s);
79 }
80
TEST(UnicodeString,AppendUnicodeStringWithNonAscii)81 TEST(UnicodeString, AppendUnicodeStringWithNonAscii) {
82 UnicodeString s;
83 s.append(UnicodeString("\xEF\xBC\x95" /* 5 */));
84 ASSERT_EQ(UnicodeString("\xEF\xBC\x95"), s);
85 s.append(UnicodeString("\xEF\xBC\x93" /* 3 */));
86 EXPECT_EQ(UnicodeString("\xEF\xBC\x95\xEF\xBC\x93" /* 53 */), s);
87 }
88
TEST(UnicodeString,IndexOf)89 TEST(UnicodeString, IndexOf) {
90 UnicodeString s("hello");
91 EXPECT_EQ(0, s.indexOf('h'));
92 EXPECT_EQ(2, s.indexOf('l'));
93 EXPECT_EQ(4, s.indexOf('o'));
94 }
95
TEST(UnicodeString,IndexOfWithNonAscii)96 TEST(UnicodeString, IndexOfWithNonAscii) {
97 UnicodeString s("\xEF\xBC\x95\xEF\xBC\x93" /* 53 */);
98 EXPECT_EQ(1, s.indexOf(0xFF13 /* 3 */));
99 }
100
TEST(UnicodeString,ReplaceWithEmptyInputs)101 TEST(UnicodeString, ReplaceWithEmptyInputs) {
102 UnicodeString s;
103 s.replace(0, 0, UnicodeString(""));
104 EXPECT_EQ(UnicodeString(""), s);
105 }
106
TEST(UnicodeString,ReplaceWithEmptyReplacement)107 TEST(UnicodeString, ReplaceWithEmptyReplacement) {
108 UnicodeString s("hello");
109 s.replace(0, 5, UnicodeString(""));
110 EXPECT_EQ(UnicodeString(""), s);
111 }
112
TEST(UnicodeString,ReplaceBegining)113 TEST(UnicodeString, ReplaceBegining) {
114 UnicodeString s("hello world");
115 s.replace(0, 5, UnicodeString("HELLO"));
116 EXPECT_EQ(UnicodeString("HELLO world"), s);
117 }
118
TEST(UnicodeString,ReplaceMiddle)119 TEST(UnicodeString, ReplaceMiddle) {
120 UnicodeString s("hello world");
121 s.replace(5, 1, UnicodeString("AB"));
122 EXPECT_EQ(UnicodeString("helloABworld"), s);
123 }
124
TEST(UnicodeString,ReplaceEnd)125 TEST(UnicodeString, ReplaceEnd) {
126 UnicodeString s("hello world");
127 s.replace(10, 1, UnicodeString("AB"));
128 EXPECT_EQ(UnicodeString("hello worlAB"), s);
129 }
130
TEST(UnicodeString,ReplaceWithNonAscii)131 TEST(UnicodeString, ReplaceWithNonAscii) {
132 UnicodeString s("hello world");
133 s.replace(3, 2, UnicodeString("\xEF\xBC\x91\xEF\xBC\x90" /* 10 */));
134 EXPECT_EQ(UnicodeString("hel\xEF\xBC\x91\xEF\xBC\x90 world"), s);
135 }
136
TEST(UnicodeString,SetCharBegining)137 TEST(UnicodeString, SetCharBegining) {
138 UnicodeString s("hello");
139 s.setCharAt(0, 'H');
140 EXPECT_EQ(UnicodeString("Hello"), s);
141 }
142
TEST(UnicodeString,SetCharMiddle)143 TEST(UnicodeString, SetCharMiddle) {
144 UnicodeString s("hello");
145 s.setCharAt(2, 'L');
146 EXPECT_EQ(UnicodeString("heLlo"), s);
147 }
148
TEST(UnicodeString,SetCharEnd)149 TEST(UnicodeString, SetCharEnd) {
150 UnicodeString s("hello");
151 s.setCharAt(4, 'O');
152 EXPECT_EQ(UnicodeString("hellO"), s);
153 }
154
TEST(UnicodeString,SetCharWithNonAscii)155 TEST(UnicodeString, SetCharWithNonAscii) {
156 UnicodeString s("hello");
157 s.setCharAt(4, 0xFF10 /* 0 */);
158 EXPECT_EQ(UnicodeString("hell\xEF\xBC\x90" /* 0 */), s);
159 }
160
TEST(UnicodeString,TempSubStringWithEmptyString)161 TEST(UnicodeString, TempSubStringWithEmptyString) {
162 EXPECT_EQ(UnicodeString(""), UnicodeString().tempSubString(0, 0));
163 }
164
TEST(UnicodeString,TempSubStringWithInvalidInputs)165 TEST(UnicodeString, TempSubStringWithInvalidInputs) {
166 UnicodeString s("hello");
167 // tempSubString() returns an empty unicode string if one of the provided
168 // paramaters is out of range.
169 EXPECT_EQ(UnicodeString(""), s.tempSubString(6));
170 EXPECT_EQ(UnicodeString(""), s.tempSubString(2, 6));
171 }
172
TEST(UnicodeString,TempSubString)173 TEST(UnicodeString, TempSubString) {
174 UnicodeString s("hello");
175 EXPECT_EQ(UnicodeString(""), s.tempSubString(0, 0));
176 EXPECT_EQ(UnicodeString("h"), s.tempSubString(0, 1));
177 EXPECT_EQ(UnicodeString("hello"), s.tempSubString(0, 5));
178 EXPECT_EQ(UnicodeString("llo"), s.tempSubString(2, 3));
179 }
180
TEST(UnicodeString,TempSubStringWithNoLength)181 TEST(UnicodeString, TempSubStringWithNoLength) {
182 UnicodeString s("hello");
183 EXPECT_EQ(UnicodeString("hello"), s.tempSubString(0));
184 EXPECT_EQ(UnicodeString("llo"), s.tempSubString(2));
185 }
186
TEST(UnicodeString,TempSubStringWithNonAscii)187 TEST(UnicodeString, TempSubStringWithNonAscii) {
188 UnicodeString s("hel\xEF\xBC\x91\xEF\xBC\x90" /* 10 */);
189 EXPECT_EQ(UnicodeString("\xEF\xBC\x91" /* 1 */), s.tempSubString(3, 1));
190 }
191
TEST(UnicodeString,OperatorEqual)192 TEST(UnicodeString, OperatorEqual) {
193 UnicodeString s("hello");
194 s = UnicodeString("Hello");
195 EXPECT_EQ(UnicodeString("Hello"), s);
196 }
197
TEST(UnicodeString,OperatorEqualWithNonAscii)198 TEST(UnicodeString, OperatorEqualWithNonAscii) {
199 UnicodeString s("hello");
200 s = UnicodeString("hel\xEF\xBC\x91\xEF\xBC\x90" /* 10 */);
201 EXPECT_EQ(UnicodeString("hel\xEF\xBC\x91\xEF\xBC\x90"), s);
202 }
203
TEST(UnicodeString,OperatorBracket)204 TEST(UnicodeString, OperatorBracket) {
205 UnicodeString s("hello");
206 EXPECT_EQ('h', s[0]);
207 EXPECT_EQ('e', s[1]);
208 EXPECT_EQ('l', s[2]);
209 EXPECT_EQ('l', s[3]);
210 EXPECT_EQ('o', s[4]);
211 }
212
TEST(UnicodeString,OperatorBracketWithNonAscii)213 TEST(UnicodeString, OperatorBracketWithNonAscii) {
214 UnicodeString s("hel\xEF\xBC\x91\xEF\xBC\x90" /* 10 */);
215 EXPECT_EQ('h', s[0]);
216 EXPECT_EQ('e', s[1]);
217 EXPECT_EQ('l', s[2]);
218 EXPECT_EQ(0xFF11 /* 1 */, s[3]);
219 EXPECT_EQ(0xFF10 /* 0 */, s[4]);
220 }
221
TEST(UnicodeString,OperatorBracketWithIteratorCacheInvalidation)222 TEST(UnicodeString, OperatorBracketWithIteratorCacheInvalidation) {
223 UnicodeString s("hello");
224 EXPECT_EQ('h', s[0]);
225 EXPECT_EQ('e', s[1]);
226 // Modify the string which should invalidate the iterator cache.
227 s.setCharAt(1, 'E');
228 EXPECT_EQ(UnicodeString("hEllo"), s);
229 EXPECT_EQ('E', s[1]);
230 // Get the previous character which should invalidate the iterator cache.
231 EXPECT_EQ('h', s[0]);
232 EXPECT_EQ('o', s[4]);
233 }
234
235 } // namespace phonenumbers
236 } // namespace i18n
237