diff options
Diffstat (limited to 'tests/corelib')
-rw-r--r-- | tests/corelib/numerics/BasicAmount.cc | 117 | ||||
-rw-r--r-- | tests/corelib/numerics/BasicAmount.h | 6 | ||||
-rw-r--r-- | tests/corelib/numerics/CommodityAmount.cc | 552 | ||||
-rw-r--r-- | tests/corelib/numerics/CommodityAmount.h | 60 |
4 files changed, 732 insertions, 3 deletions
diff --git a/tests/corelib/numerics/BasicAmount.cc b/tests/corelib/numerics/BasicAmount.cc index 44246c40..16d8b1ea 100644 --- a/tests/corelib/numerics/BasicAmount.cc +++ b/tests/corelib/numerics/BasicAmount.cc @@ -24,6 +24,9 @@ void BasicAmountTestCase::testConstructors() amount_t x11(x8); assertEqual(amount_t(0L), x0); + assertEqual(amount_t(), x0); + assertEqual(amount_t("0"), x0); + assertEqual(amount_t("0.0"), x0); assertEqual(x2, x1); assertEqual(x5, x1); assertEqual(x7, x1); @@ -70,6 +73,16 @@ void BasicAmountTestCase::testNegation() x10.negate(); assertEqual(x3, x10); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x5.valid()); + CPPUNIT_ASSERT(x6.valid()); + CPPUNIT_ASSERT(x7.valid()); + CPPUNIT_ASSERT(x8.valid()); + CPPUNIT_ASSERT(x9.valid()); + CPPUNIT_ASSERT(x10.valid()); } void BasicAmountTestCase::testAssignment() @@ -117,6 +130,18 @@ void BasicAmountTestCase::testAssignment() assertEqual(x10, x3); assertEqual(amount_t(1L), x4); assertEqual(x10, x9); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(x5.valid()); + CPPUNIT_ASSERT(x6.valid()); + CPPUNIT_ASSERT(x7.valid()); + CPPUNIT_ASSERT(x8.valid()); + CPPUNIT_ASSERT(x9.valid()); + CPPUNIT_ASSERT(x10.valid()); } void BasicAmountTestCase::testEquality() @@ -134,6 +159,13 @@ void BasicAmountTestCase::testEquality() CPPUNIT_ASSERT(x1 == x4); CPPUNIT_ASSERT(x4 == x5); CPPUNIT_ASSERT(x4 == x6); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(x5.valid()); + CPPUNIT_ASSERT(x6.valid()); } void BasicAmountTestCase::testIntegerAddition() @@ -159,6 +191,12 @@ void BasicAmountTestCase::testIntegerAddition() amount_t x4("123456789123456789123456789"); assertEqual(amount_t("246913578246913578246913578"), x4 + x4); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(y3.valid()); + CPPUNIT_ASSERT(x4.valid()); } void BasicAmountTestCase::testFractionalAddition() @@ -180,6 +218,10 @@ void BasicAmountTestCase::testFractionalAddition() amount_t x2("123456789123456789.123456789123456789"); assertEqual(amount_t("246913578246913578.246913578246913578"), x2 + x2); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x2.valid()); } void BasicAmountTestCase::testIntegerSubtraction() @@ -207,6 +249,13 @@ void BasicAmountTestCase::testIntegerSubtraction() assertEqual(amount_t("123456789115218063137220803"), x4 - y4); assertEqual(amount_t("-123456789115218063137220803"), y4 - x4); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(y3.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(y4.valid()); } void BasicAmountTestCase::testFractionalSubtraction() @@ -229,6 +278,11 @@ void BasicAmountTestCase::testFractionalSubtraction() assertEqual(amount_t("123446916777474329.874482549545456789"), x2 - y2); assertEqual(amount_t("-123446916777474329.874482549545456789"), y2 - x2); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(y2.valid()); } void BasicAmountTestCase::testIntegerMultiplication() @@ -265,6 +319,12 @@ void BasicAmountTestCase::testIntegerMultiplication() assertEqual(amount_t("15241578780673678546105778281054720515622620750190521"), x4 * x4); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(y3.valid()); + CPPUNIT_ASSERT(x4.valid()); } void BasicAmountTestCase::testFractionalMultiplication() @@ -298,6 +358,10 @@ void BasicAmountTestCase::testFractionalMultiplication() assertEqual(amount_t("15241578780673678546105778311537878.046486820281054720515622620750190521"), x2 * x2); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x2.valid()); } void BasicAmountTestCase::testIntegerDivision() @@ -330,6 +394,11 @@ void BasicAmountTestCase::testIntegerDivision() assertEqual(amount_t(1L), x4 / x4); assertEqual(amount_t("2204585520061728377204585.517857"), x4 / y4); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(y4.valid()); } void BasicAmountTestCase::testFractionalDivision() @@ -365,6 +434,11 @@ void BasicAmountTestCase::testFractionalDivision() assertEqual(amount_t(1.0), x4 / x4); assertEqual(amount_t("21739560323910.7554497273748437197344556164"), x4 / y4); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(y4.valid()); } void BasicAmountTestCase::testIntegerConversion() @@ -376,6 +450,8 @@ void BasicAmountTestCase::testIntegerConversion() assertEqual(123456.0, double(x1)); assertEqual(std::string("123456"), x1.to_string()); assertEqual(std::string("123456"), x1.quantity_string()); + + CPPUNIT_ASSERT(x1.valid()); } void BasicAmountTestCase::testFractionalConversion() @@ -387,6 +463,8 @@ void BasicAmountTestCase::testFractionalConversion() assertEqual(1234.56, double(x1)); assertEqual(std::string("1234.56"), x1.to_string()); assertEqual(std::string("1234.56"), x1.quantity_string()); + + CPPUNIT_ASSERT(x1.valid()); } void BasicAmountTestCase::testFractionalRound() @@ -430,6 +508,17 @@ void BasicAmountTestCase::testFractionalRound() assertEqual(amount_t("-9876.54"), x4.round(2)); assertEqual(amount_t("-9876.5"), x4.round(1)); assertEqual(amount_t("-9877"), x4.round(0)); + + amount_t x5("0.0000000000000000000000000000000000001"); + + assertEqual(amount_t("0.0000000000000000000000000000000000001"), + x5.round(37)); + assertEqual(amount_t(), x5.round(36)); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x4.valid()); } void BasicAmountTestCase::testTruth() @@ -452,6 +541,10 @@ void BasicAmountTestCase::testTruth() CPPUNIT_ASSERT(true); else CPPUNIT_ASSERT(false); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); } void BasicAmountTestCase::testForZero() @@ -465,6 +558,9 @@ void BasicAmountTestCase::testForZero() CPPUNIT_ASSERT(x0.realzero()); CPPUNIT_ASSERT(! x1.zero()); CPPUNIT_ASSERT(! x1.realzero()); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); } void BasicAmountTestCase::testComparisons() @@ -489,6 +585,14 @@ void BasicAmountTestCase::testComparisons() CPPUNIT_ASSERT(x3 >= x5); CPPUNIT_ASSERT(x3 < x1); CPPUNIT_ASSERT(x3 < x4); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(x5.valid()); + CPPUNIT_ASSERT(x6.valid()); } void BasicAmountTestCase::testSign() @@ -504,6 +608,12 @@ void BasicAmountTestCase::testSign() CPPUNIT_ASSERT(x2.sign() < 0); CPPUNIT_ASSERT(x3.sign() > 0); CPPUNIT_ASSERT(x4.sign() < 0); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x4.valid()); } void BasicAmountTestCase::testAbs() @@ -523,6 +633,10 @@ void BasicAmountTestCase::testAbs() assertEqual(amount_t(), x0); assertEqual(amount_t(1234L), x1); assertEqual(amount_t(1234L), x2); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); } void BasicAmountTestCase::testPrinting() @@ -544,4 +658,7 @@ void BasicAmountTestCase::testPrinting() assertEqual(std::string("982340823.380238098235098235098235098"), bufstr.str()); } + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); } diff --git a/tests/corelib/numerics/BasicAmount.h b/tests/corelib/numerics/BasicAmount.h index cff3bed8..2c107f45 100644 --- a/tests/corelib/numerics/BasicAmount.h +++ b/tests/corelib/numerics/BasicAmount.h @@ -1,5 +1,5 @@ -#ifndef _BASICAMOUNTTEST_H -#define _BASICAMOUNTTEST_H +#ifndef _BASICAMOUNT_H +#define _BASICAMOUNT_H #include "UnitTests.h" @@ -65,4 +65,4 @@ private: void operator=(const BasicAmountTestCase ©); }; -#endif /* _BASICAMOUNTTEST_H */ +#endif /* _BASICAMOUNT_H */ diff --git a/tests/corelib/numerics/CommodityAmount.cc b/tests/corelib/numerics/CommodityAmount.cc new file mode 100644 index 00000000..1862b280 --- /dev/null +++ b/tests/corelib/numerics/CommodityAmount.cc @@ -0,0 +1,552 @@ +#include "CommodityAmount.h" +#include "ledger.h" + +using namespace ledger; + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CommodityAmountTestCase, "numerics"); + +inline amount_t internalAmount(const std::string& value) { + amount_t temp; + temp.parse(value, AMOUNT_PARSE_NO_MIGRATE); + return temp; +} + +void CommodityAmountTestCase::setUp() {} +void CommodityAmountTestCase::tearDown() {} + +void CommodityAmountTestCase::testConstructors() +{ + amount_t x1("$123.45"); + amount_t x2("-$123.45"); + amount_t x3("$-123.45"); + amount_t x4("DM 123.45"); + amount_t x5("-DM 123.45"); + amount_t x6("DM -123.45"); + amount_t x7("123.45 euro"); + amount_t x8("-123.45 euro"); + amount_t x9("123.45€"); + amount_t x10("-123.45€"); + + assertEqual(amount_t("$123.45"), x1); + assertEqual(amount_t("-$123.45"), x2); + assertEqual(amount_t("$-123.45"), x3); + assertEqual(amount_t("DM 123.45"), x4); + assertEqual(amount_t("-DM 123.45"), x5); + assertEqual(amount_t("DM -123.45"), x6); + assertEqual(amount_t("123.45 euro"), x7); + assertEqual(amount_t("-123.45 euro"), x8); + assertEqual(amount_t("123.45€"), x9); + assertEqual(amount_t("-123.45€"), x10); + + assertEqual(std::string("$123.45"), x1.to_string()); + assertEqual(std::string("$-123.45"), x2.to_string()); + assertEqual(std::string("$-123.45"), x3.to_string()); + assertEqual(std::string("DM 123.45"), x4.to_string()); + assertEqual(std::string("DM -123.45"), x5.to_string()); + assertEqual(std::string("DM -123.45"), x6.to_string()); + assertEqual(std::string("123.45 euro"), x7.to_string()); + assertEqual(std::string("-123.45 euro"), x8.to_string()); + assertEqual(std::string("123.45€"), x9.to_string()); + assertEqual(std::string("-123.45€"), x10.to_string()); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(x5.valid()); + CPPUNIT_ASSERT(x6.valid()); + CPPUNIT_ASSERT(x7.valid()); + CPPUNIT_ASSERT(x8.valid()); + CPPUNIT_ASSERT(x9.valid()); + CPPUNIT_ASSERT(x10.valid()); +} + +void CommodityAmountTestCase::testNegation() +{ + amount_t x1("$123.45"); + amount_t x2("-$123.45"); + amount_t x3("$-123.45"); + amount_t x4("DM 123.45"); + amount_t x5("-DM 123.45"); + amount_t x6("DM -123.45"); + amount_t x7("123.45 euro"); + amount_t x8("-123.45 euro"); + amount_t x9("123.45€"); + amount_t x10("-123.45€"); + + assertEqual(amount_t("$-123.45"), - x1); + assertEqual(amount_t("$123.45"), - x2); + assertEqual(amount_t("$123.45"), - x3); + assertEqual(amount_t("DM -123.45"), - x4); + assertEqual(amount_t("DM 123.45"), - x5); + assertEqual(amount_t("DM 123.45"), - x6); + assertEqual(amount_t("-123.45 euro"), - x7); + assertEqual(amount_t("123.45 euro"), - x8); + assertEqual(amount_t("-123.45€"), - x9); + assertEqual(amount_t("123.45€"), - x10); + + assertEqual(std::string("$-123.45"), (- x1).to_string()); + assertEqual(std::string("$123.45"), (- x2).to_string()); + assertEqual(std::string("$123.45"), (- x3).to_string()); + assertEqual(std::string("DM -123.45"), (- x4).to_string()); + assertEqual(std::string("DM 123.45"), (- x5).to_string()); + assertEqual(std::string("DM 123.45"), (- x6).to_string()); + assertEqual(std::string("-123.45 euro"), (- x7).to_string()); + assertEqual(std::string("123.45 euro"), (- x8).to_string()); + assertEqual(std::string("-123.45€"), (- x9).to_string()); + assertEqual(std::string("123.45€"), (- x10).to_string()); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(x5.valid()); + CPPUNIT_ASSERT(x6.valid()); + CPPUNIT_ASSERT(x7.valid()); + CPPUNIT_ASSERT(x8.valid()); + CPPUNIT_ASSERT(x9.valid()); + CPPUNIT_ASSERT(x10.valid()); +} + +void CommodityAmountTestCase::testAssignment() +{ + amount_t x1 = "$123.45"; + amount_t x2 = "-$123.45"; + amount_t x3 = "$-123.45"; + amount_t x4 = "DM 123.45"; + amount_t x5 = "-DM 123.45"; + amount_t x6 = "DM -123.45"; + amount_t x7 = "123.45 euro"; + amount_t x8 = "-123.45 euro"; + amount_t x9 = "123.45€"; + amount_t x10 = "-123.45€"; + + assertEqual(amount_t("$123.45"), x1); + assertEqual(amount_t("-$123.45"), x2); + assertEqual(amount_t("$-123.45"), x3); + assertEqual(amount_t("DM 123.45"), x4); + assertEqual(amount_t("-DM 123.45"), x5); + assertEqual(amount_t("DM -123.45"), x6); + assertEqual(amount_t("123.45 euro"), x7); + assertEqual(amount_t("-123.45 euro"), x8); + assertEqual(amount_t("123.45€"), x9); + assertEqual(amount_t("-123.45€"), x10); + + assertEqual(std::string("$123.45"), x1.to_string()); + assertEqual(std::string("$-123.45"), x2.to_string()); + assertEqual(std::string("$-123.45"), x3.to_string()); + assertEqual(std::string("DM 123.45"), x4.to_string()); + assertEqual(std::string("DM -123.45"), x5.to_string()); + assertEqual(std::string("DM -123.45"), x6.to_string()); + assertEqual(std::string("123.45 euro"), x7.to_string()); + assertEqual(std::string("-123.45 euro"), x8.to_string()); + assertEqual(std::string("123.45€"), x9.to_string()); + assertEqual(std::string("-123.45€"), x10.to_string()); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(x5.valid()); + CPPUNIT_ASSERT(x6.valid()); + CPPUNIT_ASSERT(x7.valid()); + CPPUNIT_ASSERT(x8.valid()); + CPPUNIT_ASSERT(x9.valid()); + CPPUNIT_ASSERT(x10.valid()); +} + +void CommodityAmountTestCase::testEquality() +{ + amount_t x1 = "$123.45"; + amount_t x2 = "-$123.45"; + amount_t x3 = "$-123.45"; + amount_t x4 = "DM 123.45"; + amount_t x5 = "-DM 123.45"; + amount_t x6 = "DM -123.45"; + amount_t x7 = "123.45 euro"; + amount_t x8 = "-123.45 euro"; + amount_t x9 = "123.45€"; + amount_t x10 = "-123.45€"; + + CPPUNIT_ASSERT(x1 != x2); + CPPUNIT_ASSERT(x1 != x4); + CPPUNIT_ASSERT(x1 != x7); + CPPUNIT_ASSERT(x1 != x9); + CPPUNIT_ASSERT(x2 == x3); + CPPUNIT_ASSERT(x4 != x5); + CPPUNIT_ASSERT(x5 == x6); + CPPUNIT_ASSERT(x7 == - x8); + CPPUNIT_ASSERT(x9 == - x10); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(x5.valid()); + CPPUNIT_ASSERT(x6.valid()); + CPPUNIT_ASSERT(x7.valid()); + CPPUNIT_ASSERT(x8.valid()); + CPPUNIT_ASSERT(x9.valid()); + CPPUNIT_ASSERT(x10.valid()); +} + +void CommodityAmountTestCase::testAddition() +{ + // jww (2007-04-16): tbd + amount_t x1(123.123); + amount_t y1(456.456); + + assertEqual(amount_t(579.579), x1 + y1); + assertEqual(amount_t(579.579), x1 + 456.456); + assertEqual(amount_t(579.579), 456.456 + x1); + + x1 += amount_t(456.456); + assertEqual(amount_t(579.579), x1); + x1 += 456.456; + assertEqual(amount_t(1036.035), x1); + x1 += 456L; + assertEqual(amount_t(1492.035), x1); + + amount_t x2("123456789123456789.123456789123456789"); + + assertEqual(amount_t("246913578246913578.246913578246913578"), x2 + x2); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x2.valid()); +} + +void CommodityAmountTestCase::testSubtraction() +{ + // jww (2007-04-16): tbd + amount_t x1(123.123); + amount_t y1(456.456); + + assertEqual(amount_t(-333.333), x1 - y1); + assertEqual(amount_t(333.333), y1 - x1); + + x1 -= amount_t(456.456); + assertEqual(amount_t(-333.333), x1); + x1 -= 456.456; + assertEqual(amount_t(-789.789), x1); + x1 -= 456L; + assertEqual(amount_t(-1245.789), x1); + + amount_t x2("123456789123456789.123456789123456789"); + amount_t y2("9872345982459.248974239578"); + + assertEqual(amount_t("123446916777474329.874482549545456789"), x2 - y2); + assertEqual(amount_t("-123446916777474329.874482549545456789"), y2 - x2); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(y2.valid()); +} + +void CommodityAmountTestCase::testMultiplication() +{ + // jww (2007-04-16): tbd + amount_t x1(123.123); + amount_t y1(456.456); + + assertEqual(amount_t(0L), x1 * 0L); + assertEqual(amount_t(0L), amount_t(0L) * x1); + assertEqual(amount_t(0L), 0L * x1); + assertEqual(x1, x1 * 1L); + assertEqual(x1, amount_t(1L) * x1); + assertEqual(x1, 1L * x1); + assertEqual(- x1, x1 * -1L); + assertEqual(- x1, amount_t(-1L) * x1); + assertEqual(- x1, -1L * x1); + assertEqual(amount_t("56200.232088"), x1 * y1); + assertEqual(amount_t("56200.232088"), y1 * x1); + assertEqual(amount_t("56200.232088"), x1 * 456.456); + assertEqual(amount_t("56200.232088"), amount_t(456.456) * x1); + assertEqual(amount_t("56200.232088"), 456.456 * x1); + + x1 *= amount_t(123.123); + assertEqual(amount_t("15159.273129"), x1); + x1 *= 123.123; + assertEqual(amount_t("1866455.185461867"), x1); + x1 *= 123L; + assertEqual(amount_t("229573987.811809641"), x1); + + amount_t x2("123456789123456789.123456789123456789"); + + assertEqual(amount_t("15241578780673678546105778311537878.046486820281054720515622620750190521"), + x2 * x2); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x2.valid()); +} + +void CommodityAmountTestCase::testDivision() +{ + // jww (2007-04-16): tbd + amount_t x1(123.123); + amount_t y1(456.456); + + assertThrow(x1 / 0L, amount_error *); + assertEqual(amount_t("0.008121"), amount_t(1.0) / x1); + assertEqual(amount_t("0.008121"), 1.0 / x1); + assertEqual(x1, x1 / 1.0); + assertEqual(amount_t("0.008121"), amount_t(1.0) / x1); + assertEqual(amount_t("0.008121"), 1.0 / x1); + assertEqual(- x1, x1 / -1.0); + assertEqual(- amount_t("0.008121"), amount_t(-1.0) / x1); + assertEqual(- amount_t("0.008121"), -1.0 / x1); + assertEqual(amount_t("0.269736842105"), x1 / y1); + assertEqual(amount_t("3.707317073170"), y1 / x1); + assertEqual(amount_t("0.269736842105"), x1 / 456.456); + assertEqual(amount_t("3.707317073170"), amount_t(456.456) / x1); + assertEqual(amount_t("3.707317073170"), 456.456 / x1); + + x1 /= amount_t(456.456); + assertEqual(amount_t("0.269736842105"), x1); + x1 /= 456.456; + assertEqual(amount_t("0.0005909372252856792330476541"), x1); + x1 /= 456L; + assertEqual(amount_t("0.00000129591496773175270405187302631578947368421052631578947368421"), x1); + + amount_t x4("1234567891234567.89123456789"); + amount_t y4("56.789"); + + assertEqual(amount_t(1.0), x4 / x4); + assertEqual(amount_t("21739560323910.7554497273748437197344556164"), + x4 / y4); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(y4.valid()); +} + +void CommodityAmountTestCase::testConversion() +{ + // jww (2007-04-16): tbd + amount_t x1(1234.56); + + assertEqual(true, bool(x1)); + assertEqual(1234L, long(x1)); + assertEqual(1234.56, double(x1)); + assertEqual(std::string("1234.56"), x1.to_string()); + assertEqual(std::string("1234.56"), x1.quantity_string()); + + CPPUNIT_ASSERT(x1.valid()); +} + +void CommodityAmountTestCase::testRound() +{ + // jww (2007-04-16): tbd + amount_t x1("1234.567890"); + + assertEqual(amount_t("1234.56789"), x1.round(6)); + assertEqual(amount_t("1234.56789"), x1.round(5)); + assertEqual(amount_t("1234.5679"), x1.round(4)); + assertEqual(amount_t("1234.568"), x1.round(3)); + assertEqual(amount_t("1234.57"), x1.round(2)); + assertEqual(amount_t("1234.6"), x1.round(1)); + assertEqual(amount_t("1235"), x1.round(0)); + + amount_t x2("9876.543210"); + + assertEqual(amount_t("9876.543210"), x2.round(6)); + assertEqual(amount_t("9876.54321"), x2.round(5)); + assertEqual(amount_t("9876.5432"), x2.round(4)); + assertEqual(amount_t("9876.543"), x2.round(3)); + assertEqual(amount_t("9876.54"), x2.round(2)); + assertEqual(amount_t("9876.5"), x2.round(1)); + assertEqual(amount_t("9877"), x2.round(0)); + + amount_t x3("-1234.567890"); + + assertEqual(amount_t("-1234.56789"), x3.round(6)); + assertEqual(amount_t("-1234.56789"), x3.round(5)); + assertEqual(amount_t("-1234.5679"), x3.round(4)); + assertEqual(amount_t("-1234.568"), x3.round(3)); + assertEqual(amount_t("-1234.57"), x3.round(2)); + assertEqual(amount_t("-1234.6"), x3.round(1)); + assertEqual(amount_t("-1235"), x3.round(0)); + + amount_t x4("-9876.543210"); + + assertEqual(amount_t("-9876.543210"), x4.round(6)); + assertEqual(amount_t("-9876.54321"), x4.round(5)); + assertEqual(amount_t("-9876.5432"), x4.round(4)); + assertEqual(amount_t("-9876.543"), x4.round(3)); + assertEqual(amount_t("-9876.54"), x4.round(2)); + assertEqual(amount_t("-9876.5"), x4.round(1)); + assertEqual(amount_t("-9877"), x4.round(0)); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x4.valid()); +} + +void CommodityAmountTestCase::testDisplayRound() +{ + amount_t x1("$0.85"); + + x1 *= 0.19; + + CPPUNIT_ASSERT(amount_t("$0.16") != x1); + assertEqual(internalAmount("$0.1615"), x1); + assertEqual(std::string("$0.16"), x1.to_string()); + + x1 *= 7L; + + CPPUNIT_ASSERT(amount_t("$1.13") != x1); + assertEqual(internalAmount("$1.1305"), x1); + assertEqual(std::string("$1.13"), x1.to_string()); +} + +void CommodityAmountTestCase::testTruth() +{ + // jww (2007-04-16): tbd + amount_t x0; + amount_t x1("1234"); + amount_t x2("1234.56"); + + if (x0) + CPPUNIT_ASSERT(false); + else + CPPUNIT_ASSERT(true); + + if (x1) + CPPUNIT_ASSERT(true); + else + CPPUNIT_ASSERT(false); + + if (x2) + CPPUNIT_ASSERT(true); + else + CPPUNIT_ASSERT(false); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); +} + +void CommodityAmountTestCase::testForZero() +{ + // jww (2007-04-16): tbd + amount_t x0; + amount_t x1("0.000000000000000000001"); + + CPPUNIT_ASSERT(! x0); + CPPUNIT_ASSERT(x1); + CPPUNIT_ASSERT(x0.zero()); + CPPUNIT_ASSERT(x0.realzero()); + CPPUNIT_ASSERT(! x1.zero()); + CPPUNIT_ASSERT(! x1.realzero()); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); +} + +void CommodityAmountTestCase::testComparisons() +{ + // jww (2007-04-16): tbd + amount_t x0; + amount_t x1(-123L); + amount_t x2(123L); + amount_t x3(-123.45); + amount_t x4(123.45); + amount_t x5("-123.45"); + amount_t x6("123.45"); + + CPPUNIT_ASSERT(x0 > x1); + CPPUNIT_ASSERT(x0 < x2); + CPPUNIT_ASSERT(x0 > x3); + CPPUNIT_ASSERT(x0 < x4); + CPPUNIT_ASSERT(x0 > x5); + CPPUNIT_ASSERT(x0 < x6); + + CPPUNIT_ASSERT(x1 > x3); + CPPUNIT_ASSERT(x3 <= x5); + CPPUNIT_ASSERT(x3 >= x5); + CPPUNIT_ASSERT(x3 < x1); + CPPUNIT_ASSERT(x3 < x4); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(x5.valid()); + CPPUNIT_ASSERT(x6.valid()); +} + +void CommodityAmountTestCase::testSign() +{ + // jww (2007-04-16): tbd + amount_t x0; + amount_t x1("0.0000000000000000000000000000000000001"); + amount_t x2("-0.0000000000000000000000000000000000001"); + amount_t x3("1"); + amount_t x4("-1"); + + CPPUNIT_ASSERT(! x0.sign()); + CPPUNIT_ASSERT(x1.sign() > 0); + CPPUNIT_ASSERT(x2.sign() < 0); + CPPUNIT_ASSERT(x3.sign() > 0); + CPPUNIT_ASSERT(x4.sign() < 0); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); + CPPUNIT_ASSERT(x3.valid()); + CPPUNIT_ASSERT(x4.valid()); +} + +void CommodityAmountTestCase::testAbs() +{ + // jww (2007-04-16): tbd + amount_t x0; + amount_t x1(-1234L); + amount_t x2(1234L); + + assertEqual(amount_t(), abs(x0)); + assertEqual(amount_t(1234L), abs(x1)); + assertEqual(amount_t(1234L), abs(x2)); + + x0.abs(); + x1.abs(); + x2.abs(); + + assertEqual(amount_t(), x0); + assertEqual(amount_t(1234L), x1); + assertEqual(amount_t(1234L), x2); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); +} + +void CommodityAmountTestCase::testPrinting() +{ + // jww (2007-04-16): tbd + amount_t x0; + amount_t x1("982340823.380238098235098235098235098"); + + { + std::ostringstream bufstr; + bufstr << x0; + + assertEqual(std::string("0"), bufstr.str()); + } + + { + std::ostringstream bufstr; + bufstr << x1; + + assertEqual(std::string("982340823.380238098235098235098235098"), + bufstr.str()); + } + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); +} diff --git a/tests/corelib/numerics/CommodityAmount.h b/tests/corelib/numerics/CommodityAmount.h new file mode 100644 index 00000000..5ffa7810 --- /dev/null +++ b/tests/corelib/numerics/CommodityAmount.h @@ -0,0 +1,60 @@ +#ifndef _COMMODITYAMOUNT_H +#define _COMMODITYAMOUNT_H + +#include "UnitTests.h" + +class CommodityAmountTestCase : public CPPUNIT_NS::TestCase +{ + CPPUNIT_TEST_SUITE(CommodityAmountTestCase); + + CPPUNIT_TEST(testConstructors); + CPPUNIT_TEST(testNegation); + CPPUNIT_TEST(testAssignment); + CPPUNIT_TEST(testEquality); + CPPUNIT_TEST(testAddition); + CPPUNIT_TEST(testSubtraction); + CPPUNIT_TEST(testMultiplication); + CPPUNIT_TEST(testDivision); + CPPUNIT_TEST(testConversion); + CPPUNIT_TEST(testRound); + CPPUNIT_TEST(testDisplayRound); + CPPUNIT_TEST(testTruth); + CPPUNIT_TEST(testForZero); + CPPUNIT_TEST(testComparisons); + CPPUNIT_TEST(testSign); + CPPUNIT_TEST(testAbs); + CPPUNIT_TEST(testPrinting); + + CPPUNIT_TEST_SUITE_END(); + +public: + CommodityAmountTestCase() {} + virtual ~CommodityAmountTestCase() {} + + virtual void setUp(); + virtual void tearDown(); + + void testConstructors(); + void testNegation(); + void testAssignment(); + void testEquality(); + void testAddition(); + void testSubtraction(); + void testMultiplication(); + void testDivision(); + void testConversion(); + void testRound(); + void testDisplayRound(); + void testTruth(); + void testForZero(); + void testComparisons(); + void testSign(); + void testAbs(); + void testPrinting(); + +private: + CommodityAmountTestCase(const CommodityAmountTestCase ©); + void operator=(const CommodityAmountTestCase ©); +}; + +#endif /* _COMMODITYAMOUNT_H */ |