tests: Add tests for big_decimal type.

Signed-off-by: Daniel Fiala <daniel@scylladb.com>
This commit is contained in:
Daniel Fiala
2017-11-12 14:14:20 +01:00
parent 74c5f70b0a
commit ee1d69502b
3 changed files with 138 additions and 0 deletions

View File

@@ -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([

View File

@@ -91,6 +91,7 @@ boost_tests = [
'duration_test',
'loading_cache_test',
'castas_fcts_test',
'big_decimal_test',
]
other_tests = [

135
tests/big_decimal_test.cc Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_MODULE big_decimal
#include <boost/test/unit_test.hpp>
#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");
}