From 99149a061ff65e45bd7bbd04d823d81effb2be86 Mon Sep 17 00:00:00 2001 From: E Sequeira <5458743+geseq@users.noreply.github.com> Date: Sun, 9 Feb 2025 13:57:53 +0000 Subject: [PATCH 1/2] add explicit int constructor --- include/decimal.hpp | 2 ++ test/decimal_test.cpp | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/include/decimal.hpp b/include/decimal.hpp index ec58716..6ad7b29 100644 --- a/include/decimal.hpp +++ b/include/decimal.hpp @@ -152,6 +152,8 @@ class Decimal { IntType fp = 0; + explicit Decimal(int i) { fp = static_cast(i) * scale; } + Decimal(IntType fp = 0) : fp(fp) {} static constexpr IntType scale = detail::const_pow<10, nPlaces>(); diff --git a/test/decimal_test.cpp b/test/decimal_test.cpp index 5579d62..6f83923 100644 --- a/test/decimal_test.cpp +++ b/test/decimal_test.cpp @@ -1,3 +1,5 @@ +#include "decimal.hpp" + #include #include @@ -5,8 +7,6 @@ #include #include -#include "decimal.hpp" - using decimal::Decimal; class DecimalTest : public ::testing::Test { @@ -80,6 +80,40 @@ TEST_F(DecimalTest, BasicI8) { ASSERT_EQ(f0.to_string(), "0.999"); } +TEST_F(DecimalTest, IntConstructorU8) { + decimal::U8 f0(123); + ASSERT_EQ(f0.to_string(), "123"); + + decimal::U8 f1(0); + ASSERT_EQ(f1.to_string(), "0"); + + decimal::U8 f2(99999999); + ASSERT_EQ(f2.to_string(), "99999999"); + + ASSERT_EQ(f0.to_double(), 123.0); + ASSERT_EQ(f2.to_double(), 99999999.0); +} + +TEST_F(DecimalTest, IntConstructorI8) { + decimal::I8 f0(123); + ASSERT_EQ(f0.to_string(), "123"); + + decimal::I8 f1(-123); + ASSERT_EQ(f1.to_string(), "-123"); + + decimal::I8 f2(0); + ASSERT_EQ(f2.to_string(), "0"); + + decimal::I8 f3(9999999); + ASSERT_EQ(f3.to_string(), "9999999"); + + decimal::I8 f4(-9999999); + ASSERT_EQ(f4.to_string(), "-9999999"); + + ASSERT_EQ(f0.to_double(), 123.0); + ASSERT_EQ(f1.to_double(), -123.0); +} + TEST_F(DecimalTest, EqualU8) { decimal::U8 f0{}; decimal::U8 f1("123.456"); From 31e49e4db042888fb75b8dc9c1621d5f4962a36b Mon Sep 17 00:00:00 2001 From: E Sequeira <5458743+geseq@users.noreply.github.com> Date: Sun, 9 Feb 2025 14:02:57 +0000 Subject: [PATCH 2/2] cleanup --- .github/workflows/cmake.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index c0b91f2..b2b2853 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -33,4 +33,4 @@ jobs: # Build your program with the given configuration run: | cd build - ctest -C ${{env.BUILD_TYPE}} -L test --force-new-test-process -V --timeout 1800 + ctest -C ${{env.BUILD_TYPE}} -L test -V --timeout 1800