Skip to content

Commit

Permalink
unit test: check comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulXiCao committed Dec 1, 2023
1 parent ecd8e46 commit 982e611
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions test-suite/money.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,75 @@ void MoneyTest::testAutomated() {
}
}

void MoneyTest::testComparisons() {
BOOST_TEST_MESSAGE("Testing money comparisons...");

for (const auto conversionType : {Money::AutomatedConversion, Money::NoConversion, Money::BaseCurrencyConversion}) {
ExchangeRateManager::instance().add(eur_usd);
ExchangeRateManager::instance().add(eur_gbp);
Money::Settings::instance().conversionType() = conversionType;
if (conversionType == Money::BaseCurrencyConversion)
Money::Settings::instance().baseCurrency() = EUR;

// equality
BOOST_CHECK_EQUAL(Money(123.45, EUR), Money(123.45, EUR));
if (conversionType != Money::NoConversion)
BOOST_CHECK_EQUAL(Money(1, EUR), Money(eur_usd.rate(), USD));

// unequal
BOOST_CHECK_NE(Money(1, EUR), Money(2, EUR));
if (conversionType != Money::NoConversion)
BOOST_CHECK_NE(Money(1, EUR), Money(100, USD));

// less than
BOOST_CHECK_LT(Money(1, EUR), Money(2, EUR));
if (conversionType != Money::NoConversion)
BOOST_CHECK_LT(Money(1, EUR), Money(100, USD));

// less or equal than
BOOST_CHECK_LE(Money(1, EUR), Money(2, EUR));
BOOST_CHECK_LE(Money(2, EUR), Money(2, EUR));
if (conversionType != Money::NoConversion)
BOOST_CHECK_LE(Money(1, EUR), Money(100, USD));

// greater than
BOOST_CHECK_GT(Money(2, EUR), Money(1, EUR));
if (conversionType != Money::NoConversion)
BOOST_CHECK_GT(Money(100, EUR), Money(1, USD));

// less or equal than
BOOST_CHECK_GE(Money(2, EUR), Money(1, EUR));
BOOST_CHECK_GE(Money(2, EUR), Money(2, EUR));
if (conversionType != Money::NoConversion)
BOOST_CHECK_GE(Money(100, EUR), Money(1, USD));

// close
BOOST_CHECK(close(Money(1, EUR), Money(1, EUR)));
BOOST_CHECK(close(Money(1+1e-15, EUR), Money(1, EUR)));
if (conversionType != Money::NoConversion){
BOOST_CHECK(close(Money(1, EUR), Money(eur_usd.rate(), USD)));
BOOST_CHECK(close(Money(1+1e-15, EUR), Money(eur_usd.rate(), USD)));
}

// close enough
BOOST_CHECK(close_enough(Money(1, EUR), Money(1, EUR)));
BOOST_CHECK(close_enough(Money(1+1e-15, EUR), Money(1, EUR)));
if (conversionType != Money::NoConversion){
BOOST_CHECK(close_enough(Money(1, EUR), Money(eur_usd.rate(), USD)));
BOOST_CHECK(close_enough(Money(1+1e-15, EUR), Money(eur_usd.rate(), USD)));
}

ExchangeRateManager::instance().clear();
Money::Settings::instance().conversionType() = Money::NoConversion;
}
}

test_suite* MoneyTest::suite() {
auto* suite = BOOST_TEST_SUITE("Money tests");
suite->add(QUANTLIB_TEST_CASE(&MoneyTest::testNone));
suite->add(QUANTLIB_TEST_CASE(&MoneyTest::testBaseCurrency));
suite->add(QUANTLIB_TEST_CASE(&MoneyTest::testAutomated));
suite->add(QUANTLIB_TEST_CASE(&MoneyTest::testComparisons));
return suite;
}

1 change: 1 addition & 0 deletions test-suite/money.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MoneyTest {
static void testNone();
static void testBaseCurrency();
static void testAutomated();
static void testComparisons();
static boost::unit_test_framework::test_suite* suite();
};

Expand Down

0 comments on commit 982e611

Please sign in to comment.