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 <limits.h> | 8 #include <limits.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 i << ": " << cases[i].input; | 712 i << ": " << cases[i].input; |
713 } | 713 } |
714 } | 714 } |
715 | 715 |
716 TEST(StringNumberConversionsTest, StringToDouble) { | 716 TEST(StringNumberConversionsTest, StringToDouble) { |
717 static const struct { | 717 static const struct { |
718 std::string input; | 718 std::string input; |
719 double output; | 719 double output; |
720 bool success; | 720 bool success; |
721 } cases[] = { | 721 } cases[] = { |
| 722 // Test different forms of zero. |
722 {"0", 0.0, true}, | 723 {"0", 0.0, true}, |
| 724 {"+0", 0.0, true}, |
| 725 {"-0", 0.0, true}, |
| 726 {"0.0", 0.0, true}, |
| 727 {"000000000000000000000000000000.0", 0.0, true}, |
| 728 {"0.000000000000000000000000000", 0.0, true}, |
| 729 |
| 730 // Test the answer. |
723 {"42", 42.0, true}, | 731 {"42", 42.0, true}, |
724 {"-42", -42.0, true}, | 732 {"-42", -42.0, true}, |
| 733 |
| 734 // Test variances of an ordinary number. |
725 {"123.45", 123.45, true}, | 735 {"123.45", 123.45, true}, |
726 {"-123.45", -123.45, true}, | 736 {"-123.45", -123.45, true}, |
727 {"+123.45", 123.45, true}, | 737 {"+123.45", 123.45, true}, |
| 738 |
| 739 // Test different forms of representation. |
728 {"2.99792458e8", 299792458.0, true}, | 740 {"2.99792458e8", 299792458.0, true}, |
729 {"149597870.691E+3", 149597870691.0, true}, | 741 {"149597870.691E+3", 149597870691.0, true}, |
730 {"6.", 6.0, true}, | 742 {"6.", 6.0, true}, |
| 743 |
| 744 // Test around the largest/smallest value that a double can represent. |
| 745 {"9e307", 9e307, true}, |
| 746 {"1.7976e308", 1.7976e308, true}, |
| 747 {"1.7977e308", HUGE_VAL, false}, |
| 748 {"9e308", HUGE_VAL, false}, |
| 749 {"9e309", HUGE_VAL, false}, |
| 750 {"9e999", HUGE_VAL, false}, |
| 751 {"9e1999", HUGE_VAL, false}, |
| 752 {"9e19999", HUGE_VAL, false}, |
731 {"9e99999999999999999999", HUGE_VAL, false}, | 753 {"9e99999999999999999999", HUGE_VAL, false}, |
| 754 {"-9e307", -9e307, true}, |
| 755 {"-1.7976e308", -1.7976e308, true}, |
| 756 {"-1.7977e308", -HUGE_VAL, false}, |
| 757 {"-9e308", -HUGE_VAL, false}, |
| 758 {"-9e309", -HUGE_VAL, false}, |
| 759 {"-9e999", -HUGE_VAL, false}, |
| 760 {"-9e1999", -HUGE_VAL, false}, |
| 761 {"-9e19999", -HUGE_VAL, false}, |
732 {"-9e99999999999999999999", -HUGE_VAL, false}, | 762 {"-9e99999999999999999999", -HUGE_VAL, false}, |
| 763 |
| 764 // Test more exponents. |
733 {"1e-2", 0.01, true}, | 765 {"1e-2", 0.01, true}, |
734 {"42 ", 42.0, false}, | 766 {"42 ", 42.0, false}, |
735 {" 1e-2", 0.01, false}, | 767 {" 1e-2", 0.01, false}, |
736 {"1e-2 ", 0.01, false}, | 768 {"1e-2 ", 0.01, false}, |
737 {"-1E-7", -0.0000001, true}, | 769 {"-1E-7", -0.0000001, true}, |
738 {"01e02", 100, true}, | 770 {"01e02", 100, true}, |
739 {"2.3e15", 2.3e15, true}, | 771 {"2.3e15", 2.3e15, true}, |
| 772 |
| 773 // Test some invalid cases. |
740 {"\t\n\v\f\r -123.45e2", -12345.0, false}, | 774 {"\t\n\v\f\r -123.45e2", -12345.0, false}, |
741 {"+123 e4", 123.0, false}, | 775 {"+123 e4", 123.0, false}, |
742 {"123e ", 123.0, false}, | 776 {"123e ", 123.0, false}, |
743 {"123e", 123.0, false}, | 777 {"123e", 123.0, false}, |
744 {" 2.99", 2.99, false}, | 778 {" 2.99", 2.99, false}, |
745 {"1e3.4", 1000.0, false}, | 779 {"1e3.4", 1000.0, false}, |
746 {"nothing", 0.0, false}, | 780 {"nothing", 0.0, false}, |
747 {"-", 0.0, false}, | 781 {"-", 0.0, false}, |
748 {"+", 0.0, false}, | 782 {"+", 0.0, false}, |
749 {"", 0.0, false}, | 783 {"", 0.0, false}, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
850 }; | 884 }; |
851 | 885 |
852 for (const auto& test : cases) { | 886 for (const auto& test : cases) { |
853 double output; | 887 double output; |
854 EXPECT_TRUE(StringToDouble(test.input, &output)); | 888 EXPECT_TRUE(StringToDouble(test.input, &output)); |
855 EXPECT_EQ(bit_cast<uint64_t>(output), test.expected); | 889 EXPECT_EQ(bit_cast<uint64_t>(output), test.expected); |
856 } | 890 } |
857 } | 891 } |
858 | 892 |
859 } // namespace base | 893 } // namespace base |
OLD | NEW |