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
23using std::ostream;
24
25namespace i18n {
26namespace phonenumbers {
27
28// Used by GTest to print the expected and actual results in case of failure.
29ostream& operator<<(ostream& out, const UnicodeString& s) {
30  string utf8;
31  s.toUTF8String(utf8);
32  out << utf8;
33  return out;
34}
35
36TEST(UnicodeString, ToUTF8StringWithEmptyString) {
37  UnicodeString s;
38  string utf8;
39  s.toUTF8String(utf8);
40  EXPECT_EQ("", utf8);
41}
42
43TEST(UnicodeString, ToUTF8String) {
44  UnicodeString s("hello");
45  string utf8;
46  s.toUTF8String(utf8);
47  EXPECT_EQ("hello", utf8);
48}
49
50TEST(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
57TEST(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
65TEST(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
73TEST(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
81TEST(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
89TEST(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
96TEST(UnicodeString, IndexOfWithNonAscii) {
97  UnicodeString s("\xEF\xBC\x95\xEF\xBC\x93" /* 53 */);
98  EXPECT_EQ(1, s.indexOf(0xFF13 /* 3 */));
99}
100
101TEST(UnicodeString, ReplaceWithEmptyInputs) {
102  UnicodeString s;
103  s.replace(0, 0, UnicodeString(""));
104  EXPECT_EQ(UnicodeString(""), s);
105}
106
107TEST(UnicodeString, ReplaceWithEmptyReplacement) {
108  UnicodeString s("hello");
109  s.replace(0, 5, UnicodeString(""));
110  EXPECT_EQ(UnicodeString(""), s);
111}
112
113TEST(UnicodeString, ReplaceBegining) {
114  UnicodeString s("hello world");
115  s.replace(0, 5, UnicodeString("HELLO"));
116  EXPECT_EQ(UnicodeString("HELLO world"), s);
117}
118
119TEST(UnicodeString, ReplaceMiddle) {
120  UnicodeString s("hello world");
121  s.replace(5, 1, UnicodeString("AB"));
122  EXPECT_EQ(UnicodeString("helloABworld"), s);
123}
124
125TEST(UnicodeString, ReplaceEnd) {
126  UnicodeString s("hello world");
127  s.replace(10, 1, UnicodeString("AB"));
128  EXPECT_EQ(UnicodeString("hello worlAB"), s);
129}
130
131TEST(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
137TEST(UnicodeString, SetCharBegining) {
138  UnicodeString s("hello");
139  s.setCharAt(0, 'H');
140  EXPECT_EQ(UnicodeString("Hello"), s);
141}
142
143TEST(UnicodeString, SetCharMiddle) {
144  UnicodeString s("hello");
145  s.setCharAt(2, 'L');
146  EXPECT_EQ(UnicodeString("heLlo"), s);
147}
148
149TEST(UnicodeString, SetCharEnd) {
150  UnicodeString s("hello");
151  s.setCharAt(4, 'O');
152  EXPECT_EQ(UnicodeString("hellO"), s);
153}
154
155TEST(UnicodeString, SetCharWithNonAscii) {
156  UnicodeString s("hello");
157  s.setCharAt(4, 0xFF10 /* 0 */);
158  EXPECT_EQ(UnicodeString("hell\xEF\xBC\x90" /* 0 */), s);
159}
160
161TEST(UnicodeString, TempSubStringWithEmptyString) {
162  EXPECT_EQ(UnicodeString(""), UnicodeString().tempSubString(0, 0));
163}
164
165TEST(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
173TEST(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
181TEST(UnicodeString, TempSubStringWithNoLength) {
182  UnicodeString s("hello");
183  EXPECT_EQ(UnicodeString("hello"), s.tempSubString(0));
184  EXPECT_EQ(UnicodeString("llo"), s.tempSubString(2));
185}
186
187TEST(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
192TEST(UnicodeString, OperatorEqual) {
193  UnicodeString s("hello");
194  s = UnicodeString("Hello");
195  EXPECT_EQ(UnicodeString("Hello"), s);
196}
197
198TEST(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
204TEST(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
213TEST(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
222TEST(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