OLD | NEW |
| (Empty) |
1 // Copyright (C) 2011 Google Inc. | |
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 <gtest/gtest.h> | |
18 | |
19 #include "stringutil.h" | |
20 | |
21 namespace i18n { | |
22 namespace phonenumbers { | |
23 | |
24 // Test operator+(const string&, int). | |
25 TEST(StringUtilTest, OperatorPlus) { | |
26 EXPECT_EQ("hello10", string("hello") + 10); | |
27 } | |
28 | |
29 // Test SimpleItoa implementation. | |
30 TEST(StringUtilTest, SimpleItoa) { | |
31 EXPECT_EQ("10", SimpleItoa(10)); | |
32 } | |
33 | |
34 // Test TryStripPrefixString. | |
35 TEST(StringUtilTest, TryStripPrefixString) { | |
36 string s; | |
37 | |
38 EXPECT_TRUE(TryStripPrefixString("hello world", "hello", &s)); | |
39 EXPECT_EQ(" world", s); | |
40 s.clear(); | |
41 | |
42 EXPECT_FALSE(TryStripPrefixString("hello world", "helloa", &s)); | |
43 s.clear(); | |
44 | |
45 EXPECT_TRUE(TryStripPrefixString("hello world", "", &s)); | |
46 EXPECT_EQ("hello world", s); | |
47 s.clear(); | |
48 | |
49 EXPECT_FALSE(TryStripPrefixString("", "hello", &s)); | |
50 s.clear(); | |
51 } | |
52 | |
53 // Test HasSuffixString. | |
54 TEST(StringUtilTest, HasSuffixString) { | |
55 EXPECT_TRUE(HasSuffixString("hello world", "hello world")); | |
56 EXPECT_TRUE(HasSuffixString("hello world", "world")); | |
57 EXPECT_FALSE(HasSuffixString("hello world", "world!")); | |
58 EXPECT_TRUE(HasSuffixString("hello world", "")); | |
59 EXPECT_FALSE(HasSuffixString("", "hello")); | |
60 } | |
61 | |
62 // Test safe_strto32. | |
63 TEST(StringUtilTest, safe_strto32) { | |
64 int32 n; | |
65 | |
66 safe_strto32("0", &n); | |
67 EXPECT_EQ(0, n); | |
68 | |
69 safe_strto32("16", &n); | |
70 EXPECT_EQ(16, n); | |
71 | |
72 safe_strto32("2147483647", &n); | |
73 EXPECT_EQ(2147483647, n); | |
74 | |
75 safe_strto32("-2147483648", &n); | |
76 EXPECT_EQ(-2147483648LL, n); | |
77 } | |
78 | |
79 // Test safe_strtou64. | |
80 TEST(StringUtilTest, safe_strtou64) { | |
81 uint64 n; | |
82 | |
83 safe_strtou64("0", &n); | |
84 EXPECT_EQ(0U, n); | |
85 | |
86 safe_strtou64("16", &n); | |
87 EXPECT_EQ(16U, n); | |
88 | |
89 safe_strtou64("18446744073709551615UL", &n); | |
90 EXPECT_EQ(18446744073709551615ULL, n); | |
91 } | |
92 | |
93 // Test strrmm. | |
94 TEST(StringUtilTest, strrmm) { | |
95 string input("hello"); | |
96 | |
97 strrmm(&input, ""); | |
98 EXPECT_EQ(input, input); | |
99 | |
100 string empty; | |
101 strrmm(&empty, ""); | |
102 EXPECT_EQ("", empty); | |
103 | |
104 strrmm(&empty, "aa"); | |
105 EXPECT_EQ("", empty); | |
106 | |
107 strrmm(&input, "h"); | |
108 EXPECT_EQ("ello", input); | |
109 | |
110 strrmm(&input, "el"); | |
111 EXPECT_EQ("o", input); | |
112 } | |
113 | |
114 // Test GlobalReplaceSubstring. | |
115 TEST(StringUtilTest, GlobalReplaceSubstring) { | |
116 string input("hello"); | |
117 | |
118 EXPECT_EQ(0, GlobalReplaceSubstring("aaa", "", &input)); | |
119 EXPECT_EQ("hello", input); | |
120 | |
121 EXPECT_EQ(0, GlobalReplaceSubstring("", "aaa", &input)); | |
122 EXPECT_EQ("hello", input); | |
123 | |
124 EXPECT_EQ(0, GlobalReplaceSubstring("", "", &input)); | |
125 EXPECT_EQ("hello", input); | |
126 | |
127 EXPECT_EQ(0, GlobalReplaceSubstring("aaa", "bbb", &input)); | |
128 EXPECT_EQ("hello", input); | |
129 | |
130 EXPECT_EQ(1, GlobalReplaceSubstring("o", "o world", &input)); | |
131 ASSERT_EQ("hello world", input); | |
132 | |
133 EXPECT_EQ(2, GlobalReplaceSubstring("o", "O", &input)); | |
134 EXPECT_EQ("hellO wOrld", input); | |
135 } | |
136 | |
137 // Test the StringHolder class. | |
138 TEST(StringUtilTest, StringHolder) { | |
139 // Test with C string. | |
140 static const char cstring[] = "aaa"; | |
141 StringHolder sh1(cstring); | |
142 EXPECT_EQ(cstring, sh1.GetCString()); | |
143 EXPECT_EQ(NULL, sh1.GetString()); | |
144 | |
145 // Test with std::string. | |
146 string s = "bbb"; | |
147 StringHolder sh2(s); | |
148 EXPECT_EQ(NULL, sh2.GetCString()); | |
149 EXPECT_EQ(&s, sh2.GetString()); | |
150 | |
151 // Test GetLength(). | |
152 string s2 = "hello"; | |
153 StringHolder sh3(s2); | |
154 EXPECT_EQ(5U, sh3.Length()); | |
155 | |
156 // Test with uint64. | |
157 StringHolder sh4(42); | |
158 EXPECT_TRUE(sh4.GetCString() == NULL); | |
159 EXPECT_EQ(2U, sh4.Length()); | |
160 EXPECT_EQ("42", *sh4.GetString()); | |
161 } | |
162 | |
163 // Test the operator+=(string& lhs, const StringHolder& rhs) implementation. | |
164 TEST(StringUtilTest, OperatorPlusEquals) { | |
165 // Test with a const char* string to append. | |
166 string s = "h"; | |
167 static const char append1[] = "ello"; | |
168 s += StringHolder(append1); // force StringHolder usage | |
169 | |
170 EXPECT_EQ("hello", s); | |
171 | |
172 // Test with a std::string to append. | |
173 s = "h"; | |
174 string append2 = "ello"; | |
175 s += StringHolder(append2); // force StringHolder usage | |
176 | |
177 EXPECT_EQ("hello", s); | |
178 } | |
179 | |
180 // Test the StrCat implementations | |
181 TEST(StringUtilTest, StrCat) { | |
182 string s; | |
183 | |
184 // Test with 2 arguments. | |
185 s = StrCat("a", "b"); | |
186 EXPECT_EQ("ab", s); | |
187 | |
188 // Test with 3 arguments. | |
189 s = StrCat("a", "b", "c"); | |
190 EXPECT_EQ("abc", s); | |
191 | |
192 // Test with 4 arguments. | |
193 s = StrCat("a", "b", "c", "d"); | |
194 EXPECT_EQ("abcd", s); | |
195 | |
196 // Test with 5 arguments. | |
197 s = StrCat("a", "b", "c", "d", "e"); | |
198 EXPECT_EQ("abcde", s); | |
199 | |
200 // Test with 6 arguments. | |
201 s = StrCat("a", "b", "c", "d", "e", "f"); | |
202 EXPECT_EQ("abcdef", s); | |
203 | |
204 // Test with 7 arguments. | |
205 s = StrCat("a", "b", "c", "d", "e", "f", "g"); | |
206 EXPECT_EQ("abcdefg", s); | |
207 | |
208 // Test with 11 arguments. | |
209 s = StrCat("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"); | |
210 EXPECT_EQ("abcdefghijk", s); | |
211 } | |
212 | |
213 // Test the StrAppend implementations. | |
214 TEST(StringUtilTest, StrAppend) { | |
215 string s; | |
216 | |
217 // Test with 1 argument. | |
218 StrAppend(&s, "a"); | |
219 EXPECT_EQ("a", s); | |
220 | |
221 // Test with 2 arguments. | |
222 StrAppend(&s, "b", "c"); | |
223 EXPECT_EQ("abc", s); | |
224 | |
225 // Test with int argument. | |
226 StrAppend(&s, 42); | |
227 EXPECT_EQ("abc42", s); | |
228 } | |
229 | |
230 } // namespace phonenumbers | |
231 } // namespace i18n | |
OLD | NEW |