• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "phonenumbers/stringutil.h"
18 
19 #include <string>
20 #include <vector>
21 
22 #include <gtest/gtest.h>
23 
24 using std::string;
25 using std::vector;
26 
27 namespace i18n {
28 namespace phonenumbers {
29 
30 // Test operator+(const string&, int).
TEST(StringUtilTest,OperatorPlus)31 TEST(StringUtilTest, OperatorPlus) {
32   EXPECT_EQ("hello10", string("hello") + 10);
33 }
34 
35 // Test SimpleItoa implementation.
TEST(StringUtilTest,SimpleItoa)36 TEST(StringUtilTest, SimpleItoa) {
37   EXPECT_EQ("10", SimpleItoa(10));
38 }
39 
TEST(StringUtilTest,HasPrefixString)40 TEST(StringUtilTest, HasPrefixString) {
41   EXPECT_TRUE(HasPrefixString("hello world", "hello"));
42   EXPECT_FALSE(HasPrefixString("hello world", "hellO"));
43 }
44 
TEST(StringUtilTest,FindNthWithEmptyString)45 TEST(StringUtilTest, FindNthWithEmptyString) {
46   EXPECT_EQ(string::npos, FindNth("", 'a', 1));
47 }
48 
TEST(StringUtilTest,FindNthWithNNegative)49 TEST(StringUtilTest, FindNthWithNNegative) {
50   EXPECT_EQ(string::npos, FindNth("hello world", 'o', -1));
51 }
52 
TEST(StringUtilTest,FindNthWithNTooHigh)53 TEST(StringUtilTest, FindNthWithNTooHigh) {
54   EXPECT_EQ(string::npos, FindNth("hello world", 'o', 3));
55 }
56 
TEST(StringUtilTest,FindNth)57 TEST(StringUtilTest, FindNth) {
58   EXPECT_EQ(7U, FindNth("hello world", 'o', 2));
59 }
60 
TEST(StringUtilTest,SplitStringUsingWithEmptyString)61 TEST(StringUtilTest, SplitStringUsingWithEmptyString) {
62   vector<string> result;
63   SplitStringUsing("", ":", &result);
64   EXPECT_EQ(0U, result.size());
65 }
66 
TEST(StringUtilTest,SplitStringUsingWithEmptyDelimiter)67 TEST(StringUtilTest, SplitStringUsingWithEmptyDelimiter) {
68   vector<string> result;
69   SplitStringUsing("hello", "", &result);
70   EXPECT_EQ(0U, result.size());
71 }
72 
TEST(StringUtilTest,SplitStringUsing)73 TEST(StringUtilTest, SplitStringUsing) {
74   vector<string> result;
75   SplitStringUsing(":hello:world:", ":", &result);
76   EXPECT_EQ(2U, result.size());
77   EXPECT_EQ("hello", result[0]);
78   EXPECT_EQ("world", result[1]);
79 }
80 
TEST(StringUtilTest,SplitStringUsingIgnoresEmptyToken)81 TEST(StringUtilTest, SplitStringUsingIgnoresEmptyToken) {
82   vector<string> result;
83   SplitStringUsing("hello::world", ":", &result);
84   EXPECT_EQ(2U, result.size());
85   EXPECT_EQ("hello", result[0]);
86   EXPECT_EQ("world", result[1]);
87 }
88 
89 // Test TryStripPrefixString.
TEST(StringUtilTest,TryStripPrefixString)90 TEST(StringUtilTest, TryStripPrefixString) {
91   string s;
92 
93   EXPECT_TRUE(TryStripPrefixString("hello world", "hello", &s));
94   EXPECT_EQ(" world", s);
95   s.clear();
96 
97   EXPECT_FALSE(TryStripPrefixString("hello world", "helloa", &s));
98   s.clear();
99 
100   EXPECT_TRUE(TryStripPrefixString("hello world", "", &s));
101   EXPECT_EQ("hello world", s);
102   s.clear();
103 
104   EXPECT_FALSE(TryStripPrefixString("", "hello", &s));
105   s.clear();
106 }
107 
108 // Test HasSuffixString.
TEST(StringUtilTest,HasSuffixString)109 TEST(StringUtilTest, HasSuffixString) {
110   EXPECT_TRUE(HasSuffixString("hello world", "hello world"));
111   EXPECT_TRUE(HasSuffixString("hello world", "world"));
112   EXPECT_FALSE(HasSuffixString("hello world", "world!"));
113   EXPECT_TRUE(HasSuffixString("hello world", ""));
114   EXPECT_FALSE(HasSuffixString("", "hello"));
115 }
116 
117 // Test safe_strto32.
TEST(StringUtilTest,safe_strto32)118 TEST(StringUtilTest, safe_strto32) {
119   int32 n;
120 
121   safe_strto32("0", &n);
122   EXPECT_EQ(0, n);
123 
124   safe_strto32("16", &n);
125   EXPECT_EQ(16, n);
126 
127   safe_strto32("2147483647", &n);
128   EXPECT_EQ(2147483647, n);
129 
130   safe_strto32("-2147483648", &n);
131   EXPECT_EQ(-2147483648LL, n);
132 }
133 
134 // Test safe_strtou64.
TEST(StringUtilTest,safe_strtou64)135 TEST(StringUtilTest, safe_strtou64) {
136   uint64 n;
137 
138   safe_strtou64("0", &n);
139   EXPECT_EQ(0U, n);
140 
141   safe_strtou64("16", &n);
142   EXPECT_EQ(16U, n);
143 
144   safe_strtou64("18446744073709551615UL", &n);
145   EXPECT_EQ(18446744073709551615ULL, n);
146 }
147 
148 // Test strrmm.
TEST(StringUtilTest,strrmm)149 TEST(StringUtilTest, strrmm) {
150   string input("hello");
151 
152   strrmm(&input, "");
153   EXPECT_EQ(input, input);
154 
155   string empty;
156   strrmm(&empty, "");
157   EXPECT_EQ("", empty);
158 
159   strrmm(&empty, "aa");
160   EXPECT_EQ("", empty);
161 
162   strrmm(&input, "h");
163   EXPECT_EQ("ello", input);
164 
165   strrmm(&input, "el");
166   EXPECT_EQ("o", input);
167 }
168 
169 // Test GlobalReplaceSubstring.
TEST(StringUtilTest,GlobalReplaceSubstring)170 TEST(StringUtilTest, GlobalReplaceSubstring) {
171   string input("hello");
172 
173   EXPECT_EQ(0, GlobalReplaceSubstring("aaa", "", &input));
174   EXPECT_EQ("hello", input);
175 
176   EXPECT_EQ(0, GlobalReplaceSubstring("", "aaa", &input));
177   EXPECT_EQ("hello", input);
178 
179   EXPECT_EQ(0, GlobalReplaceSubstring("", "", &input));
180   EXPECT_EQ("hello", input);
181 
182   EXPECT_EQ(0, GlobalReplaceSubstring("aaa", "bbb", &input));
183   EXPECT_EQ("hello", input);
184 
185   EXPECT_EQ(1, GlobalReplaceSubstring("o", "o world", &input));
186   ASSERT_EQ("hello world", input);
187 
188   EXPECT_EQ(2, GlobalReplaceSubstring("o", "O", &input));
189   EXPECT_EQ("hellO wOrld", input);
190 }
191 
192 // Test the StringHolder class.
TEST(StringUtilTest,StringHolder)193 TEST(StringUtilTest, StringHolder) {
194   // Test with C string.
195   static const char cstring[] = "aaa";
196   StringHolder sh1(cstring);
197   EXPECT_EQ(cstring, sh1.GetCString());
198   EXPECT_EQ(NULL, sh1.GetString());
199 
200   // Test with std::string.
201   string s = "bbb";
202   StringHolder sh2(s);
203   EXPECT_EQ(NULL, sh2.GetCString());
204   EXPECT_EQ(&s, sh2.GetString());
205 
206   // Test GetLength().
207   string s2 = "hello";
208   StringHolder sh3(s2);
209   EXPECT_EQ(5U, sh3.Length());
210 
211   // Test with uint64.
212   StringHolder sh4(42);
213   EXPECT_TRUE(sh4.GetCString() == NULL);
214   EXPECT_EQ(2U, sh4.Length());
215   EXPECT_EQ("42", *sh4.GetString());
216 }
217 
218 // Test the operator+=(string& lhs, const StringHolder& rhs) implementation.
TEST(StringUtilTest,OperatorPlusEquals)219 TEST(StringUtilTest, OperatorPlusEquals) {
220   // Test with a const char* string to append.
221   string s = "h";
222   static const char append1[] = "ello";
223   s += StringHolder(append1);   // force StringHolder usage
224 
225   EXPECT_EQ("hello", s);
226 
227   // Test with a std::string to append.
228   s = "h";
229   string append2 = "ello";
230   s += StringHolder(append2);   // force StringHolder usage
231 
232   EXPECT_EQ("hello", s);
233 }
234 
235 // Test the StrCat implementations
TEST(StringUtilTest,StrCat)236 TEST(StringUtilTest, StrCat) {
237   string s;
238 
239   // Test with 2 arguments.
240   s = StrCat("a", "b");
241   EXPECT_EQ("ab", s);
242 
243   // Test with 3 arguments.
244   s = StrCat("a", "b", "c");
245   EXPECT_EQ("abc", s);
246 
247   // Test with 4 arguments.
248   s = StrCat("a", "b", "c", "d");
249   EXPECT_EQ("abcd", s);
250 
251   // Test with 5 arguments.
252   s = StrCat("a", "b", "c", "d", "e");
253   EXPECT_EQ("abcde", s);
254 
255   // Test with 6 arguments.
256   s = StrCat("a", "b", "c", "d", "e", "f");
257   EXPECT_EQ("abcdef", s);
258 
259   // Test with 7 arguments.
260   s = StrCat("a", "b", "c", "d", "e", "f", "g");
261   EXPECT_EQ("abcdefg", s);
262 
263   // Test with 8 arguments.
264   s = StrCat("a", "b", "c", "d", "e", "f", "g", "h");
265   EXPECT_EQ("abcdefgh", s);
266 
267   // Test with 9 arguments.
268   s = StrCat("a", "b", "c", "d", "e", "f", "g", "h", "i");
269   EXPECT_EQ("abcdefghi", s);
270 
271   // Test with 11 arguments.
272   s = StrCat("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k");
273   EXPECT_EQ("abcdefghijk", s);
274 }
275 
276 // Test the StrAppend implementations.
TEST(StringUtilTest,StrAppend)277 TEST(StringUtilTest, StrAppend) {
278   string s;
279 
280   // Test with 1 argument.
281   StrAppend(&s, "a");
282   ASSERT_EQ("a", s);
283 
284   // Test with 2 arguments.
285   StrAppend(&s, "b", "c");
286   ASSERT_EQ("abc", s);
287 
288   // Test with 3 arguments.
289   StrAppend(&s, "d", "e", "f");
290   ASSERT_EQ("abcdef", s);
291 
292   // Test with 4 arguments.
293   StrAppend(&s, "g", "h", "i", "j");
294   ASSERT_EQ("abcdefghij", s);
295 
296   // Test with 5 arguments.
297   StrAppend(&s, "k", "l", "m", "n", "o");
298   ASSERT_EQ("abcdefghijklmno", s);
299 
300   // Test with int argument.
301   StrAppend(&s, 42);
302   ASSERT_EQ("abcdefghijklmno42", s);
303 }
304 
305 }  // namespace phonenumbers
306 }  // namespace i18n
307