Debian Codegraph @

unittest/gunit/my_decimal-t.cc

raw symbols SCIP
1 /* Copyright (c) 2011, 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 <stddef.h>
28 #include <sys/types.h>
29
30 #include <algorithm>
31
32 #include "my_dbug.h"
33 #include "my_inttypes.h"
34 #include "my_macros.h"
35 #include "sql/my_decimal.h"
36 #include "unittest/gunit/test_utils.h"
37
38 namespace my_decimal_unittest {
39
40 using my_testing::chars_2_decimal;
41 using my_testing::Mock_error_handler;
42 using my_testing::Server_initializer;
43
44 class DecimalTest : public ::testing::Test {
45 protected:
46 void SetUp() override { initializer.SetUp(); }
47 void TearDown() override { initializer.TearDown(); }
48
49 THD *thd() { return initializer.thd(); }
50
51 Server_initializer initializer;
52
53 my_decimal d1;
54 my_decimal d2;
55 };
56
57 TEST_F(DecimalTest, CopyAndCompare) {
58 ulonglong val = 42;
59 EXPECT_EQ(0, ulonglong2decimal(val, &d1));
60
61 d2 = d1; // operator=()
62 my_decimal d3(d1); // Copy constructor.
63
64 EXPECT_EQ(0, my_decimal_cmp(&d1, &d2));
65 EXPECT_EQ(0, my_decimal_cmp(&d2, &d3));
66 EXPECT_EQ(0, my_decimal_cmp(&d3, &d1));
67
68 ulonglong val1, val2, val3;
69 EXPECT_EQ(0, decimal2ulonglong(&d1, &val1));
70 EXPECT_EQ(0, decimal2ulonglong(&d2, &val2));
71 EXPECT_EQ(0, decimal2ulonglong(&d3, &val3));
72 EXPECT_EQ(val, val1);
73 EXPECT_EQ(val, val2);
74 EXPECT_EQ(val, val3);
75
76 // The CTOR/operator=() generated by the compiler would fail here:
77 val = 45;
78 EXPECT_EQ(0, ulonglong2decimal(val, &d1));
79 EXPECT_EQ(1, my_decimal_cmp(&d1, &d2));
80 EXPECT_EQ(1, my_decimal_cmp(&d1, &d3));
81 }
82
83 TEST_F(DecimalTest, RoundOverflow) {
84 const char arg_str[] = "999999999";
85 String str(arg_str, &my_charset_bin);
86
87 EXPECT_EQ(E_DEC_OK, str2my_decimal(E_DEC_FATAL_ERROR, str.ptr(), str.length(),
88 str.charset(), &d1));
89 d1.sanity_check();
90
91 for (int ix = 0; ix < DECIMAL_MAX_POSSIBLE_PRECISION; ++ix) {
92 my_decimal d3;
93 const int expect = (ix + str.length() <= DECIMAL_MAX_POSSIBLE_PRECISION)
94 ? E_DEC_OK
95 : E_DEC_TRUNCATED;
96 const bool do_truncate = true;
97 EXPECT_EQ(expect,
98 my_decimal_round(E_DEC_FATAL_ERROR, &d1, ix, do_truncate, &d3))
99 << "ix:" << ix;
100 d3.sanity_check();
101 EXPECT_EQ(0, my_decimal_cmp(&d1, &d3));
102 }
103 }
104
105 TEST_F(DecimalTest, Swap) {
106 ulonglong val1 = 1;
107 ulonglong val2 = 2;
108 EXPECT_EQ(0, ulonglong2decimal(val1, &d1));
109 EXPECT_EQ(0, ulonglong2decimal(val2, &d2));
110 my_decimal d1copy(d1);
111 my_decimal d2copy(d2);
112 EXPECT_EQ(0, my_decimal_cmp(&d1, &d1copy));
113 EXPECT_EQ(0, my_decimal_cmp(&d2, &d2copy));
114 d1.swap(d2);
115 EXPECT_EQ(0, my_decimal_cmp(&d2, &d1copy));
116 EXPECT_EQ(0, my_decimal_cmp(&d1, &d2copy));
117 }
118
119 TEST_F(DecimalTest, Multiply) {
120 const char arg1[] =
121 "000000001."
122 "10000000000000000000"
123 "00000000000000000000"
124 "00000000000000000000"
125 "000000000000";
126 const char arg2[] = "1.75";
127 char buff[DECIMAL_MAX_STR_LENGTH];
128 int bufsz;
129 my_decimal prod;
130
131 EXPECT_EQ(E_DEC_OK, chars_2_decimal(arg1, &d1));
132 EXPECT_EQ(E_DEC_OK, chars_2_decimal(arg2, &d2));
133
134 // Limit the precision, otherwise "1.75" will be truncated to "1."
135 d1.frac = std::min(d1.frac, DECIMAL_NOT_SPECIFIED);
136 d2.frac = std::min(d2.frac, DECIMAL_NOT_SPECIFIED);
137 EXPECT_EQ(
138 0, my_decimal_mul(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, &prod, &d1, &d2));
139 EXPECT_EQ(DECIMAL_NOT_SPECIFIED, d1.frac);
140 EXPECT_EQ(2, d2.frac);
141 EXPECT_EQ(DECIMAL_NOT_SPECIFIED, prod.frac);
142 bufsz = sizeof(buff);
143 EXPECT_EQ(0, decimal2string(&prod, buff, &bufsz));
144 EXPECT_STREQ("1.9250000000000000000000000000000", buff);
145 }
146
147 /*
148 This is a simple iterative implementation based on addition and subtraction,
149 for verifying the result of decimal_mod().
150
151 decimal_mod() says:
152 DESCRIPTION
153 the modulus R in R = M mod N
154 is defined as
155
156 0 <= |R| < |M|
157 sign R == sign M
158 R = M - k*N, where k is integer
159
160 thus, there's no requirement for M or N to be integers
161 */
162 static int decimal_modulo(my_decimal *res_arg, const my_decimal *m,
163 const my_decimal *n) {
164 my_decimal abs_m(*m);
165 my_decimal abs_n(*n);
166 abs_m.sign(false);
167 abs_n.sign(false);
168
169 my_decimal r;
170 my_decimal k1(abs_n);
171 my_decimal kn(decimal_zero);
172 my_decimal next_r(abs_m);
173 int ret;
174 do {
175 r = next_r;
176
177 my_decimal res;
178 if ((ret = my_decimal_add(E_DEC_FATAL_ERROR, &res, &k1, &kn)) != E_DEC_OK) {
179 ADD_FAILURE();
180 return ret;
181 }
182 kn = res;
183
184 if ((ret = my_decimal_sub(E_DEC_FATAL_ERROR, &next_r, &abs_m, &kn) !=
185 E_DEC_OK)) {
186 ADD_FAILURE();
187 return ret;
188 }
189 } while (my_decimal_cmp(&next_r, &decimal_zero) >= 0);
190 r.sign(m->sign());
191 *res_arg = r;
192 return 0;
193 }
194
195 struct Mod_data {
196 const char *a;
197 const char *b;
198 const char *result;
199 };
200
201 Mod_data mod_test_input[] = {{"234", "10", "4"},
202 {"234.567", "10.555", "2.357"},
203 {"-234.567", "10.555", "-2.357"},
204 {"234.567", "-10.555", "2.357"},
205 {"-234.567", "-10.555", "-2.357"},
206 {"999", "0.1", "0.0"},
207 {"999", "0.7", "0.1"},
208 {"10", "123", "10"},
209 {nullptr, nullptr, nullptr}};
210
211 TEST_F(DecimalTest, Modulo) {
212 my_decimal expected_result;
213 my_decimal xxx_result;
214 my_decimal mod_result;
215 char buff_x[DECIMAL_MAX_STR_LENGTH];
216 char buff_m[DECIMAL_MAX_STR_LENGTH];
217
218 for (Mod_data *pd = mod_test_input; pd->a; ++pd) {
219 int bufsz_x = sizeof(buff_x);
220 int bufsz_m = sizeof(buff_m);
221
222 EXPECT_EQ(0, chars_2_decimal(pd->a, &d1));
223 EXPECT_EQ(0, chars_2_decimal(pd->b, &d2));
224 EXPECT_EQ(0, chars_2_decimal(pd->result, &expected_result));
225
226 EXPECT_EQ(0, my_decimal_mod(E_DEC_FATAL_ERROR, &mod_result, &d1, &d2));
227 EXPECT_EQ(0, decimal2string(&mod_result, buff_m, &bufsz_m));
228 EXPECT_EQ(0, my_decimal_cmp(&expected_result, &mod_result))
229 << " a:" << pd->a << " b:" << pd->b << " expected:" << pd->result
230 << " got mod:" << buff_m;
231
232 EXPECT_EQ(0, decimal_modulo(&xxx_result, &d1, &d2));
233 EXPECT_EQ(0, decimal2string(&xxx_result, buff_x, &bufsz_x));
234 EXPECT_EQ(0, my_decimal_cmp(&expected_result, &xxx_result))
235 << " a:" << pd->a << " b:" << pd->b << " expected:" << pd->result
236 << " got mod:" << buff_m << " got xxx:" << buff_x;
237 }
238 }
239
240 // Verifies that decimal_mul() does not return negative zero.
241 TEST_F(DecimalTest, NegativeZeroMultiply) {
242 EXPECT_EQ(E_DEC_OK, chars_2_decimal("0.0", &d1));
243 EXPECT_EQ(E_DEC_OK, chars_2_decimal("0.0", &d2));
244 EXPECT_EQ(0, my_decimal_cmp(&d1, &decimal_zero));
245 EXPECT_EQ(0, my_decimal_cmp(&d2, &decimal_zero));
246 d1.sign(true);
247 my_decimal product;
248 EXPECT_EQ(E_DEC_OK, decimal_mul(&d1, &d2, &product));
249 EXPECT_FALSE(product.sign());
250 EXPECT_EQ(0, my_decimal_cmp(&product, &decimal_zero));
251 }
252
253 // Verifies that decimal_add() *does* return negative zero.
254 TEST_F(DecimalTest, NegativeZeroAdd) {
255 EXPECT_EQ(E_DEC_OK, chars_2_decimal("0.0", &d1));
256 EXPECT_EQ(E_DEC_OK, chars_2_decimal("0.0", &d2));
257 EXPECT_EQ(0, my_decimal_cmp(&d1, &decimal_zero));
258 EXPECT_EQ(0, my_decimal_cmp(&d2, &decimal_zero));
259 d1.sign(true);
260 d2.sign(true);
261 my_decimal sum;
262 EXPECT_EQ(E_DEC_OK, decimal_add(&d1, &d2, &sum));
263 EXPECT_TRUE(sum.sign());
264 // This one will assert
265 // EXPECT_EQ(0, my_decimal_cmp(&sum, &decimal_zero));
266 }
267
268 TEST_F(DecimalTest, BinaryConversion) {
269 const int prec = 60;
270 const int scale = 0;
271 EXPECT_EQ(E_DEC_OK, chars_2_decimal("000000000", &d1));
272 int binary_size = my_decimal_get_binary_size(prec, scale);
273 uchar *bin = new uchar[binary_size];
274
275 // Convert to binary, and back.
276 EXPECT_EQ(E_DEC_OK, my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
277 &d1, bin, prec, scale));
278 EXPECT_EQ(E_DEC_OK, binary2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
279 bin, &d2, prec, scale));
280 EXPECT_GT(d2.precision(), 0U);
281 EXPECT_EQ(0, my_decimal_cmp(&d1, &d2));
282
283 // 0.0 * 0.0
284 my_decimal product;
285 EXPECT_EQ(E_DEC_OK, my_decimal_mul(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
286 &product, &d2, &d2));
287 // 0.0 * (-0.0)
288 my_decimal neg_prod;
289 my_decimal d3(d2);
290 d3.sign(true);
291 EXPECT_EQ(E_DEC_OK, my_decimal_mul(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
292 &neg_prod, &d2, &d3));
293 delete[] bin;
294 }
295
296 } // namespace my_decimal_unittest