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 787 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
798 } | 798 } |
799 | 799 |
800 TEST(StringNumberConversionsTest, HexEncode) { | 800 TEST(StringNumberConversionsTest, HexEncode) { |
801 std::string hex(HexEncode(NULL, 0)); | 801 std::string hex(HexEncode(NULL, 0)); |
802 EXPECT_EQ(hex.length(), 0U); | 802 EXPECT_EQ(hex.length(), 0U); |
803 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 803 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
804 hex = HexEncode(bytes, sizeof(bytes)); | 804 hex = HexEncode(bytes, sizeof(bytes)); |
805 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 805 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
806 } | 806 } |
807 | 807 |
808 uint64_t RawFP(double fp) { | |
809 return *reinterpret_cast<uint64_t*>(&fp); | |
Nico
2016/06/15 09:06:59
please use base/bit_cast.h instead
scottmg
2016/06/15 16:52:37
Done. (inline)
| |
810 } | |
811 | |
812 // Test cases of known-bad strtod conversions that motivated the use of dmg_fp. | |
813 // See https://bugs.chromium.org/p/chromium/issues/detail?id=593512. | |
814 TEST(StringNumberConversionsTest, StrtodFailures) { | |
815 static const struct { | |
816 const char* input; | |
817 uint64_t expected; | |
818 } cases[] = { | |
819 // http://www.exploringbinary.com/incorrectly-rounded-conversions-in-visua l-c-plus-plus/ | |
820 {"9214843084008499", 0x43405e6cec57761aULL}, | |
821 {"0.500000000000000166533453693773481063544750213623046875", | |
822 0x3fe0000000000002ULL}, | |
823 {"30078505129381147446200", 0x44997a3c7271b021ULL}, | |
824 {"1777820000000000000001", 0x4458180d5bad2e3eULL}, | |
825 {"0.500000000000000166547006220929549868969843373633921146392822265625", | |
826 0x3fe0000000000002ULL}, | |
827 {"0.50000000000000016656055874808561867439493653364479541778564453125", | |
828 0x3fe0000000000002ULL}, | |
829 {"0.3932922657273", 0x3fd92bb352c4623aULL}, | |
830 | |
831 // http://www.exploringbinary.com/incorrectly-rounded-conversions-in-gcc-a nd-glibc/ | |
832 {"0.500000000000000166533453693773481063544750213623046875", | |
833 0x3fe0000000000002ULL}, | |
834 {"3.518437208883201171875e13", 0x42c0000000000002ULL}, | |
835 {"62.5364939768271845828", 0x404f44abd5aa7ca4ULL}, | |
836 {"8.10109172351e-10", 0x3e0bd5cbaef0fd0cULL}, | |
837 {"1.50000000000000011102230246251565404236316680908203125", | |
838 0x3ff8000000000000ULL}, | |
839 {"9007199254740991.4999999999999999999999999999999995", | |
840 0x433fffffffffffffULL}, | |
841 | |
842 // http://www.exploringbinary.com/incorrect-decimal-to-floating-point-conv ersion-in-sqlite/ | |
843 {"1e-23", 0x3b282db34012b251ULL}, | |
844 {"8.533e+68", 0x4e3fa69165a8eea2ULL}, | |
845 {"4.1006e-184", 0x19dbe0d1c7ea60c9ULL}, | |
846 {"9.998e+307", 0x7fe1cc0a350ca87bULL}, | |
847 {"9.9538452227e-280", 0x0602117ae45cde43ULL}, | |
848 {"6.47660115e-260", 0x0a1fdd9e333badadULL}, | |
849 {"7.4e+47", 0x49e033d7eca0adefULL}, | |
850 {"5.92e+48", 0x4a1033d7eca0adefULL}, | |
851 {"7.35e+66", 0x4dd172b70eababa9ULL}, | |
852 {"8.32116e+55", 0x4b8b2628393e02cdULL}, | |
853 }; | |
854 | |
855 for (const auto& test : cases) { | |
856 double output; | |
857 EXPECT_TRUE(StringToDouble(test.input, &output)); | |
858 EXPECT_EQ(RawFP(output), test.expected); | |
859 } | |
860 } | |
861 | |
808 } // namespace base | 862 } // namespace base |
OLD | NEW |