From ee1d69502b33cbadca274575cb5df7fb25508564 Mon Sep 17 00:00:00 2001 From: Daniel Fiala Date: Sun, 12 Nov 2017 14:14:20 +0100 Subject: [PATCH] tests: Add tests for big_decimal type. Signed-off-by: Daniel Fiala --- configure.py | 2 + test.py | 1 + tests/big_decimal_test.cc | 135 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 tests/big_decimal_test.cc diff --git a/configure.py b/configure.py index 286b528de8..84331bfd27 100755 --- a/configure.py +++ b/configure.py @@ -248,6 +248,7 @@ scylla_tests = [ 'tests/chunked_vector_test', 'tests/loading_cache_test', 'tests/castas_fcts_test', + 'tests/big_decimal_test', ] apps = [ @@ -640,6 +641,7 @@ pure_boost_tests = set([ 'tests/vint_serialization_test', 'tests/compress_test', 'tests/chunked_vector_test', + 'tests/big_decimal_test', ]) tests_not_using_seastar_test_framework = set([ diff --git a/test.py b/test.py index 3adf51e248..d3c3b0fe85 100755 --- a/test.py +++ b/test.py @@ -91,6 +91,7 @@ boost_tests = [ 'duration_test', 'loading_cache_test', 'castas_fcts_test', + 'big_decimal_test', ] other_tests = [ diff --git a/tests/big_decimal_test.cc b/tests/big_decimal_test.cc new file mode 100644 index 0000000000..c4369b45c9 --- /dev/null +++ b/tests/big_decimal_test.cc @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2017 ScyllaDB + */ + +/* + * This file is part of Scylla. + * + * Scylla is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * + * (at your option) any later version. + * + * Scylla is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Scylla. If not, see . + */ + +#define BOOST_TEST_MODULE big_decimal + +#include +#include "utils/big_decimal.hh" + +#include "disk-error-handler.hh" + +thread_local disk_error_signal_type commit_error; +thread_local disk_error_signal_type general_disk_error; + +namespace { + +void test_div(const char *r_cstr, const int64_t q, const char *expected_cstr) { + big_decimal r{r_cstr}; + auto res = r.div(q, big_decimal::rounding_mode::HALF_EVEN); + big_decimal expected{expected_cstr}; + BOOST_REQUIRE_EQUAL(res.unscaled_value(), expected.unscaled_value()); + BOOST_REQUIRE_EQUAL(res.scale(), expected.scale()); +} + +void test_assignadd(const char *x_cstr, const char *y_cstr, const char *expected_cstr) { + big_decimal x{x_cstr}; + big_decimal y{y_cstr}; + big_decimal expected{expected_cstr}; + x += y; + BOOST_REQUIRE_EQUAL(x.unscaled_value(), expected.unscaled_value()); + BOOST_REQUIRE_EQUAL(x.scale(), expected.scale()); +} + +} /* anonymous namespoce */ + +BOOST_AUTO_TEST_CASE(test_big_decimal_construct_from_string) { + big_decimal x0{"0"}; + big_decimal x1{"0.0"}; + big_decimal x2{"0.00"}; + big_decimal x3{"0.000"}; + big_decimal x4{"0E3"}; + big_decimal x5{"0E10"}; + + BOOST_REQUIRE_EQUAL(x0.unscaled_value(), 0); + BOOST_REQUIRE_EQUAL(x0.scale(), 0); + + BOOST_REQUIRE_EQUAL(x1.unscaled_value(), 0); + BOOST_REQUIRE_EQUAL(x1.scale(), 1); + + BOOST_REQUIRE_EQUAL(x2.unscaled_value(), 0); + BOOST_REQUIRE_EQUAL(x2.scale(), 2); + + BOOST_REQUIRE_EQUAL(x3.unscaled_value(), 0); + BOOST_REQUIRE_EQUAL(x3.scale(), 3); + + BOOST_REQUIRE_EQUAL(x4.unscaled_value(), 0); + BOOST_REQUIRE_EQUAL(x4.scale(), -3); + + BOOST_REQUIRE_EQUAL(x5.unscaled_value(), 0); + BOOST_REQUIRE_EQUAL(x5.scale(), -10); +} + +BOOST_AUTO_TEST_CASE(test_big_decimal_div) { + test_div("1", 4, "0"); + test_div("1.00", 4, "0.25"); + test_div("0.10", 4, "0.02"); + test_div("1.000", 4, "0.250"); + test_div("0.100", 4, "0.025"); + test_div("1", 3, "0"); + test_div("1.00", 3, "0.33"); + test_div("1.000", 3, "0.333"); + test_div("0.100", 3, "0.033"); + test_div("11", 10, "1"); + test_div("15", 10, "2"); + test_div("16", 10, "2"); + test_div("25", 10, "2"); + test_div("26", 10, "3"); + test_div("0.11", 10, "0.01"); + test_div("0.15", 10, "0.02"); + test_div("0.16", 10, "0.02"); + test_div("0.25", 10, "0.02"); + test_div("0.26", 10, "0.03"); + test_div("10E10", 3, "3E10"); + + test_div("-1", 4, "0"); + test_div("-1.00", 4, "-0.25"); + test_div("-0.10", 4, "-0.02"); + test_div("-1.000", 4, "-0.250"); + test_div("-0.100", 4, "-0.025"); + test_div("-1", 3, "0"); + test_div("-1.00", 3, "-0.33"); + test_div("-1.000", 3, "-0.333"); + test_div("-0.100", 3, "-0.033"); + test_div("-11", 10, "-1"); + test_div("-15", 10, "-2"); + test_div("-16", 10, "-2"); + test_div("-25", 10, "-2"); + test_div("-26", 10, "-3"); + test_div("-0.11", 10, "-0.01"); + test_div("-0.15", 10, "-0.02"); + test_div("-0.16", 10, "-0.02"); + test_div("-0.25", 10, "-0.02"); + test_div("-0.26", 10, "-0.03"); + test_div("-10E10", 3, "-3E10"); +} + +BOOST_AUTO_TEST_CASE(test_big_decimal_assignadd) { + test_assignadd("1", "4", "5"); + test_assignadd("1.00", "4.00", "5.00"); + test_assignadd("1.000", "4.000", "5.000"); + test_assignadd("1", "-1", "0"); + test_assignadd("1.00", "-1.00", "0.00"); + test_assignadd("1.000", "-1.000", "0.000"); + test_assignadd("0.0", "0.000", "0.000"); + test_assignadd("1.0", "1.000", "2.000"); + test_assignadd("-1.0", "-1.000", "-2.000"); +}