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