| 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 <stdint.h> | 6 #include <stdint.h> |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 }; | 466 }; |
| 467 | 467 |
| 468 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 468 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 469 int output = 0; | 469 int output = 0; |
| 470 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output)); | 470 EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output)); |
| 471 EXPECT_EQ(cases[i].output, output); | 471 EXPECT_EQ(cases[i].output, output); |
| 472 } | 472 } |
| 473 // One additional test to verify that conversion of numbers in strings with | 473 // One additional test to verify that conversion of numbers in strings with |
| 474 // embedded NUL characters. The NUL and extra data after it should be | 474 // embedded NUL characters. The NUL and extra data after it should be |
| 475 // interpreted as junk after the number. | 475 // interpreted as junk after the number. |
| 476 const char input[] = "0xc0ffee\09"; | 476 const char input[] = "0xc0ffee\0" "9"; |
| 477 std::string input_string(input, arraysize(input) - 1); | 477 std::string input_string(input, arraysize(input) - 1); |
| 478 int output; | 478 int output; |
| 479 EXPECT_FALSE(HexStringToInt(input_string, &output)); | 479 EXPECT_FALSE(HexStringToInt(input_string, &output)); |
| 480 EXPECT_EQ(0xc0ffee, output); | 480 EXPECT_EQ(0xc0ffee, output); |
| 481 } | 481 } |
| 482 | 482 |
| 483 TEST(StringNumberConversionsTest, HexStringToUInt) { | 483 TEST(StringNumberConversionsTest, HexStringToUInt) { |
| 484 static const struct { | 484 static const struct { |
| 485 std::string input; | 485 std::string input; |
| 486 uint32 output; | 486 uint32 output; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 }; | 526 }; |
| 527 | 527 |
| 528 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 528 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 529 uint32 output = 0; | 529 uint32 output = 0; |
| 530 EXPECT_EQ(cases[i].success, HexStringToUInt(cases[i].input, &output)); | 530 EXPECT_EQ(cases[i].success, HexStringToUInt(cases[i].input, &output)); |
| 531 EXPECT_EQ(cases[i].output, output); | 531 EXPECT_EQ(cases[i].output, output); |
| 532 } | 532 } |
| 533 // One additional test to verify that conversion of numbers in strings with | 533 // One additional test to verify that conversion of numbers in strings with |
| 534 // embedded NUL characters. The NUL and extra data after it should be | 534 // embedded NUL characters. The NUL and extra data after it should be |
| 535 // interpreted as junk after the number. | 535 // interpreted as junk after the number. |
| 536 const char input[] = "0xc0ffee\09"; | 536 const char input[] = "0xc0ffee\0" "9"; |
| 537 std::string input_string(input, arraysize(input) - 1); | 537 std::string input_string(input, arraysize(input) - 1); |
| 538 uint32 output; | 538 uint32 output; |
| 539 EXPECT_FALSE(HexStringToUInt(input_string, &output)); | 539 EXPECT_FALSE(HexStringToUInt(input_string, &output)); |
| 540 EXPECT_EQ(0xc0ffeeU, output); | 540 EXPECT_EQ(0xc0ffeeU, output); |
| 541 } | 541 } |
| 542 | 542 |
| 543 TEST(StringNumberConversionsTest, HexStringToInt64) { | 543 TEST(StringNumberConversionsTest, HexStringToInt64) { |
| 544 static const struct { | 544 static const struct { |
| 545 std::string input; | 545 std::string input; |
| 546 int64 output; | 546 int64 output; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 }; | 583 }; |
| 584 | 584 |
| 585 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 585 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 586 int64 output = 0; | 586 int64 output = 0; |
| 587 EXPECT_EQ(cases[i].success, HexStringToInt64(cases[i].input, &output)); | 587 EXPECT_EQ(cases[i].success, HexStringToInt64(cases[i].input, &output)); |
| 588 EXPECT_EQ(cases[i].output, output); | 588 EXPECT_EQ(cases[i].output, output); |
| 589 } | 589 } |
| 590 // One additional test to verify that conversion of numbers in strings with | 590 // One additional test to verify that conversion of numbers in strings with |
| 591 // embedded NUL characters. The NUL and extra data after it should be | 591 // embedded NUL characters. The NUL and extra data after it should be |
| 592 // interpreted as junk after the number. | 592 // interpreted as junk after the number. |
| 593 const char input[] = "0xc0ffee\09"; | 593 const char input[] = "0xc0ffee\0" "9"; |
| 594 std::string input_string(input, arraysize(input) - 1); | 594 std::string input_string(input, arraysize(input) - 1); |
| 595 int64 output; | 595 int64 output; |
| 596 EXPECT_FALSE(HexStringToInt64(input_string, &output)); | 596 EXPECT_FALSE(HexStringToInt64(input_string, &output)); |
| 597 EXPECT_EQ(0xc0ffee, output); | 597 EXPECT_EQ(0xc0ffee, output); |
| 598 } | 598 } |
| 599 | 599 |
| 600 TEST(StringNumberConversionsTest, HexStringToUInt64) { | 600 TEST(StringNumberConversionsTest, HexStringToUInt64) { |
| 601 static const struct { | 601 static const struct { |
| 602 std::string input; | 602 std::string input; |
| 603 uint64 output; | 603 uint64 output; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 }; | 645 }; |
| 646 | 646 |
| 647 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 647 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 648 uint64 output = 0; | 648 uint64 output = 0; |
| 649 EXPECT_EQ(cases[i].success, HexStringToUInt64(cases[i].input, &output)); | 649 EXPECT_EQ(cases[i].success, HexStringToUInt64(cases[i].input, &output)); |
| 650 EXPECT_EQ(cases[i].output, output); | 650 EXPECT_EQ(cases[i].output, output); |
| 651 } | 651 } |
| 652 // One additional test to verify that conversion of numbers in strings with | 652 // One additional test to verify that conversion of numbers in strings with |
| 653 // embedded NUL characters. The NUL and extra data after it should be | 653 // embedded NUL characters. The NUL and extra data after it should be |
| 654 // interpreted as junk after the number. | 654 // interpreted as junk after the number. |
| 655 const char input[] = "0xc0ffee\09"; | 655 const char input[] = "0xc0ffee\0" "9"; |
| 656 std::string input_string(input, arraysize(input) - 1); | 656 std::string input_string(input, arraysize(input) - 1); |
| 657 uint64 output; | 657 uint64 output; |
| 658 EXPECT_FALSE(HexStringToUInt64(input_string, &output)); | 658 EXPECT_FALSE(HexStringToUInt64(input_string, &output)); |
| 659 EXPECT_EQ(0xc0ffeeU, output); | 659 EXPECT_EQ(0xc0ffeeU, output); |
| 660 } | 660 } |
| 661 | 661 |
| 662 TEST(StringNumberConversionsTest, HexStringToBytes) { | 662 TEST(StringNumberConversionsTest, HexStringToBytes) { |
| 663 static const struct { | 663 static const struct { |
| 664 const std::string input; | 664 const std::string input; |
| 665 const char* output; | 665 const char* output; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 errno = 1; | 740 errno = 1; |
| 741 EXPECT_EQ(cases[i].success, StringToDouble(cases[i].input, &output)); | 741 EXPECT_EQ(cases[i].success, StringToDouble(cases[i].input, &output)); |
| 742 if (cases[i].success) | 742 if (cases[i].success) |
| 743 EXPECT_EQ(1, errno) << i; // confirm that errno is unchanged. | 743 EXPECT_EQ(1, errno) << i; // confirm that errno is unchanged. |
| 744 EXPECT_DOUBLE_EQ(cases[i].output, output); | 744 EXPECT_DOUBLE_EQ(cases[i].output, output); |
| 745 } | 745 } |
| 746 | 746 |
| 747 // One additional test to verify that conversion of numbers in strings with | 747 // One additional test to verify that conversion of numbers in strings with |
| 748 // embedded NUL characters. The NUL and extra data after it should be | 748 // embedded NUL characters. The NUL and extra data after it should be |
| 749 // interpreted as junk after the number. | 749 // interpreted as junk after the number. |
| 750 const char input[] = "3.14\0159"; | 750 const char input[] = "3.14\0" "159"; |
| 751 std::string input_string(input, arraysize(input) - 1); | 751 std::string input_string(input, arraysize(input) - 1); |
| 752 double output; | 752 double output; |
| 753 EXPECT_FALSE(StringToDouble(input_string, &output)); | 753 EXPECT_FALSE(StringToDouble(input_string, &output)); |
| 754 EXPECT_DOUBLE_EQ(3.14, output); | 754 EXPECT_DOUBLE_EQ(3.14, output); |
| 755 } | 755 } |
| 756 | 756 |
| 757 TEST(StringNumberConversionsTest, DoubleToString) { | 757 TEST(StringNumberConversionsTest, DoubleToString) { |
| 758 static const struct { | 758 static const struct { |
| 759 double input; | 759 double input; |
| 760 const char* expected; | 760 const char* expected; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 786 | 786 |
| 787 TEST(StringNumberConversionsTest, HexEncode) { | 787 TEST(StringNumberConversionsTest, HexEncode) { |
| 788 std::string hex(HexEncode(NULL, 0)); | 788 std::string hex(HexEncode(NULL, 0)); |
| 789 EXPECT_EQ(hex.length(), 0U); | 789 EXPECT_EQ(hex.length(), 0U); |
| 790 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 790 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
| 791 hex = HexEncode(bytes, sizeof(bytes)); | 791 hex = HexEncode(bytes, sizeof(bytes)); |
| 792 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 792 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
| 793 } | 793 } |
| 794 | 794 |
| 795 } // namespace base | 795 } // namespace base |
| OLD | NEW |