| 1 | /* Copyright (c) 2018, 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 <float.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <math.h> |
| 27 | #include <stddef.h> |
| 28 | #include <stdint.h> |
| 29 | #include <limits> |
| 30 | |
| 31 | #include "m_string.h" |
| 32 | #include "unittest/gunit/benchmark.h" |
| 33 | |
| 34 | namespace m_string_unittest { |
| 35 | |
| 36 | std::string HumanReadable(double bytes) { |
| 37 | char buf[32]; |
| 38 | human_readable_num_bytes(buf, sizeof(buf), bytes); |
| 39 | return buf; |
| 40 | } |
| 41 | |
| 42 | TEST(MString, HumanReadableSize) { |
| 43 | EXPECT_EQ("1", HumanReadable(1.0)); |
| 44 | EXPECT_EQ("1024", HumanReadable(1024.0)); |
| 45 | EXPECT_EQ("1K", HumanReadable(1024.1)); |
| 46 | EXPECT_EQ("1K", HumanReadable(1025.0)); |
| 47 | |
| 48 | double data_size = 1025.0 * 1024; |
| 49 | EXPECT_EQ("1M", HumanReadable(data_size)); |
| 50 | data_size *= 1024; |
| 51 | EXPECT_EQ("1G", HumanReadable(data_size)); |
| 52 | data_size *= 1024; |
| 53 | EXPECT_EQ("1T", HumanReadable(data_size)); |
| 54 | data_size *= 1024; |
| 55 | EXPECT_EQ("1P", HumanReadable(data_size)); |
| 56 | data_size *= 1024; |
| 57 | EXPECT_EQ("1E", HumanReadable(data_size)); |
| 58 | data_size *= 1024; |
| 59 | EXPECT_EQ("1Z", HumanReadable(data_size)); |
| 60 | data_size *= 1024; |
| 61 | EXPECT_EQ("1Y", HumanReadable(data_size)); |
| 62 | data_size *= 1024; |
| 63 | EXPECT_EQ("1025Y", HumanReadable(data_size)); |
| 64 | data_size *= 1000; |
| 65 | EXPECT_EQ("1025000Y", HumanReadable(data_size)); |
| 66 | data_size *= 1000; |
| 67 | EXPECT_EQ("1025000000Y", HumanReadable(data_size)); |
| 68 | data_size *= |
| 69 | static_cast<double>(std::numeric_limits<unsigned long long>::max()); |
| 70 | EXPECT_EQ("+INF", HumanReadable(data_size)); |
| 71 | |
| 72 | // Various edge cases. We don't care much which way they round, |
| 73 | // we just want them to not give nonsensical results such as â1024Kâ |
| 74 | // for 1024.001. |
| 75 | EXPECT_EQ("1023", HumanReadable(nextafter(1024.0, -DBL_MAX))); |
| 76 | EXPECT_EQ("1K", HumanReadable(nextafter(1024.0, DBL_MAX))); |
| 77 | |
| 78 | double yotta = pow(1024.0, 8.0); |
| 79 | EXPECT_EQ("9223372036854774784Y", |
| 80 | HumanReadable( |
| 81 | nextafter(static_cast<double>(LLONG_MAX) * yotta, -DBL_MAX))); |
| 82 | EXPECT_EQ("9223372036854775808Y", |
| 83 | HumanReadable(static_cast<double>(LLONG_MAX) * yotta)); |
| 84 | EXPECT_EQ("9223372036854777856Y", |
| 85 | HumanReadable( |
| 86 | nextafter(static_cast<double>(LLONG_MAX) * yotta, DBL_MAX))); |
| 87 | |
| 88 | EXPECT_EQ("18446744073709549568Y", |
| 89 | HumanReadable( |
| 90 | nextafter(static_cast<double>(ULLONG_MAX) * yotta, -DBL_MAX))); |
| 91 | EXPECT_EQ("+INF", HumanReadable(static_cast<double>(ULLONG_MAX) * yotta)); |
| 92 | EXPECT_EQ("+INF", HumanReadable(nextafter( |
| 93 | static_cast<double>(ULLONG_MAX) * yotta, DBL_MAX))); |
| 94 | } |
| 95 | |
| 96 | static void BM_longlong10_to_str(size_t num_iterations) { |
| 97 | StopBenchmarkTiming(); |
| 98 | const int64_t value = 1234567890123456789; |
| 99 | |
| 100 | StartBenchmarkTiming(); |
| 101 | for (size_t i = 0; i < num_iterations; ++i) { |
| 102 | char buffer[std::numeric_limits<int64_t>::digits10 + 2]; |
| 103 | longlong10_to_str(value, buffer, -10); |
| 104 | } |
| 105 | } |
| 106 | BENCHMARK(BM_longlong10_to_str) |
| 107 | |
| 108 | static void BM_longlong2str(size_t num_iterations) { |
| 109 | StopBenchmarkTiming(); |
| 110 | const int64_t value = 1234567890123456789; |
| 111 | |
| 112 | StartBenchmarkTiming(); |
| 113 | for (size_t i = 0; i < num_iterations; ++i) { |
| 114 | char buffer[100]; |
| 115 | longlong2str(value, buffer, -36); |
| 116 | } |
| 117 | } |
| 118 | BENCHMARK(BM_longlong2str) |
| 119 | |
| 120 | } // namespace m_string_unittest |