Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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