| 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" |
| 6 |
| 5 #include <errno.h> | 7 #include <errno.h> |
| 6 #include <stdint.h> | 8 #include <stdint.h> |
| 7 #include <stdio.h> | 9 #include <stdio.h> |
| 8 | 10 |
| 9 #include <cmath> | 11 #include <cmath> |
| 10 #include <limits> | 12 #include <limits> |
| 11 | 13 |
| 12 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 template <typename INT> | 23 template <typename INT> |
| 23 struct IntToStringTest { | 24 struct IntToStringTest { |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 TEST(StringNumberConversionsTest, StringToInt64) { | 231 TEST(StringNumberConversionsTest, StringToInt64) { |
| 231 static const struct { | 232 static const struct { |
| 232 std::string input; | 233 std::string input; |
| 233 int64 output; | 234 int64 output; |
| 234 bool success; | 235 bool success; |
| 235 } cases[] = { | 236 } cases[] = { |
| 236 {"0", 0, true}, | 237 {"0", 0, true}, |
| 237 {"42", 42, true}, | 238 {"42", 42, true}, |
| 238 {"-2147483648", INT_MIN, true}, | 239 {"-2147483648", INT_MIN, true}, |
| 239 {"2147483647", INT_MAX, true}, | 240 {"2147483647", INT_MAX, true}, |
| 240 {"-2147483649", GG_INT64_C(-2147483649), true}, | 241 {"-2147483649", INT64_C(-2147483649), true}, |
| 241 {"-99999999999", GG_INT64_C(-99999999999), true}, | 242 {"-99999999999", INT64_C(-99999999999), true}, |
| 242 {"2147483648", GG_INT64_C(2147483648), true}, | 243 {"2147483648", INT64_C(2147483648), true}, |
| 243 {"99999999999", GG_INT64_C(99999999999), true}, | 244 {"99999999999", INT64_C(99999999999), true}, |
| 244 {"9223372036854775807", kint64max, true}, | 245 {"9223372036854775807", kint64max, true}, |
| 245 {"-9223372036854775808", kint64min, true}, | 246 {"-9223372036854775808", kint64min, true}, |
| 246 {"09", 9, true}, | 247 {"09", 9, true}, |
| 247 {"-09", -9, true}, | 248 {"-09", -9, true}, |
| 248 {"", 0, false}, | 249 {"", 0, false}, |
| 249 {" 42", 42, false}, | 250 {" 42", 42, false}, |
| 250 {"42 ", 42, false}, | 251 {"42 ", 42, false}, |
| 251 {"0x42", 0, false}, | 252 {"0x42", 0, false}, |
| 252 {"\t\n\v\f\r 42", 42, false}, | 253 {"\t\n\v\f\r 42", 42, false}, |
| 253 {"blah42", 0, false}, | 254 {"blah42", 0, false}, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 std::string input; | 298 std::string input; |
| 298 uint64 output; | 299 uint64 output; |
| 299 bool success; | 300 bool success; |
| 300 } cases[] = { | 301 } cases[] = { |
| 301 {"0", 0, true}, | 302 {"0", 0, true}, |
| 302 {"42", 42, true}, | 303 {"42", 42, true}, |
| 303 {"-2147483648", 0, false}, | 304 {"-2147483648", 0, false}, |
| 304 {"2147483647", INT_MAX, true}, | 305 {"2147483647", INT_MAX, true}, |
| 305 {"-2147483649", 0, false}, | 306 {"-2147483649", 0, false}, |
| 306 {"-99999999999", 0, false}, | 307 {"-99999999999", 0, false}, |
| 307 {"2147483648", GG_UINT64_C(2147483648), true}, | 308 {"2147483648", UINT64_C(2147483648), true}, |
| 308 {"99999999999", GG_UINT64_C(99999999999), true}, | 309 {"99999999999", UINT64_C(99999999999), true}, |
| 309 {"9223372036854775807", kint64max, true}, | 310 {"9223372036854775807", kint64max, true}, |
| 310 {"-9223372036854775808", 0, false}, | 311 {"-9223372036854775808", 0, false}, |
| 311 {"09", 9, true}, | 312 {"09", 9, true}, |
| 312 {"-09", 0, false}, | 313 {"-09", 0, false}, |
| 313 {"", 0, false}, | 314 {"", 0, false}, |
| 314 {" 42", 42, false}, | 315 {" 42", 42, false}, |
| 315 {"42 ", 42, false}, | 316 {"42 ", 42, false}, |
| 316 {"0x42", 0, false}, | 317 {"0x42", 0, false}, |
| 317 {"\t\n\v\f\r 42", 42, false}, | 318 {"\t\n\v\f\r 42", 42, false}, |
| 318 {"blah42", 0, false}, | 319 {"blah42", 0, false}, |
| 319 {"42blah", 42, false}, | 320 {"42blah", 42, false}, |
| 320 {"blah42blah", 0, false}, | 321 {"blah42blah", 0, false}, |
| 321 {"-273.15", 0, false}, | 322 {"-273.15", 0, false}, |
| 322 {"+98.6", 98, false}, | 323 {"+98.6", 98, false}, |
| 323 {"--123", 0, 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 {"-", 0, false}, | 328 {"-", 0, false}, |
| 328 {"-9223372036854775809", 0, false}, | 329 {"-9223372036854775809", 0, false}, |
| 329 {"-99999999999999999999", 0, false}, | 330 {"-99999999999999999999", 0, false}, |
| 330 {"9223372036854775808", GG_UINT64_C(9223372036854775808), true}, | 331 {"9223372036854775808", UINT64_C(9223372036854775808), true}, |
| 331 {"99999999999999999999", kuint64max, false}, | 332 {"99999999999999999999", kuint64max, false}, |
| 332 {"18446744073709551615", kuint64max, true}, | 333 {"18446744073709551615", kuint64max, true}, |
| 333 {"18446744073709551616", kuint64max, false}, | 334 {"18446744073709551616", kuint64max, false}, |
| 334 }; | 335 }; |
| 335 | 336 |
| 336 for (size_t i = 0; i < arraysize(cases); ++i) { | 337 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 337 uint64 output = 0; | 338 uint64 output = 0; |
| 338 EXPECT_EQ(cases[i].success, StringToUint64(cases[i].input, &output)); | 339 EXPECT_EQ(cases[i].success, StringToUint64(cases[i].input, &output)); |
| 339 EXPECT_EQ(cases[i].output, output); | 340 EXPECT_EQ(cases[i].output, output); |
| 340 | 341 |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 543 TEST(StringNumberConversionsTest, HexStringToInt64) { | 544 TEST(StringNumberConversionsTest, HexStringToInt64) { |
| 544 static const struct { | 545 static const struct { |
| 545 std::string input; | 546 std::string input; |
| 546 int64 output; | 547 int64 output; |
| 547 bool success; | 548 bool success; |
| 548 } cases[] = { | 549 } cases[] = { |
| 549 {"0", 0, true}, | 550 {"0", 0, true}, |
| 550 {"42", 66, true}, | 551 {"42", 66, true}, |
| 551 {"-42", -66, true}, | 552 {"-42", -66, true}, |
| 552 {"+42", 66, true}, | 553 {"+42", 66, true}, |
| 553 {"40acd88557b", GG_INT64_C(4444444448123), true}, | 554 {"40acd88557b", INT64_C(4444444448123), true}, |
| 554 {"7fffffff", INT_MAX, true}, | 555 {"7fffffff", INT_MAX, true}, |
| 555 {"-80000000", INT_MIN, true}, | 556 {"-80000000", INT_MIN, true}, |
| 556 {"ffffffff", 0xffffffff, true}, | 557 {"ffffffff", 0xffffffff, true}, |
| 557 {"DeadBeef", 0xdeadbeef, true}, | 558 {"DeadBeef", 0xdeadbeef, true}, |
| 558 {"0x42", 66, true}, | 559 {"0x42", 66, true}, |
| 559 {"-0x42", -66, true}, | 560 {"-0x42", -66, true}, |
| 560 {"+0x42", 66, true}, | 561 {"+0x42", 66, true}, |
| 561 {"0x40acd88557b", GG_INT64_C(4444444448123), true}, | 562 {"0x40acd88557b", INT64_C(4444444448123), true}, |
| 562 {"0x7fffffff", INT_MAX, true}, | 563 {"0x7fffffff", INT_MAX, true}, |
| 563 {"-0x80000000", INT_MIN, true}, | 564 {"-0x80000000", INT_MIN, true}, |
| 564 {"0xffffffff", 0xffffffff, true}, | 565 {"0xffffffff", 0xffffffff, true}, |
| 565 {"0XDeadBeef", 0xdeadbeef, true}, | 566 {"0XDeadBeef", 0xdeadbeef, true}, |
| 566 {"0x7fffffffffffffff", kint64max, true}, | 567 {"0x7fffffffffffffff", kint64max, true}, |
| 567 {"-0x8000000000000000", kint64min, true}, | 568 {"-0x8000000000000000", kint64min, true}, |
| 568 {"0x8000000000000000", kint64max, false}, // Overflow test. | 569 {"0x8000000000000000", kint64max, false}, // Overflow test. |
| 569 {"-0x8000000000000001", kint64min, false}, // Underflow test. | 570 {"-0x8000000000000001", kint64min, false}, // Underflow test. |
| 570 {"0x0f", 15, true}, | 571 {"0x0f", 15, true}, |
| 571 {"0f", 15, true}, | 572 {"0f", 15, true}, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 600 TEST(StringNumberConversionsTest, HexStringToUInt64) { | 601 TEST(StringNumberConversionsTest, HexStringToUInt64) { |
| 601 static const struct { | 602 static const struct { |
| 602 std::string input; | 603 std::string input; |
| 603 uint64 output; | 604 uint64 output; |
| 604 bool success; | 605 bool success; |
| 605 } cases[] = { | 606 } cases[] = { |
| 606 {"0", 0, true}, | 607 {"0", 0, true}, |
| 607 {"42", 66, true}, | 608 {"42", 66, true}, |
| 608 {"-42", 0, false}, | 609 {"-42", 0, false}, |
| 609 {"+42", 66, true}, | 610 {"+42", 66, true}, |
| 610 {"40acd88557b", GG_INT64_C(4444444448123), true}, | 611 {"40acd88557b", INT64_C(4444444448123), true}, |
| 611 {"7fffffff", INT_MAX, true}, | 612 {"7fffffff", INT_MAX, true}, |
| 612 {"-80000000", 0, false}, | 613 {"-80000000", 0, false}, |
| 613 {"ffffffff", 0xffffffff, true}, | 614 {"ffffffff", 0xffffffff, true}, |
| 614 {"DeadBeef", 0xdeadbeef, true}, | 615 {"DeadBeef", 0xdeadbeef, true}, |
| 615 {"0x42", 66, true}, | 616 {"0x42", 66, true}, |
| 616 {"-0x42", 0, false}, | 617 {"-0x42", 0, false}, |
| 617 {"+0x42", 66, true}, | 618 {"+0x42", 66, true}, |
| 618 {"0x40acd88557b", GG_INT64_C(4444444448123), true}, | 619 {"0x40acd88557b", INT64_C(4444444448123), true}, |
| 619 {"0x7fffffff", INT_MAX, true}, | 620 {"0x7fffffff", INT_MAX, true}, |
| 620 {"-0x80000000", 0, false}, | 621 {"-0x80000000", 0, false}, |
| 621 {"0xffffffff", 0xffffffff, true}, | 622 {"0xffffffff", 0xffffffff, true}, |
| 622 {"0XDeadBeef", 0xdeadbeef, true}, | 623 {"0XDeadBeef", 0xdeadbeef, true}, |
| 623 {"0x7fffffffffffffff", kint64max, true}, | 624 {"0x7fffffffffffffff", kint64max, true}, |
| 624 {"-0x8000000000000000", 0, false}, | 625 {"-0x8000000000000000", 0, false}, |
| 625 {"0x8000000000000000", GG_UINT64_C(0x8000000000000000), true}, | 626 {"0x8000000000000000", UINT64_C(0x8000000000000000), true}, |
| 626 {"-0x8000000000000001", 0, false}, | 627 {"-0x8000000000000001", 0, false}, |
| 627 {"0xFFFFFFFFFFFFFFFF", kuint64max, true}, | 628 {"0xFFFFFFFFFFFFFFFF", kuint64max, true}, |
| 628 {"FFFFFFFFFFFFFFFF", kuint64max, true}, | 629 {"FFFFFFFFFFFFFFFF", kuint64max, true}, |
| 629 {"0x0000000000000000", 0, true}, | 630 {"0x0000000000000000", 0, true}, |
| 630 {"0000000000000000", 0, true}, | 631 {"0000000000000000", 0, true}, |
| 631 {"1FFFFFFFFFFFFFFFF", kuint64max, false}, // Overflow test. | 632 {"1FFFFFFFFFFFFFFFF", kuint64max, false}, // Overflow test. |
| 632 {"0x0f", 15, true}, | 633 {"0x0f", 15, true}, |
| 633 {"0f", 15, true}, | 634 {"0f", 15, true}, |
| 634 {" 45", 0x45, false}, | 635 {" 45", 0x45, false}, |
| 635 {"\t\n\v\f\r 0x45", 0x45, false}, | 636 {"\t\n\v\f\r 0x45", 0x45, false}, |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 786 | 787 |
| 787 TEST(StringNumberConversionsTest, HexEncode) { | 788 TEST(StringNumberConversionsTest, HexEncode) { |
| 788 std::string hex(HexEncode(NULL, 0)); | 789 std::string hex(HexEncode(NULL, 0)); |
| 789 EXPECT_EQ(hex.length(), 0U); | 790 EXPECT_EQ(hex.length(), 0U); |
| 790 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 791 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
| 791 hex = HexEncode(bytes, sizeof(bytes)); | 792 hex = HexEncode(bytes, sizeof(bytes)); |
| 792 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 793 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
| 793 } | 794 } |
| 794 | 795 |
| 795 } // namespace base | 796 } // namespace base |
| OLD | NEW |