| 1 | /* Copyright (c) 2021, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include <string> |
| 27 | |
| 28 | #include "m_string.h" |
| 29 | |
| 30 | namespace string_to_double_unittest { |
| 31 | |
| 32 | TEST(StringToDoubleTest, Balloc) { |
| 33 | // This is the original mtr test case: |
| 34 | // concat(rpad(-75.51891750, 11283536, 0), "767521D9"); |
| 35 | std::string dec = "-75.5189175"; |
| 36 | std::string zeros(11283536 - dec.length(), '0'); |
| 37 | std::string hexdigs = "767521D9"; |
| 38 | |
| 39 | std::string str = dec + zeros + hexdigs; |
| 40 | const char *str_start = str.c_str(); |
| 41 | const char *str_end = str_start + str.length(); |
| 42 | int error = 0; |
| 43 | double result = my_strtod(str_start, &str_end, &error); |
| 44 | EXPECT_DOUBLE_EQ(result, -75.5189175); |
| 45 | EXPECT_EQ(error, 0); |
| 46 | EXPECT_EQ(*str_end, 'D'); |
| 47 | |
| 48 | str = dec + zeros + "e100"; |
| 49 | str_start = str.c_str(); |
| 50 | str_end = str_start + str.length(); |
| 51 | result = my_strtod(str_start, &str_end, &error); |
| 52 | EXPECT_DOUBLE_EQ(result, -7.55189175e+101); |
| 53 | } |
| 54 | |
| 55 | TEST(StringToDoubleTest, ManyZeros) { |
| 56 | std::string zeros(DBL_MAX_10_EXP, '0'); |
| 57 | std::string str = "0." + zeros + "12345"; |
| 58 | const char *str_start = str.c_str(); |
| 59 | const char *str_end = str_start + str.length(); |
| 60 | int error = 0; |
| 61 | double result = my_strtod(str_start, &str_end, &error); |
| 62 | EXPECT_EQ(result, 1.2345e-309); |
| 63 | |
| 64 | str = "0." + zeros + zeros + "12345"; |
| 65 | str_start = str.c_str(); |
| 66 | str_end = str_start + str.length(); |
| 67 | result = my_strtod(str_start, &str_end, &error); |
| 68 | EXPECT_DOUBLE_EQ(result, 0.0); |
| 69 | } |
| 70 | |
| 71 | TEST(StringToDoubleTest, ZerosAndOnes) { |
| 72 | std::string zeros(DBL_MAX_10_EXP, '0'); |
| 73 | std::string str = "0."; |
| 74 | for (int i = 0; i < 20; ++i) { |
| 75 | str.append(zeros); |
| 76 | str.append("1"); |
| 77 | } |
| 78 | const char *str_start = str.c_str(); |
| 79 | const char *str_end = str_start + str.length(); |
| 80 | int error = 0; |
| 81 | double result = my_strtod(str_start, &str_end, &error); |
| 82 | EXPECT_EQ(result, 1.0e-309); |
| 83 | } |
| 84 | |
| 85 | } // namespace string_to_double_unittest |