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 "base/strings/string_number_conversions.h" | 5 #include "base/strings/string_number_conversions.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 TEST(StringNumberConversionsTest, IntToString) { | 32 TEST(StringNumberConversionsTest, IntToString) { |
33 static const IntToStringTest<int> int_tests[] = { | 33 static const IntToStringTest<int> int_tests[] = { |
34 { 0, "0", "0" }, | 34 { 0, "0", "0" }, |
35 { -1, "-1", "4294967295" }, | 35 { -1, "-1", "4294967295" }, |
36 { std::numeric_limits<int>::max(), "2147483647", "2147483647" }, | 36 { std::numeric_limits<int>::max(), "2147483647", "2147483647" }, |
37 { std::numeric_limits<int>::min(), "-2147483648", "2147483648" }, | 37 { std::numeric_limits<int>::min(), "-2147483648", "2147483648" }, |
38 }; | 38 }; |
39 static const IntToStringTest<int64> int64_tests[] = { | 39 static const IntToStringTest<int64_t> int64_tests[] = { |
40 { 0, "0", "0" }, | 40 {0, "0", "0"}, |
41 { -1, "-1", "18446744073709551615" }, | 41 {-1, "-1", "18446744073709551615"}, |
42 { std::numeric_limits<int64>::max(), | 42 { |
43 "9223372036854775807", | 43 std::numeric_limits<int64_t>::max(), "9223372036854775807", |
44 "9223372036854775807", }, | 44 "9223372036854775807", |
45 { std::numeric_limits<int64>::min(), | 45 }, |
46 "-9223372036854775808", | 46 {std::numeric_limits<int64_t>::min(), "-9223372036854775808", |
47 "9223372036854775808" }, | 47 "9223372036854775808"}, |
48 }; | 48 }; |
49 | 49 |
50 for (size_t i = 0; i < arraysize(int_tests); ++i) { | 50 for (size_t i = 0; i < arraysize(int_tests); ++i) { |
51 const IntToStringTest<int>* test = &int_tests[i]; | 51 const IntToStringTest<int>* test = &int_tests[i]; |
52 EXPECT_EQ(IntToString(test->num), test->sexpected); | 52 EXPECT_EQ(IntToString(test->num), test->sexpected); |
53 EXPECT_EQ(IntToString16(test->num), UTF8ToUTF16(test->sexpected)); | 53 EXPECT_EQ(IntToString16(test->num), UTF8ToUTF16(test->sexpected)); |
54 EXPECT_EQ(UintToString(test->num), test->uexpected); | 54 EXPECT_EQ(UintToString(test->num), test->uexpected); |
55 EXPECT_EQ(UintToString16(test->num), UTF8ToUTF16(test->uexpected)); | 55 EXPECT_EQ(UintToString16(test->num), UTF8ToUTF16(test->uexpected)); |
56 } | 56 } |
57 for (size_t i = 0; i < arraysize(int64_tests); ++i) { | 57 for (size_t i = 0; i < arraysize(int64_tests); ++i) { |
58 const IntToStringTest<int64>* test = &int64_tests[i]; | 58 const IntToStringTest<int64_t>* test = &int64_tests[i]; |
59 EXPECT_EQ(Int64ToString(test->num), test->sexpected); | 59 EXPECT_EQ(Int64ToString(test->num), test->sexpected); |
60 EXPECT_EQ(Int64ToString16(test->num), UTF8ToUTF16(test->sexpected)); | 60 EXPECT_EQ(Int64ToString16(test->num), UTF8ToUTF16(test->sexpected)); |
61 EXPECT_EQ(Uint64ToString(test->num), test->uexpected); | 61 EXPECT_EQ(Uint64ToString(test->num), test->uexpected); |
62 EXPECT_EQ(Uint64ToString16(test->num), UTF8ToUTF16(test->uexpected)); | 62 EXPECT_EQ(Uint64ToString16(test->num), UTF8ToUTF16(test->uexpected)); |
63 } | 63 } |
64 } | 64 } |
65 | 65 |
66 TEST(StringNumberConversionsTest, Uint64ToString) { | 66 TEST(StringNumberConversionsTest, Uint64ToString) { |
67 static const struct { | 67 static const struct { |
68 uint64 input; | 68 uint64 input; |
69 std::string output; | 69 std::string output; |
70 } cases[] = { | 70 } cases[] = { |
71 {0, "0"}, | 71 {0, "0"}, |
72 {42, "42"}, | 72 {42, "42"}, |
73 {INT_MAX, "2147483647"}, | 73 {INT_MAX, "2147483647"}, |
74 {kuint64max, "18446744073709551615"}, | 74 {std::numeric_limits<uint64_t>::max(), "18446744073709551615"}, |
75 }; | 75 }; |
76 | 76 |
77 for (size_t i = 0; i < arraysize(cases); ++i) | 77 for (size_t i = 0; i < arraysize(cases); ++i) |
78 EXPECT_EQ(cases[i].output, Uint64ToString(cases[i].input)); | 78 EXPECT_EQ(cases[i].output, Uint64ToString(cases[i].input)); |
79 } | 79 } |
80 | 80 |
81 TEST(StringNumberConversionsTest, SizeTToString) { | 81 TEST(StringNumberConversionsTest, SizeTToString) { |
82 size_t size_t_max = std::numeric_limits<size_t>::max(); | 82 size_t size_t_max = std::numeric_limits<size_t>::max(); |
83 std::string size_t_max_string = StringPrintf("%" PRIuS, size_t_max); | 83 std::string size_t_max_string = StringPrintf("%" PRIuS, size_t_max); |
84 | 84 |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 | 224 |
225 output = 0; | 225 output = 0; |
226 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; | 226 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; |
227 EXPECT_FALSE(StringToUint(string16(negative_wide_input), &output)); | 227 EXPECT_FALSE(StringToUint(string16(negative_wide_input), &output)); |
228 EXPECT_EQ(0U, output); | 228 EXPECT_EQ(0U, output); |
229 } | 229 } |
230 | 230 |
231 TEST(StringNumberConversionsTest, StringToInt64) { | 231 TEST(StringNumberConversionsTest, StringToInt64) { |
232 static const struct { | 232 static const struct { |
233 std::string input; | 233 std::string input; |
234 int64 output; | 234 int64_t output; |
235 bool success; | 235 bool success; |
236 } cases[] = { | 236 } cases[] = { |
237 {"0", 0, true}, | 237 {"0", 0, true}, |
238 {"42", 42, true}, | 238 {"42", 42, true}, |
239 {"-2147483648", INT_MIN, true}, | 239 {"-2147483648", INT_MIN, true}, |
240 {"2147483647", INT_MAX, true}, | 240 {"2147483647", INT_MAX, true}, |
241 {"-2147483649", INT64_C(-2147483649), true}, | 241 {"-2147483649", INT64_C(-2147483649), true}, |
242 {"-99999999999", INT64_C(-99999999999), true}, | 242 {"-99999999999", INT64_C(-99999999999), true}, |
243 {"2147483648", INT64_C(2147483648), true}, | 243 {"2147483648", INT64_C(2147483648), true}, |
244 {"99999999999", INT64_C(99999999999), true}, | 244 {"99999999999", INT64_C(99999999999), true}, |
245 {"9223372036854775807", kint64max, true}, | 245 {"9223372036854775807", std::numeric_limits<int64_t>::max(), true}, |
246 {"-9223372036854775808", kint64min, true}, | 246 {"-9223372036854775808", std::numeric_limits<int64_t>::min(), true}, |
247 {"09", 9, true}, | 247 {"09", 9, true}, |
248 {"-09", -9, true}, | 248 {"-09", -9, true}, |
249 {"", 0, false}, | 249 {"", 0, false}, |
250 {" 42", 42, false}, | 250 {" 42", 42, false}, |
251 {"42 ", 42, false}, | 251 {"42 ", 42, false}, |
252 {"0x42", 0, false}, | 252 {"0x42", 0, false}, |
253 {"\t\n\v\f\r 42", 42, false}, | 253 {"\t\n\v\f\r 42", 42, false}, |
254 {"blah42", 0, false}, | 254 {"blah42", 0, false}, |
255 {"42blah", 42, false}, | 255 {"42blah", 42, false}, |
256 {"blah42blah", 0, false}, | 256 {"blah42blah", 0, false}, |
257 {"-273.15", -273, false}, | 257 {"-273.15", -273, false}, |
258 {"+98.6", 98, false}, | 258 {"+98.6", 98, false}, |
259 {"--123", 0, false}, | 259 {"--123", 0, false}, |
260 {"++123", 0, false}, | 260 {"++123", 0, false}, |
261 {"-+123", 0, false}, | 261 {"-+123", 0, false}, |
262 {"+-123", 0, false}, | 262 {"+-123", 0, false}, |
263 {"-", 0, false}, | 263 {"-", 0, false}, |
264 {"-9223372036854775809", kint64min, false}, | 264 {"-9223372036854775809", std::numeric_limits<int64_t>::min(), false}, |
265 {"-99999999999999999999", kint64min, false}, | 265 {"-99999999999999999999", std::numeric_limits<int64_t>::min(), false}, |
266 {"9223372036854775808", kint64max, false}, | 266 {"9223372036854775808", std::numeric_limits<int64_t>::max(), false}, |
267 {"99999999999999999999", kint64max, false}, | 267 {"99999999999999999999", std::numeric_limits<int64_t>::max(), false}, |
268 }; | 268 }; |
269 | 269 |
270 for (size_t i = 0; i < arraysize(cases); ++i) { | 270 for (size_t i = 0; i < arraysize(cases); ++i) { |
271 int64 output = 0; | 271 int64_t output = 0; |
272 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output)); | 272 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output)); |
273 EXPECT_EQ(cases[i].output, output); | 273 EXPECT_EQ(cases[i].output, output); |
274 | 274 |
275 string16 utf16_input = UTF8ToUTF16(cases[i].input); | 275 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
276 output = 0; | 276 output = 0; |
277 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output)); | 277 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output)); |
278 EXPECT_EQ(cases[i].output, output); | 278 EXPECT_EQ(cases[i].output, output); |
279 } | 279 } |
280 | 280 |
281 // One additional test to verify that conversion of numbers in strings with | 281 // One additional test to verify that conversion of numbers in strings with |
282 // embedded NUL characters. The NUL and extra data after it should be | 282 // embedded NUL characters. The NUL and extra data after it should be |
283 // interpreted as junk after the number. | 283 // interpreted as junk after the number. |
284 const char input[] = "6\06"; | 284 const char input[] = "6\06"; |
285 std::string input_string(input, arraysize(input) - 1); | 285 std::string input_string(input, arraysize(input) - 1); |
286 int64 output; | 286 int64_t output; |
287 EXPECT_FALSE(StringToInt64(input_string, &output)); | 287 EXPECT_FALSE(StringToInt64(input_string, &output)); |
288 EXPECT_EQ(6, output); | 288 EXPECT_EQ(6, output); |
289 | 289 |
290 string16 utf16_input = UTF8ToUTF16(input_string); | 290 string16 utf16_input = UTF8ToUTF16(input_string); |
291 output = 0; | 291 output = 0; |
292 EXPECT_FALSE(StringToInt64(utf16_input, &output)); | 292 EXPECT_FALSE(StringToInt64(utf16_input, &output)); |
293 EXPECT_EQ(6, output); | 293 EXPECT_EQ(6, output); |
294 } | 294 } |
295 | 295 |
296 TEST(StringNumberConversionsTest, StringToUint64) { | 296 TEST(StringNumberConversionsTest, StringToUint64) { |
297 static const struct { | 297 static const struct { |
298 std::string input; | 298 std::string input; |
299 uint64 output; | 299 uint64 output; |
300 bool success; | 300 bool success; |
301 } cases[] = { | 301 } cases[] = { |
302 {"0", 0, true}, | 302 {"0", 0, true}, |
303 {"42", 42, true}, | 303 {"42", 42, true}, |
304 {"-2147483648", 0, false}, | 304 {"-2147483648", 0, false}, |
305 {"2147483647", INT_MAX, true}, | 305 {"2147483647", INT_MAX, true}, |
306 {"-2147483649", 0, false}, | 306 {"-2147483649", 0, false}, |
307 {"-99999999999", 0, false}, | 307 {"-99999999999", 0, false}, |
308 {"2147483648", UINT64_C(2147483648), true}, | 308 {"2147483648", UINT64_C(2147483648), true}, |
309 {"99999999999", UINT64_C(99999999999), true}, | 309 {"99999999999", UINT64_C(99999999999), true}, |
310 {"9223372036854775807", kint64max, true}, | 310 {"9223372036854775807", std::numeric_limits<int64_t>::max(), true}, |
311 {"-9223372036854775808", 0, false}, | 311 {"-9223372036854775808", 0, false}, |
312 {"09", 9, true}, | 312 {"09", 9, true}, |
313 {"-09", 0, false}, | 313 {"-09", 0, false}, |
314 {"", 0, false}, | 314 {"", 0, false}, |
315 {" 42", 42, false}, | 315 {" 42", 42, false}, |
316 {"42 ", 42, false}, | 316 {"42 ", 42, false}, |
317 {"0x42", 0, false}, | 317 {"0x42", 0, false}, |
318 {"\t\n\v\f\r 42", 42, false}, | 318 {"\t\n\v\f\r 42", 42, false}, |
319 {"blah42", 0, false}, | 319 {"blah42", 0, false}, |
320 {"42blah", 42, false}, | 320 {"42blah", 42, false}, |
321 {"blah42blah", 0, false}, | 321 {"blah42blah", 0, false}, |
322 {"-273.15", 0, false}, | 322 {"-273.15", 0, false}, |
323 {"+98.6", 98, false}, | 323 {"+98.6", 98, false}, |
324 {"--123", 0, false}, | 324 {"--123", 0, false}, |
325 {"++123", 0, false}, | 325 {"++123", 0, false}, |
326 {"-+123", 0, false}, | 326 {"-+123", 0, false}, |
327 {"+-123", 0, false}, | 327 {"+-123", 0, false}, |
328 {"-", 0, false}, | 328 {"-", 0, false}, |
329 {"-9223372036854775809", 0, false}, | 329 {"-9223372036854775809", 0, false}, |
330 {"-99999999999999999999", 0, false}, | 330 {"-99999999999999999999", 0, false}, |
331 {"9223372036854775808", UINT64_C(9223372036854775808), true}, | 331 {"9223372036854775808", UINT64_C(9223372036854775808), true}, |
332 {"99999999999999999999", kuint64max, false}, | 332 {"99999999999999999999", std::numeric_limits<uint64_t>::max(), false}, |
333 {"18446744073709551615", kuint64max, true}, | 333 {"18446744073709551615", std::numeric_limits<uint64_t>::max(), true}, |
334 {"18446744073709551616", kuint64max, false}, | 334 {"18446744073709551616", std::numeric_limits<uint64_t>::max(), false}, |
335 }; | 335 }; |
336 | 336 |
337 for (size_t i = 0; i < arraysize(cases); ++i) { | 337 for (size_t i = 0; i < arraysize(cases); ++i) { |
338 uint64 output = 0; | 338 uint64 output = 0; |
339 EXPECT_EQ(cases[i].success, StringToUint64(cases[i].input, &output)); | 339 EXPECT_EQ(cases[i].success, StringToUint64(cases[i].input, &output)); |
340 EXPECT_EQ(cases[i].output, output); | 340 EXPECT_EQ(cases[i].output, output); |
341 | 341 |
342 string16 utf16_input = UTF8ToUTF16(cases[i].input); | 342 string16 utf16_input = UTF8ToUTF16(cases[i].input); |
343 output = 0; | 343 output = 0; |
344 EXPECT_EQ(cases[i].success, StringToUint64(utf16_input, &output)); | 344 EXPECT_EQ(cases[i].success, StringToUint64(utf16_input, &output)); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 | 425 |
426 string16 utf16_input = UTF8ToUTF16(input_string); | 426 string16 utf16_input = UTF8ToUTF16(input_string); |
427 output = 0; | 427 output = 0; |
428 EXPECT_FALSE(StringToSizeT(utf16_input, &output)); | 428 EXPECT_FALSE(StringToSizeT(utf16_input, &output)); |
429 EXPECT_EQ(6U, output); | 429 EXPECT_EQ(6U, output); |
430 } | 430 } |
431 | 431 |
432 TEST(StringNumberConversionsTest, HexStringToInt) { | 432 TEST(StringNumberConversionsTest, HexStringToInt) { |
433 static const struct { | 433 static const struct { |
434 std::string input; | 434 std::string input; |
435 int64 output; | 435 int64_t output; |
436 bool success; | 436 bool success; |
437 } cases[] = { | 437 } cases[] = { |
438 {"0", 0, true}, | 438 {"0", 0, true}, |
439 {"42", 66, true}, | 439 {"42", 66, true}, |
440 {"-42", -66, true}, | 440 {"-42", -66, true}, |
441 {"+42", 66, true}, | 441 {"+42", 66, true}, |
442 {"7fffffff", INT_MAX, true}, | 442 {"7fffffff", INT_MAX, true}, |
443 {"-80000000", INT_MIN, true}, | 443 {"-80000000", INT_MIN, true}, |
444 {"80000000", INT_MAX, false}, // Overflow test. | 444 {"80000000", INT_MAX, false}, // Overflow test. |
445 {"-80000001", INT_MIN, false}, // Underflow test. | 445 {"-80000001", INT_MIN, false}, // Underflow test. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 const char input[] = "0xc0ffee\0" "9"; | 477 const char input[] = "0xc0ffee\0" "9"; |
478 std::string input_string(input, arraysize(input) - 1); | 478 std::string input_string(input, arraysize(input) - 1); |
479 int output; | 479 int output; |
480 EXPECT_FALSE(HexStringToInt(input_string, &output)); | 480 EXPECT_FALSE(HexStringToInt(input_string, &output)); |
481 EXPECT_EQ(0xc0ffee, output); | 481 EXPECT_EQ(0xc0ffee, output); |
482 } | 482 } |
483 | 483 |
484 TEST(StringNumberConversionsTest, HexStringToUInt) { | 484 TEST(StringNumberConversionsTest, HexStringToUInt) { |
485 static const struct { | 485 static const struct { |
486 std::string input; | 486 std::string input; |
487 uint32 output; | 487 uint32_t output; |
488 bool success; | 488 bool success; |
489 } cases[] = { | 489 } cases[] = { |
490 {"0", 0, true}, | 490 {"0", 0, true}, |
491 {"42", 0x42, true}, | 491 {"42", 0x42, true}, |
492 {"-42", 0, false}, | 492 {"-42", 0, false}, |
493 {"+42", 0x42, true}, | 493 {"+42", 0x42, true}, |
494 {"7fffffff", INT_MAX, true}, | 494 {"7fffffff", INT_MAX, true}, |
495 {"-80000000", 0, false}, | 495 {"-80000000", 0, false}, |
496 {"ffffffff", 0xffffffff, true}, | 496 {"ffffffff", 0xffffffff, true}, |
497 {"DeadBeef", 0xdeadbeef, true}, | 497 {"DeadBeef", 0xdeadbeef, true}, |
498 {"0x42", 0x42, true}, | 498 {"0x42", 0x42, true}, |
499 {"-0x42", 0, false}, | 499 {"-0x42", 0, false}, |
500 {"+0x42", 0x42, true}, | 500 {"+0x42", 0x42, true}, |
501 {"0x7fffffff", INT_MAX, true}, | 501 {"0x7fffffff", INT_MAX, true}, |
502 {"-0x80000000", 0, false}, | 502 {"-0x80000000", 0, false}, |
503 {"0xffffffff", kuint32max, true}, | 503 {"0xffffffff", std::numeric_limits<uint32_t>::max(), true}, |
504 {"0XDeadBeef", 0xdeadbeef, true}, | 504 {"0XDeadBeef", 0xdeadbeef, true}, |
505 {"0x7fffffffffffffff", kuint32max, false}, // Overflow test. | 505 {"0x7fffffffffffffff", std::numeric_limits<uint32_t>::max(), |
506 {"-0x8000000000000000", 0, false}, | 506 false}, // Overflow test. |
507 {"0x8000000000000000", kuint32max, false}, // Overflow test. | 507 {"-0x8000000000000000", 0, false}, |
508 {"-0x8000000000000001", 0, false}, | 508 {"0x8000000000000000", std::numeric_limits<uint32_t>::max(), |
509 {"0xFFFFFFFFFFFFFFFF", kuint32max, false}, // Overflow test. | 509 false}, // Overflow test. |
510 {"FFFFFFFFFFFFFFFF", kuint32max, false}, // Overflow test. | 510 {"-0x8000000000000001", 0, false}, |
511 {"0x0000000000000000", 0, true}, | 511 {"0xFFFFFFFFFFFFFFFF", std::numeric_limits<uint32_t>::max(), |
512 {"0000000000000000", 0, true}, | 512 false}, // Overflow test. |
513 {"1FFFFFFFFFFFFFFFF", kuint32max, false}, // Overflow test. | 513 {"FFFFFFFFFFFFFFFF", std::numeric_limits<uint32_t>::max(), |
514 {"0x0f", 0x0f, true}, | 514 false}, // Overflow test. |
515 {"0f", 0x0f, true}, | 515 {"0x0000000000000000", 0, true}, |
516 {" 45", 0x45, false}, | 516 {"0000000000000000", 0, true}, |
517 {"\t\n\v\f\r 0x45", 0x45, false}, | 517 {"1FFFFFFFFFFFFFFFF", std::numeric_limits<uint32_t>::max(), |
518 {" 45", 0x45, false}, | 518 false}, // Overflow test. |
519 {"45 ", 0x45, false}, | 519 {"0x0f", 0x0f, true}, |
520 {"45:", 0x45, false}, | 520 {"0f", 0x0f, true}, |
521 {"efgh", 0xef, false}, | 521 {" 45", 0x45, false}, |
522 {"0xefgh", 0xef, false}, | 522 {"\t\n\v\f\r 0x45", 0x45, false}, |
523 {"hgfe", 0, false}, | 523 {" 45", 0x45, false}, |
524 {"-", 0, false}, | 524 {"45 ", 0x45, false}, |
525 {"", 0, false}, | 525 {"45:", 0x45, false}, |
526 {"0x", 0, false}, | 526 {"efgh", 0xef, false}, |
| 527 {"0xefgh", 0xef, false}, |
| 528 {"hgfe", 0, false}, |
| 529 {"-", 0, false}, |
| 530 {"", 0, false}, |
| 531 {"0x", 0, false}, |
527 }; | 532 }; |
528 | 533 |
529 for (size_t i = 0; i < arraysize(cases); ++i) { | 534 for (size_t i = 0; i < arraysize(cases); ++i) { |
530 uint32 output = 0; | 535 uint32_t output = 0; |
531 EXPECT_EQ(cases[i].success, HexStringToUInt(cases[i].input, &output)); | 536 EXPECT_EQ(cases[i].success, HexStringToUInt(cases[i].input, &output)); |
532 EXPECT_EQ(cases[i].output, output); | 537 EXPECT_EQ(cases[i].output, output); |
533 } | 538 } |
534 // One additional test to verify that conversion of numbers in strings with | 539 // One additional test to verify that conversion of numbers in strings with |
535 // embedded NUL characters. The NUL and extra data after it should be | 540 // embedded NUL characters. The NUL and extra data after it should be |
536 // interpreted as junk after the number. | 541 // interpreted as junk after the number. |
537 const char input[] = "0xc0ffee\0" "9"; | 542 const char input[] = "0xc0ffee\0" "9"; |
538 std::string input_string(input, arraysize(input) - 1); | 543 std::string input_string(input, arraysize(input) - 1); |
539 uint32 output; | 544 uint32_t output; |
540 EXPECT_FALSE(HexStringToUInt(input_string, &output)); | 545 EXPECT_FALSE(HexStringToUInt(input_string, &output)); |
541 EXPECT_EQ(0xc0ffeeU, output); | 546 EXPECT_EQ(0xc0ffeeU, output); |
542 } | 547 } |
543 | 548 |
544 TEST(StringNumberConversionsTest, HexStringToInt64) { | 549 TEST(StringNumberConversionsTest, HexStringToInt64) { |
545 static const struct { | 550 static const struct { |
546 std::string input; | 551 std::string input; |
547 int64 output; | 552 int64_t output; |
548 bool success; | 553 bool success; |
549 } cases[] = { | 554 } cases[] = { |
550 {"0", 0, true}, | 555 {"0", 0, true}, |
551 {"42", 66, true}, | 556 {"42", 66, true}, |
552 {"-42", -66, true}, | 557 {"-42", -66, true}, |
553 {"+42", 66, true}, | 558 {"+42", 66, true}, |
554 {"40acd88557b", INT64_C(4444444448123), true}, | 559 {"40acd88557b", INT64_C(4444444448123), true}, |
555 {"7fffffff", INT_MAX, true}, | 560 {"7fffffff", INT_MAX, true}, |
556 {"-80000000", INT_MIN, true}, | 561 {"-80000000", INT_MIN, true}, |
557 {"ffffffff", 0xffffffff, true}, | 562 {"ffffffff", 0xffffffff, true}, |
558 {"DeadBeef", 0xdeadbeef, true}, | 563 {"DeadBeef", 0xdeadbeef, true}, |
559 {"0x42", 66, true}, | 564 {"0x42", 66, true}, |
560 {"-0x42", -66, true}, | 565 {"-0x42", -66, true}, |
561 {"+0x42", 66, true}, | 566 {"+0x42", 66, true}, |
562 {"0x40acd88557b", INT64_C(4444444448123), true}, | 567 {"0x40acd88557b", INT64_C(4444444448123), true}, |
563 {"0x7fffffff", INT_MAX, true}, | 568 {"0x7fffffff", INT_MAX, true}, |
564 {"-0x80000000", INT_MIN, true}, | 569 {"-0x80000000", INT_MIN, true}, |
565 {"0xffffffff", 0xffffffff, true}, | 570 {"0xffffffff", 0xffffffff, true}, |
566 {"0XDeadBeef", 0xdeadbeef, true}, | 571 {"0XDeadBeef", 0xdeadbeef, true}, |
567 {"0x7fffffffffffffff", kint64max, true}, | 572 {"0x7fffffffffffffff", std::numeric_limits<int64_t>::max(), true}, |
568 {"-0x8000000000000000", kint64min, true}, | 573 {"-0x8000000000000000", std::numeric_limits<int64_t>::min(), true}, |
569 {"0x8000000000000000", kint64max, false}, // Overflow test. | 574 {"0x8000000000000000", std::numeric_limits<int64_t>::max(), |
570 {"-0x8000000000000001", kint64min, false}, // Underflow test. | 575 false}, // Overflow test. |
571 {"0x0f", 15, true}, | 576 {"-0x8000000000000001", std::numeric_limits<int64_t>::min(), |
572 {"0f", 15, true}, | 577 false}, // Underflow test. |
573 {" 45", 0x45, false}, | 578 {"0x0f", 15, true}, |
574 {"\t\n\v\f\r 0x45", 0x45, false}, | 579 {"0f", 15, true}, |
575 {" 45", 0x45, false}, | 580 {" 45", 0x45, false}, |
576 {"45 ", 0x45, false}, | 581 {"\t\n\v\f\r 0x45", 0x45, false}, |
577 {"45:", 0x45, false}, | 582 {" 45", 0x45, false}, |
578 {"efgh", 0xef, false}, | 583 {"45 ", 0x45, false}, |
579 {"0xefgh", 0xef, false}, | 584 {"45:", 0x45, false}, |
580 {"hgfe", 0, false}, | 585 {"efgh", 0xef, false}, |
581 {"-", 0, false}, | 586 {"0xefgh", 0xef, false}, |
582 {"", 0, false}, | 587 {"hgfe", 0, false}, |
583 {"0x", 0, false}, | 588 {"-", 0, false}, |
| 589 {"", 0, false}, |
| 590 {"0x", 0, false}, |
584 }; | 591 }; |
585 | 592 |
586 for (size_t i = 0; i < arraysize(cases); ++i) { | 593 for (size_t i = 0; i < arraysize(cases); ++i) { |
587 int64 output = 0; | 594 int64_t output = 0; |
588 EXPECT_EQ(cases[i].success, HexStringToInt64(cases[i].input, &output)); | 595 EXPECT_EQ(cases[i].success, HexStringToInt64(cases[i].input, &output)); |
589 EXPECT_EQ(cases[i].output, output); | 596 EXPECT_EQ(cases[i].output, output); |
590 } | 597 } |
591 // One additional test to verify that conversion of numbers in strings with | 598 // One additional test to verify that conversion of numbers in strings with |
592 // embedded NUL characters. The NUL and extra data after it should be | 599 // embedded NUL characters. The NUL and extra data after it should be |
593 // interpreted as junk after the number. | 600 // interpreted as junk after the number. |
594 const char input[] = "0xc0ffee\0" "9"; | 601 const char input[] = "0xc0ffee\0" "9"; |
595 std::string input_string(input, arraysize(input) - 1); | 602 std::string input_string(input, arraysize(input) - 1); |
596 int64 output; | 603 int64_t output; |
597 EXPECT_FALSE(HexStringToInt64(input_string, &output)); | 604 EXPECT_FALSE(HexStringToInt64(input_string, &output)); |
598 EXPECT_EQ(0xc0ffee, output); | 605 EXPECT_EQ(0xc0ffee, output); |
599 } | 606 } |
600 | 607 |
601 TEST(StringNumberConversionsTest, HexStringToUInt64) { | 608 TEST(StringNumberConversionsTest, HexStringToUInt64) { |
602 static const struct { | 609 static const struct { |
603 std::string input; | 610 std::string input; |
604 uint64 output; | 611 uint64 output; |
605 bool success; | 612 bool success; |
606 } cases[] = { | 613 } cases[] = { |
607 {"0", 0, true}, | 614 {"0", 0, true}, |
608 {"42", 66, true}, | 615 {"42", 66, true}, |
609 {"-42", 0, false}, | 616 {"-42", 0, false}, |
610 {"+42", 66, true}, | 617 {"+42", 66, true}, |
611 {"40acd88557b", INT64_C(4444444448123), true}, | 618 {"40acd88557b", INT64_C(4444444448123), true}, |
612 {"7fffffff", INT_MAX, true}, | 619 {"7fffffff", INT_MAX, true}, |
613 {"-80000000", 0, false}, | 620 {"-80000000", 0, false}, |
614 {"ffffffff", 0xffffffff, true}, | 621 {"ffffffff", 0xffffffff, true}, |
615 {"DeadBeef", 0xdeadbeef, true}, | 622 {"DeadBeef", 0xdeadbeef, true}, |
616 {"0x42", 66, true}, | 623 {"0x42", 66, true}, |
617 {"-0x42", 0, false}, | 624 {"-0x42", 0, false}, |
618 {"+0x42", 66, true}, | 625 {"+0x42", 66, true}, |
619 {"0x40acd88557b", INT64_C(4444444448123), true}, | 626 {"0x40acd88557b", INT64_C(4444444448123), true}, |
620 {"0x7fffffff", INT_MAX, true}, | 627 {"0x7fffffff", INT_MAX, true}, |
621 {"-0x80000000", 0, false}, | 628 {"-0x80000000", 0, false}, |
622 {"0xffffffff", 0xffffffff, true}, | 629 {"0xffffffff", 0xffffffff, true}, |
623 {"0XDeadBeef", 0xdeadbeef, true}, | 630 {"0XDeadBeef", 0xdeadbeef, true}, |
624 {"0x7fffffffffffffff", kint64max, true}, | 631 {"0x7fffffffffffffff", std::numeric_limits<int64_t>::max(), true}, |
625 {"-0x8000000000000000", 0, false}, | 632 {"-0x8000000000000000", 0, false}, |
626 {"0x8000000000000000", UINT64_C(0x8000000000000000), true}, | 633 {"0x8000000000000000", UINT64_C(0x8000000000000000), true}, |
627 {"-0x8000000000000001", 0, false}, | 634 {"-0x8000000000000001", 0, false}, |
628 {"0xFFFFFFFFFFFFFFFF", kuint64max, true}, | 635 {"0xFFFFFFFFFFFFFFFF", std::numeric_limits<uint64_t>::max(), true}, |
629 {"FFFFFFFFFFFFFFFF", kuint64max, true}, | 636 {"FFFFFFFFFFFFFFFF", std::numeric_limits<uint64_t>::max(), true}, |
630 {"0x0000000000000000", 0, true}, | 637 {"0x0000000000000000", 0, true}, |
631 {"0000000000000000", 0, true}, | 638 {"0000000000000000", 0, true}, |
632 {"1FFFFFFFFFFFFFFFF", kuint64max, false}, // Overflow test. | 639 {"1FFFFFFFFFFFFFFFF", std::numeric_limits<uint64_t>::max(), |
633 {"0x0f", 15, true}, | 640 false}, // Overflow test. |
634 {"0f", 15, true}, | 641 {"0x0f", 15, true}, |
635 {" 45", 0x45, false}, | 642 {"0f", 15, true}, |
636 {"\t\n\v\f\r 0x45", 0x45, false}, | 643 {" 45", 0x45, false}, |
637 {" 45", 0x45, false}, | 644 {"\t\n\v\f\r 0x45", 0x45, false}, |
638 {"45 ", 0x45, false}, | 645 {" 45", 0x45, false}, |
639 {"45:", 0x45, false}, | 646 {"45 ", 0x45, false}, |
640 {"efgh", 0xef, false}, | 647 {"45:", 0x45, false}, |
641 {"0xefgh", 0xef, false}, | 648 {"efgh", 0xef, false}, |
642 {"hgfe", 0, false}, | 649 {"0xefgh", 0xef, false}, |
643 {"-", 0, false}, | 650 {"hgfe", 0, false}, |
644 {"", 0, false}, | 651 {"-", 0, false}, |
645 {"0x", 0, false}, | 652 {"", 0, false}, |
| 653 {"0x", 0, false}, |
646 }; | 654 }; |
647 | 655 |
648 for (size_t i = 0; i < arraysize(cases); ++i) { | 656 for (size_t i = 0; i < arraysize(cases); ++i) { |
649 uint64 output = 0; | 657 uint64 output = 0; |
650 EXPECT_EQ(cases[i].success, HexStringToUInt64(cases[i].input, &output)); | 658 EXPECT_EQ(cases[i].success, HexStringToUInt64(cases[i].input, &output)); |
651 EXPECT_EQ(cases[i].output, output); | 659 EXPECT_EQ(cases[i].output, output); |
652 } | 660 } |
653 // One additional test to verify that conversion of numbers in strings with | 661 // One additional test to verify that conversion of numbers in strings with |
654 // embedded NUL characters. The NUL and extra data after it should be | 662 // embedded NUL characters. The NUL and extra data after it should be |
655 // interpreted as junk after the number. | 663 // interpreted as junk after the number. |
(...skipping 25 matching lines...) Expand all Loading... |
681 {"45 ", "\x45", 1, false}, | 689 {"45 ", "\x45", 1, false}, |
682 {"efgh", "\xef", 1, false}, | 690 {"efgh", "\xef", 1, false}, |
683 {"", "", 0, false}, | 691 {"", "", 0, false}, |
684 {"0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8, true}, | 692 {"0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8, true}, |
685 {"0123456789ABCDEF012345", | 693 {"0123456789ABCDEF012345", |
686 "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45", 11, true}, | 694 "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45", 11, true}, |
687 }; | 695 }; |
688 | 696 |
689 | 697 |
690 for (size_t i = 0; i < arraysize(cases); ++i) { | 698 for (size_t i = 0; i < arraysize(cases); ++i) { |
691 std::vector<uint8> output; | 699 std::vector<uint8_t> output; |
692 std::vector<uint8> compare; | 700 std::vector<uint8_t> compare; |
693 EXPECT_EQ(cases[i].success, HexStringToBytes(cases[i].input, &output)) << | 701 EXPECT_EQ(cases[i].success, HexStringToBytes(cases[i].input, &output)) << |
694 i << ": " << cases[i].input; | 702 i << ": " << cases[i].input; |
695 for (size_t j = 0; j < cases[i].output_len; ++j) | 703 for (size_t j = 0; j < cases[i].output_len; ++j) |
696 compare.push_back(static_cast<uint8>(cases[i].output[j])); | 704 compare.push_back(static_cast<uint8_t>(cases[i].output[j])); |
697 ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input; | 705 ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input; |
698 EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) << | 706 EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) << |
699 i << ": " << cases[i].input; | 707 i << ": " << cases[i].input; |
700 } | 708 } |
701 } | 709 } |
702 | 710 |
703 TEST(StringNumberConversionsTest, StringToDouble) { | 711 TEST(StringNumberConversionsTest, StringToDouble) { |
704 static const struct { | 712 static const struct { |
705 std::string input; | 713 std::string input; |
706 double output; | 714 double output; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
787 | 795 |
788 TEST(StringNumberConversionsTest, HexEncode) { | 796 TEST(StringNumberConversionsTest, HexEncode) { |
789 std::string hex(HexEncode(NULL, 0)); | 797 std::string hex(HexEncode(NULL, 0)); |
790 EXPECT_EQ(hex.length(), 0U); | 798 EXPECT_EQ(hex.length(), 0U); |
791 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 799 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
792 hex = HexEncode(bytes, sizeof(bytes)); | 800 hex = HexEncode(bytes, sizeof(bytes)); |
793 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 801 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
794 } | 802 } |
795 | 803 |
796 } // namespace base | 804 } // namespace base |
OLD | NEW |