Skip to content

Commit

Permalink
Add GncRational test for GncRational::round_to_numeric().
Browse files Browse the repository at this point in the history
Needed GncNumeric to be defined.
  • Loading branch information
jralls committed Feb 20, 2017
1 parent 4a46ae3 commit c3d22c4
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/libqof/qof/test/gtest-gnc-rational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
*******************************************************************/

#include <gtest/gtest.h>
#include <random>
#include "../gnc-rational.hpp"
#include "../gnc-numeric.hpp" //for RoundType

TEST(gncrational_constructors, test_default_constructor)
{
Expand Down Expand Up @@ -117,6 +119,7 @@ TEST(gncrational_operators, test_multiplication)
UINT64_C(8081008345983448486)), a.m_num);
EXPECT_EQ (100000000000000000, a.m_den);
EXPECT_EQ (GNC_ERROR_OK, a.m_error);

}

TEST(gncrational_operators, test_division)
Expand All @@ -138,3 +141,28 @@ TEST(gncrational_operators, test_division)
EXPECT_EQ (GNC_ERROR_OK, c.m_error);

}

TEST(gncrational_functions, test_round_to_numeric)
{
std::default_random_engine dre;
std::uniform_int_distribution<int64_t> di{INT64_C(0x10000000000000),
INT64_C(0x7fffffffffffff)};
static const int reps{25};
for (auto i = 0; i < reps; ++i)
{
GncRational a(di(dre), di(dre));
GncRational b(di(dre), 100);
auto c = a * b;
auto expected = c;
expected.round(100, RoundType::bankers);
auto rounded = c.round_to_numeric();
rounded.round(100, RoundType::bankers);
EXPECT_EQ(0, expected.m_num - rounded.m_num);
EXPECT_FALSE(rounded.m_num.isBig());
EXPECT_FALSE(rounded.m_den.isBig());
EXPECT_FALSE(rounded.m_num.isNan());
EXPECT_FALSE(rounded.m_den.isNan());
EXPECT_FALSE(rounded.m_num.isOverflow());
EXPECT_FALSE(rounded.m_den.isOverflow());
}
}

0 comments on commit c3d22c4

Please sign in to comment.