OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <errno.h> | 5 #include <errno.h> |
6 #include <math.h> | 6 #include <math.h> |
7 | 7 |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 118 matching lines...) Loading... | |
129 output = 0; | 129 output = 0; |
130 EXPECT_FALSE(StringToInt(utf16_input, &output)); | 130 EXPECT_FALSE(StringToInt(utf16_input, &output)); |
131 EXPECT_EQ(6, output); | 131 EXPECT_EQ(6, output); |
132 | 132 |
133 output = 0; | 133 output = 0; |
134 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; | 134 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; |
135 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); | 135 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); |
136 EXPECT_EQ(0, output); | 136 EXPECT_EQ(0, output); |
137 } | 137 } |
138 | 138 |
139 TEST(StringNumberConversionsTest, StringToUint) { | |
140 static const struct { | |
141 std::string input; | |
142 unsigned output; | |
143 bool success; | |
144 } cases[] = { | |
145 {"0", 0, true}, | |
146 {"42", 42, true}, | |
147 {"42\x99", 42, false}, | |
148 {"\x99" "42\x99", 0, false}, | |
149 {"-2147483648", 0, false}, | |
150 {"2147483647", INT_MAX, true}, | |
151 {"", 0, false}, | |
152 {" 42", 42, false}, | |
153 {"42 ", 42, false}, | |
154 {"\t\n\v\f\r 42", 42, false}, | |
155 {"blah42", 0, false}, | |
156 {"42blah", 42, false}, | |
157 {"blah42blah", 0, false}, | |
158 {"-273.15", 0, false}, | |
159 {"+98.6", 98, false}, | |
160 {"--123", 0, false}, | |
161 {"++123", 0, false}, | |
162 {"-+123", 0, false}, | |
163 {"+-123", 0, false}, | |
164 {"-", 0, false}, | |
165 {"-2147483649", 0, false}, | |
166 {"-99999999999", 0, false}, | |
167 {"4294967295", UINT_MAX, true}, | |
168 {"4294967296", UINT_MAX, false}, | |
169 {"99999999999", UINT_MAX, false}, | |
170 }; | |
171 | |
172 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | |
173 unsigned output = 0; | |
174 EXPECT_EQ(cases[i].success, StringToUint(cases[i].input, &output)); | |
175 EXPECT_EQ(cases[i].output, output); | |
176 | |
177 string16 utf16_input = UTF8ToUTF16(cases[i].input); | |
178 output = 0; | |
179 EXPECT_EQ(cases[i].success, StringToUint(utf16_input, &output)); | |
180 EXPECT_EQ(cases[i].output, output); | |
181 } | |
182 | |
183 // One additional test to verify that conversion of numbers in strings with | |
184 // embedded NUL characters. The NUL and extra data after it should be | |
185 // interpreted as junk after the number. | |
186 const char input[] = "6\06"; | |
187 std::string input_string(input, arraysize(input) - 1); | |
188 unsigned output; | |
189 EXPECT_FALSE(StringToUint(input_string, &output)); | |
190 EXPECT_EQ(6U, output); | |
191 | |
192 string16 utf16_input = UTF8ToUTF16(input_string); | |
193 output = 0; | |
194 EXPECT_FALSE(StringToUint(utf16_input, &output)); | |
195 EXPECT_EQ(6U, output); | |
196 | |
197 output = 0; | |
198 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; | |
199 EXPECT_FALSE(StringToUint(string16(negative_wide_input), &output)); | |
200 EXPECT_EQ(0U, output); | |
201 } | |
202 | |
139 TEST(StringNumberConversionsTest, StringToInt64) { | 203 TEST(StringNumberConversionsTest, StringToInt64) { |
140 static const struct { | 204 static const struct { |
141 std::string input; | 205 std::string input; |
142 int64 output; | 206 int64 output; |
143 bool success; | 207 bool success; |
144 } cases[] = { | 208 } cases[] = { |
145 {"0", 0, true}, | 209 {"0", 0, true}, |
146 {"42", 42, true}, | 210 {"42", 42, true}, |
147 {"-2147483648", INT_MIN, true}, | 211 {"-2147483648", INT_MIN, true}, |
148 {"2147483647", INT_MAX, true}, | 212 {"2147483647", INT_MAX, true}, |
(...skipping 45 matching lines...) Loading... | |
194 int64 output; | 258 int64 output; |
195 EXPECT_FALSE(StringToInt64(input_string, &output)); | 259 EXPECT_FALSE(StringToInt64(input_string, &output)); |
196 EXPECT_EQ(6, output); | 260 EXPECT_EQ(6, output); |
197 | 261 |
198 string16 utf16_input = UTF8ToUTF16(input_string); | 262 string16 utf16_input = UTF8ToUTF16(input_string); |
199 output = 0; | 263 output = 0; |
200 EXPECT_FALSE(StringToInt64(utf16_input, &output)); | 264 EXPECT_FALSE(StringToInt64(utf16_input, &output)); |
201 EXPECT_EQ(6, output); | 265 EXPECT_EQ(6, output); |
202 } | 266 } |
203 | 267 |
268 TEST(StringNumberConversionsTest, StringToUint64) { | |
269 static const struct { | |
270 std::string input; | |
271 uint64 output; | |
272 bool success; | |
273 } cases[] = { | |
274 {"0", 0, true}, | |
275 {"42", 42, true}, | |
276 {"-2147483648", 0, false}, | |
277 {"2147483647", INT_MAX, true}, | |
278 {"-2147483649", 0, false}, | |
279 {"-99999999999", 0, false}, | |
280 {"2147483648", GG_INT64_C(2147483648), true}, | |
281 {"99999999999", GG_INT64_C(99999999999), true}, | |
282 {"9223372036854775807", kint64max, true}, | |
283 {"-9223372036854775808", 0, false}, | |
284 {"09", 9, true}, | |
285 {"-09", 0, false}, | |
286 {"", 0, false}, | |
287 {" 42", 42, false}, | |
288 {"42 ", 42, false}, | |
289 {"0x42", 0, false}, | |
290 {"\t\n\v\f\r 42", 42, false}, | |
291 {"blah42", 0, false}, | |
292 {"42blah", 42, false}, | |
293 {"blah42blah", 0, false}, | |
294 {"-273.15", 0, false}, | |
295 {"+98.6", 98, false}, | |
296 {"--123", 0, false}, | |
297 {"++123", 0, false}, | |
298 {"-+123", 0, false}, | |
299 {"+-123", 0, false}, | |
300 {"-", 0, false}, | |
301 {"-9223372036854775809", 0, false}, | |
302 {"-99999999999999999999", 0, false}, | |
303 {"9223372036854775808", 9223372036854775808U, true}, | |
304 {"99999999999999999999", kuint64max, false}, | |
305 {"18446744073709551615", kuint64max, true}, | |
306 {"18446744073709551616", kuint64max, false}, | |
307 }; | |
308 | |
309 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | |
310 uint64 output = 0; | |
311 EXPECT_EQ(cases[i].success, StringToUint64(cases[i].input, &output)); | |
312 EXPECT_EQ(cases[i].output, output); | |
313 | |
314 string16 utf16_input = UTF8ToUTF16(cases[i].input); | |
315 output = 0; | |
316 EXPECT_EQ(cases[i].success, StringToUint64(utf16_input, &output)); | |
317 EXPECT_EQ(cases[i].output, output); | |
318 } | |
319 | |
320 // One additional test to verify that conversion of numbers in strings with | |
321 // embedded NUL characters. The NUL and extra data after it should be | |
322 // interpreted as junk after the number. | |
323 const char input[] = "6\06"; | |
324 std::string input_string(input, arraysize(input) - 1); | |
325 uint64 output; | |
326 EXPECT_FALSE(StringToUint64(input_string, &output)); | |
327 EXPECT_EQ(6U, output); | |
328 | |
329 string16 utf16_input = UTF8ToUTF16(input_string); | |
330 output = 0; | |
331 EXPECT_FALSE(StringToUint64(utf16_input, &output)); | |
332 EXPECT_EQ(6U, output); | |
333 } | |
334 | |
335 TEST(StringNumberConversionsTest, StringToSizeT) { | |
336 static const struct { | |
337 std::string input; | |
338 size_t output; | |
339 bool success; | |
340 } cases[] = { | |
341 {"0", 0, true}, | |
342 {"42", 42, true}, | |
343 {"-2147483648", 0, false}, | |
344 {"2147483647", INT_MAX, true}, | |
345 {"-2147483649", 0, false}, | |
346 {"-99999999999", 0, false}, | |
347 {"2147483648", static_cast<size_t>(2147483648), true}, | |
348 {"-9223372036854775808", 0, false}, | |
349 {"09", 9, true}, | |
350 {"-09", 0, false}, | |
351 {"", 0, false}, | |
352 {" 42", 42, false}, | |
353 {"42 ", 42, false}, | |
354 {"0x42", 0, false}, | |
355 {"\t\n\v\f\r 42", 42, false}, | |
356 {"blah42", 0, false}, | |
357 {"42blah", 42, false}, | |
358 {"blah42blah", 0, false}, | |
359 {"-273.15", 0, false}, | |
360 {"+98.6", 98, false}, | |
361 {"--123", 0, false}, | |
362 {"++123", 0, false}, | |
363 {"-+123", 0, false}, | |
364 {"+-123", 0, false}, | |
365 {"-", 0, false}, | |
366 {"-9223372036854775809", 0, false}, | |
367 {"-99999999999999999999", 0, false}, | |
368 {"999999999999999999999999", std::numeric_limits<size_t>::max(), false}, | |
369 }; | |
370 | |
371 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | |
372 size_t output = 0; | |
373 EXPECT_EQ(cases[i].success, StringToSizeT(cases[i].input, &output)); | |
374 EXPECT_EQ(cases[i].output, output); | |
375 | |
376 string16 utf16_input = UTF8ToUTF16(cases[i].input); | |
377 output = 0; | |
378 EXPECT_EQ(cases[i].success, StringToSizeT(utf16_input, &output)); | |
379 EXPECT_EQ(cases[i].output, output); | |
380 } | |
381 | |
382 if (sizeof(size_t) == 8) { | |
383 // there is some implementation flexibility in the size of size_t | |
384 // so let's have a special case for a 64bit value that is greater | |
385 // than 32bit UINT_MAX. | |
386 size_t output; | |
387 EXPECT_EQ(true, StringToSizeT("99999999999", &output)); | |
388 #if defined(OS_WIN) | |
389 #pragma warning(disable:4309) | |
390 #endif | |
Vitaly Buka corp
2013/05/20 19:42:49
Can you put this tests back to array and
use #if
Mostyn Bramley-Moore
2013/05/20 20:13:34
I found SIZE_MAX in stdint.h which should be more
| |
391 EXPECT_EQ( static_cast<size_t>(99999999999), output); | |
392 #if defined(OS_WIN) | |
393 #pragma warning(default:4309) | |
394 #endif | |
395 } | |
396 | |
397 | |
398 // One additional test to verify that conversion of numbers in strings with | |
399 // embedded NUL characters. The NUL and extra data after it should be | |
400 // interpreted as junk after the number. | |
401 const char input[] = "6\06"; | |
402 std::string input_string(input, arraysize(input) - 1); | |
403 size_t output; | |
404 EXPECT_FALSE(StringToSizeT(input_string, &output)); | |
405 EXPECT_EQ(6U, output); | |
406 | |
407 string16 utf16_input = UTF8ToUTF16(input_string); | |
408 output = 0; | |
409 EXPECT_FALSE(StringToSizeT(utf16_input, &output)); | |
410 EXPECT_EQ(6U, output); | |
411 } | |
412 | |
204 TEST(StringNumberConversionsTest, HexStringToInt) { | 413 TEST(StringNumberConversionsTest, HexStringToInt) { |
205 static const struct { | 414 static const struct { |
206 std::string input; | 415 std::string input; |
207 int64 output; | 416 int64 output; |
208 bool success; | 417 bool success; |
209 } cases[] = { | 418 } cases[] = { |
210 {"0", 0, true}, | 419 {"0", 0, true}, |
211 {"42", 66, true}, | 420 {"42", 66, true}, |
212 {"-42", -66, true}, | 421 {"-42", -66, true}, |
213 {"+42", 66, true}, | 422 {"+42", 66, true}, |
(...skipping 285 matching lines...) Loading... | |
499 | 708 |
500 TEST(StringNumberConversionsTest, HexEncode) { | 709 TEST(StringNumberConversionsTest, HexEncode) { |
501 std::string hex(HexEncode(NULL, 0)); | 710 std::string hex(HexEncode(NULL, 0)); |
502 EXPECT_EQ(hex.length(), 0U); | 711 EXPECT_EQ(hex.length(), 0U); |
503 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 712 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
504 hex = HexEncode(bytes, sizeof(bytes)); | 713 hex = HexEncode(bytes, sizeof(bytes)); |
505 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 714 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
506 } | 715 } |
507 | 716 |
508 } // namespace base | 717 } // namespace base |
OLD | NEW |