From c8899addfd2deed3d84be2de234681db64987722 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Mon, 30 Apr 2007 06:26:38 +0000 Subject: Rearranged the sources a bit. --- tests/corelib/numerics/BasicAmount.cc | 625 -------------------- tests/corelib/numerics/BasicAmount.h | 68 --- tests/corelib/numerics/Commodity.cc | 55 -- tests/corelib/numerics/Commodity.h | 34 -- tests/corelib/numerics/CommodityAmount.cc | 696 ----------------------- tests/corelib/numerics/CommodityAmount.h | 60 -- tests/corelib/numerics/DateTime.cc | 79 --- tests/corelib/numerics/DateTimeTest.h | 28 - tests/numerics/BasicAmount.cc | 625 ++++++++++++++++++++ tests/numerics/BasicAmount.h | 68 +++ tests/numerics/Commodity.cc | 55 ++ tests/numerics/Commodity.h | 34 ++ tests/numerics/CommodityAmount.cc | 696 +++++++++++++++++++++++ tests/numerics/CommodityAmount.h | 60 ++ tests/numerics/DateTime.cc | 79 +++ tests/numerics/DateTimeTest.h | 28 + tests/python/PyUnitTests.py | 4 + tests/python/UnitTests.py | 4 +- tests/python/corelib/.gitignore | 1 - tests/python/corelib/__init__.py | 0 tests/python/corelib/balances/__init__.py | 0 tests/python/corelib/numerics/.gitignore | 1 - tests/python/corelib/numerics/BasicAmount.py | 527 ----------------- tests/python/corelib/numerics/CommodityAmount.py | 623 -------------------- tests/python/corelib/numerics/__init__.py | 0 tests/python/corelib/values/__init__.py | 0 tests/python/numerics/.gitignore | 1 + tests/python/numerics/BasicAmount.py | 527 +++++++++++++++++ tests/python/numerics/CommodityAmount.py | 623 ++++++++++++++++++++ tests/python/numerics/__init__.py | 0 tests/python/numerics/balances/__init__.py | 0 tests/python/numerics/values/__init__.py | 0 32 files changed, 2802 insertions(+), 2799 deletions(-) delete mode 100644 tests/corelib/numerics/BasicAmount.cc delete mode 100644 tests/corelib/numerics/BasicAmount.h delete mode 100644 tests/corelib/numerics/Commodity.cc delete mode 100644 tests/corelib/numerics/Commodity.h delete mode 100644 tests/corelib/numerics/CommodityAmount.cc delete mode 100644 tests/corelib/numerics/CommodityAmount.h delete mode 100644 tests/corelib/numerics/DateTime.cc delete mode 100644 tests/corelib/numerics/DateTimeTest.h create mode 100644 tests/numerics/BasicAmount.cc create mode 100644 tests/numerics/BasicAmount.h create mode 100644 tests/numerics/Commodity.cc create mode 100644 tests/numerics/Commodity.h create mode 100644 tests/numerics/CommodityAmount.cc create mode 100644 tests/numerics/CommodityAmount.h create mode 100644 tests/numerics/DateTime.cc create mode 100644 tests/numerics/DateTimeTest.h create mode 100755 tests/python/PyUnitTests.py delete mode 100644 tests/python/corelib/.gitignore delete mode 100644 tests/python/corelib/__init__.py delete mode 100644 tests/python/corelib/balances/__init__.py delete mode 100644 tests/python/corelib/numerics/.gitignore delete mode 100644 tests/python/corelib/numerics/BasicAmount.py delete mode 100644 tests/python/corelib/numerics/CommodityAmount.py delete mode 100644 tests/python/corelib/numerics/__init__.py delete mode 100644 tests/python/corelib/values/__init__.py create mode 100644 tests/python/numerics/.gitignore create mode 100644 tests/python/numerics/BasicAmount.py create mode 100644 tests/python/numerics/CommodityAmount.py create mode 100644 tests/python/numerics/__init__.py create mode 100644 tests/python/numerics/balances/__init__.py create mode 100644 tests/python/numerics/values/__init__.py (limited to 'tests') diff --git a/tests/corelib/numerics/BasicAmount.cc b/tests/corelib/numerics/BasicAmount.cc deleted file mode 100644 index 90cf26ec..00000000 --- a/tests/corelib/numerics/BasicAmount.cc +++ /dev/null @@ -1,625 +0,0 @@ -#include "BasicAmount.h" - -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(BasicAmountTestCase, "numerics"); - -void BasicAmountTestCase::setUp() { - ledger::initialize(); -} -void BasicAmountTestCase::tearDown() { - ledger::shutdown(); -} - -void BasicAmountTestCase::testConstructors() -{ - amount_t x0; - amount_t x1(123456L); - amount_t x2(123456UL); - amount_t x3(123.456); - amount_t x5("123456"); - amount_t x6("123.456"); - amount_t x7(std::string("123456")); - amount_t x8(std::string("123.456")); - amount_t x9(x3); - amount_t x10(x6); - 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); - assertEqual(x6, x3); - assertEqual(x8, x3); - assertEqual(x10, x3); - assertEqual(x10, x9); - - CPPUNIT_ASSERT(x0.valid()); - CPPUNIT_ASSERT(x1.valid()); - CPPUNIT_ASSERT(x2.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()); - CPPUNIT_ASSERT(x11.valid()); -} - -void BasicAmountTestCase::testNegation() -{ - amount_t x0; - amount_t x1(-123456L); - amount_t x3(-123.456); - amount_t x5("-123456"); - amount_t x6("-123.456"); - amount_t x7(std::string("-123456")); - amount_t x8(std::string("-123.456")); - amount_t x9(- x3); - - assertEqual(amount_t(0L), x0); - assertEqual(x5, x1); - assertEqual(x7, x1); - assertEqual(x6, x3); - assertEqual(x8, x3); - assertEqual(- x6, x9); - assertEqual(x3.negate(), x9); - - amount_t x10(x9.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() -{ - amount_t x0; - amount_t x1 = 123456L; - amount_t x2 = 123456UL; - amount_t x3 = 123.456; - amount_t x5 = "123456"; - amount_t x6 = "123.456"; - amount_t x7 = string("123456"); - amount_t x8 = string("123.456"); - amount_t x9 = x3; - amount_t x10 = amount_t(x6); - - assertEqual(amount_t(0L), x0); - assertEqual(x2, x1); - assertEqual(x5, x1); - assertEqual(x7, x1); - assertEqual(x6, x3); - assertEqual(x8, x3); - assertEqual(x10, x3); - assertEqual(x10, x9); - - x0 = amount_t(); - x1 = 123456L; - x2 = 123456UL; - x3 = 123.456; - x5 = "123456"; - x6 = "123.456"; - x7 = std::string("123456"); - x8 = std::string("123.456"); - x9 = x3; - x10 = amount_t(x6); - - assertEqual(amount_t(0L), x0); - assertEqual(x2, x1); - assertEqual(x5, x1); - assertEqual(x7, x1); - assertEqual(x6, x3); - assertEqual(x8, x3); - assertEqual(x10, x3); - assertEqual(x10, x9); - - CPPUNIT_ASSERT(x0.valid()); - CPPUNIT_ASSERT(x1.valid()); - CPPUNIT_ASSERT(x2.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::testEquality() -{ - amount_t x1(123456L); - amount_t x2(456789L); - amount_t x3(333333L); - amount_t x4(123456.0); - amount_t x5("123456.0"); - amount_t x6(123456.0F); - - CPPUNIT_ASSERT(x1 == 123456L); - CPPUNIT_ASSERT(x1 != x2); - CPPUNIT_ASSERT(x1 == (x2 - x3)); - 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() -{ - amount_t x1(123L); - amount_t y1(456L); - - assertEqual(amount_t(579L), x1 + y1); - assertEqual(amount_t(579L), x1 + 456L); - assertEqual(amount_t(579L), 456L + x1); - - x1 += amount_t(456L); - assertEqual(amount_t(579L), x1); - x1 += 456L; - assertEqual(amount_t(1035L), x1); - - amount_t x4("123456789123456789123456789"); - - assertEqual(amount_t("246913578246913578246913578"), x4 + x4); - - CPPUNIT_ASSERT(x1.valid()); - CPPUNIT_ASSERT(y1.valid()); - CPPUNIT_ASSERT(x4.valid()); -} - -void BasicAmountTestCase::testFractionalAddition() -{ - 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 BasicAmountTestCase::testIntegerSubtraction() -{ - amount_t x1(123L); - amount_t y1(456L); - - assertEqual(amount_t(333L), y1 - x1); - assertEqual(amount_t(-333L), x1 - y1); - assertEqual(amount_t(23L), x1 - 100L); - assertEqual(amount_t(-23L), 100L - x1); - - x1 -= amount_t(456L); - assertEqual(amount_t(-333L), x1); - x1 -= 456L; - assertEqual(amount_t(-789L), x1); - - amount_t x4("123456789123456789123456789"); - amount_t y4("8238725986235986"); - - assertEqual(amount_t("123456789115218063137220803"), x4 - y4); - assertEqual(amount_t("-123456789115218063137220803"), y4 - x4); - - CPPUNIT_ASSERT(x1.valid()); - CPPUNIT_ASSERT(y1.valid()); - CPPUNIT_ASSERT(x4.valid()); - CPPUNIT_ASSERT(y4.valid()); -} - -void BasicAmountTestCase::testFractionalSubtraction() -{ - 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 BasicAmountTestCase::testIntegerMultiplication() -{ - amount_t x1(123L); - amount_t y1(456L); - - 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(56088L), x1 * y1); - assertEqual(amount_t(56088L), y1 * x1); - assertEqual(amount_t(56088L), x1 * 456L); - assertEqual(amount_t(56088L), amount_t(456L) * x1); - assertEqual(amount_t(56088L), 456L * x1); - - x1 *= amount_t(123L); - assertEqual(amount_t(15129L), x1); - x1 *= 123L; - assertEqual(amount_t(1860867L), x1); - - amount_t x4("123456789123456789123456789"); - - assertEqual(amount_t("15241578780673678546105778281054720515622620750190521"), - x4 * x4); - - CPPUNIT_ASSERT(x1.valid()); - CPPUNIT_ASSERT(y1.valid()); - CPPUNIT_ASSERT(x4.valid()); -} - -void BasicAmountTestCase::testFractionalMultiplication() -{ - 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 BasicAmountTestCase::testIntegerDivision() -{ - amount_t x1(123L); - amount_t y1(456L); - - assertThrow(x1 / 0L, amount_exception); - assertEqual(amount_t(0L), amount_t(0L) / x1); - assertEqual(amount_t(0L), 0L / x1); - assertEqual(x1, x1 / 1L); - assertEqual(amount_t("0.008130"), amount_t(1L) / x1); - assertEqual(amount_t("0.008130"), 1L / x1); - assertEqual(- x1, x1 / -1L); - assertEqual(- amount_t("0.008130"), amount_t(-1L) / x1); - assertEqual(- amount_t("0.008130"), -1L / x1); - assertEqual(amount_t("0.269737"), x1 / y1); - assertEqual(amount_t("3.707317"), y1 / x1); - assertEqual(amount_t("0.269737"), x1 / 456L); - assertEqual(amount_t("3.707317"), amount_t(456L) / x1); - assertEqual(amount_t("3.707317"), 456L / x1); - - x1 /= amount_t(456L); - assertEqual(amount_t("0.269737"), x1); - x1 /= 456L; - assertEqual(amount_t("0.00059152850877193"), x1); - - amount_t x4("123456789123456789123456789"); - amount_t y4("56"); - - 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() -{ - amount_t x1(123.123); - amount_t y1(456.456); - - assertThrow(x1 / 0L, amount_exception); - assertEqual(amount_t("0.008121959"), amount_t(1.0) / x1); - assertEqual(amount_t("0.008121959"), 1.0 / x1); - assertEqual(x1, x1 / 1.0); - assertEqual(amount_t("0.008121959"), amount_t(1.0) / x1); - assertEqual(amount_t("0.008121959"), 1.0 / x1); - assertEqual(- x1, x1 / -1.0); - assertEqual(- amount_t("0.008121959"), amount_t(-1.0) / x1); - assertEqual(- amount_t("0.008121959"), -1.0 / x1); - assertEqual(amount_t("0.269736842105263"), x1 / y1); - assertEqual(amount_t("3.707317073170732"), y1 / x1); - assertEqual(amount_t("0.269736842105263"), x1 / 456.456); - assertEqual(amount_t("3.707317073170732"), amount_t(456.456) / x1); - assertEqual(amount_t("3.707317073170732"), 456.456 / x1); - - x1 /= amount_t(456.456); - assertEqual(amount_t("0.269736842105263"), x1); - x1 /= 456.456; - assertEqual(amount_t("0.000590937225286255411255411255411255411"), x1); - x1 /= 456L; - assertEqual(amount_t("0.000001295914967733016252753094858358016252192982456140350877192982456140350877192982"), x1); - - amount_t x4("1234567891234567.89123456789"); - amount_t y4("56.789"); - - assertEqual(amount_t(1.0), x4 / x4); - assertEqual(amount_t("21739560323910.7554497273748437197344556164046"), x4 / y4); - - CPPUNIT_ASSERT(x1.valid()); - CPPUNIT_ASSERT(y1.valid()); - CPPUNIT_ASSERT(x4.valid()); - CPPUNIT_ASSERT(y4.valid()); -} - -void BasicAmountTestCase::testIntegerConversion() -{ - amount_t x1(123456L); - - assertEqual(true, bool(x1)); - assertEqual(123456L, long(x1)); - assertEqual(123456.0, double(x1)); - assertEqual(string("123456"), x1.to_string()); - assertEqual(string("123456"), x1.quantity_string()); - - CPPUNIT_ASSERT(x1.valid()); -} - -void BasicAmountTestCase::testFractionalConversion() -{ - amount_t x1(1234.56); - - assertEqual(true, bool(x1)); - assertEqual(1234L, long(x1)); - assertEqual(1234.56, double(x1)); - assertEqual(string("1234.56"), x1.to_string()); - assertEqual(string("1234.56"), x1.quantity_string()); - - CPPUNIT_ASSERT(x1.valid()); -} - -void BasicAmountTestCase::testFractionalRound() -{ - 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)); - - 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() -{ - 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 BasicAmountTestCase::testForZero() -{ - 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 BasicAmountTestCase::testComparisons() -{ - 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 BasicAmountTestCase::testSign() -{ - 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 BasicAmountTestCase::testAbs() -{ - amount_t x0; - amount_t x1(-1234L); - amount_t x2(1234L); - - assertEqual(amount_t(), x0.abs()); - assertEqual(amount_t(1234L), x1.abs()); - assertEqual(amount_t(1234L), x2.abs()); - - CPPUNIT_ASSERT(x0.valid()); - CPPUNIT_ASSERT(x1.valid()); - CPPUNIT_ASSERT(x2.valid()); -} - -void BasicAmountTestCase::testPrinting() -{ - 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/BasicAmount.h b/tests/corelib/numerics/BasicAmount.h deleted file mode 100644 index 2c107f45..00000000 --- a/tests/corelib/numerics/BasicAmount.h +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef _BASICAMOUNT_H -#define _BASICAMOUNT_H - -#include "UnitTests.h" - -class BasicAmountTestCase : public CPPUNIT_NS::TestCase -{ - CPPUNIT_TEST_SUITE(BasicAmountTestCase); - - CPPUNIT_TEST(testConstructors); - CPPUNIT_TEST(testNegation); - CPPUNIT_TEST(testAssignment); - CPPUNIT_TEST(testEquality); - CPPUNIT_TEST(testIntegerAddition); - CPPUNIT_TEST(testFractionalAddition); - CPPUNIT_TEST(testIntegerSubtraction); - CPPUNIT_TEST(testFractionalSubtraction); - CPPUNIT_TEST(testIntegerMultiplication); - CPPUNIT_TEST(testFractionalMultiplication); - CPPUNIT_TEST(testIntegerDivision); - CPPUNIT_TEST(testFractionalDivision); - CPPUNIT_TEST(testIntegerConversion); - CPPUNIT_TEST(testFractionalConversion); - CPPUNIT_TEST(testFractionalRound); - CPPUNIT_TEST(testTruth); - CPPUNIT_TEST(testForZero); - CPPUNIT_TEST(testComparisons); - CPPUNIT_TEST(testSign); - CPPUNIT_TEST(testAbs); - CPPUNIT_TEST(testPrinting); - - CPPUNIT_TEST_SUITE_END(); - -public: - BasicAmountTestCase() {} - virtual ~BasicAmountTestCase() {} - - virtual void setUp(); - virtual void tearDown(); - - void testConstructors(); - void testNegation(); - void testAssignment(); - void testEquality(); - void testIntegerAddition(); - void testFractionalAddition(); - void testIntegerSubtraction(); - void testFractionalSubtraction(); - void testIntegerMultiplication(); - void testFractionalMultiplication(); - void testIntegerDivision(); - void testFractionalDivision(); - void testIntegerConversion(); - void testFractionalConversion(); - void testFractionalRound(); - void testTruth(); - void testForZero(); - void testComparisons(); - void testSign(); - void testAbs(); - void testPrinting(); - -private: - BasicAmountTestCase(const BasicAmountTestCase ©); - void operator=(const BasicAmountTestCase ©); -}; - -#endif /* _BASICAMOUNT_H */ diff --git a/tests/corelib/numerics/Commodity.cc b/tests/corelib/numerics/Commodity.cc deleted file mode 100644 index e7e2a18c..00000000 --- a/tests/corelib/numerics/Commodity.cc +++ /dev/null @@ -1,55 +0,0 @@ -#include "Commodity.h" - -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CommodityTestCase, "numerics"); - -void CommodityTestCase::setUp() { - ledger::initialize(); -} -void CommodityTestCase::tearDown() { - ledger::shutdown(); -} - -void CommodityTestCase::testPriceHistory() -{ - ptime jan17_07 = parse_datetime("2007/01/17 00:00:00"); - ptime feb27_07 = parse_datetime("2007/02/27 18:00:00"); - ptime feb28_07 = parse_datetime("2007/02/28 06:00:00"); - ptime feb28_07sbm = parse_datetime("2007/02/28 11:59:59"); - ptime mar01_07 = parse_datetime("2007/03/01 00:00:00"); - ptime apr15_07 = parse_datetime("2007/04/15 13:00:00"); - - // jww (2007-04-17): tbd - amount_t x1("100.10 AAPL"); - - // Commodities cannot be constructed by themselves, since a great - // deal of their state depends on how they were seen to be used. - commodity_t& aapl(x1.commodity()); - - aapl.add_price(jan17_07, amount_t("$10.20")); - aapl.add_price(feb27_07, amount_t("$13.40")); - aapl.add_price(feb28_07, amount_t("$18.33")); - aapl.add_price(feb28_07sbm, amount_t("$18.30")); - aapl.add_price(mar01_07, amount_t("$19.50")); - aapl.add_price(apr15_07, amount_t("$21.22")); - - assertEqual(amount_t("$1831.83"), x1.value(feb28_07sbm)); - assertEqual(amount_t("$2124.12"), x1.value(now)); - - assertValid(x1); -} - -void CommodityTestCase::testLots() -{ - // jww (2007-04-17): tbd -} - -void CommodityTestCase::testScalingBase() -{ - // jww (2007-04-17): tbd -} - -void CommodityTestCase::testReduction() -{ - // jww (2007-04-17): tbd -} - diff --git a/tests/corelib/numerics/Commodity.h b/tests/corelib/numerics/Commodity.h deleted file mode 100644 index dafa03e9..00000000 --- a/tests/corelib/numerics/Commodity.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef _COMMMODITY_H -#define _COMMMODITY_H - -#include "UnitTests.h" - -class CommodityTestCase : public CPPUNIT_NS::TestCase -{ - CPPUNIT_TEST_SUITE(CommodityTestCase); - - CPPUNIT_TEST(testPriceHistory); - CPPUNIT_TEST(testLots); - CPPUNIT_TEST(testScalingBase); - CPPUNIT_TEST(testReduction); - - CPPUNIT_TEST_SUITE_END(); - -public: - CommodityTestCase() {} - virtual ~CommodityTestCase() {} - - virtual void setUp(); - virtual void tearDown(); - - void testPriceHistory(); - void testLots(); - void testScalingBase(); - void testReduction(); - -private: - CommodityTestCase(const CommodityTestCase ©); - void operator=(const CommodityTestCase ©); -}; - -#endif /* _COMMMODITY_H */ diff --git a/tests/corelib/numerics/CommodityAmount.cc b/tests/corelib/numerics/CommodityAmount.cc deleted file mode 100644 index 7ed7f403..00000000 --- a/tests/corelib/numerics/CommodityAmount.cc +++ /dev/null @@ -1,696 +0,0 @@ -#include "CommodityAmount.h" - -#define internalAmount(x) amount_t::exact(x) - -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CommodityAmountTestCase, "numerics"); - -void CommodityAmountTestCase::setUp() -{ - ledger::initialize(); - - // Cause the display precision for dollars to be initialized to 2. - amount_t x1("$1.00"); - assertTrue(x1); - amount_t::full_strings = true; // makes error reports from UnitTests accurate -} - -void CommodityAmountTestCase::tearDown() -{ - amount_t::full_strings = false; - ledger::shutdown(); -} - -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(string("$123.45"), x1.to_string()); - assertEqual(string("$-123.45"), x2.to_string()); - assertEqual(string("$-123.45"), x3.to_string()); - assertEqual(string("DM 123.45"), x4.to_string()); - assertEqual(string("DM -123.45"), x5.to_string()); - assertEqual(string("DM -123.45"), x6.to_string()); - assertEqual(string("123.45 euro"), x7.to_string()); - assertEqual(string("-123.45 euro"), x8.to_string()); - assertEqual(string("123.45€"), x9.to_string()); - assertEqual(string("-123.45€"), x10.to_string()); - - assertValid(x1); - assertValid(x2); - assertValid(x3); - assertValid(x4); - assertValid(x5); - assertValid(x6); - assertValid(x7); - assertValid(x8); - assertValid(x9); - assertValid(x10); -} - -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(amount_t("$-123.45"), x1.negate()); - assertEqual(amount_t("$123.45"), x2.negate()); - assertEqual(amount_t("$123.45"), x3.negate()); - - assertEqual(string("$-123.45"), (- x1).to_string()); - assertEqual(string("$123.45"), (- x2).to_string()); - assertEqual(string("$123.45"), (- x3).to_string()); - assertEqual(string("DM -123.45"), (- x4).to_string()); - assertEqual(string("DM 123.45"), (- x5).to_string()); - assertEqual(string("DM 123.45"), (- x6).to_string()); - assertEqual(string("-123.45 euro"), (- x7).to_string()); - assertEqual(string("123.45 euro"), (- x8).to_string()); - assertEqual(string("-123.45€"), (- x9).to_string()); - assertEqual(string("123.45€"), (- x10).to_string()); - - assertEqual(amount_t("$-123.45"), x1.negate()); - assertEqual(amount_t("$123.45"), x2.negate()); - assertEqual(amount_t("$123.45"), x3.negate()); - - assertValid(x1); - assertValid(x2); - assertValid(x3); - assertValid(x4); - assertValid(x5); - assertValid(x6); - assertValid(x7); - assertValid(x8); - assertValid(x9); - assertValid(x10); -} - -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(string("$123.45"), x1.to_string()); - assertEqual(string("$-123.45"), x2.to_string()); - assertEqual(string("$-123.45"), x3.to_string()); - assertEqual(string("DM 123.45"), x4.to_string()); - assertEqual(string("DM -123.45"), x5.to_string()); - assertEqual(string("DM -123.45"), x6.to_string()); - assertEqual(string("123.45 euro"), x7.to_string()); - assertEqual(string("-123.45 euro"), x8.to_string()); - assertEqual(string("123.45€"), x9.to_string()); - assertEqual(string("-123.45€"), x10.to_string()); - - assertValid(x1); - assertValid(x2); - assertValid(x3); - assertValid(x4); - assertValid(x5); - assertValid(x6); - assertValid(x7); - assertValid(x8); - assertValid(x9); - assertValid(x10); -} - -void CommodityAmountTestCase::testEquality() -{ - amount_t x0; - 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€"; - - assertTrue(x0.null()); - assertTrue(x0.zero()); - assertTrue(x0.realzero()); - assertTrue(x0.sign() == 0); - assertTrue(x0.compare(x1) < 0); - assertTrue(x0.compare(x2) > 0); - assertTrue(x0.compare(x0) == 0); - - assertTrue(x1 != x2); - assertTrue(x1 != x4); - assertTrue(x1 != x7); - assertTrue(x1 != x9); - assertTrue(x2 == x3); - assertTrue(x4 != x5); - assertTrue(x5 == x6); - assertTrue(x7 == - x8); - assertTrue(x9 == - x10); - - assertValid(x0); - assertValid(x1); - assertValid(x2); - assertValid(x3); - assertValid(x4); - assertValid(x5); - assertValid(x6); - assertValid(x7); - assertValid(x8); - assertValid(x9); - assertValid(x10); -} - -void CommodityAmountTestCase::testAddition() -{ - amount_t x0; - amount_t x1("$123.45"); - amount_t x2(internalAmount("$123.456789")); - amount_t x3("DM 123.45"); - amount_t x4("123.45 euro"); - amount_t x5("123.45€"); - amount_t x6("123.45"); - - assertEqual(amount_t("$246.90"), x1 + x1); - assertNotEqual(amount_t("$246.91"), x1 + x2); - assertEqual(internalAmount("$246.906789"), x1 + x2); - - // Converting to string drops internal precision - assertEqual(string("$246.90"), (x1 + x1).to_string()); - assertEqual(string("$246.91"), (x1 + x2).to_string()); - - assertThrow(x1 + x0, amount_exception); - assertThrow(x1 + x3, amount_exception); - assertThrow(x1 + x4, amount_exception); - assertThrow(x1 + x5, amount_exception); - assertThrow(x1 + x6, amount_exception); - assertThrow(x1 + 123.45, amount_exception); - assertThrow(x1 + 123L, amount_exception); - - assertEqual(amount_t("DM 246.90"), x3 + x3); - assertEqual(amount_t("246.90 euro"), x4 + x4); - assertEqual(amount_t("246.90€"), x5 + x5); - - assertEqual(string("DM 246.90"), (x3 + x3).to_string()); - assertEqual(string("246.90 euro"), (x4 + x4).to_string()); - assertEqual(string("246.90€"), (x5 + x5).to_string()); - - x1 += amount_t("$456.45"); - assertEqual(amount_t("$579.90"), x1); - x1 += amount_t("$456.45"); - assertEqual(amount_t("$1036.35"), x1); - x1 += amount_t("$456"); - assertEqual(amount_t("$1492.35"), x1); - - amount_t x7(internalAmount("$123456789123456789.123456789123456789")); - - assertEqual(internalAmount("$246913578246913578.246913578246913578"), x7 + x7); - - assertValid(x1); - assertValid(x2); - assertValid(x3); - assertValid(x4); - assertValid(x5); - assertValid(x6); - assertValid(x7); -} - -void CommodityAmountTestCase::testSubtraction() -{ - amount_t x0; - amount_t x1("$123.45"); - amount_t x2(internalAmount("$123.456789")); - amount_t x3("DM 123.45"); - amount_t x4("123.45 euro"); - amount_t x5("123.45€"); - amount_t x6("123.45"); - - assertNotEqual(amount_t(), x1 - x1); - assertEqual(amount_t("$0"), x1 - x1); - assertEqual(amount_t("$23.45"), x1 - amount_t("$100.00")); - assertEqual(amount_t("$-23.45"), amount_t("$100.00") - x1); - assertNotEqual(amount_t("$-0.01"), x1 - x2); - assertEqual(internalAmount("$-0.006789"), x1 - x2); - - // Converting to string drops internal precision. If an amount is - // zero, it drops the commodity as well. - assertEqual(string("$0.00"), (x1 - x1).to_string()); - assertEqual(string("$-0.01"), (x1 - x2).to_string()); - - assertThrow(x1 - x0, amount_exception); - assertThrow(x1 - x3, amount_exception); - assertThrow(x1 - x4, amount_exception); - assertThrow(x1 - x5, amount_exception); - assertThrow(x1 - x6, amount_exception); - assertThrow(x1 - 123.45, amount_exception); - assertThrow(x1 - 123L, amount_exception); - - assertEqual(amount_t("DM 0.00"), x3 - x3); - assertEqual(amount_t("DM 23.45"), x3 - amount_t("DM 100.00")); - assertEqual(amount_t("DM -23.45"), amount_t("DM 100.00") - x3); - assertEqual(amount_t("0.00 euro"), x4 - x4); - assertEqual(amount_t("23.45 euro"), x4 - amount_t("100.00 euro")); - assertEqual(amount_t("-23.45 euro"), amount_t("100.00 euro") - x4); - assertEqual(amount_t("0.00€"), x5 - x5); - assertEqual(amount_t("23.45€"), x5 - amount_t("100.00€")); - assertEqual(amount_t("-23.45€"), amount_t("100.00€") - x5); - - assertEqual(string("DM 0.00"), (x3 - x3).to_string()); - assertEqual(string("DM 23.45"), (x3 - amount_t("DM 100.00")).to_string()); - assertEqual(string("DM -23.45"), (amount_t("DM 100.00") - x3).to_string()); - assertEqual(string("0.00 euro"), (x4 - x4).to_string()); - assertEqual(string("23.45 euro"), (x4 - amount_t("100.00 euro")).to_string()); - assertEqual(string("-23.45 euro"), (amount_t("100.00 euro") - x4).to_string()); - assertEqual(string("0.00€"), (x5 - x5).to_string()); - assertEqual(string("23.45€"), (x5 - amount_t("100.00€")).to_string()); - assertEqual(string("-23.45€"), (amount_t("100.00€") - x5).to_string()); - - x1 -= amount_t("$456.45"); - assertEqual(amount_t("$-333.00"), x1); - x1 -= amount_t("$456.45"); - assertEqual(amount_t("$-789.45"), x1); - x1 -= amount_t("$456"); - assertEqual(amount_t("$-1245.45"), x1); - - amount_t x7(internalAmount("$123456789123456789.123456789123456789")); - amount_t x8(internalAmount("$2354974984698.98459845984598")); - - assertEqual(internalAmount("$123454434148472090.138858329277476789"), x7 - x8); - assertEqual(string("$123454434148472090.138858329277476789"), (x7 - x8).to_string()); - assertEqual(string("$123454434148472090.14"), - (amount_t("$1.00") * (x7 - x8)).to_string()); - assertEqual(internalAmount("$-123454434148472090.138858329277476789"), x8 - x7); - assertEqual(string("$-123454434148472090.138858329277476789"), (x8 - x7).to_string()); - assertEqual(string("$-123454434148472090.14"), - (amount_t("$1.00") * (x8 - x7)).to_string()); - - assertValid(x1); - assertValid(x2); - assertValid(x3); - assertValid(x4); - assertValid(x5); - assertValid(x6); - assertValid(x7); - assertValid(x8); -} - -void CommodityAmountTestCase::testMultiplication() -{ - amount_t x1("$123.12"); - amount_t y1("$456.45"); - amount_t x2(internalAmount("$123.456789")); - amount_t x3("DM 123.45"); - amount_t x4("123.45 euro"); - amount_t x5("123.45€"); - - assertEqual(amount_t("$0.00"), x1 * 0L); - assertEqual(amount_t("$0.00"), 0L * x1); - assertEqual(x1, x1 * 1L); - assertEqual(x1, 1L * x1); - assertEqual(- x1, x1 * -1L); - assertEqual(- x1, -1L * x1); - assertEqual(internalAmount("$56198.124"), x1 * y1); - assertEqual(string("$56198.12"), (x1 * y1).to_string()); - assertEqual(internalAmount("$56198.124"), y1 * x1); - assertEqual(string("$56198.12"), (y1 * x1).to_string()); - - // Internal amounts retain their precision, even when being - // converted to strings - assertEqual(internalAmount("$15199.99986168"), x1 * x2); - assertEqual(internalAmount("$15199.99986168"), x2 * x1); - assertEqual(string("$15200.00"), (x1 * x2).to_string()); - assertEqual(string("$15199.99986168"), (x2 * x1).to_string()); - - assertThrow(x1 * x3, amount_exception); - assertThrow(x1 * x4, amount_exception); - assertThrow(x1 * x5, amount_exception); - - x1 *= amount_t("123.12"); - assertEqual(internalAmount("$15158.5344"), x1); - assertEqual(string("$15158.53"), x1.to_string()); - x1 *= 123.12; - assertEqual(internalAmount("$1866318.755328"), x1); - assertEqual(string("$1866318.76"), x1.to_string()); - x1 *= 123L; - assertEqual(internalAmount("$229557206.905344"), x1); - assertEqual(string("$229557206.91"), x1.to_string()); - - amount_t x7(internalAmount("$123456789123456789.123456789123456789")); - - assertEqual(internalAmount("$15241578780673678546105778311537878.046486820281054720515622620750190521"), - x7 * x7); - - assertValid(x1); - assertValid(x2); - assertValid(x3); - assertValid(x4); - assertValid(x5); - assertValid(x7); -} - -void CommodityAmountTestCase::testDivision() -{ - amount_t x1("$123.12"); - amount_t y1("$456.45"); - amount_t x2(internalAmount("$123.456789")); - amount_t x3("DM 123.45"); - amount_t x4("123.45 euro"); - amount_t x5("123.45€"); - - assertThrow(x1 / 0L, amount_exception); - assertEqual(amount_t("$0.00"), 0L / x1); - assertEqual(x1, x1 / 1L); - assertEqual(internalAmount("$0.00812216"), 1L / x1); - assertEqual(- x1, x1 / -1L); - assertEqual(internalAmount("$-0.00812216"), -1L / x1); - assertEqual(internalAmount("$0.26973382"), x1 / y1); - assertEqual(string("$0.27"), (x1 / y1).to_string()); - assertEqual(internalAmount("$3.70735867"), y1 / x1); - assertEqual(string("$3.71"), (y1 / x1).to_string()); - - // Internal amounts retain their precision, even when being - // converted to strings - assertEqual(internalAmount("$0.99727201"), x1 / x2); - assertEqual(internalAmount("$1.00273545321637426901"), x2 / x1); - assertEqual(string("$1.00"), (x1 / x2).to_string()); - assertEqual(string("$1.00273545321637426901"), (x2 / x1).to_string()); - - assertThrow(x1 / x3, amount_exception); - assertThrow(x1 / x4, amount_exception); - assertThrow(x1 / x5, amount_exception); - - x1 /= amount_t("123.12"); - assertEqual(internalAmount("$1.00"), x1); - assertEqual(string("$1.00"), x1.to_string()); - x1 /= 123.12; - assertEqual(internalAmount("$0.00812216"), x1); - assertEqual(string("$0.01"), x1.to_string()); - x1 /= 123L; - assertEqual(internalAmount("$0.00006603"), x1); - assertEqual(string("$0.00"), x1.to_string()); - - amount_t x6(internalAmount("$237235987235987.98723987235978")); - amount_t x7(internalAmount("$123456789123456789.123456789123456789")); - - assertEqual(amount_t("$1"), x7 / x7); - assertEqual(internalAmount("$0.0019216115121765559608381226612019501046413574469262"), - x6 / x7); - assertEqual(internalAmount("$520.39654928343335571379527154924040947271699678158689736256"), - x7 / x6); - - assertValid(x1); - assertValid(x2); - assertValid(x3); - assertValid(x4); - assertValid(x5); - assertValid(x6); - assertValid(x7); -} - -void CommodityAmountTestCase::testConversion() -{ - amount_t x1("$1234.56"); - - assertEqual(true, bool(x1)); - assertEqual(1234L, long(x1)); - assertEqual(1234.56, double(x1)); - assertEqual(string("$1234.56"), x1.to_string()); - assertEqual(string("1234.56"), x1.quantity_string()); - - assertValid(x1); -} - -void CommodityAmountTestCase::testRound() -{ - amount_t x1(internalAmount("$1234.567890")); - - assertEqual(internalAmount("$1234.56789"), x1.round(6)); - assertEqual(internalAmount("$1234.56789"), x1.round(5)); - assertEqual(internalAmount("$1234.5679"), x1.round(4)); - assertEqual(internalAmount("$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(internalAmount("$9876.543210")); - - assertEqual(internalAmount("$9876.543210"), x2.round(6)); - assertEqual(internalAmount("$9876.54321"), x2.round(5)); - assertEqual(internalAmount("$9876.5432"), x2.round(4)); - assertEqual(internalAmount("$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(internalAmount("$-1234.567890")); - - assertEqual(internalAmount("$-1234.56789"), x3.round(6)); - assertEqual(internalAmount("$-1234.56789"), x3.round(5)); - assertEqual(internalAmount("$-1234.5679"), x3.round(4)); - assertEqual(internalAmount("$-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(internalAmount("$-9876.543210")); - - assertEqual(internalAmount("$-9876.543210"), x4.round(6)); - assertEqual(internalAmount("$-9876.54321"), x4.round(5)); - assertEqual(internalAmount("$-9876.5432"), x4.round(4)); - assertEqual(internalAmount("$-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)); - - amount_t x5("$123.45"); - - x5 *= 100.12; - - assertEqual(internalAmount("$12359.814"), x5); - assertEqual(string("$12359.81"), x5.to_string()); - assertEqual(string("$12359.814"), x5.to_fullstring()); - assertEqual(string("$12359.814"), x5.unround().to_string()); - - assertValid(x1); - assertValid(x2); - assertValid(x3); - assertValid(x4); - assertValid(x5); -} - -void CommodityAmountTestCase::testDisplayRound() -{ - amount_t x1("$0.85"); - amount_t x2("$0.1"); - - x1 *= 0.19; - - assertNotEqual(amount_t("$0.16"), x1); - assertEqual(internalAmount("$0.1615"), x1); - assertEqual(string("$0.16"), x1.to_string()); - - assertEqual(amount_t("$0.10"), x2); - assertNotEqual(internalAmount("$0.101"), x2); - assertEqual(string("$0.10"), x2.to_string()); - - x1 *= 7L; - - assertNotEqual(amount_t("$1.13"), x1); - assertEqual(internalAmount("$1.1305"), x1); - assertEqual(string("$1.13"), x1.to_string()); -} - -void CommodityAmountTestCase::testTruth() -{ - amount_t x1("$1234"); - amount_t x2("$1234.56"); - - if (x1) - CPPUNIT_ASSERT(true); - else - CPPUNIT_ASSERT(false); - - if (x2) - CPPUNIT_ASSERT(true); - else - CPPUNIT_ASSERT(false); - - assertValid(x1); - assertValid(x2); -} - -void CommodityAmountTestCase::testForZero() -{ - amount_t x1(internalAmount("$0.000000000000000000001")); - - assertFalse(x1); - assertTrue(x1.zero()); - assertFalse(x1.realzero()); - - assertValid(x1); -} - -void CommodityAmountTestCase::testComparisons() -{ - amount_t x0; - amount_t x1("$-123"); - amount_t x2("$123.00"); - amount_t x3(internalAmount("$-123.4544")); - amount_t x4(internalAmount("$123.4544")); - amount_t x5("$-123.45"); - amount_t x6("$123.45"); - - assertTrue(x0 > x1); - assertTrue(x0 < x2); - assertTrue(x0 > x3); - assertTrue(x0 < x4); - assertTrue(x0 > x5); - assertTrue(x0 < x6); - - assertTrue(x1 > x3); - assertTrue(x3 <= x5); - assertTrue(x3 < x5); - assertTrue(x3 <= x5); - assertFalse(x3 == x5); - assertTrue(x3 < x1); - assertTrue(x3 < x4); - - assertValid(x0); - assertValid(x1); - assertValid(x2); - assertValid(x3); - assertValid(x4); - assertValid(x5); - assertValid(x6); -} - -void CommodityAmountTestCase::testSign() -{ - amount_t x0; - amount_t x1(internalAmount("$0.0000000000000000000000000000000000001")); - amount_t x2(internalAmount("$-0.0000000000000000000000000000000000001")); - amount_t x3("$1"); - amount_t x4("$-1"); - - assertFalse(x0.sign()); - assertTrue(x1.sign() != 0); - assertTrue(x2.sign() != 0); - assertTrue(x3.sign() > 0); - assertTrue(x4.sign() < 0); - - assertValid(x0); - assertValid(x1); - assertValid(x2); - assertValid(x3); - assertValid(x4); -} - -void CommodityAmountTestCase::testAbs() -{ - amount_t x0; - amount_t x1("$-1234.56"); - amount_t x2("$1234.56"); - - assertEqual(amount_t(), x0.abs()); - assertEqual(amount_t("$1234.56"), x1.abs()); - assertEqual(amount_t("$1234.56"), x2.abs()); - - assertValid(x0); - assertValid(x1); - assertValid(x2); -} - -void CommodityAmountTestCase::testPrinting() -{ - amount_t x0; - amount_t x1(internalAmount("$982340823.386238098235098235098235098")); - amount_t x2("$982340823.38"); - - { - std::ostringstream bufstr; - bufstr << x0; - - assertEqual(std::string("0"), bufstr.str()); - } - - { - std::ostringstream bufstr; - bufstr << x1; - - assertEqual(std::string("$982340823.386238098235098235098235098"), - bufstr.str()); - } - - { - std::ostringstream bufstr; - bufstr << (x1 * x2).to_string(); - - assertEqual(std::string("$964993493285024293.18099172508158508135413499124"), - bufstr.str()); - } - - { - std::ostringstream bufstr; - bufstr << (x2 * x1).to_string(); - - assertEqual(std::string("$964993493285024293.18"), bufstr.str()); - } - - assertValid(x0); - assertValid(x1); - assertValid(x2); -} - diff --git a/tests/corelib/numerics/CommodityAmount.h b/tests/corelib/numerics/CommodityAmount.h deleted file mode 100644 index 5ffa7810..00000000 --- a/tests/corelib/numerics/CommodityAmount.h +++ /dev/null @@ -1,60 +0,0 @@ -#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 */ diff --git a/tests/corelib/numerics/DateTime.cc b/tests/corelib/numerics/DateTime.cc deleted file mode 100644 index 24b8dd16..00000000 --- a/tests/corelib/numerics/DateTime.cc +++ /dev/null @@ -1,79 +0,0 @@ -#include "DateTimeTest.h" - -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(DateTimeTestCase, "numerics"); - -void DateTimeTestCase::setUp() {} -void DateTimeTestCase::tearDown() {} - -void DateTimeTestCase::testConstructors() -{ - std::time_t time_t_now = std::time(NULL); - struct tm * moment = std::localtime(&time_t_now); - - std::time_t localMoment = std::mktime(moment); - - ptime d0; - ptime d1(parse_datetime("1990/01/01")); - ptime d3(boost::posix_time::from_time_t(localMoment)); - ptime d4(parse_datetime("2006/12/25")); - //ptime d5(parse_datetime("12/25")); - ptime d6(parse_datetime("2006.12.25")); - //ptime d7(parse_datetime("12.25")); - ptime d8(parse_datetime("2006-12-25")); - //ptime d9(parse_datetime("12-25")); -#if 0 - ptime d10(parse_datetime("tue")); - ptime d11(parse_datetime("tuesday")); - ptime d12(parse_datetime("feb")); - ptime d13(parse_datetime("february")); - ptime d14(parse_datetime("2006")); -#endif - ptime d15(d3); - - assertTrue(d0.is_not_a_date_time()); - assertFalse(d1.is_not_a_date_time()); - assertFalse(d4.is_not_a_date_time()); - - assertTrue(now > d1); - //assertTrue(now <= d3); - assertTrue(now > d4); - - assertEqual(d3, d15); - assertEqual(d4, d6); - assertEqual(d4, d8); - //assertEqual(d5, d7); - //assertEqual(d5, d9); -#if 0 - assertEqual(d10, d11); - assertEqual(d12, d13); -#endif - -#if 0 - assertThrow(parse_datetime("2007/02/29"), datetime_error *); - assertThrow(parse_datetime("2007/13/01"), datetime_error *); - assertThrow(parse_datetime("2007/00/01"), datetime_error *); - assertThrow(parse_datetime("2007/01/00"), datetime_error *); - assertThrow(parse_datetime("2007/00/00"), datetime_error *); - assertThrow(parse_datetime("2007/05/32"), datetime_error *); - - assertThrow(parse_datetime("2006x/12/25"), datetime_error *); - assertThrow(parse_datetime("2006/12x/25"), datetime_error *); - //assertThrow(parse_datetime("2006/12/25x"), datetime_error *); - - assertThrow(parse_datetime("feb/12/25"), datetime_error *); - assertThrow(parse_datetime("2006/mon/25"), datetime_error *); - assertThrow(parse_datetime("2006/12/web"), datetime_error *); - - assertThrow(parse_datetime("12*25"), datetime_error *); - - assertThrow(parse_datetime("tuf"), datetime_error *); - assertThrow(parse_datetime("tufsday"), datetime_error *); - assertThrow(parse_datetime("fec"), datetime_error *); - assertThrow(parse_datetime("fecruary"), datetime_error *); - assertThrow(parse_datetime("207x"), datetime_error *); - assertThrow(parse_datetime("hello"), datetime_error *); - - interval_t i1; - interval_t i2; -#endif -} diff --git a/tests/corelib/numerics/DateTimeTest.h b/tests/corelib/numerics/DateTimeTest.h deleted file mode 100644 index abc914cd..00000000 --- a/tests/corelib/numerics/DateTimeTest.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef _DATETIMETEST_H -#define _DATETIMETEST_H - -#include "UnitTests.h" - -class DateTimeTestCase : public CPPUNIT_NS::TestCase -{ - CPPUNIT_TEST_SUITE(DateTimeTestCase); - - CPPUNIT_TEST(testConstructors); - - CPPUNIT_TEST_SUITE_END(); - -public: - DateTimeTestCase() {} - virtual ~DateTimeTestCase() {} - - virtual void setUp(); - virtual void tearDown(); - - void testConstructors(); - -private: - DateTimeTestCase(const DateTimeTestCase ©); - void operator=(const DateTimeTestCase ©); -}; - -#endif /* _DATETIMETEST_H */ diff --git a/tests/numerics/BasicAmount.cc b/tests/numerics/BasicAmount.cc new file mode 100644 index 00000000..90cf26ec --- /dev/null +++ b/tests/numerics/BasicAmount.cc @@ -0,0 +1,625 @@ +#include "BasicAmount.h" + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(BasicAmountTestCase, "numerics"); + +void BasicAmountTestCase::setUp() { + ledger::initialize(); +} +void BasicAmountTestCase::tearDown() { + ledger::shutdown(); +} + +void BasicAmountTestCase::testConstructors() +{ + amount_t x0; + amount_t x1(123456L); + amount_t x2(123456UL); + amount_t x3(123.456); + amount_t x5("123456"); + amount_t x6("123.456"); + amount_t x7(std::string("123456")); + amount_t x8(std::string("123.456")); + amount_t x9(x3); + amount_t x10(x6); + 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); + assertEqual(x6, x3); + assertEqual(x8, x3); + assertEqual(x10, x3); + assertEqual(x10, x9); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.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()); + CPPUNIT_ASSERT(x11.valid()); +} + +void BasicAmountTestCase::testNegation() +{ + amount_t x0; + amount_t x1(-123456L); + amount_t x3(-123.456); + amount_t x5("-123456"); + amount_t x6("-123.456"); + amount_t x7(std::string("-123456")); + amount_t x8(std::string("-123.456")); + amount_t x9(- x3); + + assertEqual(amount_t(0L), x0); + assertEqual(x5, x1); + assertEqual(x7, x1); + assertEqual(x6, x3); + assertEqual(x8, x3); + assertEqual(- x6, x9); + assertEqual(x3.negate(), x9); + + amount_t x10(x9.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() +{ + amount_t x0; + amount_t x1 = 123456L; + amount_t x2 = 123456UL; + amount_t x3 = 123.456; + amount_t x5 = "123456"; + amount_t x6 = "123.456"; + amount_t x7 = string("123456"); + amount_t x8 = string("123.456"); + amount_t x9 = x3; + amount_t x10 = amount_t(x6); + + assertEqual(amount_t(0L), x0); + assertEqual(x2, x1); + assertEqual(x5, x1); + assertEqual(x7, x1); + assertEqual(x6, x3); + assertEqual(x8, x3); + assertEqual(x10, x3); + assertEqual(x10, x9); + + x0 = amount_t(); + x1 = 123456L; + x2 = 123456UL; + x3 = 123.456; + x5 = "123456"; + x6 = "123.456"; + x7 = std::string("123456"); + x8 = std::string("123.456"); + x9 = x3; + x10 = amount_t(x6); + + assertEqual(amount_t(0L), x0); + assertEqual(x2, x1); + assertEqual(x5, x1); + assertEqual(x7, x1); + assertEqual(x6, x3); + assertEqual(x8, x3); + assertEqual(x10, x3); + assertEqual(x10, x9); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.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::testEquality() +{ + amount_t x1(123456L); + amount_t x2(456789L); + amount_t x3(333333L); + amount_t x4(123456.0); + amount_t x5("123456.0"); + amount_t x6(123456.0F); + + CPPUNIT_ASSERT(x1 == 123456L); + CPPUNIT_ASSERT(x1 != x2); + CPPUNIT_ASSERT(x1 == (x2 - x3)); + 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() +{ + amount_t x1(123L); + amount_t y1(456L); + + assertEqual(amount_t(579L), x1 + y1); + assertEqual(amount_t(579L), x1 + 456L); + assertEqual(amount_t(579L), 456L + x1); + + x1 += amount_t(456L); + assertEqual(amount_t(579L), x1); + x1 += 456L; + assertEqual(amount_t(1035L), x1); + + amount_t x4("123456789123456789123456789"); + + assertEqual(amount_t("246913578246913578246913578"), x4 + x4); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x4.valid()); +} + +void BasicAmountTestCase::testFractionalAddition() +{ + 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 BasicAmountTestCase::testIntegerSubtraction() +{ + amount_t x1(123L); + amount_t y1(456L); + + assertEqual(amount_t(333L), y1 - x1); + assertEqual(amount_t(-333L), x1 - y1); + assertEqual(amount_t(23L), x1 - 100L); + assertEqual(amount_t(-23L), 100L - x1); + + x1 -= amount_t(456L); + assertEqual(amount_t(-333L), x1); + x1 -= 456L; + assertEqual(amount_t(-789L), x1); + + amount_t x4("123456789123456789123456789"); + amount_t y4("8238725986235986"); + + assertEqual(amount_t("123456789115218063137220803"), x4 - y4); + assertEqual(amount_t("-123456789115218063137220803"), y4 - x4); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(y4.valid()); +} + +void BasicAmountTestCase::testFractionalSubtraction() +{ + 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 BasicAmountTestCase::testIntegerMultiplication() +{ + amount_t x1(123L); + amount_t y1(456L); + + 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(56088L), x1 * y1); + assertEqual(amount_t(56088L), y1 * x1); + assertEqual(amount_t(56088L), x1 * 456L); + assertEqual(amount_t(56088L), amount_t(456L) * x1); + assertEqual(amount_t(56088L), 456L * x1); + + x1 *= amount_t(123L); + assertEqual(amount_t(15129L), x1); + x1 *= 123L; + assertEqual(amount_t(1860867L), x1); + + amount_t x4("123456789123456789123456789"); + + assertEqual(amount_t("15241578780673678546105778281054720515622620750190521"), + x4 * x4); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x4.valid()); +} + +void BasicAmountTestCase::testFractionalMultiplication() +{ + 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 BasicAmountTestCase::testIntegerDivision() +{ + amount_t x1(123L); + amount_t y1(456L); + + assertThrow(x1 / 0L, amount_exception); + assertEqual(amount_t(0L), amount_t(0L) / x1); + assertEqual(amount_t(0L), 0L / x1); + assertEqual(x1, x1 / 1L); + assertEqual(amount_t("0.008130"), amount_t(1L) / x1); + assertEqual(amount_t("0.008130"), 1L / x1); + assertEqual(- x1, x1 / -1L); + assertEqual(- amount_t("0.008130"), amount_t(-1L) / x1); + assertEqual(- amount_t("0.008130"), -1L / x1); + assertEqual(amount_t("0.269737"), x1 / y1); + assertEqual(amount_t("3.707317"), y1 / x1); + assertEqual(amount_t("0.269737"), x1 / 456L); + assertEqual(amount_t("3.707317"), amount_t(456L) / x1); + assertEqual(amount_t("3.707317"), 456L / x1); + + x1 /= amount_t(456L); + assertEqual(amount_t("0.269737"), x1); + x1 /= 456L; + assertEqual(amount_t("0.00059152850877193"), x1); + + amount_t x4("123456789123456789123456789"); + amount_t y4("56"); + + 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() +{ + amount_t x1(123.123); + amount_t y1(456.456); + + assertThrow(x1 / 0L, amount_exception); + assertEqual(amount_t("0.008121959"), amount_t(1.0) / x1); + assertEqual(amount_t("0.008121959"), 1.0 / x1); + assertEqual(x1, x1 / 1.0); + assertEqual(amount_t("0.008121959"), amount_t(1.0) / x1); + assertEqual(amount_t("0.008121959"), 1.0 / x1); + assertEqual(- x1, x1 / -1.0); + assertEqual(- amount_t("0.008121959"), amount_t(-1.0) / x1); + assertEqual(- amount_t("0.008121959"), -1.0 / x1); + assertEqual(amount_t("0.269736842105263"), x1 / y1); + assertEqual(amount_t("3.707317073170732"), y1 / x1); + assertEqual(amount_t("0.269736842105263"), x1 / 456.456); + assertEqual(amount_t("3.707317073170732"), amount_t(456.456) / x1); + assertEqual(amount_t("3.707317073170732"), 456.456 / x1); + + x1 /= amount_t(456.456); + assertEqual(amount_t("0.269736842105263"), x1); + x1 /= 456.456; + assertEqual(amount_t("0.000590937225286255411255411255411255411"), x1); + x1 /= 456L; + assertEqual(amount_t("0.000001295914967733016252753094858358016252192982456140350877192982456140350877192982"), x1); + + amount_t x4("1234567891234567.89123456789"); + amount_t y4("56.789"); + + assertEqual(amount_t(1.0), x4 / x4); + assertEqual(amount_t("21739560323910.7554497273748437197344556164046"), x4 / y4); + + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(y1.valid()); + CPPUNIT_ASSERT(x4.valid()); + CPPUNIT_ASSERT(y4.valid()); +} + +void BasicAmountTestCase::testIntegerConversion() +{ + amount_t x1(123456L); + + assertEqual(true, bool(x1)); + assertEqual(123456L, long(x1)); + assertEqual(123456.0, double(x1)); + assertEqual(string("123456"), x1.to_string()); + assertEqual(string("123456"), x1.quantity_string()); + + CPPUNIT_ASSERT(x1.valid()); +} + +void BasicAmountTestCase::testFractionalConversion() +{ + amount_t x1(1234.56); + + assertEqual(true, bool(x1)); + assertEqual(1234L, long(x1)); + assertEqual(1234.56, double(x1)); + assertEqual(string("1234.56"), x1.to_string()); + assertEqual(string("1234.56"), x1.quantity_string()); + + CPPUNIT_ASSERT(x1.valid()); +} + +void BasicAmountTestCase::testFractionalRound() +{ + 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)); + + 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() +{ + 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 BasicAmountTestCase::testForZero() +{ + 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 BasicAmountTestCase::testComparisons() +{ + 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 BasicAmountTestCase::testSign() +{ + 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 BasicAmountTestCase::testAbs() +{ + amount_t x0; + amount_t x1(-1234L); + amount_t x2(1234L); + + assertEqual(amount_t(), x0.abs()); + assertEqual(amount_t(1234L), x1.abs()); + assertEqual(amount_t(1234L), x2.abs()); + + CPPUNIT_ASSERT(x0.valid()); + CPPUNIT_ASSERT(x1.valid()); + CPPUNIT_ASSERT(x2.valid()); +} + +void BasicAmountTestCase::testPrinting() +{ + 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/numerics/BasicAmount.h b/tests/numerics/BasicAmount.h new file mode 100644 index 00000000..2c107f45 --- /dev/null +++ b/tests/numerics/BasicAmount.h @@ -0,0 +1,68 @@ +#ifndef _BASICAMOUNT_H +#define _BASICAMOUNT_H + +#include "UnitTests.h" + +class BasicAmountTestCase : public CPPUNIT_NS::TestCase +{ + CPPUNIT_TEST_SUITE(BasicAmountTestCase); + + CPPUNIT_TEST(testConstructors); + CPPUNIT_TEST(testNegation); + CPPUNIT_TEST(testAssignment); + CPPUNIT_TEST(testEquality); + CPPUNIT_TEST(testIntegerAddition); + CPPUNIT_TEST(testFractionalAddition); + CPPUNIT_TEST(testIntegerSubtraction); + CPPUNIT_TEST(testFractionalSubtraction); + CPPUNIT_TEST(testIntegerMultiplication); + CPPUNIT_TEST(testFractionalMultiplication); + CPPUNIT_TEST(testIntegerDivision); + CPPUNIT_TEST(testFractionalDivision); + CPPUNIT_TEST(testIntegerConversion); + CPPUNIT_TEST(testFractionalConversion); + CPPUNIT_TEST(testFractionalRound); + CPPUNIT_TEST(testTruth); + CPPUNIT_TEST(testForZero); + CPPUNIT_TEST(testComparisons); + CPPUNIT_TEST(testSign); + CPPUNIT_TEST(testAbs); + CPPUNIT_TEST(testPrinting); + + CPPUNIT_TEST_SUITE_END(); + +public: + BasicAmountTestCase() {} + virtual ~BasicAmountTestCase() {} + + virtual void setUp(); + virtual void tearDown(); + + void testConstructors(); + void testNegation(); + void testAssignment(); + void testEquality(); + void testIntegerAddition(); + void testFractionalAddition(); + void testIntegerSubtraction(); + void testFractionalSubtraction(); + void testIntegerMultiplication(); + void testFractionalMultiplication(); + void testIntegerDivision(); + void testFractionalDivision(); + void testIntegerConversion(); + void testFractionalConversion(); + void testFractionalRound(); + void testTruth(); + void testForZero(); + void testComparisons(); + void testSign(); + void testAbs(); + void testPrinting(); + +private: + BasicAmountTestCase(const BasicAmountTestCase ©); + void operator=(const BasicAmountTestCase ©); +}; + +#endif /* _BASICAMOUNT_H */ diff --git a/tests/numerics/Commodity.cc b/tests/numerics/Commodity.cc new file mode 100644 index 00000000..e7e2a18c --- /dev/null +++ b/tests/numerics/Commodity.cc @@ -0,0 +1,55 @@ +#include "Commodity.h" + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CommodityTestCase, "numerics"); + +void CommodityTestCase::setUp() { + ledger::initialize(); +} +void CommodityTestCase::tearDown() { + ledger::shutdown(); +} + +void CommodityTestCase::testPriceHistory() +{ + ptime jan17_07 = parse_datetime("2007/01/17 00:00:00"); + ptime feb27_07 = parse_datetime("2007/02/27 18:00:00"); + ptime feb28_07 = parse_datetime("2007/02/28 06:00:00"); + ptime feb28_07sbm = parse_datetime("2007/02/28 11:59:59"); + ptime mar01_07 = parse_datetime("2007/03/01 00:00:00"); + ptime apr15_07 = parse_datetime("2007/04/15 13:00:00"); + + // jww (2007-04-17): tbd + amount_t x1("100.10 AAPL"); + + // Commodities cannot be constructed by themselves, since a great + // deal of their state depends on how they were seen to be used. + commodity_t& aapl(x1.commodity()); + + aapl.add_price(jan17_07, amount_t("$10.20")); + aapl.add_price(feb27_07, amount_t("$13.40")); + aapl.add_price(feb28_07, amount_t("$18.33")); + aapl.add_price(feb28_07sbm, amount_t("$18.30")); + aapl.add_price(mar01_07, amount_t("$19.50")); + aapl.add_price(apr15_07, amount_t("$21.22")); + + assertEqual(amount_t("$1831.83"), x1.value(feb28_07sbm)); + assertEqual(amount_t("$2124.12"), x1.value(now)); + + assertValid(x1); +} + +void CommodityTestCase::testLots() +{ + // jww (2007-04-17): tbd +} + +void CommodityTestCase::testScalingBase() +{ + // jww (2007-04-17): tbd +} + +void CommodityTestCase::testReduction() +{ + // jww (2007-04-17): tbd +} + diff --git a/tests/numerics/Commodity.h b/tests/numerics/Commodity.h new file mode 100644 index 00000000..dafa03e9 --- /dev/null +++ b/tests/numerics/Commodity.h @@ -0,0 +1,34 @@ +#ifndef _COMMMODITY_H +#define _COMMMODITY_H + +#include "UnitTests.h" + +class CommodityTestCase : public CPPUNIT_NS::TestCase +{ + CPPUNIT_TEST_SUITE(CommodityTestCase); + + CPPUNIT_TEST(testPriceHistory); + CPPUNIT_TEST(testLots); + CPPUNIT_TEST(testScalingBase); + CPPUNIT_TEST(testReduction); + + CPPUNIT_TEST_SUITE_END(); + +public: + CommodityTestCase() {} + virtual ~CommodityTestCase() {} + + virtual void setUp(); + virtual void tearDown(); + + void testPriceHistory(); + void testLots(); + void testScalingBase(); + void testReduction(); + +private: + CommodityTestCase(const CommodityTestCase ©); + void operator=(const CommodityTestCase ©); +}; + +#endif /* _COMMMODITY_H */ diff --git a/tests/numerics/CommodityAmount.cc b/tests/numerics/CommodityAmount.cc new file mode 100644 index 00000000..7ed7f403 --- /dev/null +++ b/tests/numerics/CommodityAmount.cc @@ -0,0 +1,696 @@ +#include "CommodityAmount.h" + +#define internalAmount(x) amount_t::exact(x) + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CommodityAmountTestCase, "numerics"); + +void CommodityAmountTestCase::setUp() +{ + ledger::initialize(); + + // Cause the display precision for dollars to be initialized to 2. + amount_t x1("$1.00"); + assertTrue(x1); + amount_t::full_strings = true; // makes error reports from UnitTests accurate +} + +void CommodityAmountTestCase::tearDown() +{ + amount_t::full_strings = false; + ledger::shutdown(); +} + +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(string("$123.45"), x1.to_string()); + assertEqual(string("$-123.45"), x2.to_string()); + assertEqual(string("$-123.45"), x3.to_string()); + assertEqual(string("DM 123.45"), x4.to_string()); + assertEqual(string("DM -123.45"), x5.to_string()); + assertEqual(string("DM -123.45"), x6.to_string()); + assertEqual(string("123.45 euro"), x7.to_string()); + assertEqual(string("-123.45 euro"), x8.to_string()); + assertEqual(string("123.45€"), x9.to_string()); + assertEqual(string("-123.45€"), x10.to_string()); + + assertValid(x1); + assertValid(x2); + assertValid(x3); + assertValid(x4); + assertValid(x5); + assertValid(x6); + assertValid(x7); + assertValid(x8); + assertValid(x9); + assertValid(x10); +} + +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(amount_t("$-123.45"), x1.negate()); + assertEqual(amount_t("$123.45"), x2.negate()); + assertEqual(amount_t("$123.45"), x3.negate()); + + assertEqual(string("$-123.45"), (- x1).to_string()); + assertEqual(string("$123.45"), (- x2).to_string()); + assertEqual(string("$123.45"), (- x3).to_string()); + assertEqual(string("DM -123.45"), (- x4).to_string()); + assertEqual(string("DM 123.45"), (- x5).to_string()); + assertEqual(string("DM 123.45"), (- x6).to_string()); + assertEqual(string("-123.45 euro"), (- x7).to_string()); + assertEqual(string("123.45 euro"), (- x8).to_string()); + assertEqual(string("-123.45€"), (- x9).to_string()); + assertEqual(string("123.45€"), (- x10).to_string()); + + assertEqual(amount_t("$-123.45"), x1.negate()); + assertEqual(amount_t("$123.45"), x2.negate()); + assertEqual(amount_t("$123.45"), x3.negate()); + + assertValid(x1); + assertValid(x2); + assertValid(x3); + assertValid(x4); + assertValid(x5); + assertValid(x6); + assertValid(x7); + assertValid(x8); + assertValid(x9); + assertValid(x10); +} + +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(string("$123.45"), x1.to_string()); + assertEqual(string("$-123.45"), x2.to_string()); + assertEqual(string("$-123.45"), x3.to_string()); + assertEqual(string("DM 123.45"), x4.to_string()); + assertEqual(string("DM -123.45"), x5.to_string()); + assertEqual(string("DM -123.45"), x6.to_string()); + assertEqual(string("123.45 euro"), x7.to_string()); + assertEqual(string("-123.45 euro"), x8.to_string()); + assertEqual(string("123.45€"), x9.to_string()); + assertEqual(string("-123.45€"), x10.to_string()); + + assertValid(x1); + assertValid(x2); + assertValid(x3); + assertValid(x4); + assertValid(x5); + assertValid(x6); + assertValid(x7); + assertValid(x8); + assertValid(x9); + assertValid(x10); +} + +void CommodityAmountTestCase::testEquality() +{ + amount_t x0; + 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€"; + + assertTrue(x0.null()); + assertTrue(x0.zero()); + assertTrue(x0.realzero()); + assertTrue(x0.sign() == 0); + assertTrue(x0.compare(x1) < 0); + assertTrue(x0.compare(x2) > 0); + assertTrue(x0.compare(x0) == 0); + + assertTrue(x1 != x2); + assertTrue(x1 != x4); + assertTrue(x1 != x7); + assertTrue(x1 != x9); + assertTrue(x2 == x3); + assertTrue(x4 != x5); + assertTrue(x5 == x6); + assertTrue(x7 == - x8); + assertTrue(x9 == - x10); + + assertValid(x0); + assertValid(x1); + assertValid(x2); + assertValid(x3); + assertValid(x4); + assertValid(x5); + assertValid(x6); + assertValid(x7); + assertValid(x8); + assertValid(x9); + assertValid(x10); +} + +void CommodityAmountTestCase::testAddition() +{ + amount_t x0; + amount_t x1("$123.45"); + amount_t x2(internalAmount("$123.456789")); + amount_t x3("DM 123.45"); + amount_t x4("123.45 euro"); + amount_t x5("123.45€"); + amount_t x6("123.45"); + + assertEqual(amount_t("$246.90"), x1 + x1); + assertNotEqual(amount_t("$246.91"), x1 + x2); + assertEqual(internalAmount("$246.906789"), x1 + x2); + + // Converting to string drops internal precision + assertEqual(string("$246.90"), (x1 + x1).to_string()); + assertEqual(string("$246.91"), (x1 + x2).to_string()); + + assertThrow(x1 + x0, amount_exception); + assertThrow(x1 + x3, amount_exception); + assertThrow(x1 + x4, amount_exception); + assertThrow(x1 + x5, amount_exception); + assertThrow(x1 + x6, amount_exception); + assertThrow(x1 + 123.45, amount_exception); + assertThrow(x1 + 123L, amount_exception); + + assertEqual(amount_t("DM 246.90"), x3 + x3); + assertEqual(amount_t("246.90 euro"), x4 + x4); + assertEqual(amount_t("246.90€"), x5 + x5); + + assertEqual(string("DM 246.90"), (x3 + x3).to_string()); + assertEqual(string("246.90 euro"), (x4 + x4).to_string()); + assertEqual(string("246.90€"), (x5 + x5).to_string()); + + x1 += amount_t("$456.45"); + assertEqual(amount_t("$579.90"), x1); + x1 += amount_t("$456.45"); + assertEqual(amount_t("$1036.35"), x1); + x1 += amount_t("$456"); + assertEqual(amount_t("$1492.35"), x1); + + amount_t x7(internalAmount("$123456789123456789.123456789123456789")); + + assertEqual(internalAmount("$246913578246913578.246913578246913578"), x7 + x7); + + assertValid(x1); + assertValid(x2); + assertValid(x3); + assertValid(x4); + assertValid(x5); + assertValid(x6); + assertValid(x7); +} + +void CommodityAmountTestCase::testSubtraction() +{ + amount_t x0; + amount_t x1("$123.45"); + amount_t x2(internalAmount("$123.456789")); + amount_t x3("DM 123.45"); + amount_t x4("123.45 euro"); + amount_t x5("123.45€"); + amount_t x6("123.45"); + + assertNotEqual(amount_t(), x1 - x1); + assertEqual(amount_t("$0"), x1 - x1); + assertEqual(amount_t("$23.45"), x1 - amount_t("$100.00")); + assertEqual(amount_t("$-23.45"), amount_t("$100.00") - x1); + assertNotEqual(amount_t("$-0.01"), x1 - x2); + assertEqual(internalAmount("$-0.006789"), x1 - x2); + + // Converting to string drops internal precision. If an amount is + // zero, it drops the commodity as well. + assertEqual(string("$0.00"), (x1 - x1).to_string()); + assertEqual(string("$-0.01"), (x1 - x2).to_string()); + + assertThrow(x1 - x0, amount_exception); + assertThrow(x1 - x3, amount_exception); + assertThrow(x1 - x4, amount_exception); + assertThrow(x1 - x5, amount_exception); + assertThrow(x1 - x6, amount_exception); + assertThrow(x1 - 123.45, amount_exception); + assertThrow(x1 - 123L, amount_exception); + + assertEqual(amount_t("DM 0.00"), x3 - x3); + assertEqual(amount_t("DM 23.45"), x3 - amount_t("DM 100.00")); + assertEqual(amount_t("DM -23.45"), amount_t("DM 100.00") - x3); + assertEqual(amount_t("0.00 euro"), x4 - x4); + assertEqual(amount_t("23.45 euro"), x4 - amount_t("100.00 euro")); + assertEqual(amount_t("-23.45 euro"), amount_t("100.00 euro") - x4); + assertEqual(amount_t("0.00€"), x5 - x5); + assertEqual(amount_t("23.45€"), x5 - amount_t("100.00€")); + assertEqual(amount_t("-23.45€"), amount_t("100.00€") - x5); + + assertEqual(string("DM 0.00"), (x3 - x3).to_string()); + assertEqual(string("DM 23.45"), (x3 - amount_t("DM 100.00")).to_string()); + assertEqual(string("DM -23.45"), (amount_t("DM 100.00") - x3).to_string()); + assertEqual(string("0.00 euro"), (x4 - x4).to_string()); + assertEqual(string("23.45 euro"), (x4 - amount_t("100.00 euro")).to_string()); + assertEqual(string("-23.45 euro"), (amount_t("100.00 euro") - x4).to_string()); + assertEqual(string("0.00€"), (x5 - x5).to_string()); + assertEqual(string("23.45€"), (x5 - amount_t("100.00€")).to_string()); + assertEqual(string("-23.45€"), (amount_t("100.00€") - x5).to_string()); + + x1 -= amount_t("$456.45"); + assertEqual(amount_t("$-333.00"), x1); + x1 -= amount_t("$456.45"); + assertEqual(amount_t("$-789.45"), x1); + x1 -= amount_t("$456"); + assertEqual(amount_t("$-1245.45"), x1); + + amount_t x7(internalAmount("$123456789123456789.123456789123456789")); + amount_t x8(internalAmount("$2354974984698.98459845984598")); + + assertEqual(internalAmount("$123454434148472090.138858329277476789"), x7 - x8); + assertEqual(string("$123454434148472090.138858329277476789"), (x7 - x8).to_string()); + assertEqual(string("$123454434148472090.14"), + (amount_t("$1.00") * (x7 - x8)).to_string()); + assertEqual(internalAmount("$-123454434148472090.138858329277476789"), x8 - x7); + assertEqual(string("$-123454434148472090.138858329277476789"), (x8 - x7).to_string()); + assertEqual(string("$-123454434148472090.14"), + (amount_t("$1.00") * (x8 - x7)).to_string()); + + assertValid(x1); + assertValid(x2); + assertValid(x3); + assertValid(x4); + assertValid(x5); + assertValid(x6); + assertValid(x7); + assertValid(x8); +} + +void CommodityAmountTestCase::testMultiplication() +{ + amount_t x1("$123.12"); + amount_t y1("$456.45"); + amount_t x2(internalAmount("$123.456789")); + amount_t x3("DM 123.45"); + amount_t x4("123.45 euro"); + amount_t x5("123.45€"); + + assertEqual(amount_t("$0.00"), x1 * 0L); + assertEqual(amount_t("$0.00"), 0L * x1); + assertEqual(x1, x1 * 1L); + assertEqual(x1, 1L * x1); + assertEqual(- x1, x1 * -1L); + assertEqual(- x1, -1L * x1); + assertEqual(internalAmount("$56198.124"), x1 * y1); + assertEqual(string("$56198.12"), (x1 * y1).to_string()); + assertEqual(internalAmount("$56198.124"), y1 * x1); + assertEqual(string("$56198.12"), (y1 * x1).to_string()); + + // Internal amounts retain their precision, even when being + // converted to strings + assertEqual(internalAmount("$15199.99986168"), x1 * x2); + assertEqual(internalAmount("$15199.99986168"), x2 * x1); + assertEqual(string("$15200.00"), (x1 * x2).to_string()); + assertEqual(string("$15199.99986168"), (x2 * x1).to_string()); + + assertThrow(x1 * x3, amount_exception); + assertThrow(x1 * x4, amount_exception); + assertThrow(x1 * x5, amount_exception); + + x1 *= amount_t("123.12"); + assertEqual(internalAmount("$15158.5344"), x1); + assertEqual(string("$15158.53"), x1.to_string()); + x1 *= 123.12; + assertEqual(internalAmount("$1866318.755328"), x1); + assertEqual(string("$1866318.76"), x1.to_string()); + x1 *= 123L; + assertEqual(internalAmount("$229557206.905344"), x1); + assertEqual(string("$229557206.91"), x1.to_string()); + + amount_t x7(internalAmount("$123456789123456789.123456789123456789")); + + assertEqual(internalAmount("$15241578780673678546105778311537878.046486820281054720515622620750190521"), + x7 * x7); + + assertValid(x1); + assertValid(x2); + assertValid(x3); + assertValid(x4); + assertValid(x5); + assertValid(x7); +} + +void CommodityAmountTestCase::testDivision() +{ + amount_t x1("$123.12"); + amount_t y1("$456.45"); + amount_t x2(internalAmount("$123.456789")); + amount_t x3("DM 123.45"); + amount_t x4("123.45 euro"); + amount_t x5("123.45€"); + + assertThrow(x1 / 0L, amount_exception); + assertEqual(amount_t("$0.00"), 0L / x1); + assertEqual(x1, x1 / 1L); + assertEqual(internalAmount("$0.00812216"), 1L / x1); + assertEqual(- x1, x1 / -1L); + assertEqual(internalAmount("$-0.00812216"), -1L / x1); + assertEqual(internalAmount("$0.26973382"), x1 / y1); + assertEqual(string("$0.27"), (x1 / y1).to_string()); + assertEqual(internalAmount("$3.70735867"), y1 / x1); + assertEqual(string("$3.71"), (y1 / x1).to_string()); + + // Internal amounts retain their precision, even when being + // converted to strings + assertEqual(internalAmount("$0.99727201"), x1 / x2); + assertEqual(internalAmount("$1.00273545321637426901"), x2 / x1); + assertEqual(string("$1.00"), (x1 / x2).to_string()); + assertEqual(string("$1.00273545321637426901"), (x2 / x1).to_string()); + + assertThrow(x1 / x3, amount_exception); + assertThrow(x1 / x4, amount_exception); + assertThrow(x1 / x5, amount_exception); + + x1 /= amount_t("123.12"); + assertEqual(internalAmount("$1.00"), x1); + assertEqual(string("$1.00"), x1.to_string()); + x1 /= 123.12; + assertEqual(internalAmount("$0.00812216"), x1); + assertEqual(string("$0.01"), x1.to_string()); + x1 /= 123L; + assertEqual(internalAmount("$0.00006603"), x1); + assertEqual(string("$0.00"), x1.to_string()); + + amount_t x6(internalAmount("$237235987235987.98723987235978")); + amount_t x7(internalAmount("$123456789123456789.123456789123456789")); + + assertEqual(amount_t("$1"), x7 / x7); + assertEqual(internalAmount("$0.0019216115121765559608381226612019501046413574469262"), + x6 / x7); + assertEqual(internalAmount("$520.39654928343335571379527154924040947271699678158689736256"), + x7 / x6); + + assertValid(x1); + assertValid(x2); + assertValid(x3); + assertValid(x4); + assertValid(x5); + assertValid(x6); + assertValid(x7); +} + +void CommodityAmountTestCase::testConversion() +{ + amount_t x1("$1234.56"); + + assertEqual(true, bool(x1)); + assertEqual(1234L, long(x1)); + assertEqual(1234.56, double(x1)); + assertEqual(string("$1234.56"), x1.to_string()); + assertEqual(string("1234.56"), x1.quantity_string()); + + assertValid(x1); +} + +void CommodityAmountTestCase::testRound() +{ + amount_t x1(internalAmount("$1234.567890")); + + assertEqual(internalAmount("$1234.56789"), x1.round(6)); + assertEqual(internalAmount("$1234.56789"), x1.round(5)); + assertEqual(internalAmount("$1234.5679"), x1.round(4)); + assertEqual(internalAmount("$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(internalAmount("$9876.543210")); + + assertEqual(internalAmount("$9876.543210"), x2.round(6)); + assertEqual(internalAmount("$9876.54321"), x2.round(5)); + assertEqual(internalAmount("$9876.5432"), x2.round(4)); + assertEqual(internalAmount("$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(internalAmount("$-1234.567890")); + + assertEqual(internalAmount("$-1234.56789"), x3.round(6)); + assertEqual(internalAmount("$-1234.56789"), x3.round(5)); + assertEqual(internalAmount("$-1234.5679"), x3.round(4)); + assertEqual(internalAmount("$-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(internalAmount("$-9876.543210")); + + assertEqual(internalAmount("$-9876.543210"), x4.round(6)); + assertEqual(internalAmount("$-9876.54321"), x4.round(5)); + assertEqual(internalAmount("$-9876.5432"), x4.round(4)); + assertEqual(internalAmount("$-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)); + + amount_t x5("$123.45"); + + x5 *= 100.12; + + assertEqual(internalAmount("$12359.814"), x5); + assertEqual(string("$12359.81"), x5.to_string()); + assertEqual(string("$12359.814"), x5.to_fullstring()); + assertEqual(string("$12359.814"), x5.unround().to_string()); + + assertValid(x1); + assertValid(x2); + assertValid(x3); + assertValid(x4); + assertValid(x5); +} + +void CommodityAmountTestCase::testDisplayRound() +{ + amount_t x1("$0.85"); + amount_t x2("$0.1"); + + x1 *= 0.19; + + assertNotEqual(amount_t("$0.16"), x1); + assertEqual(internalAmount("$0.1615"), x1); + assertEqual(string("$0.16"), x1.to_string()); + + assertEqual(amount_t("$0.10"), x2); + assertNotEqual(internalAmount("$0.101"), x2); + assertEqual(string("$0.10"), x2.to_string()); + + x1 *= 7L; + + assertNotEqual(amount_t("$1.13"), x1); + assertEqual(internalAmount("$1.1305"), x1); + assertEqual(string("$1.13"), x1.to_string()); +} + +void CommodityAmountTestCase::testTruth() +{ + amount_t x1("$1234"); + amount_t x2("$1234.56"); + + if (x1) + CPPUNIT_ASSERT(true); + else + CPPUNIT_ASSERT(false); + + if (x2) + CPPUNIT_ASSERT(true); + else + CPPUNIT_ASSERT(false); + + assertValid(x1); + assertValid(x2); +} + +void CommodityAmountTestCase::testForZero() +{ + amount_t x1(internalAmount("$0.000000000000000000001")); + + assertFalse(x1); + assertTrue(x1.zero()); + assertFalse(x1.realzero()); + + assertValid(x1); +} + +void CommodityAmountTestCase::testComparisons() +{ + amount_t x0; + amount_t x1("$-123"); + amount_t x2("$123.00"); + amount_t x3(internalAmount("$-123.4544")); + amount_t x4(internalAmount("$123.4544")); + amount_t x5("$-123.45"); + amount_t x6("$123.45"); + + assertTrue(x0 > x1); + assertTrue(x0 < x2); + assertTrue(x0 > x3); + assertTrue(x0 < x4); + assertTrue(x0 > x5); + assertTrue(x0 < x6); + + assertTrue(x1 > x3); + assertTrue(x3 <= x5); + assertTrue(x3 < x5); + assertTrue(x3 <= x5); + assertFalse(x3 == x5); + assertTrue(x3 < x1); + assertTrue(x3 < x4); + + assertValid(x0); + assertValid(x1); + assertValid(x2); + assertValid(x3); + assertValid(x4); + assertValid(x5); + assertValid(x6); +} + +void CommodityAmountTestCase::testSign() +{ + amount_t x0; + amount_t x1(internalAmount("$0.0000000000000000000000000000000000001")); + amount_t x2(internalAmount("$-0.0000000000000000000000000000000000001")); + amount_t x3("$1"); + amount_t x4("$-1"); + + assertFalse(x0.sign()); + assertTrue(x1.sign() != 0); + assertTrue(x2.sign() != 0); + assertTrue(x3.sign() > 0); + assertTrue(x4.sign() < 0); + + assertValid(x0); + assertValid(x1); + assertValid(x2); + assertValid(x3); + assertValid(x4); +} + +void CommodityAmountTestCase::testAbs() +{ + amount_t x0; + amount_t x1("$-1234.56"); + amount_t x2("$1234.56"); + + assertEqual(amount_t(), x0.abs()); + assertEqual(amount_t("$1234.56"), x1.abs()); + assertEqual(amount_t("$1234.56"), x2.abs()); + + assertValid(x0); + assertValid(x1); + assertValid(x2); +} + +void CommodityAmountTestCase::testPrinting() +{ + amount_t x0; + amount_t x1(internalAmount("$982340823.386238098235098235098235098")); + amount_t x2("$982340823.38"); + + { + std::ostringstream bufstr; + bufstr << x0; + + assertEqual(std::string("0"), bufstr.str()); + } + + { + std::ostringstream bufstr; + bufstr << x1; + + assertEqual(std::string("$982340823.386238098235098235098235098"), + bufstr.str()); + } + + { + std::ostringstream bufstr; + bufstr << (x1 * x2).to_string(); + + assertEqual(std::string("$964993493285024293.18099172508158508135413499124"), + bufstr.str()); + } + + { + std::ostringstream bufstr; + bufstr << (x2 * x1).to_string(); + + assertEqual(std::string("$964993493285024293.18"), bufstr.str()); + } + + assertValid(x0); + assertValid(x1); + assertValid(x2); +} + diff --git a/tests/numerics/CommodityAmount.h b/tests/numerics/CommodityAmount.h new file mode 100644 index 00000000..5ffa7810 --- /dev/null +++ b/tests/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 */ diff --git a/tests/numerics/DateTime.cc b/tests/numerics/DateTime.cc new file mode 100644 index 00000000..24b8dd16 --- /dev/null +++ b/tests/numerics/DateTime.cc @@ -0,0 +1,79 @@ +#include "DateTimeTest.h" + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(DateTimeTestCase, "numerics"); + +void DateTimeTestCase::setUp() {} +void DateTimeTestCase::tearDown() {} + +void DateTimeTestCase::testConstructors() +{ + std::time_t time_t_now = std::time(NULL); + struct tm * moment = std::localtime(&time_t_now); + + std::time_t localMoment = std::mktime(moment); + + ptime d0; + ptime d1(parse_datetime("1990/01/01")); + ptime d3(boost::posix_time::from_time_t(localMoment)); + ptime d4(parse_datetime("2006/12/25")); + //ptime d5(parse_datetime("12/25")); + ptime d6(parse_datetime("2006.12.25")); + //ptime d7(parse_datetime("12.25")); + ptime d8(parse_datetime("2006-12-25")); + //ptime d9(parse_datetime("12-25")); +#if 0 + ptime d10(parse_datetime("tue")); + ptime d11(parse_datetime("tuesday")); + ptime d12(parse_datetime("feb")); + ptime d13(parse_datetime("february")); + ptime d14(parse_datetime("2006")); +#endif + ptime d15(d3); + + assertTrue(d0.is_not_a_date_time()); + assertFalse(d1.is_not_a_date_time()); + assertFalse(d4.is_not_a_date_time()); + + assertTrue(now > d1); + //assertTrue(now <= d3); + assertTrue(now > d4); + + assertEqual(d3, d15); + assertEqual(d4, d6); + assertEqual(d4, d8); + //assertEqual(d5, d7); + //assertEqual(d5, d9); +#if 0 + assertEqual(d10, d11); + assertEqual(d12, d13); +#endif + +#if 0 + assertThrow(parse_datetime("2007/02/29"), datetime_error *); + assertThrow(parse_datetime("2007/13/01"), datetime_error *); + assertThrow(parse_datetime("2007/00/01"), datetime_error *); + assertThrow(parse_datetime("2007/01/00"), datetime_error *); + assertThrow(parse_datetime("2007/00/00"), datetime_error *); + assertThrow(parse_datetime("2007/05/32"), datetime_error *); + + assertThrow(parse_datetime("2006x/12/25"), datetime_error *); + assertThrow(parse_datetime("2006/12x/25"), datetime_error *); + //assertThrow(parse_datetime("2006/12/25x"), datetime_error *); + + assertThrow(parse_datetime("feb/12/25"), datetime_error *); + assertThrow(parse_datetime("2006/mon/25"), datetime_error *); + assertThrow(parse_datetime("2006/12/web"), datetime_error *); + + assertThrow(parse_datetime("12*25"), datetime_error *); + + assertThrow(parse_datetime("tuf"), datetime_error *); + assertThrow(parse_datetime("tufsday"), datetime_error *); + assertThrow(parse_datetime("fec"), datetime_error *); + assertThrow(parse_datetime("fecruary"), datetime_error *); + assertThrow(parse_datetime("207x"), datetime_error *); + assertThrow(parse_datetime("hello"), datetime_error *); + + interval_t i1; + interval_t i2; +#endif +} diff --git a/tests/numerics/DateTimeTest.h b/tests/numerics/DateTimeTest.h new file mode 100644 index 00000000..abc914cd --- /dev/null +++ b/tests/numerics/DateTimeTest.h @@ -0,0 +1,28 @@ +#ifndef _DATETIMETEST_H +#define _DATETIMETEST_H + +#include "UnitTests.h" + +class DateTimeTestCase : public CPPUNIT_NS::TestCase +{ + CPPUNIT_TEST_SUITE(DateTimeTestCase); + + CPPUNIT_TEST(testConstructors); + + CPPUNIT_TEST_SUITE_END(); + +public: + DateTimeTestCase() {} + virtual ~DateTimeTestCase() {} + + virtual void setUp(); + virtual void tearDown(); + + void testConstructors(); + +private: + DateTimeTestCase(const DateTimeTestCase ©); + void operator=(const DateTimeTestCase ©); +}; + +#endif /* _DATETIMETEST_H */ diff --git a/tests/python/PyUnitTests.py b/tests/python/PyUnitTests.py new file mode 100755 index 00000000..71267798 --- /dev/null +++ b/tests/python/PyUnitTests.py @@ -0,0 +1,4 @@ +#!/bin/sh + +PYTHONPATH="%builddir%":"%srcdir%":$PYTHONPATH \ + python "%srcdir%"/tests/python/UnitTests.py diff --git a/tests/python/UnitTests.py b/tests/python/UnitTests.py index 7a10c9e6..84b8432b 100644 --- a/tests/python/UnitTests.py +++ b/tests/python/UnitTests.py @@ -1,7 +1,7 @@ from unittest import TextTestRunner, TestSuite -import tests.python.corelib.numerics.BasicAmount as BasicAmount -import tests.python.corelib.numerics.CommodityAmount as CommodityAmount +import tests.python.numerics.BasicAmount as BasicAmount +import tests.python.numerics.CommodityAmount as CommodityAmount suites = [ BasicAmount.suite(), diff --git a/tests/python/corelib/.gitignore b/tests/python/corelib/.gitignore deleted file mode 100644 index 0d20b648..00000000 --- a/tests/python/corelib/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.pyc diff --git a/tests/python/corelib/__init__.py b/tests/python/corelib/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/python/corelib/balances/__init__.py b/tests/python/corelib/balances/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/python/corelib/numerics/.gitignore b/tests/python/corelib/numerics/.gitignore deleted file mode 100644 index 0d20b648..00000000 --- a/tests/python/corelib/numerics/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.pyc diff --git a/tests/python/corelib/numerics/BasicAmount.py b/tests/python/corelib/numerics/BasicAmount.py deleted file mode 100644 index bdc7dbe9..00000000 --- a/tests/python/corelib/numerics/BasicAmount.py +++ /dev/null @@ -1,527 +0,0 @@ -import sys -sys.path.append("/home/johnw/src/ledger") -sys.path.append("/home/johnw/Products/ledger") - -import unittest -import exceptions - -from ledger import amount - -class BasicAmountTestCase(unittest.TestCase): - def testConstructors(self): - x0 = amount() - x1 = amount(123456) - x2 = amount(123456L) - x3 = amount(123.456) - x5 = amount("123456") - x6 = amount("123.456") - x9 = amount(x3) - x10 = amount(x6) - - self.assertEqual(amount(0), x0) - self.assertEqual(x2, x1) - self.assertEqual(x5, x1) - self.assertEqual(x6, x3) - self.assertEqual(x10, x3) - self.assertEqual(x10, x9) - - self.assertTrue(x0.valid()) - self.assertTrue(x1.valid()) - self.assertTrue(x2.valid()) - self.assertTrue(x3.valid()) - self.assertTrue(x5.valid()) - self.assertTrue(x6.valid()) - self.assertTrue(x9.valid()) - self.assertTrue(x10.valid()) - - def testNegation(self): - x0 = amount() - x1 = amount(-123456) - x3 = amount(-123.456) - x5 = amount("-123456") - x6 = amount("-123.456") - x9 = amount(- x3) - - self.assertEqual(amount(0), x0) - self.assertEqual(x5, x1) - self.assertEqual(x6, x3) - self.assertEqual(- x6, x9) - self.assertEqual(x3.negate(), x9) - - x10 = amount(x9.negate()) - - self.assertEqual(x3, x10) - - self.assertTrue(x0.valid()) - self.assertTrue(x1.valid()) - self.assertTrue(x3.valid()) - self.assertTrue(x5.valid()) - self.assertTrue(x6.valid()) - self.assertTrue(x9.valid()) - self.assertTrue(x10.valid()) - - def testAssignment(self): - x0 = amount() - x1 = amount(123456) - x2 = amount(123456L) - x3 = amount(123.456) - x5 = amount("123456") - x6 = amount("123.456") - x9 = x3 - x10 = amount(x6) - - self.assertEqual(amount(0), x0) - self.assertEqual(x2, x1) - self.assertEqual(x5, x1) - self.assertEqual(x6, x3) - self.assertEqual(x10, x3) - self.assertEqual(x10, x9) - - x0 = amount() - x1 = amount(123456) - x2 = amount(123456L) - x3 = amount(123.456) - x5 = amount("123456") - x6 = amount("123.456") - x9 = x3 - x10 = amount(x6) - - self.assertEqual(amount(0), x0) - self.assertEqual(x2, x1) - self.assertEqual(x5, x1) - self.assertEqual(x6, x3) - self.assertEqual(x10, x3) - self.assertEqual(x10, x9) - - self.assertTrue(x0.valid()) - self.assertTrue(x1.valid()) - self.assertTrue(x2.valid()) - self.assertTrue(x3.valid()) - self.assertTrue(x5.valid()) - self.assertTrue(x6.valid()) - self.assertTrue(x9.valid()) - self.assertTrue(x10.valid()) - - def testEquality(self): - x1 = amount(123456) - x2 = amount(456789) - x3 = amount(333333) - x4 = amount(123456.0) - x5 = amount("123456.0") - - self.assertTrue(x1 == 123456) - self.assertTrue(x1 != x2) - self.assertTrue(x1 == (x2 - x3)) - self.assertTrue(x1 == x4) - self.assertTrue(x4 == x5) - - self.assertTrue(x1.valid()) - self.assertTrue(x2.valid()) - self.assertTrue(x3.valid()) - self.assertTrue(x4.valid()) - self.assertTrue(x5.valid()) - - def testIntegerAddition(self): - x1 = amount(123) - y1 = amount(456) - - self.assertEqual(amount(579), x1 + y1) - self.assertEqual(amount(579), x1 + 456) - self.assertEqual(amount(579), 456 + x1) - - x1 += amount(456) - self.assertEqual(amount(579), x1) - x1 += 456 - self.assertEqual(amount(1035), x1) - - x4 = amount("123456789123456789123456789") - - self.assertEqual(amount("246913578246913578246913578"), x4 + x4) - - self.assertTrue(x1.valid()) - self.assertTrue(y1.valid()) - self.assertTrue(x4.valid()) - - def testFractionalAddition(self): - x1 = amount(123.123) - y1 = amount(456.456) - - self.assertEqual(amount(579.579), x1 + y1) - self.assertEqual(amount(579.579), x1 + 456.456) - self.assertEqual(amount(579.579), 456.456 + x1) - - x1 += amount(456.456) - self.assertEqual(amount(579.579), x1) - x1 += 456.456 - self.assertEqual(amount(1036.035), x1) - x1 += 456 - self.assertEqual(amount(1492.035), x1) - - x2 = amount("123456789123456789.123456789123456789") - - self.assertEqual(amount("246913578246913578.246913578246913578"), x2 + x2) - - self.assertTrue(x1.valid()) - self.assertTrue(y1.valid()) - self.assertTrue(x2.valid()) - - def testIntegerSubtraction(self): - x1 = amount(123) - y1 = amount(456) - - self.assertEqual(amount(333), y1 - x1) - self.assertEqual(amount(-333), x1 - y1) - self.assertEqual(amount(23), x1 - 100) - self.assertEqual(amount(-23), 100 - x1) - - x1 -= amount(456) - self.assertEqual(amount(-333), x1) - x1 -= 456 - self.assertEqual(amount(-789), x1) - - x4 = amount("123456789123456789123456789") - y4 = amount("8238725986235986") - - self.assertEqual(amount("123456789115218063137220803"), x4 - y4) - self.assertEqual(amount("-123456789115218063137220803"), y4 - x4) - - self.assertTrue(x1.valid()) - self.assertTrue(y1.valid()) - self.assertTrue(x4.valid()) - self.assertTrue(y4.valid()) - - def testFractionalSubtraction(self): - x1 = amount(123.123) - y1 = amount(456.456) - - self.assertEqual(amount(-333.333), x1 - y1) - self.assertEqual(amount(333.333), y1 - x1) - - x1 -= amount(456.456) - self.assertEqual(amount(-333.333), x1) - x1 -= 456.456 - self.assertEqual(amount(-789.789), x1) - x1 -= 456 - self.assertEqual(amount(-1245.789), x1) - - x2 = amount("123456789123456789.123456789123456789") - y2 = amount("9872345982459.248974239578") - - self.assertEqual(amount("123446916777474329.874482549545456789"), x2 - y2) - self.assertEqual(amount("-123446916777474329.874482549545456789"), y2 - x2) - - self.assertTrue(x1.valid()) - self.assertTrue(y1.valid()) - self.assertTrue(x2.valid()) - self.assertTrue(y2.valid()) - - def testIntegerMultiplication(self): - x1 = amount(123) - y1 = amount(456) - - self.assertEqual(amount(0), x1 * 0) - self.assertEqual(amount(0), amount(0) * x1) - self.assertEqual(amount(0), 0 * x1) - self.assertEqual(x1, x1 * 1) - self.assertEqual(x1, amount(1) * x1) - self.assertEqual(x1, 1 * x1) - self.assertEqual(- x1, x1 * -1) - self.assertEqual(- x1, amount(-1) * x1) - self.assertEqual(- x1, -1 * x1) - self.assertEqual(amount(56088), x1 * y1) - self.assertEqual(amount(56088), y1 * x1) - self.assertEqual(amount(56088), x1 * 456) - self.assertEqual(amount(56088), amount(456) * x1) - self.assertEqual(amount(56088), 456 * x1) - - x1 *= amount(123) - self.assertEqual(amount(15129), x1) - x1 *= 123 - self.assertEqual(amount(1860867), x1) - - x4 = amount("123456789123456789123456789") - - self.assertEqual(amount("15241578780673678546105778281054720515622620750190521"), - x4 * x4) - - self.assertTrue(x1.valid()) - self.assertTrue(y1.valid()) - self.assertTrue(x4.valid()) - - def testFractionalMultiplication(self): - x1 = amount(123.123) - y1 = amount(456.456) - - self.assertEqual(amount(0), x1 * 0) - self.assertEqual(amount(0), amount(0) * x1) - self.assertEqual(amount(0), 0 * x1) - self.assertEqual(x1, x1 * 1) - self.assertEqual(x1, amount(1) * x1) - self.assertEqual(x1, 1 * x1) - self.assertEqual(- x1, x1 * -1) - self.assertEqual(- x1, amount(-1) * x1) - self.assertEqual(- x1, -1 * x1) - self.assertEqual(amount("56200.232088"), x1 * y1) - self.assertEqual(amount("56200.232088"), y1 * x1) - self.assertEqual(amount("56200.232088"), x1 * 456.456) - self.assertEqual(amount("56200.232088"), amount(456.456) * x1) - self.assertEqual(amount("56200.232088"), 456.456 * x1) - - x1 *= amount(123.123) - self.assertEqual(amount("15159.273129"), x1) - x1 *= 123.123 - self.assertEqual(amount("1866455.185461867"), x1) - x1 *= 123 - self.assertEqual(amount("229573987.811809641"), x1) - - x2 = amount("123456789123456789.123456789123456789") - - self.assertEqual(amount("15241578780673678546105778311537878.046486820281054720515622620750190521"), - x2 * x2) - - self.assertTrue(x1.valid()) - self.assertTrue(y1.valid()) - self.assertTrue(x2.valid()) - - def divideByZero(self, amt): - return amt / 0 - - def testIntegerDivision(self): - x1 = amount(123) - y1 = amount(456) - - self.assertRaises(exceptions.ArithmeticError, self.divideByZero, x1) - self.assertEqual(amount(0), amount(0) / x1) - self.assertEqual(amount(0), 0 / x1) - self.assertEqual(x1, x1 / 1) - self.assertEqual(amount("0.008130"), amount(1) / x1) - self.assertEqual(amount("0.008130"), 1 / x1) - self.assertEqual(- x1, x1 / -1) - self.assertEqual(- amount("0.008130"), amount(-1) / x1) - self.assertEqual(- amount("0.008130"), -1 / x1) - self.assertEqual(amount("0.269737"), x1 / y1) - self.assertEqual(amount("3.707317"), y1 / x1) - self.assertEqual(amount("0.269737"), x1 / 456) - self.assertEqual(amount("3.707317"), amount(456) / x1) - self.assertEqual(amount("3.707317"), 456 / x1) - - x1 /= amount(456) - self.assertEqual(amount("0.269737"), x1) - x1 /= 456 - self.assertEqual(amount("0.00059152850877193"), x1) - - x4 = amount("123456789123456789123456789") - y4 = amount("56") - - self.assertEqual(amount(1), x4 / x4) - self.assertEqual(amount("2204585520061728377204585.517857"), x4 / y4) - - self.assertTrue(x1.valid()) - self.assertTrue(y1.valid()) - self.assertTrue(x4.valid()) - self.assertTrue(y4.valid()) - - def testFractionalDivision(self): - x1 = amount(123.123) - y1 = amount(456.456) - - self.assertRaises(exceptions.ArithmeticError, self.divideByZero, x1) - self.assertEqual(amount("0.008121959"), amount(1.0) / x1) - self.assertEqual(amount("0.008121959"), 1.0 / x1) - self.assertEqual(x1, x1 / 1.0) - self.assertEqual(amount("0.008121959"), amount(1.0) / x1) - self.assertEqual(amount("0.008121959"), 1.0 / x1) - self.assertEqual(- x1, x1 / -1.0) - self.assertEqual(- amount("0.008121959"), amount(-1.0) / x1) - self.assertEqual(- amount("0.008121959"), -1.0 / x1) - self.assertEqual(amount("0.269736842105263"), x1 / y1) - self.assertEqual(amount("3.707317073170732"), y1 / x1) - self.assertEqual(amount("0.269736842105263"), x1 / 456.456) - self.assertEqual(amount("3.707317073170732"), amount(456.456) / x1) - self.assertEqual(amount("3.707317073170732"), 456.456 / x1) - - x1 /= amount(456.456) - self.assertEqual(amount("0.269736842105263"), x1) - x1 /= 456.456 - self.assertEqual(amount("0.000590937225286255411255411255411255411"), x1) - x1 /= 456 - self.assertEqual(amount("0.000001295914967733016252753094858358016252192982456140350877192982456140350877192982"), x1) - - x4 = amount("1234567891234567.89123456789") - y4 = amount("56.789") - - self.assertEqual(amount(1.0), x4 / x4) - self.assertEqual(amount("21739560323910.7554497273748437197344556164046"), - x4 / y4) - - self.assertTrue(x1.valid()) - self.assertTrue(y1.valid()) - self.assertTrue(x4.valid()) - self.assertTrue(y4.valid()) - - def testIntegerConversion(self): - x1 = amount(123456) - - self.assertEqual(True, bool(x1)) - self.assertEqual(123456, int(x1)) - self.assertEqual(123456.0, float(x1)) - self.assertEqual("123456", x1.to_string()) - self.assertEqual("123456", x1.quantity_string()) - - self.assertTrue(x1.valid()) - - def testFractionalConversion(self): - x1 = amount(1234.56) - - self.assertEqual(True, not (not x1)) - self.assertEqual(1234, int(x1)) - self.assertEqual(1234.56, float(x1)) - self.assertEqual("1234.56", x1.to_string()) - self.assertEqual("1234.56", x1.quantity_string()) - - self.assertTrue(x1.valid()) - - def testFractionalRound(self): - x1 = amount("1234.567890") - - self.assertEqual(amount("1234.56789"), x1.round(6)) - self.assertEqual(amount("1234.56789"), x1.round(5)) - self.assertEqual(amount("1234.5679"), x1.round(4)) - self.assertEqual(amount("1234.568"), x1.round(3)) - self.assertEqual(amount("1234.57"), x1.round(2)) - self.assertEqual(amount("1234.6"), x1.round(1)) - self.assertEqual(amount("1235"), x1.round(0)) - - x2 = amount("9876.543210") - - self.assertEqual(amount("9876.543210"), x2.round(6)) - self.assertEqual(amount("9876.54321"), x2.round(5)) - self.assertEqual(amount("9876.5432"), x2.round(4)) - self.assertEqual(amount("9876.543"), x2.round(3)) - self.assertEqual(amount("9876.54"), x2.round(2)) - self.assertEqual(amount("9876.5"), x2.round(1)) - self.assertEqual(amount("9877"), x2.round(0)) - - x3 = amount("-1234.567890") - - self.assertEqual(amount("-1234.56789"), x3.round(6)) - self.assertEqual(amount("-1234.56789"), x3.round(5)) - self.assertEqual(amount("-1234.5679"), x3.round(4)) - self.assertEqual(amount("-1234.568"), x3.round(3)) - self.assertEqual(amount("-1234.57"), x3.round(2)) - self.assertEqual(amount("-1234.6"), x3.round(1)) - self.assertEqual(amount("-1235"), x3.round(0)) - - x4 = amount("-9876.543210") - - self.assertEqual(amount("-9876.543210"), x4.round(6)) - self.assertEqual(amount("-9876.54321"), x4.round(5)) - self.assertEqual(amount("-9876.5432"), x4.round(4)) - self.assertEqual(amount("-9876.543"), x4.round(3)) - self.assertEqual(amount("-9876.54"), x4.round(2)) - self.assertEqual(amount("-9876.5"), x4.round(1)) - self.assertEqual(amount("-9877"), x4.round(0)) - - self.assertTrue(x1.valid()) - self.assertTrue(x2.valid()) - self.assertTrue(x3.valid()) - self.assertTrue(x4.valid()) - - def testTruth(self): - x0 = amount() - x1 = amount("1234") - x2 = amount("1234.56") - - self.assertTrue(not x0) - self.assertTrue(x1 ) - self.assertTrue(x2) - - self.assertTrue(x0.valid()) - self.assertTrue(x1.valid()) - self.assertTrue(x2.valid()) - - def testForZero(self): - x0 = amount() - x1 = amount("0.000000000000000000001") - - self.assertTrue(not x0) - self.assertTrue(x1) - self.assertTrue(x0.zero()) - self.assertTrue(x0.realzero()) - self.assertTrue(not x1.zero()) - self.assertTrue(not x1.realzero()) - - self.assertTrue(x0.valid()) - self.assertTrue(x1.valid()) - - def testComparisons(self): - x0 = amount() - x1 = amount(-123) - x2 = amount(123) - x3 = amount(-123.45) - x4 = amount(123.45) - x5 = amount("-123.45") - x6 = amount("123.45") - - self.assertTrue(x0 > x1) - self.assertTrue(x0 < x2) - self.assertTrue(x0 > x3) - self.assertTrue(x0 < x4) - self.assertTrue(x0 > x5) - self.assertTrue(x0 < x6) - - self.assertTrue(x1 > x3) - self.assertTrue(x3 <= x5) - self.assertTrue(x3 >= x5) - self.assertTrue(x3 < x1) - self.assertTrue(x3 < x4) - - self.assertTrue(x0.valid()) - self.assertTrue(x1.valid()) - self.assertTrue(x2.valid()) - self.assertTrue(x3.valid()) - self.assertTrue(x4.valid()) - self.assertTrue(x5.valid()) - self.assertTrue(x6.valid()) - - def testSign(self): - x0 = amount() - x1 = amount("0.0000000000000000000000000000000000001") - x2 = amount("-0.0000000000000000000000000000000000001") - x3 = amount("1") - x4 = amount("-1") - - self.assertTrue(not x0.sign()) - self.assertTrue(x1.sign() > 0) - self.assertTrue(x2.sign() < 0) - self.assertTrue(x3.sign() > 0) - self.assertTrue(x4.sign() < 0) - - self.assertTrue(x0.valid()) - self.assertTrue(x1.valid()) - self.assertTrue(x2.valid()) - self.assertTrue(x3.valid()) - self.assertTrue(x4.valid()) - - def testAbs(self): - x0 = amount() - x1 = amount(-1234) - x2 = amount(1234) - - self.assertEqual(amount(), abs(x0)) - self.assertEqual(amount(1234), abs(x1)) - self.assertEqual(amount(1234), abs(x2)) - - self.assertTrue(x0.valid()) - self.assertTrue(x1.valid()) - self.assertTrue(x2.valid()) - - def testPrinting(self): - pass - - -def suite(): - return unittest.TestLoader().loadTestsFromTestCase(BasicAmountTestCase) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/python/corelib/numerics/CommodityAmount.py b/tests/python/corelib/numerics/CommodityAmount.py deleted file mode 100644 index 80f58b21..00000000 --- a/tests/python/corelib/numerics/CommodityAmount.py +++ /dev/null @@ -1,623 +0,0 @@ -# -*- coding: utf-8 -*- - -import unittest -import exceptions -import operator - -from ledger import amount - -internalAmount = amount.exact - -class CommodityAmountTestCase(unittest.TestCase): - def setUp(self): - # Cause the display precision for dollars to be initialized to 2. - x1 = amount("$1.00") - self.assertTrue(x1) - amount.full_strings = True # makes error reports from UnitTests accurate - - def tearDown(self): - amount.full_strings = False - - def assertValid(self, amt): - self.assertTrue(amt.valid()) - - def testConstructors(self): - x1 = amount("$123.45") - x2 = amount("-$123.45") - x3 = amount("$-123.45") - x4 = amount("DM 123.45") - x5 = amount("-DM 123.45") - x6 = amount("DM -123.45") - x7 = amount("123.45 euro") - x8 = amount("-123.45 euro") - x9 = amount("123.45€") - x10 = amount("-123.45€") - - self.assertEqual(amount("$123.45"), x1) - self.assertEqual(amount("-$123.45"), x2) - self.assertEqual(amount("$-123.45"), x3) - self.assertEqual(amount("DM 123.45"), x4) - self.assertEqual(amount("-DM 123.45"), x5) - self.assertEqual(amount("DM -123.45"), x6) - self.assertEqual(amount("123.45 euro"), x7) - self.assertEqual(amount("-123.45 euro"), x8) - self.assertEqual(amount("123.45€"), x9) - self.assertEqual(amount("-123.45€"), x10) - - self.assertEqual("$123.45", x1.to_string()) - self.assertEqual("$-123.45", x2.to_string()) - self.assertEqual("$-123.45", x3.to_string()) - self.assertEqual("DM 123.45", x4.to_string()) - self.assertEqual("DM -123.45", x5.to_string()) - self.assertEqual("DM -123.45", x6.to_string()) - self.assertEqual("123.45 euro", x7.to_string()) - self.assertEqual("-123.45 euro", x8.to_string()) - self.assertEqual("123.45€", x9.to_string()) - self.assertEqual("-123.45€", x10.to_string()) - - self.assertValid(x1) - self.assertValid(x2) - self.assertValid(x3) - self.assertValid(x4) - self.assertValid(x5) - self.assertValid(x6) - self.assertValid(x7) - self.assertValid(x8) - self.assertValid(x9) - self.assertValid(x10) - - def testNegation(self): - x1 = amount("$123.45") - x2 = amount("-$123.45") - x3 = amount("$-123.45") - x4 = amount("DM 123.45") - x5 = amount("-DM 123.45") - x6 = amount("DM -123.45") - x7 = amount("123.45 euro") - x8 = amount("-123.45 euro") - x9 = amount("123.45€") - x10 = amount("-123.45€") - - self.assertEqual(amount("$-123.45"), - x1) - self.assertEqual(amount("$123.45"), - x2) - self.assertEqual(amount("$123.45"), - x3) - self.assertEqual(amount("DM -123.45"), - x4) - self.assertEqual(amount("DM 123.45"), - x5) - self.assertEqual(amount("DM 123.45"), - x6) - self.assertEqual(amount("-123.45 euro"), - x7) - self.assertEqual(amount("123.45 euro"), - x8) - self.assertEqual(amount("-123.45€"), - x9) - self.assertEqual(amount("123.45€"), - x10) - - self.assertEqual(amount("$-123.45"), x1.negate()) - self.assertEqual(amount("$123.45"), x2.negate()) - self.assertEqual(amount("$123.45"), x3.negate()) - - self.assertEqual("$-123.45", (- x1).to_string()) - self.assertEqual("$123.45", (- x2).to_string()) - self.assertEqual("$123.45", (- x3).to_string()) - self.assertEqual("DM -123.45", (- x4).to_string()) - self.assertEqual("DM 123.45", (- x5).to_string()) - self.assertEqual("DM 123.45", (- x6).to_string()) - self.assertEqual("-123.45 euro", (- x7).to_string()) - self.assertEqual("123.45 euro", (- x8).to_string()) - self.assertEqual("-123.45€", (- x9).to_string()) - self.assertEqual("123.45€", (- x10).to_string()) - - self.assertEqual(amount("$-123.45"), x1.negate()) - self.assertEqual(amount("$123.45"), x2.negate()) - self.assertEqual(amount("$123.45"), x3.negate()) - - self.assertValid(x1) - self.assertValid(x2) - self.assertValid(x3) - self.assertValid(x4) - self.assertValid(x5) - self.assertValid(x6) - self.assertValid(x7) - self.assertValid(x8) - self.assertValid(x9) - self.assertValid(x10) - - def testAssignment(self): - x1 = amount("$123.45") - x2 = amount("-$123.45") - x3 = amount("$-123.45") - x4 = amount("DM 123.45") - x5 = amount("-DM 123.45") - x6 = amount("DM -123.45") - x7 = amount("123.45 euro") - x8 = amount("-123.45 euro") - x9 = amount("123.45€") - x10 = amount("-123.45€") - - self.assertEqual(amount("$123.45"), x1) - self.assertEqual(amount("-$123.45"), x2) - self.assertEqual(amount("$-123.45"), x3) - self.assertEqual(amount("DM 123.45"), x4) - self.assertEqual(amount("-DM 123.45"), x5) - self.assertEqual(amount("DM -123.45"), x6) - self.assertEqual(amount("123.45 euro"), x7) - self.assertEqual(amount("-123.45 euro"), x8) - self.assertEqual(amount("123.45€"), x9) - self.assertEqual(amount("-123.45€"), x10) - - self.assertEqual("$123.45", x1.to_string()) - self.assertEqual("$-123.45", x2.to_string()) - self.assertEqual("$-123.45", x3.to_string()) - self.assertEqual("DM 123.45", x4.to_string()) - self.assertEqual("DM -123.45", x5.to_string()) - self.assertEqual("DM -123.45", x6.to_string()) - self.assertEqual("123.45 euro", x7.to_string()) - self.assertEqual("-123.45 euro", x8.to_string()) - self.assertEqual("123.45€", x9.to_string()) - self.assertEqual("-123.45€", x10.to_string()) - - self.assertValid(x1) - self.assertValid(x2) - self.assertValid(x3) - self.assertValid(x4) - self.assertValid(x5) - self.assertValid(x6) - self.assertValid(x7) - self.assertValid(x8) - self.assertValid(x9) - self.assertValid(x10) - - def testEquality(self): - x0 = amount() - x1 = amount("$123.45") - x2 = amount("-$123.45") - x3 = amount("$-123.45") - x4 = amount("DM 123.45") - x5 = amount("-DM 123.45") - x6 = amount("DM -123.45") - x7 = amount("123.45 euro") - x8 = amount("-123.45 euro") - x9 = amount("123.45€") - x10 = amount("-123.45€") - - self.assertTrue(x0.null()) - self.assertTrue(x0.zero()) - self.assertTrue(x0.realzero()) - self.assertTrue(x0.sign() == 0) - self.assertTrue(x0.compare(x1) < 0) - self.assertTrue(x0.compare(x2) > 0) - self.assertTrue(x0.compare(x0) == 0) - - self.assertTrue(x1 != x2) - self.assertTrue(x1 != x4) - self.assertTrue(x1 != x7) - self.assertTrue(x1 != x9) - self.assertTrue(x2 == x3) - self.assertTrue(x4 != x5) - self.assertTrue(x5 == x6) - self.assertTrue(x7 == - x8) - self.assertTrue(x9 == - x10) - - self.assertValid(x0) - self.assertValid(x1) - self.assertValid(x2) - self.assertValid(x3) - self.assertValid(x4) - self.assertValid(x5) - self.assertValid(x6) - self.assertValid(x7) - self.assertValid(x8) - self.assertValid(x9) - self.assertValid(x10) - - def testAddition(self): - x0 = amount() - x1 = amount("$123.45") - x2 = amount(internalAmount("$123.456789")) - x3 = amount("DM 123.45") - x4 = amount("123.45 euro") - x5 = amount("123.45€") - x6 = amount("123.45") - - self.assertEqual(amount("$246.90"), x1 + x1) - self.assertNotEqual(amount("$246.91"), x1 + x2) - self.assertEqual(internalAmount("$246.906789"), x1 + x2) - - # Converting to string drops internal precision - self.assertEqual("$246.90", (x1 + x1).to_string()) - self.assertEqual("$246.91", (x1 + x2).to_string()) - - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x0) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x3) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x4) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x5) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x6) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, 123.45) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, 123) - - self.assertEqual(amount("DM 246.90"), x3 + x3) - self.assertEqual(amount("246.90 euro"), x4 + x4) - self.assertEqual(amount("246.90€"), x5 + x5) - - self.assertEqual("DM 246.90", (x3 + x3).to_string()) - self.assertEqual("246.90 euro", (x4 + x4).to_string()) - self.assertEqual("246.90€", (x5 + x5).to_string()) - - x1 += amount("$456.45") - self.assertEqual(amount("$579.90"), x1) - x1 += amount("$456.45") - self.assertEqual(amount("$1036.35"), x1) - x1 += amount("$456") - self.assertEqual(amount("$1492.35"), x1) - - x7 = amount(internalAmount("$123456789123456789.123456789123456789")) - - self.assertEqual(internalAmount("$246913578246913578.246913578246913578"), x7 + x7) - - self.assertValid(x1) - self.assertValid(x2) - self.assertValid(x3) - self.assertValid(x4) - self.assertValid(x5) - self.assertValid(x6) - self.assertValid(x7) - - def testSubtraction(self): - x0 = amount() - x1 = amount("$123.45") - x2 = amount(internalAmount("$123.456789")) - x3 = amount("DM 123.45") - x4 = amount("123.45 euro") - x5 = amount("123.45€") - x6 = amount("123.45") - - self.assertNotEqual(amount(), x1 - x1) - self.assertEqual(amount("$0"), x1 - x1) - self.assertEqual(amount("$23.45"), x1 - amount("$100.00")) - self.assertEqual(amount("$-23.45"), amount("$100.00") - x1) - self.assertNotEqual(amount("$-0.01"), x1 - x2) - self.assertEqual(internalAmount("$-0.006789"), x1 - x2) - - # Converting to string drops internal precision. If an amount is - # zero, it drops the commodity as well. - self.assertEqual("$0.00", (x1 - x1).to_string()) - self.assertEqual("$-0.01", (x1 - x2).to_string()) - - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x0) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x3) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x4) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x5) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x6) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, 123.45) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, 123) - - self.assertEqual(amount("DM 0.00"), x3 - x3) - self.assertEqual(amount("DM 23.45"), x3 - amount("DM 100.00")) - self.assertEqual(amount("DM -23.45"), amount("DM 100.00") - x3) - self.assertEqual(amount("0.00 euro"), x4 - x4) - self.assertEqual(amount("23.45 euro"), x4 - amount("100.00 euro")) - self.assertEqual(amount("-23.45 euro"), amount("100.00 euro") - x4) - self.assertEqual(amount("0.00€"), x5 - x5) - self.assertEqual(amount("23.45€"), x5 - amount("100.00€")) - self.assertEqual(amount("-23.45€"), amount("100.00€") - x5) - - self.assertEqual("DM 0.00", (x3 - x3).to_string()) - self.assertEqual("DM 23.45", (x3 - amount("DM 100.00")).to_string()) - self.assertEqual("DM -23.45", (amount("DM 100.00") - x3).to_string()) - self.assertEqual("0.00 euro", (x4 - x4).to_string()) - self.assertEqual("23.45 euro", (x4 - amount("100.00 euro")).to_string()) - self.assertEqual("-23.45 euro", (amount("100.00 euro") - x4).to_string()) - self.assertEqual("0.00€", (x5 - x5).to_string()) - self.assertEqual("23.45€", (x5 - amount("100.00€")).to_string()) - self.assertEqual("-23.45€", (amount("100.00€") - x5).to_string()) - - x1 -= amount("$456.45") - self.assertEqual(amount("$-333.00"), x1) - x1 -= amount("$456.45") - self.assertEqual(amount("$-789.45"), x1) - x1 -= amount("$456") - self.assertEqual(amount("$-1245.45"), x1) - - x7 = amount(internalAmount("$123456789123456789.123456789123456789")) - x8 = amount(internalAmount("$2354974984698.98459845984598")) - - self.assertEqual(internalAmount("$123454434148472090.138858329277476789"), x7 - x8) - self.assertEqual("$123454434148472090.138858329277476789", (x7 - x8).to_string()) - self.assertEqual("$123454434148472090.14", - (amount("$1.00") * (x7 - x8)).to_string()) - self.assertEqual(internalAmount("$-123454434148472090.138858329277476789"), x8 - x7) - self.assertEqual("$-123454434148472090.138858329277476789", (x8 - x7).to_string()) - self.assertEqual("$-123454434148472090.14", - (amount("$1.00") * (x8 - x7)).to_string()) - - self.assertValid(x1) - self.assertValid(x2) - self.assertValid(x3) - self.assertValid(x4) - self.assertValid(x5) - self.assertValid(x6) - self.assertValid(x7) - self.assertValid(x8) - - def testMultiplication(self): - x1 = amount("$123.12") - y1 = amount("$456.45") - x2 = amount(internalAmount("$123.456789")) - x3 = amount("DM 123.45") - x4 = amount("123.45 euro") - x5 = amount("123.45€") - - self.assertEqual(amount("$0.00"), x1 * 0) - self.assertEqual(amount("$0.00"), 0 * x1) - self.assertEqual(x1, x1 * 1) - self.assertEqual(x1, 1 * x1) - self.assertEqual(- x1, x1 * -1) - self.assertEqual(- x1, -1 * x1) - self.assertEqual(internalAmount("$56198.124"), x1 * y1) - self.assertEqual("$56198.12", (x1 * y1).to_string()) - self.assertEqual(internalAmount("$56198.124"), y1 * x1) - self.assertEqual("$56198.12", (y1 * x1).to_string()) - - # Internal amounts retain their precision, even when being - # converted to strings - self.assertEqual(internalAmount("$15199.99986168"), x1 * x2) - self.assertEqual(internalAmount("$15199.99986168"), x2 * x1) - self.assertEqual("$15200.00", (x1 * x2).to_string()) - self.assertEqual("$15199.99986168", (x2 * x1).to_string()) - - self.assertRaises(exceptions.ArithmeticError, operator.mul, x1, x3) - self.assertRaises(exceptions.ArithmeticError, operator.mul, x1, x4) - self.assertRaises(exceptions.ArithmeticError, operator.mul, x1, x5) - - x1 *= amount("123.12") - self.assertEqual(internalAmount("$15158.5344"), x1) - self.assertEqual("$15158.53", x1.to_string()) - x1 *= 123.12 - self.assertEqual(internalAmount("$1866318.755328"), x1) - self.assertEqual("$1866318.76", x1.to_string()) - x1 *= 123 - self.assertEqual(internalAmount("$229557206.905344"), x1) - self.assertEqual("$229557206.91", x1.to_string()) - - x7 = amount(internalAmount("$123456789123456789.123456789123456789")) - - self.assertEqual(internalAmount("$15241578780673678546105778311537878.046486820281054720515622620750190521"), - x7 * x7) - - self.assertValid(x1) - self.assertValid(x2) - self.assertValid(x3) - self.assertValid(x4) - self.assertValid(x5) - self.assertValid(x7) - - def testDivision(self): - x1 = amount("$123.12") - y1 = amount("$456.45") - x2 = amount(internalAmount("$123.456789")) - x3 = amount("DM 123.45") - x4 = amount("123.45 euro") - x5 = amount("123.45€") - - self.assertRaises(exceptions.ArithmeticError, operator.div, x1, 0) - self.assertEqual(amount("$0.00"), 0 / x1) - self.assertEqual(x1, x1 / 1) - self.assertEqual(internalAmount("$0.00812216"), 1 / x1) - self.assertEqual(- x1, x1 / -1) - self.assertEqual(internalAmount("$-0.00812216"), -1 / x1) - self.assertEqual(internalAmount("$0.26973382"), x1 / y1) - self.assertEqual("$0.27", (x1 / y1).to_string()) - self.assertEqual(internalAmount("$3.70735867"), y1 / x1) - self.assertEqual("$3.71", (y1 / x1).to_string()) - - # Internal amounts retain their precision, even when being - # converted to strings - self.assertEqual(internalAmount("$0.99727201"), x1 / x2) - self.assertEqual(internalAmount("$1.00273545321637426901"), x2 / x1) - self.assertEqual("$1.00", (x1 / x2).to_string()) - self.assertEqual("$1.00273545321637426901", (x2 / x1).to_string()) - - self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x3) - self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x4) - self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x5) - - x1 /= amount("123.12") - self.assertEqual(internalAmount("$1.00"), x1) - self.assertEqual("$1.00", x1.to_string()) - x1 /= 123.12 - self.assertEqual(internalAmount("$0.00812216"), x1) - self.assertEqual("$0.01", x1.to_string()) - x1 /= 123 - self.assertEqual(internalAmount("$0.00006603"), x1) - self.assertEqual("$0.00", x1.to_string()) - - x6 = amount(internalAmount("$237235987235987.98723987235978")) - x7 = amount(internalAmount("$123456789123456789.123456789123456789")) - - self.assertEqual(amount("$1"), x7 / x7) - self.assertEqual(internalAmount("$0.0019216115121765559608381226612019501046413574469262"), - x6 / x7) - self.assertEqual(internalAmount("$520.39654928343335571379527154924040947271699678158689736256"), - x7 / x6) - - self.assertValid(x1) - self.assertValid(x2) - self.assertValid(x3) - self.assertValid(x4) - self.assertValid(x5) - self.assertValid(x6) - self.assertValid(x7) - - def testConversion(self): - x1 = amount("$1234.56") - - self.assertEqual(True, bool(x1)) - self.assertEqual(1234, int(x1)) - self.assertEqual(1234.56, float(x1)) - self.assertEqual("$1234.56", x1.to_string()) - self.assertEqual("1234.56", x1.quantity_string()) - - self.assertValid(x1) - - def testRound(self): - x1 = amount(internalAmount("$1234.567890")) - - self.assertEqual(internalAmount("$1234.56789"), x1.round(6)) - self.assertEqual(internalAmount("$1234.56789"), x1.round(5)) - self.assertEqual(internalAmount("$1234.5679"), x1.round(4)) - self.assertEqual(internalAmount("$1234.568"), x1.round(3)) - self.assertEqual(amount("$1234.57"), x1.round(2)) - self.assertEqual(amount("$1234.6"), x1.round(1)) - self.assertEqual(amount("$1235"), x1.round(0)) - - x2 = amount(internalAmount("$9876.543210")) - - self.assertEqual(internalAmount("$9876.543210"), x2.round(6)) - self.assertEqual(internalAmount("$9876.54321"), x2.round(5)) - self.assertEqual(internalAmount("$9876.5432"), x2.round(4)) - self.assertEqual(internalAmount("$9876.543"), x2.round(3)) - self.assertEqual(amount("$9876.54"), x2.round(2)) - self.assertEqual(amount("$9876.5"), x2.round(1)) - self.assertEqual(amount("$9877"), x2.round(0)) - - x3 = amount(internalAmount("$-1234.567890")) - - self.assertEqual(internalAmount("$-1234.56789"), x3.round(6)) - self.assertEqual(internalAmount("$-1234.56789"), x3.round(5)) - self.assertEqual(internalAmount("$-1234.5679"), x3.round(4)) - self.assertEqual(internalAmount("$-1234.568"), x3.round(3)) - self.assertEqual(amount("$-1234.57"), x3.round(2)) - self.assertEqual(amount("$-1234.6"), x3.round(1)) - self.assertEqual(amount("$-1235"), x3.round(0)) - - x4 = amount(internalAmount("$-9876.543210")) - - self.assertEqual(internalAmount("$-9876.543210"), x4.round(6)) - self.assertEqual(internalAmount("$-9876.54321"), x4.round(5)) - self.assertEqual(internalAmount("$-9876.5432"), x4.round(4)) - self.assertEqual(internalAmount("$-9876.543"), x4.round(3)) - self.assertEqual(amount("$-9876.54"), x4.round(2)) - self.assertEqual(amount("$-9876.5"), x4.round(1)) - self.assertEqual(amount("$-9877"), x4.round(0)) - - self.assertValid(x1) - self.assertValid(x2) - self.assertValid(x3) - self.assertValid(x4) - - def testDisplayRound(self): - x1 = amount("$0.85") - x2 = amount("$0.1") - - x1 *= 0.19 - - self.assertNotEqual(amount("$0.16"), x1) - self.assertEqual(internalAmount("$0.1615"), x1) - self.assertEqual("$0.16", x1.to_string()) - - self.assertEqual(amount("$0.10"), x2) - self.assertNotEqual(internalAmount("$0.101"), x2) - self.assertEqual("$0.10", x2.to_string()) - - x1 *= 7 - - self.assertNotEqual(amount("$1.13"), x1) - self.assertEqual(internalAmount("$1.1305"), x1) - self.assertEqual("$1.13", x1.to_string()) - - def testTruth(self): - x1 = amount("$1234") - x2 = amount("$1234.56") - - if x1: - self.assertTrue(True) - else: - self.assertTrue(False) - - if x2: - self.assertTrue(True) - else: - self.assertTrue(False) - - self.assertValid(x1) - self.assertValid(x2) - - def testForZero(self): - x1 = amount(internalAmount("$0.000000000000000000001")) - - self.assertFalse(x1) - self.assertTrue(x1.zero()) - self.assertFalse(x1.realzero()) - - self.assertValid(x1) - - def testComparisons(self): - x0 = amount() - x1 = amount("$-123") - x2 = amount("$123.00") - x3 = amount(internalAmount("$-123.4544")) - x4 = amount(internalAmount("$123.4544")) - x5 = amount("$-123.45") - x6 = amount("$123.45") - - self.assertTrue(x0 > x1) - self.assertTrue(x0 < x2) - self.assertTrue(x0 > x3) - self.assertTrue(x0 < x4) - self.assertTrue(x0 > x5) - self.assertTrue(x0 < x6) - - self.assertTrue(x1 > x3) - self.assertTrue(x3 <= x5) - self.assertTrue(x3 < x5) - self.assertTrue(x3 <= x5) - self.assertFalse(x3 == x5) - self.assertTrue(x3 < x1) - self.assertTrue(x3 < x4) - - self.assertValid(x0) - self.assertValid(x1) - self.assertValid(x2) - self.assertValid(x3) - self.assertValid(x4) - self.assertValid(x5) - self.assertValid(x6) - - def testSign(self): - x0 = amount() - x1 = amount(internalAmount("$0.0000000000000000000000000000000000001")) - x2 = amount(internalAmount("$-0.0000000000000000000000000000000000001")) - x3 = amount("$1") - x4 = amount("$-1") - - self.assertFalse(x0.sign()) - self.assertTrue(x1.sign() != 0) - self.assertTrue(x2.sign() != 0) - self.assertTrue(x3.sign() > 0) - self.assertTrue(x4.sign() < 0) - - self.assertValid(x0) - self.assertValid(x1) - self.assertValid(x2) - self.assertValid(x3) - self.assertValid(x4) - - def testAbs(self): - x0 = amount() - x1 = amount("$-1234.56") - x2 = amount("$1234.56") - - self.assertEqual(amount(), abs(x0)) - self.assertEqual(amount("$1234.56"), abs(x1)) - self.assertEqual(amount("$1234.56"), abs(x2)) - - self.assertValid(x0) - self.assertValid(x1) - self.assertValid(x2) - - def testPrinting(self): - pass - - -def suite(): - return unittest.TestLoader().loadTestsFromTestCase(CommodityAmountTestCase) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/python/corelib/numerics/__init__.py b/tests/python/corelib/numerics/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/python/corelib/values/__init__.py b/tests/python/corelib/values/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/python/numerics/.gitignore b/tests/python/numerics/.gitignore new file mode 100644 index 00000000..0d20b648 --- /dev/null +++ b/tests/python/numerics/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/tests/python/numerics/BasicAmount.py b/tests/python/numerics/BasicAmount.py new file mode 100644 index 00000000..bdc7dbe9 --- /dev/null +++ b/tests/python/numerics/BasicAmount.py @@ -0,0 +1,527 @@ +import sys +sys.path.append("/home/johnw/src/ledger") +sys.path.append("/home/johnw/Products/ledger") + +import unittest +import exceptions + +from ledger import amount + +class BasicAmountTestCase(unittest.TestCase): + def testConstructors(self): + x0 = amount() + x1 = amount(123456) + x2 = amount(123456L) + x3 = amount(123.456) + x5 = amount("123456") + x6 = amount("123.456") + x9 = amount(x3) + x10 = amount(x6) + + self.assertEqual(amount(0), x0) + self.assertEqual(x2, x1) + self.assertEqual(x5, x1) + self.assertEqual(x6, x3) + self.assertEqual(x10, x3) + self.assertEqual(x10, x9) + + self.assertTrue(x0.valid()) + self.assertTrue(x1.valid()) + self.assertTrue(x2.valid()) + self.assertTrue(x3.valid()) + self.assertTrue(x5.valid()) + self.assertTrue(x6.valid()) + self.assertTrue(x9.valid()) + self.assertTrue(x10.valid()) + + def testNegation(self): + x0 = amount() + x1 = amount(-123456) + x3 = amount(-123.456) + x5 = amount("-123456") + x6 = amount("-123.456") + x9 = amount(- x3) + + self.assertEqual(amount(0), x0) + self.assertEqual(x5, x1) + self.assertEqual(x6, x3) + self.assertEqual(- x6, x9) + self.assertEqual(x3.negate(), x9) + + x10 = amount(x9.negate()) + + self.assertEqual(x3, x10) + + self.assertTrue(x0.valid()) + self.assertTrue(x1.valid()) + self.assertTrue(x3.valid()) + self.assertTrue(x5.valid()) + self.assertTrue(x6.valid()) + self.assertTrue(x9.valid()) + self.assertTrue(x10.valid()) + + def testAssignment(self): + x0 = amount() + x1 = amount(123456) + x2 = amount(123456L) + x3 = amount(123.456) + x5 = amount("123456") + x6 = amount("123.456") + x9 = x3 + x10 = amount(x6) + + self.assertEqual(amount(0), x0) + self.assertEqual(x2, x1) + self.assertEqual(x5, x1) + self.assertEqual(x6, x3) + self.assertEqual(x10, x3) + self.assertEqual(x10, x9) + + x0 = amount() + x1 = amount(123456) + x2 = amount(123456L) + x3 = amount(123.456) + x5 = amount("123456") + x6 = amount("123.456") + x9 = x3 + x10 = amount(x6) + + self.assertEqual(amount(0), x0) + self.assertEqual(x2, x1) + self.assertEqual(x5, x1) + self.assertEqual(x6, x3) + self.assertEqual(x10, x3) + self.assertEqual(x10, x9) + + self.assertTrue(x0.valid()) + self.assertTrue(x1.valid()) + self.assertTrue(x2.valid()) + self.assertTrue(x3.valid()) + self.assertTrue(x5.valid()) + self.assertTrue(x6.valid()) + self.assertTrue(x9.valid()) + self.assertTrue(x10.valid()) + + def testEquality(self): + x1 = amount(123456) + x2 = amount(456789) + x3 = amount(333333) + x4 = amount(123456.0) + x5 = amount("123456.0") + + self.assertTrue(x1 == 123456) + self.assertTrue(x1 != x2) + self.assertTrue(x1 == (x2 - x3)) + self.assertTrue(x1 == x4) + self.assertTrue(x4 == x5) + + self.assertTrue(x1.valid()) + self.assertTrue(x2.valid()) + self.assertTrue(x3.valid()) + self.assertTrue(x4.valid()) + self.assertTrue(x5.valid()) + + def testIntegerAddition(self): + x1 = amount(123) + y1 = amount(456) + + self.assertEqual(amount(579), x1 + y1) + self.assertEqual(amount(579), x1 + 456) + self.assertEqual(amount(579), 456 + x1) + + x1 += amount(456) + self.assertEqual(amount(579), x1) + x1 += 456 + self.assertEqual(amount(1035), x1) + + x4 = amount("123456789123456789123456789") + + self.assertEqual(amount("246913578246913578246913578"), x4 + x4) + + self.assertTrue(x1.valid()) + self.assertTrue(y1.valid()) + self.assertTrue(x4.valid()) + + def testFractionalAddition(self): + x1 = amount(123.123) + y1 = amount(456.456) + + self.assertEqual(amount(579.579), x1 + y1) + self.assertEqual(amount(579.579), x1 + 456.456) + self.assertEqual(amount(579.579), 456.456 + x1) + + x1 += amount(456.456) + self.assertEqual(amount(579.579), x1) + x1 += 456.456 + self.assertEqual(amount(1036.035), x1) + x1 += 456 + self.assertEqual(amount(1492.035), x1) + + x2 = amount("123456789123456789.123456789123456789") + + self.assertEqual(amount("246913578246913578.246913578246913578"), x2 + x2) + + self.assertTrue(x1.valid()) + self.assertTrue(y1.valid()) + self.assertTrue(x2.valid()) + + def testIntegerSubtraction(self): + x1 = amount(123) + y1 = amount(456) + + self.assertEqual(amount(333), y1 - x1) + self.assertEqual(amount(-333), x1 - y1) + self.assertEqual(amount(23), x1 - 100) + self.assertEqual(amount(-23), 100 - x1) + + x1 -= amount(456) + self.assertEqual(amount(-333), x1) + x1 -= 456 + self.assertEqual(amount(-789), x1) + + x4 = amount("123456789123456789123456789") + y4 = amount("8238725986235986") + + self.assertEqual(amount("123456789115218063137220803"), x4 - y4) + self.assertEqual(amount("-123456789115218063137220803"), y4 - x4) + + self.assertTrue(x1.valid()) + self.assertTrue(y1.valid()) + self.assertTrue(x4.valid()) + self.assertTrue(y4.valid()) + + def testFractionalSubtraction(self): + x1 = amount(123.123) + y1 = amount(456.456) + + self.assertEqual(amount(-333.333), x1 - y1) + self.assertEqual(amount(333.333), y1 - x1) + + x1 -= amount(456.456) + self.assertEqual(amount(-333.333), x1) + x1 -= 456.456 + self.assertEqual(amount(-789.789), x1) + x1 -= 456 + self.assertEqual(amount(-1245.789), x1) + + x2 = amount("123456789123456789.123456789123456789") + y2 = amount("9872345982459.248974239578") + + self.assertEqual(amount("123446916777474329.874482549545456789"), x2 - y2) + self.assertEqual(amount("-123446916777474329.874482549545456789"), y2 - x2) + + self.assertTrue(x1.valid()) + self.assertTrue(y1.valid()) + self.assertTrue(x2.valid()) + self.assertTrue(y2.valid()) + + def testIntegerMultiplication(self): + x1 = amount(123) + y1 = amount(456) + + self.assertEqual(amount(0), x1 * 0) + self.assertEqual(amount(0), amount(0) * x1) + self.assertEqual(amount(0), 0 * x1) + self.assertEqual(x1, x1 * 1) + self.assertEqual(x1, amount(1) * x1) + self.assertEqual(x1, 1 * x1) + self.assertEqual(- x1, x1 * -1) + self.assertEqual(- x1, amount(-1) * x1) + self.assertEqual(- x1, -1 * x1) + self.assertEqual(amount(56088), x1 * y1) + self.assertEqual(amount(56088), y1 * x1) + self.assertEqual(amount(56088), x1 * 456) + self.assertEqual(amount(56088), amount(456) * x1) + self.assertEqual(amount(56088), 456 * x1) + + x1 *= amount(123) + self.assertEqual(amount(15129), x1) + x1 *= 123 + self.assertEqual(amount(1860867), x1) + + x4 = amount("123456789123456789123456789") + + self.assertEqual(amount("15241578780673678546105778281054720515622620750190521"), + x4 * x4) + + self.assertTrue(x1.valid()) + self.assertTrue(y1.valid()) + self.assertTrue(x4.valid()) + + def testFractionalMultiplication(self): + x1 = amount(123.123) + y1 = amount(456.456) + + self.assertEqual(amount(0), x1 * 0) + self.assertEqual(amount(0), amount(0) * x1) + self.assertEqual(amount(0), 0 * x1) + self.assertEqual(x1, x1 * 1) + self.assertEqual(x1, amount(1) * x1) + self.assertEqual(x1, 1 * x1) + self.assertEqual(- x1, x1 * -1) + self.assertEqual(- x1, amount(-1) * x1) + self.assertEqual(- x1, -1 * x1) + self.assertEqual(amount("56200.232088"), x1 * y1) + self.assertEqual(amount("56200.232088"), y1 * x1) + self.assertEqual(amount("56200.232088"), x1 * 456.456) + self.assertEqual(amount("56200.232088"), amount(456.456) * x1) + self.assertEqual(amount("56200.232088"), 456.456 * x1) + + x1 *= amount(123.123) + self.assertEqual(amount("15159.273129"), x1) + x1 *= 123.123 + self.assertEqual(amount("1866455.185461867"), x1) + x1 *= 123 + self.assertEqual(amount("229573987.811809641"), x1) + + x2 = amount("123456789123456789.123456789123456789") + + self.assertEqual(amount("15241578780673678546105778311537878.046486820281054720515622620750190521"), + x2 * x2) + + self.assertTrue(x1.valid()) + self.assertTrue(y1.valid()) + self.assertTrue(x2.valid()) + + def divideByZero(self, amt): + return amt / 0 + + def testIntegerDivision(self): + x1 = amount(123) + y1 = amount(456) + + self.assertRaises(exceptions.ArithmeticError, self.divideByZero, x1) + self.assertEqual(amount(0), amount(0) / x1) + self.assertEqual(amount(0), 0 / x1) + self.assertEqual(x1, x1 / 1) + self.assertEqual(amount("0.008130"), amount(1) / x1) + self.assertEqual(amount("0.008130"), 1 / x1) + self.assertEqual(- x1, x1 / -1) + self.assertEqual(- amount("0.008130"), amount(-1) / x1) + self.assertEqual(- amount("0.008130"), -1 / x1) + self.assertEqual(amount("0.269737"), x1 / y1) + self.assertEqual(amount("3.707317"), y1 / x1) + self.assertEqual(amount("0.269737"), x1 / 456) + self.assertEqual(amount("3.707317"), amount(456) / x1) + self.assertEqual(amount("3.707317"), 456 / x1) + + x1 /= amount(456) + self.assertEqual(amount("0.269737"), x1) + x1 /= 456 + self.assertEqual(amount("0.00059152850877193"), x1) + + x4 = amount("123456789123456789123456789") + y4 = amount("56") + + self.assertEqual(amount(1), x4 / x4) + self.assertEqual(amount("2204585520061728377204585.517857"), x4 / y4) + + self.assertTrue(x1.valid()) + self.assertTrue(y1.valid()) + self.assertTrue(x4.valid()) + self.assertTrue(y4.valid()) + + def testFractionalDivision(self): + x1 = amount(123.123) + y1 = amount(456.456) + + self.assertRaises(exceptions.ArithmeticError, self.divideByZero, x1) + self.assertEqual(amount("0.008121959"), amount(1.0) / x1) + self.assertEqual(amount("0.008121959"), 1.0 / x1) + self.assertEqual(x1, x1 / 1.0) + self.assertEqual(amount("0.008121959"), amount(1.0) / x1) + self.assertEqual(amount("0.008121959"), 1.0 / x1) + self.assertEqual(- x1, x1 / -1.0) + self.assertEqual(- amount("0.008121959"), amount(-1.0) / x1) + self.assertEqual(- amount("0.008121959"), -1.0 / x1) + self.assertEqual(amount("0.269736842105263"), x1 / y1) + self.assertEqual(amount("3.707317073170732"), y1 / x1) + self.assertEqual(amount("0.269736842105263"), x1 / 456.456) + self.assertEqual(amount("3.707317073170732"), amount(456.456) / x1) + self.assertEqual(amount("3.707317073170732"), 456.456 / x1) + + x1 /= amount(456.456) + self.assertEqual(amount("0.269736842105263"), x1) + x1 /= 456.456 + self.assertEqual(amount("0.000590937225286255411255411255411255411"), x1) + x1 /= 456 + self.assertEqual(amount("0.000001295914967733016252753094858358016252192982456140350877192982456140350877192982"), x1) + + x4 = amount("1234567891234567.89123456789") + y4 = amount("56.789") + + self.assertEqual(amount(1.0), x4 / x4) + self.assertEqual(amount("21739560323910.7554497273748437197344556164046"), + x4 / y4) + + self.assertTrue(x1.valid()) + self.assertTrue(y1.valid()) + self.assertTrue(x4.valid()) + self.assertTrue(y4.valid()) + + def testIntegerConversion(self): + x1 = amount(123456) + + self.assertEqual(True, bool(x1)) + self.assertEqual(123456, int(x1)) + self.assertEqual(123456.0, float(x1)) + self.assertEqual("123456", x1.to_string()) + self.assertEqual("123456", x1.quantity_string()) + + self.assertTrue(x1.valid()) + + def testFractionalConversion(self): + x1 = amount(1234.56) + + self.assertEqual(True, not (not x1)) + self.assertEqual(1234, int(x1)) + self.assertEqual(1234.56, float(x1)) + self.assertEqual("1234.56", x1.to_string()) + self.assertEqual("1234.56", x1.quantity_string()) + + self.assertTrue(x1.valid()) + + def testFractionalRound(self): + x1 = amount("1234.567890") + + self.assertEqual(amount("1234.56789"), x1.round(6)) + self.assertEqual(amount("1234.56789"), x1.round(5)) + self.assertEqual(amount("1234.5679"), x1.round(4)) + self.assertEqual(amount("1234.568"), x1.round(3)) + self.assertEqual(amount("1234.57"), x1.round(2)) + self.assertEqual(amount("1234.6"), x1.round(1)) + self.assertEqual(amount("1235"), x1.round(0)) + + x2 = amount("9876.543210") + + self.assertEqual(amount("9876.543210"), x2.round(6)) + self.assertEqual(amount("9876.54321"), x2.round(5)) + self.assertEqual(amount("9876.5432"), x2.round(4)) + self.assertEqual(amount("9876.543"), x2.round(3)) + self.assertEqual(amount("9876.54"), x2.round(2)) + self.assertEqual(amount("9876.5"), x2.round(1)) + self.assertEqual(amount("9877"), x2.round(0)) + + x3 = amount("-1234.567890") + + self.assertEqual(amount("-1234.56789"), x3.round(6)) + self.assertEqual(amount("-1234.56789"), x3.round(5)) + self.assertEqual(amount("-1234.5679"), x3.round(4)) + self.assertEqual(amount("-1234.568"), x3.round(3)) + self.assertEqual(amount("-1234.57"), x3.round(2)) + self.assertEqual(amount("-1234.6"), x3.round(1)) + self.assertEqual(amount("-1235"), x3.round(0)) + + x4 = amount("-9876.543210") + + self.assertEqual(amount("-9876.543210"), x4.round(6)) + self.assertEqual(amount("-9876.54321"), x4.round(5)) + self.assertEqual(amount("-9876.5432"), x4.round(4)) + self.assertEqual(amount("-9876.543"), x4.round(3)) + self.assertEqual(amount("-9876.54"), x4.round(2)) + self.assertEqual(amount("-9876.5"), x4.round(1)) + self.assertEqual(amount("-9877"), x4.round(0)) + + self.assertTrue(x1.valid()) + self.assertTrue(x2.valid()) + self.assertTrue(x3.valid()) + self.assertTrue(x4.valid()) + + def testTruth(self): + x0 = amount() + x1 = amount("1234") + x2 = amount("1234.56") + + self.assertTrue(not x0) + self.assertTrue(x1 ) + self.assertTrue(x2) + + self.assertTrue(x0.valid()) + self.assertTrue(x1.valid()) + self.assertTrue(x2.valid()) + + def testForZero(self): + x0 = amount() + x1 = amount("0.000000000000000000001") + + self.assertTrue(not x0) + self.assertTrue(x1) + self.assertTrue(x0.zero()) + self.assertTrue(x0.realzero()) + self.assertTrue(not x1.zero()) + self.assertTrue(not x1.realzero()) + + self.assertTrue(x0.valid()) + self.assertTrue(x1.valid()) + + def testComparisons(self): + x0 = amount() + x1 = amount(-123) + x2 = amount(123) + x3 = amount(-123.45) + x4 = amount(123.45) + x5 = amount("-123.45") + x6 = amount("123.45") + + self.assertTrue(x0 > x1) + self.assertTrue(x0 < x2) + self.assertTrue(x0 > x3) + self.assertTrue(x0 < x4) + self.assertTrue(x0 > x5) + self.assertTrue(x0 < x6) + + self.assertTrue(x1 > x3) + self.assertTrue(x3 <= x5) + self.assertTrue(x3 >= x5) + self.assertTrue(x3 < x1) + self.assertTrue(x3 < x4) + + self.assertTrue(x0.valid()) + self.assertTrue(x1.valid()) + self.assertTrue(x2.valid()) + self.assertTrue(x3.valid()) + self.assertTrue(x4.valid()) + self.assertTrue(x5.valid()) + self.assertTrue(x6.valid()) + + def testSign(self): + x0 = amount() + x1 = amount("0.0000000000000000000000000000000000001") + x2 = amount("-0.0000000000000000000000000000000000001") + x3 = amount("1") + x4 = amount("-1") + + self.assertTrue(not x0.sign()) + self.assertTrue(x1.sign() > 0) + self.assertTrue(x2.sign() < 0) + self.assertTrue(x3.sign() > 0) + self.assertTrue(x4.sign() < 0) + + self.assertTrue(x0.valid()) + self.assertTrue(x1.valid()) + self.assertTrue(x2.valid()) + self.assertTrue(x3.valid()) + self.assertTrue(x4.valid()) + + def testAbs(self): + x0 = amount() + x1 = amount(-1234) + x2 = amount(1234) + + self.assertEqual(amount(), abs(x0)) + self.assertEqual(amount(1234), abs(x1)) + self.assertEqual(amount(1234), abs(x2)) + + self.assertTrue(x0.valid()) + self.assertTrue(x1.valid()) + self.assertTrue(x2.valid()) + + def testPrinting(self): + pass + + +def suite(): + return unittest.TestLoader().loadTestsFromTestCase(BasicAmountTestCase) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/python/numerics/CommodityAmount.py b/tests/python/numerics/CommodityAmount.py new file mode 100644 index 00000000..80f58b21 --- /dev/null +++ b/tests/python/numerics/CommodityAmount.py @@ -0,0 +1,623 @@ +# -*- coding: utf-8 -*- + +import unittest +import exceptions +import operator + +from ledger import amount + +internalAmount = amount.exact + +class CommodityAmountTestCase(unittest.TestCase): + def setUp(self): + # Cause the display precision for dollars to be initialized to 2. + x1 = amount("$1.00") + self.assertTrue(x1) + amount.full_strings = True # makes error reports from UnitTests accurate + + def tearDown(self): + amount.full_strings = False + + def assertValid(self, amt): + self.assertTrue(amt.valid()) + + def testConstructors(self): + x1 = amount("$123.45") + x2 = amount("-$123.45") + x3 = amount("$-123.45") + x4 = amount("DM 123.45") + x5 = amount("-DM 123.45") + x6 = amount("DM -123.45") + x7 = amount("123.45 euro") + x8 = amount("-123.45 euro") + x9 = amount("123.45€") + x10 = amount("-123.45€") + + self.assertEqual(amount("$123.45"), x1) + self.assertEqual(amount("-$123.45"), x2) + self.assertEqual(amount("$-123.45"), x3) + self.assertEqual(amount("DM 123.45"), x4) + self.assertEqual(amount("-DM 123.45"), x5) + self.assertEqual(amount("DM -123.45"), x6) + self.assertEqual(amount("123.45 euro"), x7) + self.assertEqual(amount("-123.45 euro"), x8) + self.assertEqual(amount("123.45€"), x9) + self.assertEqual(amount("-123.45€"), x10) + + self.assertEqual("$123.45", x1.to_string()) + self.assertEqual("$-123.45", x2.to_string()) + self.assertEqual("$-123.45", x3.to_string()) + self.assertEqual("DM 123.45", x4.to_string()) + self.assertEqual("DM -123.45", x5.to_string()) + self.assertEqual("DM -123.45", x6.to_string()) + self.assertEqual("123.45 euro", x7.to_string()) + self.assertEqual("-123.45 euro", x8.to_string()) + self.assertEqual("123.45€", x9.to_string()) + self.assertEqual("-123.45€", x10.to_string()) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + self.assertValid(x6) + self.assertValid(x7) + self.assertValid(x8) + self.assertValid(x9) + self.assertValid(x10) + + def testNegation(self): + x1 = amount("$123.45") + x2 = amount("-$123.45") + x3 = amount("$-123.45") + x4 = amount("DM 123.45") + x5 = amount("-DM 123.45") + x6 = amount("DM -123.45") + x7 = amount("123.45 euro") + x8 = amount("-123.45 euro") + x9 = amount("123.45€") + x10 = amount("-123.45€") + + self.assertEqual(amount("$-123.45"), - x1) + self.assertEqual(amount("$123.45"), - x2) + self.assertEqual(amount("$123.45"), - x3) + self.assertEqual(amount("DM -123.45"), - x4) + self.assertEqual(amount("DM 123.45"), - x5) + self.assertEqual(amount("DM 123.45"), - x6) + self.assertEqual(amount("-123.45 euro"), - x7) + self.assertEqual(amount("123.45 euro"), - x8) + self.assertEqual(amount("-123.45€"), - x9) + self.assertEqual(amount("123.45€"), - x10) + + self.assertEqual(amount("$-123.45"), x1.negate()) + self.assertEqual(amount("$123.45"), x2.negate()) + self.assertEqual(amount("$123.45"), x3.negate()) + + self.assertEqual("$-123.45", (- x1).to_string()) + self.assertEqual("$123.45", (- x2).to_string()) + self.assertEqual("$123.45", (- x3).to_string()) + self.assertEqual("DM -123.45", (- x4).to_string()) + self.assertEqual("DM 123.45", (- x5).to_string()) + self.assertEqual("DM 123.45", (- x6).to_string()) + self.assertEqual("-123.45 euro", (- x7).to_string()) + self.assertEqual("123.45 euro", (- x8).to_string()) + self.assertEqual("-123.45€", (- x9).to_string()) + self.assertEqual("123.45€", (- x10).to_string()) + + self.assertEqual(amount("$-123.45"), x1.negate()) + self.assertEqual(amount("$123.45"), x2.negate()) + self.assertEqual(amount("$123.45"), x3.negate()) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + self.assertValid(x6) + self.assertValid(x7) + self.assertValid(x8) + self.assertValid(x9) + self.assertValid(x10) + + def testAssignment(self): + x1 = amount("$123.45") + x2 = amount("-$123.45") + x3 = amount("$-123.45") + x4 = amount("DM 123.45") + x5 = amount("-DM 123.45") + x6 = amount("DM -123.45") + x7 = amount("123.45 euro") + x8 = amount("-123.45 euro") + x9 = amount("123.45€") + x10 = amount("-123.45€") + + self.assertEqual(amount("$123.45"), x1) + self.assertEqual(amount("-$123.45"), x2) + self.assertEqual(amount("$-123.45"), x3) + self.assertEqual(amount("DM 123.45"), x4) + self.assertEqual(amount("-DM 123.45"), x5) + self.assertEqual(amount("DM -123.45"), x6) + self.assertEqual(amount("123.45 euro"), x7) + self.assertEqual(amount("-123.45 euro"), x8) + self.assertEqual(amount("123.45€"), x9) + self.assertEqual(amount("-123.45€"), x10) + + self.assertEqual("$123.45", x1.to_string()) + self.assertEqual("$-123.45", x2.to_string()) + self.assertEqual("$-123.45", x3.to_string()) + self.assertEqual("DM 123.45", x4.to_string()) + self.assertEqual("DM -123.45", x5.to_string()) + self.assertEqual("DM -123.45", x6.to_string()) + self.assertEqual("123.45 euro", x7.to_string()) + self.assertEqual("-123.45 euro", x8.to_string()) + self.assertEqual("123.45€", x9.to_string()) + self.assertEqual("-123.45€", x10.to_string()) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + self.assertValid(x6) + self.assertValid(x7) + self.assertValid(x8) + self.assertValid(x9) + self.assertValid(x10) + + def testEquality(self): + x0 = amount() + x1 = amount("$123.45") + x2 = amount("-$123.45") + x3 = amount("$-123.45") + x4 = amount("DM 123.45") + x5 = amount("-DM 123.45") + x6 = amount("DM -123.45") + x7 = amount("123.45 euro") + x8 = amount("-123.45 euro") + x9 = amount("123.45€") + x10 = amount("-123.45€") + + self.assertTrue(x0.null()) + self.assertTrue(x0.zero()) + self.assertTrue(x0.realzero()) + self.assertTrue(x0.sign() == 0) + self.assertTrue(x0.compare(x1) < 0) + self.assertTrue(x0.compare(x2) > 0) + self.assertTrue(x0.compare(x0) == 0) + + self.assertTrue(x1 != x2) + self.assertTrue(x1 != x4) + self.assertTrue(x1 != x7) + self.assertTrue(x1 != x9) + self.assertTrue(x2 == x3) + self.assertTrue(x4 != x5) + self.assertTrue(x5 == x6) + self.assertTrue(x7 == - x8) + self.assertTrue(x9 == - x10) + + self.assertValid(x0) + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + self.assertValid(x6) + self.assertValid(x7) + self.assertValid(x8) + self.assertValid(x9) + self.assertValid(x10) + + def testAddition(self): + x0 = amount() + x1 = amount("$123.45") + x2 = amount(internalAmount("$123.456789")) + x3 = amount("DM 123.45") + x4 = amount("123.45 euro") + x5 = amount("123.45€") + x6 = amount("123.45") + + self.assertEqual(amount("$246.90"), x1 + x1) + self.assertNotEqual(amount("$246.91"), x1 + x2) + self.assertEqual(internalAmount("$246.906789"), x1 + x2) + + # Converting to string drops internal precision + self.assertEqual("$246.90", (x1 + x1).to_string()) + self.assertEqual("$246.91", (x1 + x2).to_string()) + + self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x0) + self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x3) + self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x4) + self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x5) + self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x6) + self.assertRaises(exceptions.ArithmeticError, operator.add, x1, 123.45) + self.assertRaises(exceptions.ArithmeticError, operator.add, x1, 123) + + self.assertEqual(amount("DM 246.90"), x3 + x3) + self.assertEqual(amount("246.90 euro"), x4 + x4) + self.assertEqual(amount("246.90€"), x5 + x5) + + self.assertEqual("DM 246.90", (x3 + x3).to_string()) + self.assertEqual("246.90 euro", (x4 + x4).to_string()) + self.assertEqual("246.90€", (x5 + x5).to_string()) + + x1 += amount("$456.45") + self.assertEqual(amount("$579.90"), x1) + x1 += amount("$456.45") + self.assertEqual(amount("$1036.35"), x1) + x1 += amount("$456") + self.assertEqual(amount("$1492.35"), x1) + + x7 = amount(internalAmount("$123456789123456789.123456789123456789")) + + self.assertEqual(internalAmount("$246913578246913578.246913578246913578"), x7 + x7) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + self.assertValid(x6) + self.assertValid(x7) + + def testSubtraction(self): + x0 = amount() + x1 = amount("$123.45") + x2 = amount(internalAmount("$123.456789")) + x3 = amount("DM 123.45") + x4 = amount("123.45 euro") + x5 = amount("123.45€") + x6 = amount("123.45") + + self.assertNotEqual(amount(), x1 - x1) + self.assertEqual(amount("$0"), x1 - x1) + self.assertEqual(amount("$23.45"), x1 - amount("$100.00")) + self.assertEqual(amount("$-23.45"), amount("$100.00") - x1) + self.assertNotEqual(amount("$-0.01"), x1 - x2) + self.assertEqual(internalAmount("$-0.006789"), x1 - x2) + + # Converting to string drops internal precision. If an amount is + # zero, it drops the commodity as well. + self.assertEqual("$0.00", (x1 - x1).to_string()) + self.assertEqual("$-0.01", (x1 - x2).to_string()) + + self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x0) + self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x3) + self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x4) + self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x5) + self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x6) + self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, 123.45) + self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, 123) + + self.assertEqual(amount("DM 0.00"), x3 - x3) + self.assertEqual(amount("DM 23.45"), x3 - amount("DM 100.00")) + self.assertEqual(amount("DM -23.45"), amount("DM 100.00") - x3) + self.assertEqual(amount("0.00 euro"), x4 - x4) + self.assertEqual(amount("23.45 euro"), x4 - amount("100.00 euro")) + self.assertEqual(amount("-23.45 euro"), amount("100.00 euro") - x4) + self.assertEqual(amount("0.00€"), x5 - x5) + self.assertEqual(amount("23.45€"), x5 - amount("100.00€")) + self.assertEqual(amount("-23.45€"), amount("100.00€") - x5) + + self.assertEqual("DM 0.00", (x3 - x3).to_string()) + self.assertEqual("DM 23.45", (x3 - amount("DM 100.00")).to_string()) + self.assertEqual("DM -23.45", (amount("DM 100.00") - x3).to_string()) + self.assertEqual("0.00 euro", (x4 - x4).to_string()) + self.assertEqual("23.45 euro", (x4 - amount("100.00 euro")).to_string()) + self.assertEqual("-23.45 euro", (amount("100.00 euro") - x4).to_string()) + self.assertEqual("0.00€", (x5 - x5).to_string()) + self.assertEqual("23.45€", (x5 - amount("100.00€")).to_string()) + self.assertEqual("-23.45€", (amount("100.00€") - x5).to_string()) + + x1 -= amount("$456.45") + self.assertEqual(amount("$-333.00"), x1) + x1 -= amount("$456.45") + self.assertEqual(amount("$-789.45"), x1) + x1 -= amount("$456") + self.assertEqual(amount("$-1245.45"), x1) + + x7 = amount(internalAmount("$123456789123456789.123456789123456789")) + x8 = amount(internalAmount("$2354974984698.98459845984598")) + + self.assertEqual(internalAmount("$123454434148472090.138858329277476789"), x7 - x8) + self.assertEqual("$123454434148472090.138858329277476789", (x7 - x8).to_string()) + self.assertEqual("$123454434148472090.14", + (amount("$1.00") * (x7 - x8)).to_string()) + self.assertEqual(internalAmount("$-123454434148472090.138858329277476789"), x8 - x7) + self.assertEqual("$-123454434148472090.138858329277476789", (x8 - x7).to_string()) + self.assertEqual("$-123454434148472090.14", + (amount("$1.00") * (x8 - x7)).to_string()) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + self.assertValid(x6) + self.assertValid(x7) + self.assertValid(x8) + + def testMultiplication(self): + x1 = amount("$123.12") + y1 = amount("$456.45") + x2 = amount(internalAmount("$123.456789")) + x3 = amount("DM 123.45") + x4 = amount("123.45 euro") + x5 = amount("123.45€") + + self.assertEqual(amount("$0.00"), x1 * 0) + self.assertEqual(amount("$0.00"), 0 * x1) + self.assertEqual(x1, x1 * 1) + self.assertEqual(x1, 1 * x1) + self.assertEqual(- x1, x1 * -1) + self.assertEqual(- x1, -1 * x1) + self.assertEqual(internalAmount("$56198.124"), x1 * y1) + self.assertEqual("$56198.12", (x1 * y1).to_string()) + self.assertEqual(internalAmount("$56198.124"), y1 * x1) + self.assertEqual("$56198.12", (y1 * x1).to_string()) + + # Internal amounts retain their precision, even when being + # converted to strings + self.assertEqual(internalAmount("$15199.99986168"), x1 * x2) + self.assertEqual(internalAmount("$15199.99986168"), x2 * x1) + self.assertEqual("$15200.00", (x1 * x2).to_string()) + self.assertEqual("$15199.99986168", (x2 * x1).to_string()) + + self.assertRaises(exceptions.ArithmeticError, operator.mul, x1, x3) + self.assertRaises(exceptions.ArithmeticError, operator.mul, x1, x4) + self.assertRaises(exceptions.ArithmeticError, operator.mul, x1, x5) + + x1 *= amount("123.12") + self.assertEqual(internalAmount("$15158.5344"), x1) + self.assertEqual("$15158.53", x1.to_string()) + x1 *= 123.12 + self.assertEqual(internalAmount("$1866318.755328"), x1) + self.assertEqual("$1866318.76", x1.to_string()) + x1 *= 123 + self.assertEqual(internalAmount("$229557206.905344"), x1) + self.assertEqual("$229557206.91", x1.to_string()) + + x7 = amount(internalAmount("$123456789123456789.123456789123456789")) + + self.assertEqual(internalAmount("$15241578780673678546105778311537878.046486820281054720515622620750190521"), + x7 * x7) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + self.assertValid(x7) + + def testDivision(self): + x1 = amount("$123.12") + y1 = amount("$456.45") + x2 = amount(internalAmount("$123.456789")) + x3 = amount("DM 123.45") + x4 = amount("123.45 euro") + x5 = amount("123.45€") + + self.assertRaises(exceptions.ArithmeticError, operator.div, x1, 0) + self.assertEqual(amount("$0.00"), 0 / x1) + self.assertEqual(x1, x1 / 1) + self.assertEqual(internalAmount("$0.00812216"), 1 / x1) + self.assertEqual(- x1, x1 / -1) + self.assertEqual(internalAmount("$-0.00812216"), -1 / x1) + self.assertEqual(internalAmount("$0.26973382"), x1 / y1) + self.assertEqual("$0.27", (x1 / y1).to_string()) + self.assertEqual(internalAmount("$3.70735867"), y1 / x1) + self.assertEqual("$3.71", (y1 / x1).to_string()) + + # Internal amounts retain their precision, even when being + # converted to strings + self.assertEqual(internalAmount("$0.99727201"), x1 / x2) + self.assertEqual(internalAmount("$1.00273545321637426901"), x2 / x1) + self.assertEqual("$1.00", (x1 / x2).to_string()) + self.assertEqual("$1.00273545321637426901", (x2 / x1).to_string()) + + self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x3) + self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x4) + self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x5) + + x1 /= amount("123.12") + self.assertEqual(internalAmount("$1.00"), x1) + self.assertEqual("$1.00", x1.to_string()) + x1 /= 123.12 + self.assertEqual(internalAmount("$0.00812216"), x1) + self.assertEqual("$0.01", x1.to_string()) + x1 /= 123 + self.assertEqual(internalAmount("$0.00006603"), x1) + self.assertEqual("$0.00", x1.to_string()) + + x6 = amount(internalAmount("$237235987235987.98723987235978")) + x7 = amount(internalAmount("$123456789123456789.123456789123456789")) + + self.assertEqual(amount("$1"), x7 / x7) + self.assertEqual(internalAmount("$0.0019216115121765559608381226612019501046413574469262"), + x6 / x7) + self.assertEqual(internalAmount("$520.39654928343335571379527154924040947271699678158689736256"), + x7 / x6) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + self.assertValid(x6) + self.assertValid(x7) + + def testConversion(self): + x1 = amount("$1234.56") + + self.assertEqual(True, bool(x1)) + self.assertEqual(1234, int(x1)) + self.assertEqual(1234.56, float(x1)) + self.assertEqual("$1234.56", x1.to_string()) + self.assertEqual("1234.56", x1.quantity_string()) + + self.assertValid(x1) + + def testRound(self): + x1 = amount(internalAmount("$1234.567890")) + + self.assertEqual(internalAmount("$1234.56789"), x1.round(6)) + self.assertEqual(internalAmount("$1234.56789"), x1.round(5)) + self.assertEqual(internalAmount("$1234.5679"), x1.round(4)) + self.assertEqual(internalAmount("$1234.568"), x1.round(3)) + self.assertEqual(amount("$1234.57"), x1.round(2)) + self.assertEqual(amount("$1234.6"), x1.round(1)) + self.assertEqual(amount("$1235"), x1.round(0)) + + x2 = amount(internalAmount("$9876.543210")) + + self.assertEqual(internalAmount("$9876.543210"), x2.round(6)) + self.assertEqual(internalAmount("$9876.54321"), x2.round(5)) + self.assertEqual(internalAmount("$9876.5432"), x2.round(4)) + self.assertEqual(internalAmount("$9876.543"), x2.round(3)) + self.assertEqual(amount("$9876.54"), x2.round(2)) + self.assertEqual(amount("$9876.5"), x2.round(1)) + self.assertEqual(amount("$9877"), x2.round(0)) + + x3 = amount(internalAmount("$-1234.567890")) + + self.assertEqual(internalAmount("$-1234.56789"), x3.round(6)) + self.assertEqual(internalAmount("$-1234.56789"), x3.round(5)) + self.assertEqual(internalAmount("$-1234.5679"), x3.round(4)) + self.assertEqual(internalAmount("$-1234.568"), x3.round(3)) + self.assertEqual(amount("$-1234.57"), x3.round(2)) + self.assertEqual(amount("$-1234.6"), x3.round(1)) + self.assertEqual(amount("$-1235"), x3.round(0)) + + x4 = amount(internalAmount("$-9876.543210")) + + self.assertEqual(internalAmount("$-9876.543210"), x4.round(6)) + self.assertEqual(internalAmount("$-9876.54321"), x4.round(5)) + self.assertEqual(internalAmount("$-9876.5432"), x4.round(4)) + self.assertEqual(internalAmount("$-9876.543"), x4.round(3)) + self.assertEqual(amount("$-9876.54"), x4.round(2)) + self.assertEqual(amount("$-9876.5"), x4.round(1)) + self.assertEqual(amount("$-9877"), x4.round(0)) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + + def testDisplayRound(self): + x1 = amount("$0.85") + x2 = amount("$0.1") + + x1 *= 0.19 + + self.assertNotEqual(amount("$0.16"), x1) + self.assertEqual(internalAmount("$0.1615"), x1) + self.assertEqual("$0.16", x1.to_string()) + + self.assertEqual(amount("$0.10"), x2) + self.assertNotEqual(internalAmount("$0.101"), x2) + self.assertEqual("$0.10", x2.to_string()) + + x1 *= 7 + + self.assertNotEqual(amount("$1.13"), x1) + self.assertEqual(internalAmount("$1.1305"), x1) + self.assertEqual("$1.13", x1.to_string()) + + def testTruth(self): + x1 = amount("$1234") + x2 = amount("$1234.56") + + if x1: + self.assertTrue(True) + else: + self.assertTrue(False) + + if x2: + self.assertTrue(True) + else: + self.assertTrue(False) + + self.assertValid(x1) + self.assertValid(x2) + + def testForZero(self): + x1 = amount(internalAmount("$0.000000000000000000001")) + + self.assertFalse(x1) + self.assertTrue(x1.zero()) + self.assertFalse(x1.realzero()) + + self.assertValid(x1) + + def testComparisons(self): + x0 = amount() + x1 = amount("$-123") + x2 = amount("$123.00") + x3 = amount(internalAmount("$-123.4544")) + x4 = amount(internalAmount("$123.4544")) + x5 = amount("$-123.45") + x6 = amount("$123.45") + + self.assertTrue(x0 > x1) + self.assertTrue(x0 < x2) + self.assertTrue(x0 > x3) + self.assertTrue(x0 < x4) + self.assertTrue(x0 > x5) + self.assertTrue(x0 < x6) + + self.assertTrue(x1 > x3) + self.assertTrue(x3 <= x5) + self.assertTrue(x3 < x5) + self.assertTrue(x3 <= x5) + self.assertFalse(x3 == x5) + self.assertTrue(x3 < x1) + self.assertTrue(x3 < x4) + + self.assertValid(x0) + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + self.assertValid(x6) + + def testSign(self): + x0 = amount() + x1 = amount(internalAmount("$0.0000000000000000000000000000000000001")) + x2 = amount(internalAmount("$-0.0000000000000000000000000000000000001")) + x3 = amount("$1") + x4 = amount("$-1") + + self.assertFalse(x0.sign()) + self.assertTrue(x1.sign() != 0) + self.assertTrue(x2.sign() != 0) + self.assertTrue(x3.sign() > 0) + self.assertTrue(x4.sign() < 0) + + self.assertValid(x0) + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + + def testAbs(self): + x0 = amount() + x1 = amount("$-1234.56") + x2 = amount("$1234.56") + + self.assertEqual(amount(), abs(x0)) + self.assertEqual(amount("$1234.56"), abs(x1)) + self.assertEqual(amount("$1234.56"), abs(x2)) + + self.assertValid(x0) + self.assertValid(x1) + self.assertValid(x2) + + def testPrinting(self): + pass + + +def suite(): + return unittest.TestLoader().loadTestsFromTestCase(CommodityAmountTestCase) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/python/numerics/__init__.py b/tests/python/numerics/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/python/numerics/balances/__init__.py b/tests/python/numerics/balances/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/python/numerics/values/__init__.py b/tests/python/numerics/values/__init__.py new file mode 100644 index 00000000..e69de29b -- cgit v1.2.3