|
| 1 | +/* This file is part of the LIBDECIMAL project and is licensed under the MIT License. |
| 2 | + * Copyright © 2025–2026 Varga Labs, Toronto, ON, Canada 🇨🇦 Contact: info@vargalabs.com */ |
| 3 | + |
| 4 | +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN |
| 5 | +#include <stdint.h> |
| 6 | +#include <doctest/all> |
| 7 | +#include <decimal.hpp> |
| 8 | +#include <stdfloat> |
| 9 | + |
| 10 | +#define TRACE std::cerr |
| 11 | +#define uint128_t __uint128_t |
| 12 | + |
| 13 | +struct value_t{ std::string a,b,c; }; |
| 14 | +using decimal_t = typename math::decimal_t<uint64_t>; |
| 15 | + |
| 16 | +TEST_CASE("pi") { |
| 17 | + using uint128_t = BID_UINT128; |
| 18 | + TRACE << math::sin(math::constants<uint32_t>::pi) << " " << math::constants<uint32_t>::pi << std::endl; |
| 19 | + TRACE << math::sin(math::constants<uint64_t>::pi) << " " << math::constants<uint64_t>::pi << std::endl; |
| 20 | + TRACE << math::sin(math::constants<uint128_t>::pi) << " " << math::constants<uint128_t>::pi << std::endl; |
| 21 | + TRACE << math::exp(math::constants<uint64_t>::zero) << std::endl; |
| 22 | + TRACE << math::exp(math::constants<uint32_t>::zero) << std::endl; |
| 23 | +} |
| 24 | + |
| 25 | +TEST_CASE("sin-32") { |
| 26 | + using namespace math::literals; |
| 27 | + using decimal_t = math::decimal_t<uint64_t>; |
| 28 | + std::pair<std::string,std::string> values[] = {{"0", "0"},{"0.5235988", "0.5000000"},{"0.7853982", "0.7071068"}, |
| 29 | + {"1.0471975", "0.8660254"},{"1.5707963", "1.0000000"},{"3.1415927", "0.0000000"},{"4.7123890", "-1.0000000"}, |
| 30 | + {"6.2831853", "0.0000000"},{"-0.5235988", "-0.5000000"},{"-0.7853982", "-0.7071068"},{"-1.0471975", "-0.8660254"}, |
| 31 | + {"-1.5707963", "-1.0000000"},{"-3.1415927", "0.0000000"},{"0.3926991", "0.3826834"},{"2.6179939", "0.5000000"}, |
| 32 | + {"3.6651914", "-0.5000000"},{"3.9269908", "-0.7071068"},{"2.3561945", "0.7071068"},{"5.7595865", "-0.5000000"},{"5.4977871", "-0.7071068"} }; |
| 33 | + for(auto& test : values){ |
| 34 | + std::cerr << test.second << " " << math::sin( decimal_t(test.first)) << std::endl; |
| 35 | + //CHECK(test.first == test.second); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +TEST_CASE("testing") { |
| 40 | + CHECK( static_cast<std::string>(math::decimal_t<uint64_t>("nan")) == "nan"); |
| 41 | + CHECK( static_cast<std::string>(math::decimal_t<uint64_t>("-inf")) == "-inf"); |
| 42 | + CHECK( static_cast<std::string>(math::decimal_t<uint64_t>("+inf")) == "+inf"); |
| 43 | +} |
| 44 | + |
| 45 | +TEST_CASE("bid64-to-string") { |
| 46 | + std::string test_values[] = |
| 47 | + {"1000000000000001","1.000000000000001","100.0000000000001","1000.000000000001", "10000.00000000001","100000.0000000001", |
| 48 | + "1000000.000000001","10000000.00000001","100000000.0000001", "1000000000.000001","10000000000.00001","100000000000.0001", |
| 49 | + "1000000000000.001","10000000000000.01","100000000000000.1", "nan", "-inf", "+inf"}; |
| 50 | + for(std::string value : test_values) |
| 51 | + CHECK( static_cast<std::string>(math::decimal_t<uint64_t>(value)) == value); |
| 52 | +} |
| 53 | +TEST_CASE("double to decimal") { |
| 54 | + using namespace math::literals; |
| 55 | + double test_values[] = {10.01, 1.001, 0.0001}; |
| 56 | + for(double value : test_values){ |
| 57 | + math::decimal_t<uint64_t> bid = value; |
| 58 | + TRACE << value << " " << bid.value << " " << bid << " " << static_cast<long double>(bid) << " " << static_cast<std::string>(0.1_dec + 0.3_dec) << std::endl; |
| 59 | + } |
| 60 | +} |
| 61 | +TEST_CASE("decimal_t<uint64_t> : a + b = c") { |
| 62 | + value_t test_values[] = { |
| 63 | + {"0.01","0.03","0.04"}, |
| 64 | + {"0.1","0.2","0.3"}}; |
| 65 | + for(value_t value : test_values){ |
| 66 | + math::decimal_t<uint64_t> A(value.a), B(value.b), C(value.c); |
| 67 | + TRACE << A << "+" << B << "=" << C << " " << (A+B) << std::endl; |
| 68 | + CHECK( static_cast<std::string>(A+B) == value.c); |
| 69 | + CHECK( A+B == C); |
| 70 | + } |
| 71 | +} |
| 72 | +TEST_CASE("decimal_t<T> literals") { |
| 73 | + using namespace math::literals; |
| 74 | + CHECK(static_cast<std::string>(0.1_dec + 0.3_dec) == "0.4"); |
| 75 | + CHECK(static_cast<std::string>(0.1_dec32 + 0.3_dec32) == "0.4"); |
| 76 | + CHECK(static_cast<std::string>(0.1_dec64 + 0.3_dec64) == "0.4"); |
| 77 | + //CHECK(static_cast<std::string>(0.1_dec128 + 0.3_dec128) == "0.4"); |
| 78 | +} |
| 79 | +TEST_CASE("decimal_t<T> operator") { |
| 80 | + using namespace math::literals; |
| 81 | + CHECK(0.1_dec < 0.2_dec); |
| 82 | + CHECK(0.2_dec > 0.01_dec); |
| 83 | + CHECK(0.2_dec >= 0.01_dec); |
| 84 | + CHECK(0.1_dec >= 0.1_dec); |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +TEST_CASE("bid64-python-testcases") { |
| 89 | + using namespace math::literals; |
| 90 | + using decimal_t = math::decimal_t<uint64_t>; |
| 91 | + |
| 92 | + decimal_t test_values[] = { |
| 93 | + 1.23456_dec64, 345677.1234_dec64, -12.2345678_dec64, -9999999999999999_dec64, 9999999999999999_dec64, |
| 94 | + decimal_t("NaN"), decimal_t("Inf"), decimal_t("-Inf") }; |
| 95 | + for (auto & decimal: test_values) |
| 96 | + TRACE << static_cast<std::string>(decimal) << " " << decimal.value << std::endl; |
| 97 | +} |
| 98 | +TEST_CASE("zeroes") { |
| 99 | + |
| 100 | + using namespace math::literals; |
| 101 | + TRACE << "ZERO: " << std::hex |
| 102 | + << (0.0_dec32).value << " " << (0.0_dec64).value << "(0.0_dec128).value " << std::endl; |
| 103 | +} |
| 104 | + |
| 105 | +TEST_CASE("from-integer") { |
| 106 | + using namespace math::literals; |
| 107 | + CHECK(static_cast<std::string>(0.0_dec32) == static_cast<std::string>(math::decimal_t<uint32_t>(0, -3)) ); |
| 108 | + CHECK(static_cast<std::string>(4.001_dec32) == static_cast<std::string>(math::decimal_t<uint32_t>(4001UL, -3)) ); |
| 109 | + CHECK(static_cast<std::string>(1.111111_dec32) == static_cast<std::string>(math::decimal_t<uint32_t>(1111111UL, -6)) ); |
| 110 | + CHECK(static_cast<std::string>(9.99999_dec32) == static_cast<std::string>(math::decimal_t<uint32_t>(999999UL, -5)) ); |
| 111 | + CHECK(static_cast<std::string>(8.999999_dec32) == static_cast<std::string>(math::decimal_t<uint32_t>(8999999UL, -6)) ); |
| 112 | + CHECK(static_cast<std::string>(9.999999_dec32) == static_cast<std::string>(math::decimal_t<uint32_t>(9999999UL, -6)) ); |
| 113 | + |
| 114 | + CHECK(static_cast<std::string>(0_dec64) == static_cast<std::string>(math::decimal_t<uint64_t>(0, -3)) ); |
| 115 | + CHECK(static_cast<std::string>(4.001_dec64) == static_cast<std::string>(math::decimal_t<uint64_t>(4001ULL, -3)) ); |
| 116 | + CHECK(static_cast<std::string>(9.99999999999999_dec64) == static_cast<std::string>(math::decimal_t<uint64_t>(999999999999999ULL, -14)) ); |
| 117 | + CHECK(static_cast<std::string>(9.999999999999999_dec64) == static_cast<std::string>(math::decimal_t<uint64_t>(9999999999999999ULL, -15)) ); |
| 118 | + |
| 119 | +} |
0 commit comments