| 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 "m_ctype.h" |
| 27 | #include "my_sys.h" |
| 28 | #include "sql/sql_const.h" |
| 29 | |
| 30 | namespace double_to_string_to_double_unittest { |
| 31 | |
| 32 | const double test_values[] = {DBL_MAX, DBL_MIN, -DBL_MAX, -DBL_MIN}; |
| 33 | |
| 34 | class GcvtTest : public ::testing::TestWithParam<double> { |
| 35 | protected: |
| 36 | void SetUp() override { test_input = GetParam(); } |
| 37 | double test_input; |
| 38 | }; |
| 39 | |
| 40 | INSTANTIATE_TEST_SUITE_P(Foo, GcvtTest, ::testing::ValuesIn(test_values)); |
| 41 | |
| 42 | CHARSET_INFO *init_collation(const char *name) { |
| 43 | MY_CHARSET_LOADER loader; |
| 44 | return my_collation_get_by_name(&loader, name, MYF(0)); |
| 45 | } |
| 46 | |
| 47 | TEST_P(GcvtTest, Convert) { |
| 48 | // DBL_MIN and -DBL_MAX requires + 1 |
| 49 | // -DBL_MIN requires another + 1 |
| 50 | const int width = MAX_DOUBLE_STR_LENGTH + 2; |
| 51 | |
| 52 | char buff[MAX_DOUBLE_STR_LENGTH * 2] = {}; |
| 53 | bool error{false}; |
| 54 | |
| 55 | char *to = &buff[0]; |
| 56 | size_t len = my_gcvt(test_input, MY_GCVT_ARG_DOUBLE, width, to, &error); |
| 57 | EXPECT_LE(len, width); |
| 58 | EXPECT_FALSE(error); |
| 59 | |
| 60 | int int_error{0}; |
| 61 | const char *str_start = to; |
| 62 | const char *str_end = to + width; |
| 63 | double result = my_strtod(str_start, &str_end, &int_error); |
| 64 | EXPECT_EQ(0, int_error) << "buff[" << buff << "]" << std::endl; |
| 65 | EXPECT_EQ(test_input, result) << "buff[" << buff << "]" << std::endl; |
| 66 | |
| 67 | CHARSET_INFO *cs = init_collation("utf8mb4_0900_as_ci"); |
| 68 | const char *end; |
| 69 | int conv_error; |
| 70 | double nr = my_strntod(cs, str_start, width, &end, &conv_error); |
| 71 | EXPECT_EQ(0, conv_error) << "buff[" << buff << "]" << std::endl; |
| 72 | EXPECT_EQ(test_input, nr) << "buff[" << buff << "]" << std::endl; |
| 73 | } |
| 74 | |
| 75 | } // namespace double_to_string_to_double_unittest |