| 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 | /* |
| 25 | NOTE: This is a more-or-less direct port of the main() program |
| 26 | in strings/decimal.c to a Google Test. |
| 27 | */ |
| 28 | |
| 29 | #include "my_config.h" |
| 30 | |
| 31 | #include <gtest/gtest.h> |
| 32 | #include <math.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include "decimal.h" |
| 36 | #include "m_string.h" |
| 37 | #include "my_inttypes.h" |
| 38 | #include "my_macros.h" |
| 39 | #include "sql/my_decimal.h" |
| 40 | #include "unittest/gunit/benchmark.h" |
| 41 | |
| 42 | namespace decimal_unittest { |
| 43 | |
| 44 | #define DIG_PER_DEC1 9 |
| 45 | #define DIG_BASE 1000000000 |
| 46 | #define ROUND_UP(X) (((X) + DIG_PER_DEC1 - 1) / DIG_PER_DEC1) |
| 47 | typedef decimal_digit_t dec1; |
| 48 | |
| 49 | int full = 0; |
| 50 | decimal_t a, b, c; |
| 51 | decimal_digit_t buf1[50], buf2[50], buf3[50]; |
| 52 | |
| 53 | void dump_decimal(decimal_t *d) { |
| 54 | int i; |
| 55 | printf("/* intg=%d, frac=%d, sign=%d, buf[]={", d->intg, d->frac, d->sign); |
| 56 | for (i = 0; i < ROUND_UP(d->frac) + ROUND_UP(d->intg) - 1; i++) |
| 57 | printf("%09d, ", d->buf[i]); |
| 58 | printf("%09d} */ ", d->buf[i]); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | The purpose of all these define wrappers is to get a "call stack" |
| 63 | whenever some EXPECT_XX generates a failure. A sample error message: |
| 64 | |
| 65 | # .../unittest/gunit/decimal-t.cc:134: FailureValue of: s |
| 66 | # Actual: "0" |
| 67 | # Expected: orig |
| 68 | # Which is: "1000000000" |
| 69 | # arguments were: '999999999', -9, HALF_UP |
| 70 | # Google Test trace: |
| 71 | # .../unittest/gunit/decimal-t.cc:387: |
| 72 | # .../unittest/gunit/decimal-t.cc:686: |
| 73 | */ |
| 74 | |
| 75 | #define check_result_code(p1, p2) \ |
| 76 | { \ |
| 77 | SCOPED_TRACE(""); \ |
| 78 | do_check_result_code(p1, p2); \ |
| 79 | } |
| 80 | |
| 81 | #define print_decimal(p1, p2, p3, p4, p5) \ |
| 82 | { \ |
| 83 | SCOPED_TRACE(""); \ |
| 84 | do_print_decimal(p1, p2, p3, p4, p5); \ |
| 85 | } |
| 86 | |
| 87 | #define test_s2d(p1, p2, p3) \ |
| 88 | { \ |
| 89 | SCOPED_TRACE(""); \ |
| 90 | do_test_s2d(p1, p2, p3); \ |
| 91 | } |
| 92 | |
| 93 | #define test_d2f(p1, p2) \ |
| 94 | { \ |
| 95 | SCOPED_TRACE(""); \ |
| 96 | do_test_d2f(p1, p2); \ |
| 97 | } |
| 98 | |
| 99 | #define test_d2b2d(p1, p2, p3, p4, p5) \ |
| 100 | { \ |
| 101 | SCOPED_TRACE(""); \ |
| 102 | do_test_d2b2d(p1, p2, p3, p4, p5); \ |
| 103 | } |
| 104 | |
| 105 | #define test_f2d(p1, p2) \ |
| 106 | { \ |
| 107 | SCOPED_TRACE(""); \ |
| 108 | do_test_f2d(p1, p2); \ |
| 109 | } |
| 110 | |
| 111 | #define test_ull2d(p1, p2, p3) \ |
| 112 | { \ |
| 113 | SCOPED_TRACE(""); \ |
| 114 | do_test_ull2d(p1, p2, p3); \ |
| 115 | } |
| 116 | |
| 117 | #define test_ll2d(p1, p2, p3) \ |
| 118 | { \ |
| 119 | SCOPED_TRACE(""); \ |
| 120 | do_test_ll2d(p1, p2, p3); \ |
| 121 | } |
| 122 | |
| 123 | #define test_d2ull(p1, p2, p3) \ |
| 124 | { \ |
| 125 | SCOPED_TRACE(""); \ |
| 126 | do_test_d2ull(p1, p2, p3); \ |
| 127 | } |
| 128 | |
| 129 | #define test_d2ll(p1, p2, p3) \ |
| 130 | { \ |
| 131 | SCOPED_TRACE(""); \ |
| 132 | do_test_d2ll(p1, p2, p3); \ |
| 133 | } |
| 134 | |
| 135 | #define test_da(p1, p2, p3, p4) \ |
| 136 | { \ |
| 137 | SCOPED_TRACE(""); \ |
| 138 | do_test_da(p1, p2, p3, p4); \ |
| 139 | } |
| 140 | |
| 141 | #define test_ds(p1, p2, p3, p4) \ |
| 142 | { \ |
| 143 | SCOPED_TRACE(""); \ |
| 144 | do_test_ds(p1, p2, p3, p4); \ |
| 145 | } |
| 146 | |
| 147 | #define test_dc(p1, p2, p3) \ |
| 148 | { \ |
| 149 | SCOPED_TRACE(""); \ |
| 150 | do_test_dc(p1, p2, p3); \ |
| 151 | } |
| 152 | |
| 153 | #define test_dm(p1, p2, p3, p4) \ |
| 154 | { \ |
| 155 | SCOPED_TRACE(""); \ |
| 156 | do_test_dm(p1, p2, p3, p4); \ |
| 157 | } |
| 158 | |
| 159 | #define test_dv(p1, p2, p3, p4) \ |
| 160 | { \ |
| 161 | SCOPED_TRACE(""); \ |
| 162 | do_test_dv(p1, p2, p3, p4); \ |
| 163 | } |
| 164 | |
| 165 | #define test_md(p1, p2, p3, p4) \ |
| 166 | { \ |
| 167 | SCOPED_TRACE(""); \ |
| 168 | do_test_md(p1, p2, p3, p4); \ |
| 169 | } |
| 170 | |
| 171 | #define test_ro(p1, p2, p3, p4, p5) \ |
| 172 | { \ |
| 173 | SCOPED_TRACE(""); \ |
| 174 | do_test_ro(p1, p2, p3, p4, p5); \ |
| 175 | } |
| 176 | |
| 177 | #define test_format(p1, p2, p3, p4, p5) \ |
| 178 | { \ |
| 179 | SCOPED_TRACE(""); \ |
| 180 | do_test_format(p1, p2, p3, p4, p5); \ |
| 181 | } |
| 182 | |
| 183 | #define test_mx(p1, p2, p3) \ |
| 184 | { \ |
| 185 | SCOPED_TRACE(""); \ |
| 186 | do_test_mx(p1, p2, p3); \ |
| 187 | } |
| 188 | |
| 189 | #define test_pr(p1, p2, p3, p4, p5) \ |
| 190 | { \ |
| 191 | SCOPED_TRACE(""); \ |
| 192 | do_test_pr(p1, p2, p3, p4, p5); \ |
| 193 | } |
| 194 | |
| 195 | #define test_widen_fraction(p1, p2, p3) \ |
| 196 | { \ |
| 197 | SCOPED_TRACE(""); \ |
| 198 | do_test_widen_fraction(p1, p2, p3); \ |
| 199 | } |
| 200 | |
| 201 | #define test_sh(p1, p2, p3, p4) \ |
| 202 | { \ |
| 203 | SCOPED_TRACE(""); \ |
| 204 | do_test_sh(p1, p2, p3, p4); \ |
| 205 | } |
| 206 | |
| 207 | #define test_fr(p1, p2) \ |
| 208 | { \ |
| 209 | SCOPED_TRACE(""); \ |
| 210 | do_test_fr(p1, p2); \ |
| 211 | } |
| 212 | |
| 213 | void do_check_result_code(int actual, int want) { EXPECT_EQ(want, actual); } |
| 214 | |
| 215 | void do_print_decimal(decimal_t *d, const char *orig, int actual, int want, |
| 216 | const char *msg) { |
| 217 | char s[100]; |
| 218 | int slen = sizeof(s); |
| 219 | |
| 220 | if (full) dump_decimal(d); |
| 221 | decimal2string(d, s, &slen); |
| 222 | check_result_code(actual, want); |
| 223 | if (orig) { |
| 224 | EXPECT_STREQ(orig, s) << " arguments were: " << msg; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void test_d2s() { |
| 229 | char s[100]; |
| 230 | int slen, res; |
| 231 | |
| 232 | /***********************************/ |
| 233 | printf("==== decimal2string ====\n"); |
| 234 | a.buf[0] = 12345; |
| 235 | a.intg = 5; |
| 236 | a.frac = 0; |
| 237 | a.sign = false; |
| 238 | slen = sizeof(s); |
| 239 | res = decimal2string(&a, s, &slen); |
| 240 | dump_decimal(&a); |
| 241 | printf(" --> res=%d str='%s' len=%d\n", res, s, slen); |
| 242 | |
| 243 | a.buf[1] = 987000000; |
| 244 | a.frac = 3; |
| 245 | slen = sizeof(s); |
| 246 | res = decimal2string(&a, s, &slen); |
| 247 | dump_decimal(&a); |
| 248 | printf(" --> res=%d str='%s' len=%d\n", res, s, slen); |
| 249 | |
| 250 | a.sign = true; |
| 251 | slen = sizeof(s); |
| 252 | res = decimal2string(&a, s, &slen); |
| 253 | dump_decimal(&a); |
| 254 | printf(" --> res=%d str='%s' len=%d\n", res, s, slen); |
| 255 | |
| 256 | slen = 8; |
| 257 | res = decimal2string(&a, s, &slen); |
| 258 | dump_decimal(&a); |
| 259 | printf(" --> res=%d str='%s' len=%d\n", res, s, slen); |
| 260 | |
| 261 | slen = 5; |
| 262 | res = decimal2string(&a, s, &slen); |
| 263 | dump_decimal(&a); |
| 264 | printf(" --> res=%d str='%s' len=%d\n", res, s, slen); |
| 265 | |
| 266 | a.buf[0] = 987000000; |
| 267 | a.frac = 3; |
| 268 | a.intg = 0; |
| 269 | slen = sizeof(s); |
| 270 | res = decimal2string(&a, s, &slen); |
| 271 | dump_decimal(&a); |
| 272 | printf(" --> res=%d str='%s' len=%d\n", res, s, slen); |
| 273 | } |
| 274 | |
| 275 | void do_test_s2d(const char *s, const char *orig, int ex) { |
| 276 | char s1[100]; |
| 277 | int res; |
| 278 | sprintf(s1, "'%s'", s); |
| 279 | const char *end = strend(s); |
| 280 | res = string2decimal(s, &a, &end); |
| 281 | print_decimal(&a, orig, res, ex, s1); |
| 282 | } |
| 283 | |
| 284 | void do_test_d2f(const char *s, int ex) { |
| 285 | char s1[100]; |
| 286 | double x; |
| 287 | int res; |
| 288 | |
| 289 | sprintf(s1, "'%s'", s); |
| 290 | const char *end = strend(s); |
| 291 | string2decimal(s, &a, &end); |
| 292 | res = decimal2double(&a, &x); |
| 293 | if (full) dump_decimal(&a); |
| 294 | check_result_code(res, ex); |
| 295 | } |
| 296 | |
| 297 | void do_test_d2b2d(const char *str, int p, int s, const char *orig, int ex) { |
| 298 | char s1[100]; |
| 299 | char s2[100 * 2]; |
| 300 | uchar buf[100]; |
| 301 | int res, i, size = decimal_bin_size(p, s); |
| 302 | |
| 303 | sprintf(s1, "'%s'", str); |
| 304 | const char *end = strend(str); |
| 305 | string2decimal(str, &a, &end); |
| 306 | res = decimal2bin(&a, buf, p, s); |
| 307 | sprintf(s2, "%-31s {%2d, %2d} => res=%d size=%-2d ", s1, p, s, res, size); |
| 308 | if (full) { |
| 309 | printf("0x"); |
| 310 | for (i = 0; i < size; i++) printf("%02x", ((uchar *)buf)[i]); |
| 311 | } |
| 312 | res = bin2decimal(buf, &a, p, s); |
| 313 | print_decimal(&a, orig, res, ex, s2); |
| 314 | } |
| 315 | |
| 316 | void do_test_f2d(double from, int ex) { |
| 317 | int res; |
| 318 | char s1[100]; |
| 319 | |
| 320 | res = double2decimal(from, &a); |
| 321 | sprintf(s1, "%-40.*f => res=%d ", DBL_DIG - 2, from, res); |
| 322 | print_decimal(&a, nullptr, res, ex, s1); |
| 323 | } |
| 324 | |
| 325 | void do_test_ull2d(ulonglong from, const char *orig, int ex) { |
| 326 | char s[100]; |
| 327 | char s1[100 * 2]; |
| 328 | int res; |
| 329 | |
| 330 | res = ulonglong2decimal(from, &a); |
| 331 | longlong10_to_str(from, s, 10); |
| 332 | sprintf(s1, "%-40s => res=%d ", s, res); |
| 333 | print_decimal(&a, orig, res, ex, s1); |
| 334 | } |
| 335 | |
| 336 | void do_test_ll2d(longlong from, const char *orig, int ex) { |
| 337 | char s[100]; |
| 338 | char s1[100 * 2]; |
| 339 | int res; |
| 340 | |
| 341 | res = longlong2decimal(from, &a); |
| 342 | longlong10_to_str(from, s, -10); |
| 343 | sprintf(s1, "%-40s => res=%d ", s, res); |
| 344 | print_decimal(&a, orig, res, ex, s1); |
| 345 | } |
| 346 | |
| 347 | void do_test_d2ull(const char *s, const char *orig, int ex) { |
| 348 | char s1[100]; |
| 349 | char s2[100 * 2]; |
| 350 | ulonglong x; |
| 351 | int res; |
| 352 | |
| 353 | const char *end = strend(s); |
| 354 | string2decimal(s, &a, &end); |
| 355 | res = decimal2ulonglong(&a, &x); |
| 356 | if (full) dump_decimal(&a); |
| 357 | longlong10_to_str(x, s1, 10); |
| 358 | sprintf(s2, "%-40s => res=%d %s\n", s, res, s1); |
| 359 | check_result_code(res, ex); |
| 360 | if (orig) { |
| 361 | EXPECT_STREQ(orig, s1) << " arguments were: " << s2; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | void do_test_d2ll(const char *s, const char *orig, int ex) { |
| 366 | char s1[100]; |
| 367 | char s2[100 * 2]; |
| 368 | longlong x; |
| 369 | int res; |
| 370 | |
| 371 | const char *end = strend(s); |
| 372 | string2decimal(s, &a, &end); |
| 373 | res = decimal2longlong(&a, &x); |
| 374 | if (full) dump_decimal(&a); |
| 375 | longlong10_to_str(x, s1, -10); |
| 376 | sprintf(s2, "%-40s => res=%d %s\n", s, res, s1); |
| 377 | check_result_code(res, ex); |
| 378 | if (orig) { |
| 379 | EXPECT_STREQ(orig, s1) << " arguments were: " << s2; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void do_test_da(const char *s1, const char *s2, const char *orig, int ex) { |
| 384 | char s[100]; |
| 385 | int res; |
| 386 | sprintf(s, "'%s' + '%s'", s1, s2); |
| 387 | const char *end = strend(s1); |
| 388 | string2decimal(s1, &a, &end); |
| 389 | end = strend(s2); |
| 390 | string2decimal(s2, &b, &end); |
| 391 | res = decimal_add(&a, &b, &c); |
| 392 | print_decimal(&c, orig, res, ex, s); |
| 393 | } |
| 394 | |
| 395 | void do_test_ds(const char *s1, const char *s2, const char *orig, int ex) { |
| 396 | char s[100]; |
| 397 | int res; |
| 398 | sprintf(s, "'%s' - '%s'", s1, s2); |
| 399 | const char *end = strend(s1); |
| 400 | string2decimal(s1, &a, &end); |
| 401 | end = strend(s2); |
| 402 | string2decimal(s2, &b, &end); |
| 403 | res = decimal_sub(&a, &b, &c); |
| 404 | print_decimal(&c, orig, res, ex, s); |
| 405 | } |
| 406 | |
| 407 | void do_test_dc(const char *s1, const char *s2, int orig) { |
| 408 | char s[100]; |
| 409 | int res; |
| 410 | sprintf(s, "'%s' <=> '%s'", s1, s2); |
| 411 | const char *end = strend(s1); |
| 412 | string2decimal(s1, &a, &end); |
| 413 | end = strend(s2); |
| 414 | string2decimal(s2, &b, &end); |
| 415 | res = decimal_cmp(&a, &b); |
| 416 | EXPECT_EQ(orig, res) << " arguments were: " << s; |
| 417 | } |
| 418 | |
| 419 | void do_test_dm(const char *s1, const char *s2, const char *orig, int ex) { |
| 420 | char s[100]; |
| 421 | int res; |
| 422 | sprintf(s, "'%s' * '%s'", s1, s2); |
| 423 | const char *end = strend(s1); |
| 424 | string2decimal(s1, &a, &end); |
| 425 | end = strend(s2); |
| 426 | string2decimal(s2, &b, &end); |
| 427 | res = decimal_mul(&a, &b, &c); |
| 428 | print_decimal(&c, orig, res, ex, s); |
| 429 | } |
| 430 | |
| 431 | void do_test_dv(const char *s1, const char *s2, const char *orig, int ex) { |
| 432 | char s[100]; |
| 433 | int res; |
| 434 | sprintf(s, "'%s' / '%s'", s1, s2); |
| 435 | const char *end = strend(s1); |
| 436 | string2decimal(s1, &a, &end); |
| 437 | end = strend(s2); |
| 438 | string2decimal(s2, &b, &end); |
| 439 | res = decimal_div(&a, &b, &c, 5); |
| 440 | check_result_code(res, ex); |
| 441 | if (res != E_DEC_DIV_ZERO) print_decimal(&c, orig, res, ex, s); |
| 442 | } |
| 443 | |
| 444 | void do_test_md(const char *s1, const char *s2, const char *orig, int ex) { |
| 445 | char s[100]; |
| 446 | int res; |
| 447 | sprintf(s, "'%s' %% '%s'", s1, s2); |
| 448 | const char *end = strend(s1); |
| 449 | string2decimal(s1, &a, &end); |
| 450 | end = strend(s2); |
| 451 | string2decimal(s2, &b, &end); |
| 452 | res = decimal_mod(&a, &b, &c); |
| 453 | check_result_code(res, ex); |
| 454 | if (res != E_DEC_DIV_ZERO) print_decimal(&c, orig, res, ex, s); |
| 455 | } |
| 456 | |
| 457 | const char *round_mode[] = {"TRUNCATE", "HALF_EVEN", "HALF_UP", "CEILING", |
| 458 | "FLOOR"}; |
| 459 | |
| 460 | void do_test_ro(const char *s1, int n, decimal_round_mode mode, |
| 461 | const char *orig, int ex) { |
| 462 | char s[100]; |
| 463 | int res; |
| 464 | sprintf(s, "'%s', %d, %s", s1, n, round_mode[mode]); |
| 465 | const char *end = strend(s1); |
| 466 | string2decimal(s1, &a, &end); |
| 467 | res = decimal_round(&a, &b, n, mode); |
| 468 | print_decimal(&b, orig, res, ex, s); |
| 469 | } |
| 470 | |
| 471 | void do_test_format(const char *s1, const char *s2, int n, const char *orig, |
| 472 | int ex) { |
| 473 | char s[200]; |
| 474 | decimal_t a, b, c, d; |
| 475 | decimal_digit_t buf1[9], buf2[9], buf3[9], buf4[9]; |
| 476 | int res; |
| 477 | a.buf = buf1; |
| 478 | b.buf = buf2; |
| 479 | c.buf = buf3; |
| 480 | d.buf = buf4; |
| 481 | a.len = sizeof(buf1) / sizeof(dec1); |
| 482 | b.len = sizeof(buf2) / sizeof(dec1); |
| 483 | c.len = sizeof(buf3) / sizeof(dec1); |
| 484 | d.len = sizeof(buf4) / sizeof(dec1); |
| 485 | |
| 486 | sprintf(s, "'%s' %% '%s'", s1, s2); |
| 487 | const char *end = strend(s1); |
| 488 | string2decimal(s1, &a, &end); |
| 489 | end = strend(s2); |
| 490 | string2decimal(s2, &b, &end); |
| 491 | decimal_mod(&a, &b, &c); |
| 492 | res = decimal_round(&c, &d, n, HALF_UP); |
| 493 | print_decimal(&d, orig, res, ex, s); |
| 494 | } |
| 495 | void do_test_mx(int precision, int frac, const char *orig) { |
| 496 | char s[100]; |
| 497 | sprintf(s, "%d, %d", precision, frac); |
| 498 | max_decimal(precision, frac, &a); |
| 499 | print_decimal(&a, orig, 0, 0, s); |
| 500 | } |
| 501 | |
| 502 | static void do_test_pr(const char *s1, int prec, int dec, const char *orig, |
| 503 | int ex) { |
| 504 | char s[100]; |
| 505 | char s2[100]; |
| 506 | int slen = sizeof(s2); |
| 507 | |
| 508 | sprintf(s, "'%s', %d, %d", s1, prec, dec); |
| 509 | const char *end = strend(s1); |
| 510 | string2decimal(s1, &a, &end); |
| 511 | const int res = decimal2string(&a, s2, &slen, prec, dec); |
| 512 | check_result_code(res, ex); |
| 513 | if (orig) { |
| 514 | EXPECT_STREQ(orig, s2) << " arguments were: " << s; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | void do_test_widen_fraction(const char *s1, int increase, const char *orig) { |
| 519 | decimal_t a; |
| 520 | decimal_digit_t buf1[9]; |
| 521 | a.buf = buf1; |
| 522 | a.len = array_elements(buf1); |
| 523 | |
| 524 | const char *end = strend(s1); |
| 525 | string2decimal(s1, &a, &end); |
| 526 | widen_fraction(a.frac + increase, &a); |
| 527 | |
| 528 | char s[100]; |
| 529 | int slen = sizeof(s); |
| 530 | int result = decimal2string(&a, s, &slen); |
| 531 | EXPECT_EQ(result, 0); |
| 532 | EXPECT_STREQ(orig, s) << " arguments were: " << s1; |
| 533 | } |
| 534 | |
| 535 | void do_test_sh(const char *s1, int shift, const char *orig, int ex) { |
| 536 | char s[100]; |
| 537 | int res; |
| 538 | sprintf(s, "'%s' %s %d", s1, ((shift < 0) ? ">>" : "<<"), abs(shift)); |
| 539 | const char *end = strend(s1); |
| 540 | string2decimal(s1, &a, &end); |
| 541 | res = decimal_shift(&a, shift); |
| 542 | print_decimal(&a, orig, res, ex, s); |
| 543 | } |
| 544 | |
| 545 | void do_test_fr(const char *s1, const char *orig) { |
| 546 | char s[100]; |
| 547 | sprintf(s, "'%s'", s1); |
| 548 | const char *end = strend(s1); |
| 549 | string2decimal(s1, &a, &end); |
| 550 | a.frac = decimal_actual_fraction(&a); |
| 551 | print_decimal(&a, orig, 0, 0, s); |
| 552 | } |
| 553 | |
| 554 | static void SetupDecimals() { |
| 555 | a.buf = buf1; |
| 556 | a.len = sizeof(buf1) / sizeof(dec1); |
| 557 | b.buf = buf2; |
| 558 | b.len = sizeof(buf2) / sizeof(dec1); |
| 559 | c.buf = buf3; |
| 560 | c.len = sizeof(buf3) / sizeof(dec1); |
| 561 | } |
| 562 | |
| 563 | class DecimalTest : public ::testing::Test { |
| 564 | protected: |
| 565 | void SetUp() override { SetupDecimals(); } |
| 566 | }; |
| 567 | |
| 568 | TEST_F(DecimalTest, String2Decimal) { |
| 569 | test_s2d("12345", "12345", 0); |
| 570 | test_s2d("12345.", "12345", 0); |
| 571 | test_s2d("123.45", "123.45", 0); |
| 572 | test_s2d("-123.45", "-123.45", 0); |
| 573 | test_s2d(".00012345000098765", "0.00012345000098765", 0); |
| 574 | test_s2d(".12345000098765", "0.12345000098765", 0); |
| 575 | test_s2d("-.000000012345000098765", "-0.000000012345000098765", 0); |
| 576 | test_s2d("1234500009876.5", "1234500009876.5", 0); |
| 577 | a.len = 1; |
| 578 | test_s2d("123450000098765", "98765", 2); |
| 579 | test_s2d("123450.000098765", "123450", 1); |
| 580 | a.len = sizeof(buf1) / sizeof(dec1); |
| 581 | test_s2d("123E5", "12300000", 0); |
| 582 | test_s2d("123E-2", "1.23", 0); |
| 583 | } |
| 584 | |
| 585 | TEST_F(DecimalTest, Decimal2Double) { |
| 586 | test_d2f("12345", 0); |
| 587 | test_d2f("123.45", 0); |
| 588 | test_d2f("-123.45", 0); |
| 589 | test_d2f("0.00012345000098765", 0); |
| 590 | test_d2f("1234500009876.5", 0); |
| 591 | } |
| 592 | |
| 593 | TEST_F(DecimalTest, Double2Decimal) { |
| 594 | test_f2d(12345, 0); |
| 595 | test_f2d(1.0 / 3, 0); |
| 596 | test_f2d(-123.45, 0); |
| 597 | test_f2d(0.00012345000098765, 0); |
| 598 | test_f2d(1234500009876.5, 0); |
| 599 | } |
| 600 | |
| 601 | TEST_F(DecimalTest, Ulonglong2Decimal) { |
| 602 | test_ull2d(12345ULL, "12345", 0); |
| 603 | test_ull2d(0ULL, "0", 0); |
| 604 | test_ull2d(18446744073709551615ULL, "18446744073709551615", 0); |
| 605 | } |
| 606 | |
| 607 | TEST_F(DecimalTest, Decimal2Ulonglong) { |
| 608 | test_d2ull("12345", "12345", 0); |
| 609 | test_d2ull("0", "0", 0); |
| 610 | /* ULLONG_MAX = 18446744073709551615ULL */ |
| 611 | test_d2ull("18446744073709551615", "18446744073709551615", 0); |
| 612 | test_d2ull("18446744073709551616", "18446744073709551615", 2); |
| 613 | test_d2ull("-1", "0", 2); |
| 614 | test_d2ull("1.23", "1", 1); |
| 615 | test_d2ull("9999999999999999999999999.000", "18446744073709551615", 2); |
| 616 | } |
| 617 | |
| 618 | TEST_F(DecimalTest, Longlong2Decimal) { |
| 619 | test_ll2d(-12345LL, "-12345", 0); |
| 620 | test_ll2d(-1LL, "-1", 0); |
| 621 | test_ll2d(-9223372036854775807LL, "-9223372036854775807", 0); |
| 622 | test_ll2d(9223372036854775808ULL, "-9223372036854775808", 0); |
| 623 | } |
| 624 | |
| 625 | TEST_F(DecimalTest, Decimal2Longlong) { |
| 626 | /* LLONG_MAX = 9223372036854775807LL */ |
| 627 | test_d2ll("18446744073709551615", "9223372036854775807", 2); |
| 628 | test_d2ll("-1", "-1", 0); |
| 629 | test_d2ll("-1.23", "-1", 1); |
| 630 | test_d2ll("-9223372036854775807", "-9223372036854775807", 0); |
| 631 | test_d2ll("-9223372036854775808", "-9223372036854775808", 0); |
| 632 | test_d2ll("9223372036854775808", "9223372036854775807", 2); |
| 633 | } |
| 634 | |
| 635 | TEST_F(DecimalTest, DoAdd) { |
| 636 | test_da(".00012345000098765", "123.45", "123.45012345000098765", 0); |
| 637 | test_da(".1", ".45", "0.55", 0); |
| 638 | test_da("1234500009876.5", ".00012345000098765", |
| 639 | "1234500009876.50012345000098765", 0); |
| 640 | test_da("9999909999999.5", ".555", "9999910000000.055", 0); |
| 641 | test_da("99999999", "1", "100000000", 0); |
| 642 | test_da("989999999", "1", "990000000", 0); |
| 643 | test_da("999999999", "1", "1000000000", 0); |
| 644 | test_da("12345", "123.45", "12468.45", 0); |
| 645 | test_da("-12345", "-123.45", "-12468.45", 0); |
| 646 | test_ds("-12345", "123.45", "-12468.45", 0); |
| 647 | test_ds("12345", "-123.45", "12468.45", 0); |
| 648 | } |
| 649 | |
| 650 | TEST_F(DecimalTest, DoSub) { |
| 651 | test_ds(".00012345000098765", "123.45", "-123.44987654999901235", 0); |
| 652 | test_ds("1234500009876.5", ".00012345000098765", |
| 653 | "1234500009876.49987654999901235", 0); |
| 654 | test_ds("9999900000000.5", ".555", "9999899999999.945", 0); |
| 655 | test_ds("1111.5551", "1111.555", "0.0001", 0); |
| 656 | test_ds(".555", ".555", "0", 0); |
| 657 | test_ds("10000000", "1", "9999999", 0); |
| 658 | test_ds("1000001000", ".1", "1000000999.9", 0); |
| 659 | test_ds("1000000000", ".1", "999999999.9", 0); |
| 660 | test_ds("12345", "123.45", "12221.55", 0); |
| 661 | test_ds("-12345", "-123.45", "-12221.55", 0); |
| 662 | test_da("-12345", "123.45", "-12221.55", 0); |
| 663 | test_da("12345", "-123.45", "12221.55", 0); |
| 664 | test_ds("123.45", "12345", "-12221.55", 0); |
| 665 | test_ds("-123.45", "-12345", "12221.55", 0); |
| 666 | test_da("123.45", "-12345", "-12221.55", 0); |
| 667 | test_da("-123.45", "12345", "12221.55", 0); |
| 668 | test_da("5", "-6.0", "-1.0", 0); |
| 669 | } |
| 670 | |
| 671 | TEST_F(DecimalTest, DecimalMul) { |
| 672 | test_dm("12", "10", "120", 0); |
| 673 | test_dm("-123.456", "98765.4321", "-12193185.1853376", 0); |
| 674 | test_dm("-123456000000", "98765432100000", "-12193185185337600000000000", 0); |
| 675 | test_dm("123456", "987654321", "121931851853376", 0); |
| 676 | test_dm("123456", "9876543210", "1219318518533760", 0); |
| 677 | test_dm("123", "0.01", "1.23", 0); |
| 678 | test_dm("123", "0", "0", 0); |
| 679 | } |
| 680 | |
| 681 | TEST_F(DecimalTest, DecimalDiv) { |
| 682 | test_dv("120", "10", "12.000000000", 0); |
| 683 | test_dv("123", "0.01", "12300.000000000", 0); |
| 684 | test_dv("120", "100000000000.00000", "0.000000001200000000", 0); |
| 685 | test_dv("123", "0", "", 4); |
| 686 | test_dv("0", "0", "", 4); |
| 687 | test_dv("-12193185.1853376", "98765.4321", "-123.456000000000000000", 0); |
| 688 | test_dv("121931851853376", "987654321", "123456.000000000", 0); |
| 689 | test_dv("0", "987", "0", 0); |
| 690 | test_dv("1", "3", "0.333333333", 0); |
| 691 | test_dv("1.000000000000", "3", "0.333333333333333333", 0); |
| 692 | test_dv("1", "1", "1.000000000", 0); |
| 693 | test_dv("0.0123456789012345678912345", "9999999999", |
| 694 | "0.000000000001234567890246913578148141", 0); |
| 695 | test_dv("10.333000000", "12.34500", "0.837019036046982584042122316", 0); |
| 696 | test_dv("10.000000000060", "2", "5.000000000030000000", 0); |
| 697 | } |
| 698 | |
| 699 | TEST_F(DecimalTest, DecimalMod) { |
| 700 | test_md("234", "10", "4", 0); |
| 701 | test_md("234.567", "10.555", "2.357", 0); |
| 702 | test_md("-234.567", "10.555", "-2.357", 0); |
| 703 | test_md("234.567", "-10.555", "2.357", 0); |
| 704 | c.buf[1] = 0x3ABECA; |
| 705 | test_md("99999999999999999999999999999999999999", "3", "0", 0); |
| 706 | if (c.buf[1] != 0x3ABECA) { |
| 707 | ADD_FAILURE() << "overflow " << c.buf[1]; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | TEST_F(DecimalTest, Decimal2BinBin2Decimal) { |
| 712 | test_d2b2d("-10.55", 4, 2, "-10.55", 0); |
| 713 | test_d2b2d("0.0123456789012345678912345", 30, 25, |
| 714 | "0.0123456789012345678912345", 0); |
| 715 | test_d2b2d("12345", 5, 0, "12345", 0); |
| 716 | test_d2b2d("12345", 10, 3, "12345.000", 0); |
| 717 | test_d2b2d("123.45", 10, 3, "123.450", 0); |
| 718 | test_d2b2d("-123.45", 20, 10, "-123.4500000000", 0); |
| 719 | test_d2b2d(".00012345000098765", 15, 14, "0.00012345000098", 0); |
| 720 | test_d2b2d(".00012345000098765", 22, 20, "0.00012345000098765000", 0); |
| 721 | test_d2b2d(".12345000098765", 30, 20, "0.12345000098765000000", 0); |
| 722 | test_d2b2d("-.000000012345000098765", 30, 20, "-0.00000001234500009876", 0); |
| 723 | test_d2b2d("1234500009876.5", 30, 5, "1234500009876.50000", 0); |
| 724 | test_d2b2d("111111111.11", 10, 2, "11111111.11", 0); |
| 725 | test_d2b2d("000000000.01", 7, 3, "0.010", 0); |
| 726 | test_d2b2d("123.4", 10, 2, "123.40", 0); |
| 727 | } |
| 728 | |
| 729 | TEST_F(DecimalTest, DecimalCmp) { |
| 730 | test_dc("12", "13", -1); |
| 731 | test_dc("13", "12", 1); |
| 732 | test_dc("-10", "10", -1); |
| 733 | test_dc("10", "-10", 1); |
| 734 | test_dc("-12", "-13", 1); |
| 735 | test_dc("0", "12", -1); |
| 736 | test_dc("-10", "0", -1); |
| 737 | test_dc("4", "4", 0); |
| 738 | } |
| 739 | |
| 740 | TEST_F(DecimalTest, DecimalRound) { |
| 741 | test_ro("5678.123451", -4, TRUNCATE, "0", 0); |
| 742 | test_ro("5678.123451", -3, TRUNCATE, "5000", 0); |
| 743 | test_ro("5678.123451", -2, TRUNCATE, "5600", 0); |
| 744 | test_ro("5678.123451", -1, TRUNCATE, "5670", 0); |
| 745 | test_ro("5678.123451", 0, TRUNCATE, "5678", 0); |
| 746 | test_ro("5678.123451", 1, TRUNCATE, "5678.1", 0); |
| 747 | test_ro("5678.123451", 2, TRUNCATE, "5678.12", 0); |
| 748 | test_ro("5678.123451", 3, TRUNCATE, "5678.123", 0); |
| 749 | test_ro("5678.123451", 4, TRUNCATE, "5678.1234", 0); |
| 750 | test_ro("5678.123451", 5, TRUNCATE, "5678.12345", 0); |
| 751 | test_ro("5678.123451", 6, TRUNCATE, "5678.123451", 0); |
| 752 | test_ro("-5678.123451", -4, TRUNCATE, "0", 0); |
| 753 | memset(buf2, 33, sizeof(buf2)); |
| 754 | test_ro("99999999999999999999999999999999999999", -31, TRUNCATE, |
| 755 | "99999990000000000000000000000000000000", 0); |
| 756 | test_ro("15.1", 0, HALF_UP, "15", 0); |
| 757 | test_ro("15.5", 0, HALF_UP, "16", 0); |
| 758 | test_ro("15.9", 0, HALF_UP, "16", 0); |
| 759 | test_ro("-15.1", 0, HALF_UP, "-15", 0); |
| 760 | test_ro("-15.5", 0, HALF_UP, "-16", 0); |
| 761 | test_ro("-15.9", 0, HALF_UP, "-16", 0); |
| 762 | test_ro("15.1", 1, HALF_UP, "15.1", 0); |
| 763 | test_ro("-15.1", 1, HALF_UP, "-15.1", 0); |
| 764 | test_ro("15.17", 1, HALF_UP, "15.2", 0); |
| 765 | test_ro("15.4", -1, HALF_UP, "20", 0); |
| 766 | test_ro("-15.4", -1, HALF_UP, "-20", 0); |
| 767 | test_ro("5.4", -1, HALF_UP, "10", 0); |
| 768 | test_ro(".999", 0, HALF_UP, "1", 0); |
| 769 | memset(buf2, 33, sizeof(buf2)); |
| 770 | test_ro("999999999", -9, HALF_UP, "1000000000", 0); |
| 771 | test_ro("15.1", 0, HALF_EVEN, "15", 0); |
| 772 | test_ro("15.5", 0, HALF_EVEN, "16", 0); |
| 773 | test_ro("14.5", 0, HALF_EVEN, "14", 0); |
| 774 | test_ro("15.9", 0, HALF_EVEN, "16", 0); |
| 775 | test_ro("15.1", 0, CEILING, "16", 0); |
| 776 | test_ro("-15.1", 0, CEILING, "-15", 0); |
| 777 | test_ro("15.1", 0, FLOOR, "15", 0); |
| 778 | test_ro("-15.1", 0, FLOOR, "-16", 0); |
| 779 | test_ro("999999999999999999999.999", 0, CEILING, "1000000000000000000000", 0); |
| 780 | test_ro("-999999999999999999999.999", 0, FLOOR, "-1000000000000000000000", 0); |
| 781 | |
| 782 | b.buf[0] = DIG_BASE + 1; |
| 783 | b.buf++; |
| 784 | test_ro(".3", 0, HALF_UP, "0", 0); |
| 785 | b.buf--; |
| 786 | if (b.buf[0] != DIG_BASE + 1) { |
| 787 | ADD_FAILURE() << "underflow " << b.buf[0]; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | TEST_F(DecimalTest, FormatFunc) { |
| 792 | test_format("999999999999999999999999999999999999999999999999999999999999999", |
| 793 | "999999999999999999999999999999999999999999999999999999999999999", |
| 794 | 42, "0.000000000000000000000000000000000000000000", 0); |
| 795 | } |
| 796 | |
| 797 | TEST_F(DecimalTest, MaxDecimal) { |
| 798 | test_mx(1, 1, "0.9"); |
| 799 | test_mx(1, 0, "9"); |
| 800 | test_mx(2, 1, "9.9"); |
| 801 | test_mx(4, 2, "99.99"); |
| 802 | test_mx(6, 3, "999.999"); |
| 803 | test_mx(8, 4, "9999.9999"); |
| 804 | test_mx(10, 5, "99999.99999"); |
| 805 | test_mx(12, 6, "999999.999999"); |
| 806 | test_mx(14, 7, "9999999.9999999"); |
| 807 | test_mx(16, 8, "99999999.99999999"); |
| 808 | test_mx(18, 9, "999999999.999999999"); |
| 809 | test_mx(20, 10, "9999999999.9999999999"); |
| 810 | test_mx(20, 20, "0.99999999999999999999"); |
| 811 | test_mx(20, 0, "99999999999999999999"); |
| 812 | test_mx(40, 20, "99999999999999999999.99999999999999999999"); |
| 813 | } |
| 814 | |
| 815 | TEST_F(DecimalTest, Decimal2String) { |
| 816 | test_pr("123.123", 0, 0, "123.123", 0); |
| 817 | /* For fixed precision, we no longer count the '.' here. */ |
| 818 | test_pr("123.123", 6, 3, "123.123", 0); |
| 819 | test_pr("123.123", 8, 3, "00123.123", 0); |
| 820 | test_pr("123.123", 8, 4, "0123.1230", 0); |
| 821 | test_pr("123.123", 8, 5, "123.12300", 0); |
| 822 | test_pr("123.123", 8, 2, "000123.12", 1); |
| 823 | test_pr("123.123", 8, 6, "23.123000", 2); |
| 824 | } |
| 825 | |
| 826 | TEST_F(DecimalTest, WidenFraction) { |
| 827 | test_widen_fraction("123.0", 1, "123.00"); |
| 828 | test_widen_fraction("1234567890.123456789", 1, "1234567890.1234567890"); |
| 829 | test_widen_fraction("123.0", 0, "123.0"); |
| 830 | test_widen_fraction("123.0", 4, "123.00000"); |
| 831 | } |
| 832 | |
| 833 | TEST_F(DecimalTest, DecimalShift) { |
| 834 | test_sh("123.123", 1, "1231.23", 0); |
| 835 | test_sh("123457189.123123456789000", 1, "1234571891.23123456789", 0); |
| 836 | test_sh("123457189.123123456789000", 4, "1234571891231.23456789", 0); |
| 837 | test_sh("123457189.123123456789000", 8, "12345718912312345.6789", 0); |
| 838 | test_sh("123457189.123123456789000", 9, "123457189123123456.789", 0); |
| 839 | test_sh("123457189.123123456789000", 10, "1234571891231234567.89", 0); |
| 840 | test_sh("123457189.123123456789000", 17, "12345718912312345678900000", 0); |
| 841 | test_sh("123457189.123123456789000", 18, "123457189123123456789000000", 0); |
| 842 | test_sh("123457189.123123456789000", 19, "1234571891231234567890000000", 0); |
| 843 | test_sh("123457189.123123456789000", 26, |
| 844 | "12345718912312345678900000000000000", 0); |
| 845 | test_sh("123457189.123123456789000", 27, |
| 846 | "123457189123123456789000000000000000", 0); |
| 847 | test_sh("123457189.123123456789000", 28, |
| 848 | "1234571891231234567890000000000000000", 0); |
| 849 | test_sh("000000000000000000000000123457189.123123456789000", 26, |
| 850 | "12345718912312345678900000000000000", 0); |
| 851 | test_sh("00000000123457189.123123456789000", 27, |
| 852 | "123457189123123456789000000000000000", 0); |
| 853 | test_sh("00000000000000000123457189.123123456789000", 28, |
| 854 | "1234571891231234567890000000000000000", 0); |
| 855 | test_sh("123", 1, "1230", 0); |
| 856 | test_sh("123", 10, "1230000000000", 0); |
| 857 | test_sh(".123", 1, "1.23", 0); |
| 858 | test_sh(".123", 10, "1230000000", 0); |
| 859 | test_sh(".123", 14, "12300000000000", 0); |
| 860 | test_sh("000.000", 1000, "0", 0); |
| 861 | test_sh("000.", 1000, "0", 0); |
| 862 | test_sh(".000", 1000, "0", 0); |
| 863 | test_sh("1", 1000, "1", 2); |
| 864 | test_sh("123.123", -1, "12.3123", 0); |
| 865 | test_sh("123987654321.123456789000", -1, "12398765432.1123456789", 0); |
| 866 | test_sh("123987654321.123456789000", -2, "1239876543.21123456789", 0); |
| 867 | test_sh("123987654321.123456789000", -3, "123987654.321123456789", 0); |
| 868 | test_sh("123987654321.123456789000", -8, "1239.87654321123456789", 0); |
| 869 | test_sh("123987654321.123456789000", -9, "123.987654321123456789", 0); |
| 870 | test_sh("123987654321.123456789000", -10, "12.3987654321123456789", 0); |
| 871 | test_sh("123987654321.123456789000", -11, "1.23987654321123456789", 0); |
| 872 | test_sh("123987654321.123456789000", -12, "0.123987654321123456789", 0); |
| 873 | test_sh("123987654321.123456789000", -13, "0.0123987654321123456789", 0); |
| 874 | test_sh("123987654321.123456789000", -14, "0.00123987654321123456789", 0); |
| 875 | test_sh("00000087654321.123456789000", -14, "0.00000087654321123456789", 0); |
| 876 | a.len = 2; |
| 877 | test_sh("123.123", -2, "1.23123", 0); |
| 878 | test_sh("123.123", -3, "0.123123", 0); |
| 879 | test_sh("123.123", -6, "0.000123123", 0); |
| 880 | test_sh("123.123", -7, "0.0000123123", 0); |
| 881 | test_sh("123.123", -15, "0.000000000000123123", 0); |
| 882 | test_sh("123.123", -16, "0.000000000000012312", 1); |
| 883 | test_sh("123.123", -17, "0.000000000000001231", 1); |
| 884 | test_sh("123.123", -18, "0.000000000000000123", 1); |
| 885 | test_sh("123.123", -19, "0.000000000000000012", 1); |
| 886 | test_sh("123.123", -20, "0.000000000000000001", 1); |
| 887 | test_sh("123.123", -21, "0", 1); |
| 888 | test_sh(".000000000123", -1, "0.0000000000123", 0); |
| 889 | test_sh(".000000000123", -6, "0.000000000000000123", 0); |
| 890 | test_sh(".000000000123", -7, "0.000000000000000012", 1); |
| 891 | test_sh(".000000000123", -8, "0.000000000000000001", 1); |
| 892 | test_sh(".000000000123", -9, "0", 1); |
| 893 | test_sh(".000000000123", 1, "0.00000000123", 0); |
| 894 | test_sh(".000000000123", 8, "0.0123", 0); |
| 895 | test_sh(".000000000123", 9, "0.123", 0); |
| 896 | test_sh(".000000000123", 10, "1.23", 0); |
| 897 | test_sh(".000000000123", 17, "12300000", 0); |
| 898 | test_sh(".000000000123", 18, "123000000", 0); |
| 899 | test_sh(".000000000123", 19, "1230000000", 0); |
| 900 | test_sh(".000000000123", 20, "12300000000", 0); |
| 901 | test_sh(".000000000123", 21, "123000000000", 0); |
| 902 | test_sh(".000000000123", 22, "1230000000000", 0); |
| 903 | test_sh(".000000000123", 23, "12300000000000", 0); |
| 904 | test_sh(".000000000123", 24, "123000000000000", 0); |
| 905 | test_sh(".000000000123", 25, "1230000000000000", 0); |
| 906 | test_sh(".000000000123", 26, "12300000000000000", 0); |
| 907 | test_sh(".000000000123", 27, "123000000000000000", 0); |
| 908 | test_sh(".000000000123", 28, "0.000000000123", 2); |
| 909 | test_sh("123456789.987654321", -1, "12345678.998765432", 1); |
| 910 | test_sh("123456789.987654321", -2, "1234567.899876543", 1); |
| 911 | test_sh("123456789.987654321", -8, "1.234567900", 1); |
| 912 | test_sh("123456789.987654321", -9, "0.123456789987654321", 0); |
| 913 | test_sh("123456789.987654321", -10, "0.012345678998765432", 1); |
| 914 | test_sh("123456789.987654321", -17, "0.000000001234567900", 1); |
| 915 | test_sh("123456789.987654321", -18, "0.000000000123456790", 1); |
| 916 | test_sh("123456789.987654321", -19, "0.000000000012345679", 1); |
| 917 | test_sh("123456789.987654321", -26, "0.000000000000000001", 1); |
| 918 | test_sh("123456789.987654321", -27, "0", 1); |
| 919 | test_sh("123456789.987654321", 1, "1234567900", 1); |
| 920 | test_sh("123456789.987654321", 2, "12345678999", 1); |
| 921 | test_sh("123456789.987654321", 4, "1234567899877", 1); |
| 922 | test_sh("123456789.987654321", 8, "12345678998765432", 1); |
| 923 | test_sh("123456789.987654321", 9, "123456789987654321", 0); |
| 924 | test_sh("123456789.987654321", 10, "123456789.987654321", 2); |
| 925 | test_sh("123456789.987654321", 0, "123456789.987654321", 0); |
| 926 | a.len = sizeof(buf1) / sizeof(dec1); |
| 927 | } |
| 928 | |
| 929 | TEST_F(DecimalTest, DecimalActualFraction) { |
| 930 | test_fr("1.123456789000000000", "1.123456789"); |
| 931 | test_fr("1.12345678000000000", "1.12345678"); |
| 932 | test_fr("1.1234567000000000", "1.1234567"); |
| 933 | test_fr("1.123456000000000", "1.123456"); |
| 934 | test_fr("1.12345000000000", "1.12345"); |
| 935 | test_fr("1.1234000000000", "1.1234"); |
| 936 | test_fr("1.123000000000", "1.123"); |
| 937 | test_fr("1.12000000000", "1.12"); |
| 938 | test_fr("1.1000000000", "1.1"); |
| 939 | test_fr("1.000000000", "1"); |
| 940 | test_fr("1.0", "1"); |
| 941 | test_fr("10000000000000000000.0", "10000000000000000000"); |
| 942 | } |
| 943 | |
| 944 | // Some test data from DBT-3. |
| 945 | static const char *decimal_testdata[] = { |
| 946 | "45983.16", "0.09", "983", "0.09", "36.00", "45983.16", |
| 947 | "0.09", "0.1", "8.00", "13309.60", "0.10", "28955.64", |
| 948 | "0", "28.00", "28955.64", "0.09", "0", "24.00", |
| 949 | "22824.48", "0.10", "49620.16", "0.07", "32.00", "49620.16", |
| 950 | "0.07", "0.0", "38.00", "44694.46", "0.00", "45.00", |
| 951 | "4058", "54058.05", "0.06", "058", "0.06", "45.00", |
| 952 | "4058", "0.0", "49.00", "796", "46796.47", "73426.50", |
| 953 | "0.08", "0", "37.00", "61998.31", "0.08", "13608.60", |
| 954 | "0.07", "12.00", "13608.60", "0.07", "0.0", "9.00", |
| 955 | "11594.16", "0.08", "81639.88", "0", "46.00", "81639.88", |
| 956 | "0.10", "0", "28.00", "31809.96", "0.03", "73943.82", |
| 957 | "0.08", "38.00", "73943.82", "0.08", "0.0", "35.00", |
| 958 | "43058.75", "0.06", "6476.15", "0", "5.00", "6476.15", |
| 959 | "0.04", "0", "28.00", "47227.60", "0.05", "64605.44", |
| 960 | "0.02", "32.00", "64605.44", "0.02", "0.0", "2.00", |
| 961 | "2210.32", "0.09", "6582.96", "0", "4.00", "6582.96", |
| 962 | "0.09", "0", "44.00", "79059.64", "0.05", "9159.66", |
| 963 | "0.04", "6.00", "9159.66", "0.04", "0.0", "31.00", |
| 964 | "40217.23", "0.09", "47344.32", "0", "32.00", "47344.32", |
| 965 | "0.02", "0", "5.00", "7532.30", "0.05", "41.00", |
| 966 | "28", "75928.31", "0.09", "41.00", "75928.31", "0.09", |
| 967 | "0.0", "24.00", "32410.80", "32410.80", "0.02", "68065.96", |
| 968 | "0", "34.00", "68065.96", "0.06", "0", "7.00", |
| 969 | "13418.23", "0.06", "29004.25", "0.06", "25.00", "29004.25", |
| 970 | "0.06", "0.0", "34.00", "65854.94", "0.08", "47397.28", |
| 971 | "0", "28.00", "47397.28", "0.03", "0", "42.00", |
| 972 | "75043.92", "0.09", "62105.20", "0.09", "40.00", "62105.20", |
| 973 | "0.09", "0.0", "39.00", "70542.42", "0.05", "78083.70", |
| 974 | "0", "43.00", "78083.70", "0.05", "0", "44.00", |
| 975 | "84252.52", "0.04", "53782.08", "0.09", "44.00", "53782.08", |
| 976 | "0.09", "0.0", "26.00", "43383.08", "0.08", "82746.18", |
| 977 | "0", "46.00", "82746.18", "0.06", "0", "32.00", |
| 978 | "48338.88", "0.07", "63360.93", "0.01", "43.00", "63360.93", |
| 979 | "0.01", "0.0", "40.00", "54494.40", "0.06", "21.00", |
| 980 | "75", "40675.95", "0", "21.00", "40675.95", "0.05", |
| 981 | "0", "26.00", "42995.94", "0.03", "39353.82", "0.00", |
| 982 | "22.00", "39353.82", "0.00", "0.0", "21.00", "27076.98", |
| 983 | "0.09", "31.00"}; |
| 984 | |
| 985 | static void BM_Decimal2Bin_10_2(size_t iters) { |
| 986 | StopBenchmarkTiming(); |
| 987 | constexpr size_t num_elements = array_elements(decimal_testdata); |
| 988 | decimal_t decimals[num_elements]; |
| 989 | decimal_digit_t decimal_buf[num_elements][9]; |
| 990 | |
| 991 | for (size_t i = 0; i < num_elements; ++i) { |
| 992 | const char *end = strend(decimal_testdata[i]); |
| 993 | decimals[i].buf = decimal_buf[i]; |
| 994 | decimals[i].len = array_elements(decimal_buf[i]); |
| 995 | int res = string2decimal(decimal_testdata[i], &decimals[i], &end); |
| 996 | ASSERT_EQ(E_DEC_OK, res) << decimal_testdata[i] << " wasn't converted"; |
| 997 | } |
| 998 | StartBenchmarkTiming(); |
| 999 | |
| 1000 | int dummy = 0; |
| 1001 | for (size_t i = 0; i < iters; ++i) { |
| 1002 | uchar buf[100]; |
| 1003 | decimal2bin(&decimals[i % num_elements], buf, 10, 2); |
| 1004 | dummy += buf[0]; |
| 1005 | } |
| 1006 | |
| 1007 | ASSERT_NE(0, dummy); // To keep the optimizer from removing the loop. |
| 1008 | } |
| 1009 | BENCHMARK(BM_Decimal2Bin_10_2) |
| 1010 | |
| 1011 | static void BM_Bin2Decimal_10_2(size_t iters) { |
| 1012 | StopBenchmarkTiming(); |
| 1013 | constexpr size_t num_elements = array_elements(decimal_testdata); |
| 1014 | constexpr int bin_size = 5; |
| 1015 | ASSERT_EQ(bin_size, decimal_bin_size(10, 2)) << "Need to adjust bin_size"; |
| 1016 | uchar packed_buf[num_elements][bin_size]; |
| 1017 | |
| 1018 | decimal_t decimal; |
| 1019 | decimal_digit_t decimal_buf[9]; |
| 1020 | decimal.buf = decimal_buf; |
| 1021 | decimal.len = array_elements(decimal_buf); |
| 1022 | |
| 1023 | for (size_t i = 0; i < num_elements; ++i) { |
| 1024 | const char *end = strend(decimal_testdata[i]); |
| 1025 | int res = string2decimal(decimal_testdata[i], &decimal, &end); |
| 1026 | ASSERT_EQ(E_DEC_OK, res) << decimal_testdata[i] << " wasn't converted"; |
| 1027 | res = decimal2bin(&decimal, packed_buf[i], 10, 2); |
| 1028 | ASSERT_EQ(E_DEC_OK, res) |
| 1029 | << decimal_testdata[i] << " wasn't converted in stage 2"; |
| 1030 | } |
| 1031 | StartBenchmarkTiming(); |
| 1032 | |
| 1033 | size_t dummy = 0; |
| 1034 | for (size_t i = 0; i < iters; ++i) { |
| 1035 | bin2decimal(packed_buf[i % num_elements], &decimal, 10, 2); |
| 1036 | dummy += decimal_buf[0]; |
| 1037 | } |
| 1038 | |
| 1039 | ASSERT_NE(static_cast<size_t>(-1), |
| 1040 | dummy); // To keep the optimizer from removing the loop. |
| 1041 | } |
| 1042 | BENCHMARK(BM_Bin2Decimal_10_2) |
| 1043 | |
| 1044 | static void BM_Decimal2String(size_t iterations) { |
| 1045 | StopBenchmarkTiming(); |
| 1046 | constexpr size_t num_elements = array_elements(decimal_testdata); |
| 1047 | decimal_t decimals[num_elements]; |
| 1048 | decimal_digit_t decimal_buf[num_elements][9]; |
| 1049 | |
| 1050 | for (size_t i = 0; i < num_elements; ++i) { |
| 1051 | const char *end = strend(decimal_testdata[i]); |
| 1052 | decimals[i].buf = decimal_buf[i]; |
| 1053 | decimals[i].len = array_elements(decimal_buf[i]); |
| 1054 | int res = string2decimal(decimal_testdata[i], &decimals[i], &end); |
| 1055 | ASSERT_EQ(E_DEC_OK, res) << decimal_testdata[i] << " wasn't converted"; |
| 1056 | } |
| 1057 | StartBenchmarkTiming(); |
| 1058 | |
| 1059 | for (size_t i = 0; i < iterations; ++i) { |
| 1060 | for (const decimal_t &dec : decimals) { |
| 1061 | char buffer[20]; |
| 1062 | int length = sizeof(buffer); |
| 1063 | decimal2string(&dec, buffer, &length); |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | BENCHMARK(BM_Decimal2String) |
| 1068 | |
| 1069 | struct DecimalToStringParam { |
| 1070 | const char *input_string; |
| 1071 | int buffer_size; |
| 1072 | const char *result_string; |
| 1073 | int error_code; |
| 1074 | }; |
| 1075 | |
| 1076 | class DecimalToStringTest |
| 1077 | : public testing::TestWithParam<DecimalToStringParam> { |
| 1078 | protected: |
| 1079 | void SetUp() override { SetupDecimals(); } |
| 1080 | }; |
| 1081 | |
| 1082 | TEST_P(DecimalToStringTest, DecimalToString) { |
| 1083 | const char *end = strend(GetParam().input_string); |
| 1084 | EXPECT_EQ(E_DEC_OK, string2decimal(GetParam().input_string, &a, &end)); |
| 1085 | |
| 1086 | char buffer[DECIMAL_MAX_STR_LENGTH + 1]; |
| 1087 | int length = GetParam().buffer_size; |
| 1088 | EXPECT_EQ(GetParam().error_code, decimal2string(&a, buffer, &length)); |
| 1089 | EXPECT_EQ(strlen(GetParam().result_string), length); |
| 1090 | EXPECT_STREQ(GetParam().result_string, buffer); |
| 1091 | } |
| 1092 | |
| 1093 | // Tests how decimal2string() handles the case where the buffer is too small to |
| 1094 | // hold the full decimal value. |
| 1095 | static const DecimalToStringParam TO_STRING_OVERFLOW_TRUNCATE[] = { |
| 1096 | {"123.456", 2, "3", E_DEC_OVERFLOW}, |
| 1097 | {"123.456", 3, "23", E_DEC_OVERFLOW}, |
| 1098 | {"123.456", 4, "123", E_DEC_TRUNCATED}, |
| 1099 | {"123.456", 5, "123", E_DEC_TRUNCATED}, |
| 1100 | {"123.456", 6, "123.4", E_DEC_TRUNCATED}, |
| 1101 | {"123.456", 7, "123.45", E_DEC_TRUNCATED}, |
| 1102 | {"123.456", 8, "123.456", E_DEC_OK}, |
| 1103 | {"1230000", 3, "00", E_DEC_OVERFLOW}, |
| 1104 | {"1230000", 4, "000", E_DEC_OVERFLOW}, |
| 1105 | {"1230000", 5, "0000", E_DEC_OVERFLOW}, |
| 1106 | {"1230000", 6, "30000", E_DEC_OVERFLOW}, |
| 1107 | {"0.12345", 2, "0", E_DEC_TRUNCATED}, |
| 1108 | {"0.12345", 3, "0", E_DEC_TRUNCATED}, |
| 1109 | {"0.12345", 4, "0.1", E_DEC_TRUNCATED}, |
| 1110 | {"0.00000", 2, "0", E_DEC_TRUNCATED}, |
| 1111 | {"0.00000", 3, "0", E_DEC_TRUNCATED}, |
| 1112 | {"0.00000", 4, "0.0", E_DEC_TRUNCATED}, |
| 1113 | }; |
| 1114 | |
| 1115 | INSTANTIATE_TEST_SUITE_P(OverflowTruncate, DecimalToStringTest, |
| 1116 | testing::ValuesIn(TO_STRING_OVERFLOW_TRUNCATE)); |
| 1117 | |
| 1118 | } // namespace decimal_unittest |