| 1 | /* Copyright (c) 2012, 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 "my_config.h" |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <limits> |
| 29 | |
| 30 | #include "my_byteorder.h" |
| 31 | #include "my_inttypes.h" |
| 32 | |
| 33 | namespace byteorder_unittest { |
| 34 | |
| 35 | using std::numeric_limits; |
| 36 | |
| 37 | /* |
| 38 | This class is used to instantiate parameterized tests for float and double. |
| 39 | */ |
| 40 | template <typename T> |
| 41 | class FloatingTest { |
| 42 | protected: |
| 43 | T input; |
| 44 | T output; |
| 45 | uchar buf[sizeof(T)]; |
| 46 | }; |
| 47 | |
| 48 | class Float4Test : public FloatingTest<float>, |
| 49 | public ::testing::TestWithParam<float> { |
| 50 | void SetUp() override { |
| 51 | input = GetParam(); |
| 52 | output = numeric_limits<float>::quiet_NaN(); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | INSTANTIATE_TEST_SUITE_P(Foo, Float4Test, |
| 57 | ::testing::Values(numeric_limits<float>::min(), |
| 58 | numeric_limits<float>::max(), |
| 59 | numeric_limits<float>::epsilon(), |
| 60 | -numeric_limits<float>::min(), |
| 61 | -numeric_limits<float>::max(), |
| 62 | -numeric_limits<float>::epsilon(), |
| 63 | -1.0f, 0.0f, 1.0f)); |
| 64 | /* |
| 65 | The actual test case for float: store and get some values. |
| 66 | */ |
| 67 | TEST_P(Float4Test, PutAndGet) { |
| 68 | float4store(buf, input); |
| 69 | output = float4get(buf); |
| 70 | EXPECT_EQ(input, output); |
| 71 | floatstore(buf, input); |
| 72 | output = floatget(buf); |
| 73 | EXPECT_EQ(input, output); |
| 74 | } |
| 75 | |
| 76 | class Float8Test : public FloatingTest<double>, |
| 77 | public ::testing::TestWithParam<double> { |
| 78 | void SetUp() override { |
| 79 | input = GetParam(); |
| 80 | output = numeric_limits<double>::quiet_NaN(); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | INSTANTIATE_TEST_SUITE_P(Foo, Float8Test, |
| 85 | ::testing::Values(numeric_limits<double>::min(), |
| 86 | numeric_limits<double>::max(), |
| 87 | numeric_limits<double>::epsilon(), |
| 88 | -numeric_limits<double>::min(), |
| 89 | -numeric_limits<double>::max(), |
| 90 | -numeric_limits<double>::epsilon(), |
| 91 | -1.0, 0.0, 1.0)); |
| 92 | /* |
| 93 | The actual test case for double: store and get some values. |
| 94 | */ |
| 95 | TEST_P(Float8Test, PutAndGet) { |
| 96 | float8store(buf, input); |
| 97 | output = float8get(buf); |
| 98 | EXPECT_EQ(input, output); |
| 99 | doublestore(buf, input); |
| 100 | output = doubleget(buf); |
| 101 | EXPECT_EQ(input, output); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | A test fixture class, parameterized on type. |
| 106 | Will be instantiated for all IntegralTypes below. |
| 107 | */ |
| 108 | template <typename T> |
| 109 | class IntegralTest : public ::testing::Test { |
| 110 | protected: |
| 111 | typedef std::numeric_limits<T> Limit; |
| 112 | |
| 113 | T input; |
| 114 | T output; |
| 115 | uchar buf[sizeof(T)]; |
| 116 | |
| 117 | typename std::vector<T> values; |
| 118 | |
| 119 | IntegralTest() : input(0), output(0) {} |
| 120 | |
| 121 | void SetUp() override { |
| 122 | values.push_back(Limit::min()); |
| 123 | values.push_back(Limit::min() / T(2)); |
| 124 | values.push_back(T(0)); |
| 125 | values.push_back(T(42)); |
| 126 | values.push_back(Limit::max() / T(2)); |
| 127 | values.push_back(Limit::max()); |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | /* |
| 132 | A class to make our 3, 5 and 6 digit integers look like builtins. |
| 133 | */ |
| 134 | template <int ndigits> |
| 135 | struct sizeNint { |
| 136 | // For numeric_limits. |
| 137 | typedef ulonglong value_type; |
| 138 | |
| 139 | sizeNint() : value(0) {} |
| 140 | explicit sizeNint(ulonglong v) { |
| 141 | switch (ndigits) { |
| 142 | case 3: |
| 143 | value = v & 0xFFFFFFULL; |
| 144 | break; |
| 145 | case 5: |
| 146 | value = v & 0xFFFFFFFFFFULL; |
| 147 | break; |
| 148 | case 6: |
| 149 | value = v & 0xFFFFFFFFFFFFULL; |
| 150 | break; |
| 151 | default: |
| 152 | ADD_FAILURE() << "unxpected number of digits"; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | sizeNint operator/(const sizeNint &that) const { |
| 157 | return sizeNint(this->value / that.value); |
| 158 | } |
| 159 | |
| 160 | bool operator==(const sizeNint &that) const { |
| 161 | return this->value == that.value; |
| 162 | } |
| 163 | |
| 164 | ulonglong value; |
| 165 | }; |
| 166 | |
| 167 | // googletest needs to be able to print arguments to EXPECT_EQ. |
| 168 | template <int ndigits> |
| 169 | std::ostream &operator<<(std::ostream &s, const sizeNint<ndigits> &v) { |
| 170 | return s << v.value; |
| 171 | } |
| 172 | |
| 173 | // Instantiate the PutAndGet test for all these types: |
| 174 | typedef ::testing::Types<short, ushort, sizeNint<3>, sizeNint<5>, sizeNint<6>, |
| 175 | int, unsigned, longlong, ulonglong> |
| 176 | IntegralTypes; |
| 177 | |
| 178 | TYPED_TEST_SUITE(IntegralTest, IntegralTypes); |
| 179 | |
| 180 | /* |
| 181 | Wrap all the __get, __store, __korr macros in functions. |
| 182 | */ |
| 183 | template <typename T> |
| 184 | void put_integral(uchar *, T) { |
| 185 | ADD_FAILURE() << "unknown type in put_integral"; |
| 186 | } |
| 187 | template <typename T> |
| 188 | void get_integral(T &, uchar *) { |
| 189 | ADD_FAILURE() << "unknown type in get_integral"; |
| 190 | } |
| 191 | |
| 192 | template <> |
| 193 | void put_integral(uchar *buf, short val) { |
| 194 | shortstore(buf, val); |
| 195 | } |
| 196 | template <> |
| 197 | void get_integral(short &val, uchar *buf) { |
| 198 | val = shortget(buf); |
| 199 | } |
| 200 | |
| 201 | // Hmm, there's no ushortstore... |
| 202 | template <> |
| 203 | void put_integral(uchar *buf, ushort val) { |
| 204 | shortstore(buf, val); |
| 205 | } |
| 206 | template <> |
| 207 | void get_integral(ushort &val, uchar *buf) { |
| 208 | val = ushortget(buf); |
| 209 | } |
| 210 | |
| 211 | template <> |
| 212 | void put_integral(uchar *buf, int val) { |
| 213 | longstore(buf, val); |
| 214 | } |
| 215 | template <> |
| 216 | void get_integral(int &val, uchar *buf) { |
| 217 | val = longget(buf); |
| 218 | } |
| 219 | |
| 220 | // Hmm, there's no ulongstore... |
| 221 | template <> |
| 222 | void put_integral(uchar *buf, unsigned val) { |
| 223 | longstore(buf, val); |
| 224 | } |
| 225 | template <> |
| 226 | void get_integral(unsigned &val, uchar *buf) { |
| 227 | val = ulongget(buf); |
| 228 | } |
| 229 | |
| 230 | template <> |
| 231 | void put_integral(uchar *buf, longlong val) { |
| 232 | longlongstore(buf, val); |
| 233 | } |
| 234 | template <> |
| 235 | void get_integral(longlong &val, uchar *buf) { |
| 236 | val = longlongget(buf); |
| 237 | } |
| 238 | |
| 239 | // Reading ulonglong is different from all the above .... |
| 240 | template <> |
| 241 | void put_integral(uchar *buf, ulonglong val) { |
| 242 | int8store(buf, val); |
| 243 | } |
| 244 | template <> |
| 245 | void get_integral(ulonglong &val, uchar *buf) { |
| 246 | val = uint8korr(buf); |
| 247 | } |
| 248 | |
| 249 | template <> |
| 250 | void put_integral(uchar *buf, sizeNint<3> val) { |
| 251 | int3store(buf, static_cast<uint>(val.value)); |
| 252 | } |
| 253 | template <> |
| 254 | void get_integral(sizeNint<3> &val, uchar *buf) { |
| 255 | val.value = uint3korr(buf); |
| 256 | } |
| 257 | |
| 258 | template <> |
| 259 | void put_integral(uchar *buf, sizeNint<5> val) { |
| 260 | int5store(buf, val.value); |
| 261 | } |
| 262 | template <> |
| 263 | void get_integral(sizeNint<5> &val, uchar *buf) { |
| 264 | val.value = uint5korr(buf); |
| 265 | } |
| 266 | |
| 267 | template <> |
| 268 | void put_integral(uchar *buf, sizeNint<6> val) { |
| 269 | int6store(buf, val.value); |
| 270 | } |
| 271 | template <> |
| 272 | void get_integral(sizeNint<6> &val, uchar *buf) { |
| 273 | val.value = uint6korr(buf); |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | This is the actual test which will be instantiated for all IntegralTypes. |
| 278 | */ |
| 279 | TYPED_TEST(IntegralTest, PutAndGet) { |
| 280 | for (size_t ix = 0; ix < this->values.size(); ++ix) { |
| 281 | this->input = this->values[ix]; |
| 282 | put_integral(this->buf, this->input); |
| 283 | get_integral(this->output, this->buf); |
| 284 | // Visual studio rejects: EXPECT_EQ(this->input, this->output); |
| 285 | TypeParam myinput = this->input; |
| 286 | TypeParam myoutput = this->output; |
| 287 | EXPECT_EQ(myinput, myoutput); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | } // namespace byteorder_unittest |