diff options
author | John Wiegley <johnw@newartisans.com> | 2007-05-08 10:33:25 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 03:38:42 -0400 |
commit | 42d799a1fdde6278fe42be58786937e49fcf1c3f (patch) | |
tree | feebcb9be4b03093e961b083bf90fe05f21b9329 | |
parent | 4e9056b6ce395531098a72d431b911f4ecbbbaab (diff) | |
download | fork-ledger-42d799a1fdde6278fe42be58786937e49fcf1c3f.tar.gz fork-ledger-42d799a1fdde6278fe42be58786937e49fcf1c3f.tar.bz2 fork-ledger-42d799a1fdde6278fe42be58786937e49fcf1c3f.zip |
Added tests to complete coverage.
-rw-r--r-- | src/amount.cc | 6 | ||||
-rw-r--r-- | tests/numerics/t_amount.cc | 72 | ||||
-rw-r--r-- | tests/numerics/t_amount.h | 2 | ||||
-rw-r--r-- | tests/python/numerics/t_amount.py | 70 |
4 files changed, 147 insertions, 3 deletions
diff --git a/src/amount.cc b/src/amount.cc index 382ea36c..aef13ba8 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -210,10 +210,10 @@ void amount_t::_release() DEBUG("amounts.refs", quantity << " ref--, now " << (quantity->ref - 1)); if (--quantity->ref == 0) { - if (! (quantity->has_flags(BIGINT_BULK_ALLOC))) - checked_delete(quantity); - else + if (quantity->has_flags(BIGINT_BULK_ALLOC)) quantity->~bigint_t(); + else + checked_delete(quantity); } } diff --git a/tests/numerics/t_amount.cc b/tests/numerics/t_amount.cc index 002b70b0..51d7a2f7 100644 --- a/tests/numerics/t_amount.cc +++ b/tests/numerics/t_amount.cc @@ -20,6 +20,71 @@ void AmountTestCase::tearDown() ledger::set_session_context(); } +void AmountTestCase::testParser() +{ + amount_t x0; + amount_t x1; + amount_t x2; + amount_t x3; + amount_t x4(123.456); + amount_t x5(x4); + amount_t x6(x4); + amount_t x7(x4); + amount_t x8("$123.456"); + amount_t x9(x8); + amount_t x10(x8); + amount_t x11(x8); + amount_t x12("$100"); + + assertEqual(amount_t::precision_t(3), x12.commodity().precision()); + + x1.parse("$100.0000", AMOUNT_PARSE_NO_MIGRATE); + assertEqual(amount_t::precision_t(3), x12.commodity().precision()); + assertEqual(x1.commodity(), x12.commodity()); + assertEqual(x1, x12); + + x0.parse("$100.0000"); + assertEqual(amount_t::precision_t(4), x12.commodity().precision()); + assertEqual(x0.commodity(), x12.commodity()); + assertEqual(x0, x12); + + x2.parse("$100.00", AMOUNT_PARSE_NO_REDUCE); + assertEqual(x2, x12); + x3.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE | AMOUNT_PARSE_NO_REDUCE); + assertEqual(x3, x12); + + x4.parse("$100.00"); + assertEqual(x4, x12); + x5.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE); + assertEqual(x5, x12); + x6.parse("$100.00", AMOUNT_PARSE_NO_REDUCE); + assertEqual(x6, x12); + x7.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE | AMOUNT_PARSE_NO_REDUCE); + assertEqual(x7, x12); + + x8.parse("$100.00"); + assertEqual(x8, x12); + x9.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE); + assertEqual(x9, x12); + x10.parse("$100.00", AMOUNT_PARSE_NO_REDUCE); + assertEqual(x10, x12); + x11.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE | AMOUNT_PARSE_NO_REDUCE); + assertEqual(x11, x12); + + assertTrue(x0.valid()); + assertTrue(x1.valid()); + assertTrue(x2.valid()); + assertTrue(x3.valid()); + assertTrue(x5.valid()); + assertTrue(x6.valid()); + assertTrue(x7.valid()); + assertTrue(x8.valid()); + assertTrue(x9.valid()); + assertTrue(x10.valid()); + assertTrue(x11.valid()); + assertTrue(x12.valid()); +} + void AmountTestCase::testConstructors() { amount_t x0; @@ -108,6 +173,7 @@ void AmountTestCase::testCommodityConstructors() void AmountTestCase::testAssignment() { + amount_t x0; amount_t x1 = 123456L; amount_t x2 = 123456UL; amount_t x3 = 123.456; @@ -144,6 +210,12 @@ void AmountTestCase::testAssignment() assertEqual(x10, x3); assertEqual(x10, x9); + assertFalse(x1.is_null()); + x1 = x0; // sets x1 back to uninitialized state + assertTrue(x0.is_null()); + assertTrue(x1.is_null()); + + assertTrue(x0.valid()); assertTrue(x1.valid()); assertTrue(x2.valid()); assertTrue(x3.valid()); diff --git a/tests/numerics/t_amount.h b/tests/numerics/t_amount.h index 0bf0017f..9b3d36f0 100644 --- a/tests/numerics/t_amount.h +++ b/tests/numerics/t_amount.h @@ -9,6 +9,7 @@ class AmountTestCase : public CPPUNIT_NS::TestCase CPPUNIT_TEST(testConstructors); CPPUNIT_TEST(testCommodityConstructors); + CPPUNIT_TEST(testParser); CPPUNIT_TEST(testAssignment); CPPUNIT_TEST(testCommodityAssignment); CPPUNIT_TEST(testEquality); @@ -60,6 +61,7 @@ public: void testConstructors(); void testCommodityConstructors(); + void testParser(); void testAssignment(); void testCommodityAssignment(); void testEquality(); diff --git a/tests/python/numerics/t_amount.py b/tests/python/numerics/t_amount.py index bde8eae1..856e13ee 100644 --- a/tests/python/numerics/t_amount.py +++ b/tests/python/numerics/t_amount.py @@ -23,6 +23,69 @@ class AmountTestCase(unittest.TestCase): def assertValid(self, amt): self.assertTrue(amt.valid()) + def testParser(self): + x0 = amount() + x1 = amount() + x2 = amount() + x3 = amount() + x4 = amount(123.456) + x5 = amount(x4) + x6 = amount(x4) + x7 = amount(x4) + x8 = amount("$123.456") + x9 = amount(x8) + x10 = amount(x8) + x11 = amount(x8) + x12 = amount("$100") + + self.assertEqual(3, x12.commodity().precision()) + + x1.parse("$100.0000", AMOUNT_PARSE_NO_MIGRATE) + self.assertEqual(3, x12.commodity().precision()) + self.assertEqual(x1.commodity(), x12.commodity()) + self.assertEqual(x1, x12) + + x0.parse("$100.0000") + self.assertEqual(4, x12.commodity().precision()) + self.assertEqual(x0.commodity(), x12.commodity()) + self.assertEqual(x0, x12) + + x2.parse("$100.00", AMOUNT_PARSE_NO_REDUCE) + self.assertEqual(x2, x12) + x3.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE | AMOUNT_PARSE_NO_REDUCE) + self.assertEqual(x3, x12) + + x4.parse("$100.00") + self.assertEqual(x4, x12) + x5.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE) + self.assertEqual(x5, x12) + x6.parse("$100.00", AMOUNT_PARSE_NO_REDUCE) + self.assertEqual(x6, x12) + x7.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE | AMOUNT_PARSE_NO_REDUCE) + self.assertEqual(x7, x12) + + x8.parse("$100.00") + self.assertEqual(x8, x12) + x9.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE) + self.assertEqual(x9, x12) + x10.parse("$100.00", AMOUNT_PARSE_NO_REDUCE) + self.assertEqual(x10, x12) + x11.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE | AMOUNT_PARSE_NO_REDUCE) + self.assertEqual(x11, x12) + + self.assertValid(x0) + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x5) + self.assertValid(x6) + self.assertValid(x7) + self.assertValid(x8) + self.assertValid(x9) + self.assertValid(x10) + self.assertValid(x11) + self.assertValid(x12) + def testConstructors(self): x0 = amount() x1 = amount(123456) @@ -98,6 +161,7 @@ class AmountTestCase(unittest.TestCase): self.assertValid(x10) def testAssignment(self): + x0 = amount() x1 = amount(123456) x2 = amount(123456L) x3 = amount(123.456) @@ -126,6 +190,12 @@ class AmountTestCase(unittest.TestCase): self.assertEqual(x10, x3) self.assertEqual(x10, x9) + self.assertFalse(x1.is_null()) + x1 = x0 # sets x1 back to uninitialized state + self.assertTrue(x0.is_null()) + self.assertTrue(x1.is_null()) + + self.assertValid(x0) self.assertValid(x1) self.assertValid(x2) self.assertValid(x3) |