16 #include "my_config.h"
17 #include <gtest/gtest.h>
19 #include "test_utils.h"
23 namespace my_decimal_unittest {
25 using my_testing::chars_2_decimal;
32 virtual void SetUp() { initializer.SetUp(); }
33 virtual void TearDown() { initializer.TearDown(); }
35 THD *thd() {
return initializer.thd(); }
47 EXPECT_EQ(0, ulonglong2decimal(val, &d1));
56 ulonglong val1, val2, val3;
57 EXPECT_EQ(0, decimal2ulonglong(&d1, &val1));
58 EXPECT_EQ(0, decimal2ulonglong(&d2, &val2));
59 EXPECT_EQ(0, decimal2ulonglong(&d3, &val3));
66 EXPECT_EQ(0, ulonglong2decimal(val, &d1));
72 TEST_F(DecimalTest, RoundOverflow)
74 const char arg_str[]=
"999999999";
75 String str(arg_str, &my_charset_bin);
78 string2my_decimal(E_DEC_FATAL_ERROR, &str, &d1));
81 for (
int ix= 0; ix < DECIMAL_MAX_POSSIBLE_PRECISION; ++ix)
85 (ix + str.length() <= DECIMAL_MAX_POSSIBLE_PRECISION) ?
86 E_DEC_OK : E_DEC_TRUNCATED;
87 const bool do_truncate=
true;
89 my_decimal_round(E_DEC_FATAL_ERROR, &d1, ix, do_truncate, &d3))
97 TEST_F(DecimalTest, Swap)
101 EXPECT_EQ(0, ulonglong2decimal(val1, &d1));
102 EXPECT_EQ(0, ulonglong2decimal(val2, &d2));
114 TEST_F(DecimalTest, Multiply)
118 "10000000000000000000" "00000000000000000000" "00000000000000000000"
120 const char arg2[]=
"1.75";
125 EXPECT_EQ(E_DEC_OK, chars_2_decimal(arg1, &d1));
126 EXPECT_EQ(E_DEC_OK, chars_2_decimal(arg2, &d2));
129 set_if_smaller(d1.frac, NOT_FIXED_DEC);
130 set_if_smaller(d2.frac, NOT_FIXED_DEC);
131 EXPECT_EQ(0, my_decimal_mul(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
133 EXPECT_EQ(NOT_FIXED_DEC, d1.frac);
134 EXPECT_EQ(2, d2.frac);
135 EXPECT_EQ(NOT_FIXED_DEC, prod.frac);
137 EXPECT_EQ(0, decimal2string(&prod, buff, &bufsz, 0, 0, 0));
138 EXPECT_STREQ(
"1.9250000000000000000000000000000", buff);
157 int decimal_modulo(uint mask,
177 if ((ret= my_decimal_add(E_DEC_FATAL_ERROR, &res, &k1, &kn)) != E_DEC_OK)
184 if ((ret= my_decimal_sub(E_DEC_FATAL_ERROR,
185 &next_r, &abs_m, &kn) != E_DEC_OK))
206 {
"234" ,
"10",
"4" },
207 {
"234.567" ,
"10.555",
"2.357" },
208 {
"-234.567",
"10.555",
"-2.357" },
209 {
"234.567" ,
"-10.555",
"2.357" },
210 {
"-234.567",
"-10.555",
"-2.357" },
211 {
"999" ,
"0.1",
"0.0" },
212 {
"999" ,
"0.7",
"0.1" },
213 {
"10" ,
"123",
"10" },
218 TEST_F(DecimalTest, Modulo)
226 for (Mod_data *pd= mod_test_input; pd->a; ++pd)
228 int bufsz_x=
sizeof(buff_x);
229 int bufsz_m=
sizeof(buff_m);
231 EXPECT_EQ(0, chars_2_decimal(pd->a, &d1));
232 EXPECT_EQ(0, chars_2_decimal(pd->b, &d2));
233 EXPECT_EQ(0, chars_2_decimal(pd->result, &expected_result));
235 EXPECT_EQ(0, my_decimal_mod(E_DEC_FATAL_ERROR, &mod_result, &d1, &d2));
236 EXPECT_EQ(0, decimal2string(&mod_result, buff_m, &bufsz_m, 0, 0, 0));
240 <<
" expected:" << pd->result
241 <<
" got mod:" << buff_m
244 EXPECT_EQ(0, decimal_modulo(E_DEC_FATAL_ERROR, &xxx_result, &d1, &d2));
245 EXPECT_EQ(0, decimal2string(&xxx_result, buff_x, &bufsz_x, 0, 0, 0));
249 <<
" expected:" << pd->result
250 <<
" got mod:" << buff_m
251 <<
" got xxx:" << buff_x
257 TEST_F(DecimalTest, BinaryConversion)
261 EXPECT_EQ(E_DEC_OK, chars_2_decimal(
"000000000", &d1));
262 int binary_size= my_decimal_get_binary_size(prec, scale);
263 uchar *bin=
new uchar[binary_size];
266 EXPECT_EQ(E_DEC_OK, my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
267 &d1, bin, prec, scale));
268 EXPECT_EQ(E_DEC_OK, binary2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
269 bin, &d2, prec, scale));
270 EXPECT_GT(d2.precision(), 0
U);
275 EXPECT_EQ(E_DEC_OK, my_decimal_mul(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
276 &product, &d2, &d2));
281 EXPECT_EQ(E_DEC_OK, my_decimal_mul(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
282 &neg_prod, &d2, &d3));