OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <math.h> | 5 #include <math.h> |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 {"-+123", 0, false}, | 98 {"-+123", 0, false}, |
99 {"+-123", 0, false}, | 99 {"+-123", 0, false}, |
100 {"-", 0, false}, | 100 {"-", 0, false}, |
101 {"-2147483649", INT_MIN, false}, | 101 {"-2147483649", INT_MIN, false}, |
102 {"-99999999999", INT_MIN, false}, | 102 {"-99999999999", INT_MIN, false}, |
103 {"2147483648", INT_MAX, false}, | 103 {"2147483648", INT_MAX, false}, |
104 {"99999999999", INT_MAX, false}, | 104 {"99999999999", INT_MAX, false}, |
105 }; | 105 }; |
106 | 106 |
107 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 107 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
108 const char* ascii_chars = cases[i].input.c_str(); | |
109 int output = 0; | 108 int output = 0; |
110 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output)); | 109 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output)); |
111 EXPECT_EQ(cases[i].output, output); | 110 EXPECT_EQ(cases[i].output, output); |
112 output = 0; | |
113 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input.begin(), | |
114 cases[i].input.end(), | |
115 &output)); | |
116 EXPECT_EQ(cases[i].output, output); | |
117 output = 0; | |
118 EXPECT_EQ(cases[i].success, StringToInt( | |
119 ascii_chars, ascii_chars + cases[i].input.length(), &output)); | |
120 EXPECT_EQ(cases[i].output, output); | |
121 | 111 |
122 string16 utf16_input = UTF8ToUTF16(cases[i].input); | 112 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
123 const char16* utf16_chars = utf16_input.c_str(); | |
124 output = 0; | 113 output = 0; |
125 EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output)); | 114 EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output)); |
126 EXPECT_EQ(cases[i].output, output); | 115 EXPECT_EQ(cases[i].output, output); |
127 output = 0; | |
128 EXPECT_EQ(cases[i].success, StringToInt(utf16_input.begin(), | |
129 utf16_input.end(), | |
130 &output)); | |
131 EXPECT_EQ(cases[i].output, output); | |
132 output = 0; | |
133 EXPECT_EQ(cases[i].success, StringToInt( | |
134 utf16_chars, utf16_chars + utf16_input.length(), &output)); | |
135 EXPECT_EQ(cases[i].output, output); | |
136 } | 116 } |
137 | 117 |
138 // One additional test to verify that conversion of numbers in strings with | 118 // One additional test to verify that conversion of numbers in strings with |
139 // embedded NUL characters. The NUL and extra data after it should be | 119 // embedded NUL characters. The NUL and extra data after it should be |
140 // interpreted as junk after the number. | 120 // interpreted as junk after the number. |
141 const char input[] = "6\06"; | 121 const char input[] = "6\06"; |
142 std::string input_string(input, arraysize(input) - 1); | 122 std::string input_string(input, arraysize(input) - 1); |
143 int output; | 123 int output; |
144 EXPECT_FALSE(StringToInt(input_string, &output)); | 124 EXPECT_FALSE(StringToInt(input_string, &output)); |
145 EXPECT_EQ(6, output); | 125 EXPECT_EQ(6, output); |
146 output = 0; | |
147 EXPECT_FALSE(StringToInt(input_string.begin(), input_string.end(), &output)); | |
148 EXPECT_EQ(6, output); | |
149 output = 0; | |
150 EXPECT_FALSE(StringToInt(input, input + arraysize(input), &output)); | |
151 EXPECT_EQ(6, output); | |
152 | 126 |
153 string16 utf16_input = UTF8ToUTF16(input_string); | 127 string16 utf16_input = UTF8ToUTF16(input_string); |
154 const char16* utf16_chars = utf16_input.c_str(); | |
155 output = 0; | 128 output = 0; |
156 EXPECT_FALSE(StringToInt(utf16_input, &output)); | 129 EXPECT_FALSE(StringToInt(utf16_input, &output)); |
157 EXPECT_EQ(6, output); | 130 EXPECT_EQ(6, output); |
158 output = 0; | |
159 EXPECT_FALSE(StringToInt(utf16_input.begin(), utf16_input.end(), &output)); | |
160 EXPECT_EQ(6, output); | |
161 output = 0; | |
162 EXPECT_FALSE(StringToInt(utf16_chars, | |
163 utf16_chars + utf16_input.length(), | |
164 &output)); | |
165 EXPECT_EQ(6, output); | |
166 | 131 |
167 output = 0; | 132 output = 0; |
168 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; | 133 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; |
169 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); | 134 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); |
170 EXPECT_EQ(0, output); | 135 EXPECT_EQ(0, output); |
171 } | 136 } |
172 | 137 |
173 TEST(StringNumberConversionsTest, StringToInt64) { | 138 TEST(StringNumberConversionsTest, StringToInt64) { |
174 static const struct { | 139 static const struct { |
175 std::string input; | 140 std::string input; |
(...skipping 27 matching lines...) Expand all Loading... |
203 {"-+123", 0, false}, | 168 {"-+123", 0, false}, |
204 {"+-123", 0, false}, | 169 {"+-123", 0, false}, |
205 {"-", 0, false}, | 170 {"-", 0, false}, |
206 {"-9223372036854775809", kint64min, false}, | 171 {"-9223372036854775809", kint64min, false}, |
207 {"-99999999999999999999", kint64min, false}, | 172 {"-99999999999999999999", kint64min, false}, |
208 {"9223372036854775808", kint64max, false}, | 173 {"9223372036854775808", kint64max, false}, |
209 {"99999999999999999999", kint64max, false}, | 174 {"99999999999999999999", kint64max, false}, |
210 }; | 175 }; |
211 | 176 |
212 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 177 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
213 const char* ascii_chars = cases[i].input.c_str(); | |
214 int64 output = 0; | 178 int64 output = 0; |
215 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output)); | 179 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output)); |
216 EXPECT_EQ(cases[i].output, output); | 180 EXPECT_EQ(cases[i].output, output); |
217 output = 0; | |
218 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input.begin(), | |
219 cases[i].input.end(), | |
220 &output)); | |
221 EXPECT_EQ(cases[i].output, output); | |
222 output = 0; | |
223 EXPECT_EQ(cases[i].success, StringToInt64( | |
224 ascii_chars, ascii_chars + cases[i].input.length(), &output)); | |
225 EXPECT_EQ(cases[i].output, output); | |
226 | 181 |
227 string16 utf16_input = UTF8ToUTF16(cases[i].input); | 182 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
228 const char16* utf16_chars = utf16_input.c_str(); | |
229 output = 0; | 183 output = 0; |
230 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output)); | 184 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output)); |
231 EXPECT_EQ(cases[i].output, output); | 185 EXPECT_EQ(cases[i].output, output); |
232 output = 0; | |
233 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input.begin(), | |
234 utf16_input.end(), | |
235 &output)); | |
236 EXPECT_EQ(cases[i].output, output); | |
237 output = 0; | |
238 EXPECT_EQ(cases[i].success, StringToInt64( | |
239 utf16_chars, utf16_chars + utf16_input.length(), &output)); | |
240 EXPECT_EQ(cases[i].output, output); | |
241 } | 186 } |
242 | 187 |
243 // One additional test to verify that conversion of numbers in strings with | 188 // One additional test to verify that conversion of numbers in strings with |
244 // embedded NUL characters. The NUL and extra data after it should be | 189 // embedded NUL characters. The NUL and extra data after it should be |
245 // interpreted as junk after the number. | 190 // interpreted as junk after the number. |
246 const char input[] = "6\06"; | 191 const char input[] = "6\06"; |
247 std::string input_string(input, arraysize(input) - 1); | 192 std::string input_string(input, arraysize(input) - 1); |
248 int64 output; | 193 int64 output; |
249 EXPECT_FALSE(StringToInt64(input_string, &output)); | 194 EXPECT_FALSE(StringToInt64(input_string, &output)); |
250 EXPECT_EQ(6, output); | 195 EXPECT_EQ(6, output); |
251 output = 0; | |
252 EXPECT_FALSE(StringToInt64(input_string.begin(), | |
253 input_string.end(), | |
254 &output)); | |
255 EXPECT_EQ(6, output); | |
256 output = 0; | |
257 EXPECT_FALSE(StringToInt64(input, input + arraysize(input), &output)); | |
258 EXPECT_EQ(6, output); | |
259 | 196 |
260 string16 utf16_input = UTF8ToUTF16(input_string); | 197 string16 utf16_input = UTF8ToUTF16(input_string); |
261 const char16* utf16_chars = utf16_input.c_str(); | |
262 output = 0; | 198 output = 0; |
263 EXPECT_FALSE(StringToInt64(utf16_input, &output)); | 199 EXPECT_FALSE(StringToInt64(utf16_input, &output)); |
264 EXPECT_EQ(6, output); | 200 EXPECT_EQ(6, output); |
265 output = 0; | |
266 EXPECT_FALSE(StringToInt64(utf16_input.begin(), utf16_input.end(), &output)); | |
267 EXPECT_EQ(6, output); | |
268 output = 0; | |
269 EXPECT_FALSE(StringToInt64(utf16_chars, | |
270 utf16_chars + utf16_input.length(), | |
271 &output)); | |
272 EXPECT_EQ(6, output); | |
273 } | 201 } |
274 | 202 |
275 TEST(StringNumberConversionsTest, HexStringToInt) { | 203 TEST(StringNumberConversionsTest, HexStringToInt) { |
276 static const struct { | 204 static const struct { |
277 std::string input; | 205 std::string input; |
278 int output; | 206 int output; |
279 bool success; | 207 bool success; |
280 } cases[] = { | 208 } cases[] = { |
281 {"0", 0, true}, | 209 {"0", 0, true}, |
282 {"42", 66, true}, | 210 {"42", 66, true}, |
(...skipping 20 matching lines...) Expand all Loading... |
303 {"efgh", 0xef, false}, | 231 {"efgh", 0xef, false}, |
304 {"0xefgh", 0xef, false}, | 232 {"0xefgh", 0xef, false}, |
305 {"hgfe", 0, false}, | 233 {"hgfe", 0, false}, |
306 {"100000000", -1, false}, // don't care about |output|, just |success| | 234 {"100000000", -1, false}, // don't care about |output|, just |success| |
307 {"-", 0, false}, | 235 {"-", 0, false}, |
308 {"", 0, false}, | 236 {"", 0, false}, |
309 {"0x", 0, false}, | 237 {"0x", 0, false}, |
310 }; | 238 }; |
311 | 239 |
312 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 240 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
313 const char* ascii_chars = cases[i].input.c_str(); | |
314 int output = 0; | 241 int output = 0; |
315 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output)); | 242 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output)); |
316 EXPECT_EQ(cases[i].output, output); | 243 EXPECT_EQ(cases[i].output, output); |
317 output = 0; | |
318 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input.begin(), | |
319 cases[i].input.end(), | |
320 &output)); | |
321 EXPECT_EQ(cases[i].output, output); | |
322 output = 0; | |
323 EXPECT_EQ(cases[i].success, HexStringToInt( | |
324 ascii_chars, ascii_chars + cases[i].input.length(), &output)); | |
325 EXPECT_EQ(cases[i].output, output); | |
326 } | 244 } |
327 // One additional test to verify that conversion of numbers in strings with | 245 // One additional test to verify that conversion of numbers in strings with |
328 // embedded NUL characters. The NUL and extra data after it should be | 246 // embedded NUL characters. The NUL and extra data after it should be |
329 // interpreted as junk after the number. | 247 // interpreted as junk after the number. |
330 const char input[] = "0xc0ffee\09"; | 248 const char input[] = "0xc0ffee\09"; |
331 std::string input_string(input, arraysize(input) - 1); | 249 std::string input_string(input, arraysize(input) - 1); |
332 int output; | 250 int output; |
333 EXPECT_FALSE(HexStringToInt(input_string, &output)); | 251 EXPECT_FALSE(HexStringToInt(input_string, &output)); |
334 EXPECT_EQ(0xc0ffee, output); | 252 EXPECT_EQ(0xc0ffee, output); |
335 output = 0; | |
336 EXPECT_FALSE(HexStringToInt(input_string.begin(), | |
337 input_string.end(), | |
338 &output)); | |
339 EXPECT_EQ(0xc0ffee, output); | |
340 output = 0; | |
341 EXPECT_FALSE(HexStringToInt(input, input + arraysize(input), &output)); | |
342 EXPECT_EQ(0xc0ffee, output); | |
343 } | 253 } |
344 | 254 |
345 TEST(StringNumberConversionsTest, HexStringToBytes) { | 255 TEST(StringNumberConversionsTest, HexStringToBytes) { |
346 static const struct { | 256 static const struct { |
347 const std::string input; | 257 const std::string input; |
348 const char* output; | 258 const char* output; |
349 size_t output_len; | 259 size_t output_len; |
350 bool success; | 260 bool success; |
351 } cases[] = { | 261 } cases[] = { |
352 {"0", "", 0, false}, // odd number of characters fails | 262 {"0", "", 0, false}, // odd number of characters fails |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 | 346 |
437 TEST(StringNumberConversionsTest, HexEncode) { | 347 TEST(StringNumberConversionsTest, HexEncode) { |
438 std::string hex(HexEncode(NULL, 0)); | 348 std::string hex(HexEncode(NULL, 0)); |
439 EXPECT_EQ(hex.length(), 0U); | 349 EXPECT_EQ(hex.length(), 0U); |
440 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 350 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
441 hex = HexEncode(bytes, sizeof(bytes)); | 351 hex = HexEncode(bytes, sizeof(bytes)); |
442 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 352 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
443 } | 353 } |
444 | 354 |
445 } // namespace base | 355 } // namespace base |
OLD | NEW |