Skip to content

Commit

Permalink
add explicit int constructor (#30)
Browse files Browse the repository at this point in the history
* add explicit int constructor
  • Loading branch information
geseq authored Feb 9, 2025
1 parent 4e5d409 commit 0db5123
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions include/decimal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ class Decimal {

IntType fp = 0;

explicit Decimal(int i) { fp = static_cast<IntType>(i) * scale; }

Decimal(IntType fp = 0) : fp(fp) {}

static constexpr IntType scale = detail::const_pow<10, nPlaces>();
Expand Down
38 changes: 36 additions & 2 deletions test/decimal_test.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "decimal.hpp"

#include <gtest/gtest.h>

#include <cassert>
#include <cstdint>
#include <cwchar>
#include <iostream>

#include "decimal.hpp"

using decimal::Decimal;

class DecimalTest : public ::testing::Test {
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 0db5123

Please sign in to comment.