diff options
-rw-r--r-- | src/main.cc | 4 | ||||
-rw-r--r-- | src/py_amount.cc | 162 | ||||
-rw-r--r-- | tests/numerics/BasicAmount.cc | 248 | ||||
-rw-r--r-- | tests/numerics/BasicAmount.h | 28 |
4 files changed, 237 insertions, 205 deletions
diff --git a/src/main.cc b/src/main.cc index 99f1ff93..ade5b6a1 100644 --- a/src/main.cc +++ b/src/main.cc @@ -445,7 +445,7 @@ int main(int argc, char * argv[], char * envp[]) status = read_and_report(report.get(), argc, argv, envp); - if (! ledger::do_cleanup) { + IF_VERIFY() { report.release(); session.release(); } @@ -480,7 +480,7 @@ int main(int argc, char * argv[], char * envp[]) status = _status; } - if (ledger::do_cleanup) + IF_VERIFY() ledger::shutdown(); return status; diff --git a/src/py_amount.cc b/src/py_amount.cc index e39f32e0..c2d1291d 100644 --- a/src/py_amount.cc +++ b/src/py_amount.cc @@ -54,11 +54,68 @@ void export_amount() scope().attr("AMOUNT_PARSE_NO_REDUCE") = AMOUNT_PARSE_NO_REDUCE; class_< amount_t > ("amount") - .def(init<amount_t>()) + .def("initialize", &amount_t::initialize) + .staticmethod("initialize") + .def("shutdown", &amount_t::shutdown) + .staticmethod("shutdown") + + .add_static_property("keep_base", &amount_t::keep_base) + + .add_static_property("keep_price", &amount_t::keep_price) + .add_static_property("keep_date", &amount_t::keep_date) + .add_static_property("keep_tag", &amount_t::keep_tag) + + .add_static_property("full_strings", &amount_t::full_strings) + + .def(init<double>()) + .def(init<long>()) .def(init<std::string>()) .def(init<char *>()) - .def(init<long>()) - .def(init<double>()) + + .def("exact", &amount_t::exact) + .staticmethod("exact") + + .def(init<amount_t>()) + + .def("compare", &amount_t::compare) + + .def(self == self) + .def(self == long()) + .def(long() == self) + .def(self == double()) + .def(double() == self) + + .def(self != self) + .def(self != long()) + .def(long() != self) + .def(self != double()) + .def(double() != self) + + .def(! self) + + .def(self < self) + .def(self < long()) + .def(long() < self) + .def(self < double()) + .def(double() < self) + + .def(self <= self) + .def(self <= long()) + .def(long() <= self) + .def(self <= double()) + .def(double() <= self) + + .def(self > self) + .def(self > long()) + .def(long() > self) + .def(self > double()) + .def(double() > self) + + .def(self >= self) + .def(self >= long()) + .def(long() >= self) + .def(self >= double()) + .def(double() >= self) .def(self += self) .def(self += long()) @@ -100,53 +157,46 @@ void export_amount() .def(self / double()) .def(double() / self) + .def("negate", &amount_t::negate) + .def("in_place_negate", &amount_t::in_place_negate, + return_value_policy<reference_existing_object>()) .def(- self) - .def(self < self) - .def(self < long()) - .def(long() < self) - .def(self < double()) - .def(double() < self) - - .def(self <= self) - .def(self <= long()) - .def(long() <= self) - .def(self <= double()) - .def(double() <= self) + .def("abs", &amount_t::abs) + .def("__abs__", &amount_t::abs) - .def(self > self) - .def(self > long()) - .def(long() > self) - .def(self > double()) - .def(double() > self) + .def("round", py_round_1) + .def("round", py_round_2) + .def("unround", &amount_t::unround) - .def(self >= self) - .def(self >= long()) - .def(long() >= self) - .def(self >= double()) - .def(double() >= self) + .def("reduce", &amount_t::reduce) + .def("in_place_reduce", &amount_t::in_place_reduce, + return_value_policy<reference_existing_object>()) - .def(self == self) - .def(self == long()) - .def(long() == self) - .def(self == double()) - .def(double() == self) + .def("unreduce", &amount_t::unreduce) + .def("in_place_unreduce", &amount_t::in_place_unreduce, + return_value_policy<reference_existing_object>()) - .def(self != self) - .def(self != long()) - .def(long() != self) - .def(self != double()) - .def(double() != self) + .def("value", &amount_t::value) - .def(! self) + .def("sign", &amount_t::sign) + .def("__nonzero__", &amount_t::nonzero) + .def("nonzero", &amount_t::nonzero) + .def("zero", &amount_t::zero) + .def("realzero", &amount_t::realzero) + .def("is_null", &amount_t::is_null) - .def("__int__", &amount_t::to_long) + .def("to_double", &amount_t::to_double) .def("__float__", &amount_t::to_double) - .def("__nonzero__", &amount_t::nonzero) - .def("__abs__", &amount_t::abs) + .def("to_long", &amount_t::to_long) + .def("__int__", &amount_t::to_long) + .def("to_string", &amount_t::to_string) .def("__str__", &amount_t::to_string) + .def("to_fullstring", &amount_t::to_fullstring) .def("__repr__", &amount_t::to_fullstring) + .def("quantity_string", &amount_t::quantity_string) + .def("has_commodity", &amount_t::has_commodity) .add_property("commodity", @@ -155,41 +205,23 @@ void export_amount() make_function(&amount_t::set_commodity, with_custodian_and_ward<1, 2>())) - .def("annotate_commodity", &amount_t::annotate_commodity) - .def("strip_annotations", &amount_t::strip_annotations) .def("clear_commodity", &amount_t::clear_commodity) + .def("number", &amount_t::number) - //.add_static_property("full_strings", &amount_t::full_strings) - - .def("to_string", &amount_t::to_string) - .def("to_fullstring", &amount_t::to_fullstring) - .def("quantity_string", &amount_t::quantity_string) - - .def("exact", &amount_t::exact) - .staticmethod("exact") + .def("annotate_commodity", &amount_t::annotate_commodity) + .def("strip_annotations", &amount_t::strip_annotations) - .def("compare", &amount_t::compare) + .def("price", &amount_t::price) .def("date", &amount_t::date) - .def("negate", &amount_t::negate) - .def("is_null", &amount_t::is_null) + .def("tag", &amount_t::tag) + .def("parse", py_parse_1) .def("parse", py_parse_2) - .def("price", &amount_t::price) - .def("realzero", &amount_t::realzero) - .def("reduce", &amount_t::reduce) - .def("round", py_round_1) - .def("round", py_round_2) - .def("sign", &amount_t::sign) - .def("unround", &amount_t::unround) - .def("value", &amount_t::value) - .def("zero", &amount_t::zero) - .def("valid", &amount_t::valid) + .def("parse_conversion", &amount_t::parse_conversion) + .staticmethod("parse_conversion") - .def("initialize", &amount_t::initialize) - .staticmethod("initialize") - .def("shutdown", &amount_t::shutdown) - .staticmethod("shutdown") + .def("valid", &amount_t::valid) ; class_< commodity_base_t::updater_t, commodity_updater_wrap, diff --git a/tests/numerics/BasicAmount.cc b/tests/numerics/BasicAmount.cc index 6ce39d1d..c1a1f0fa 100644 --- a/tests/numerics/BasicAmount.cc +++ b/tests/numerics/BasicAmount.cc @@ -48,40 +48,6 @@ void BasicAmountTestCase::testConstructors() 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; @@ -160,6 +126,38 @@ void BasicAmountTestCase::testEquality() CPPUNIT_ASSERT(x6.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::testIntegerAddition() { amount_t x1(123L); @@ -410,30 +408,53 @@ void BasicAmountTestCase::testFractionalDivision() CPPUNIT_ASSERT(y4.valid()); } -void BasicAmountTestCase::testIntegerConversion() +void BasicAmountTestCase::testNegation() { - amount_t x1(123456L); + 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(true, bool(x1)); - assertEqual(123456L, x1.to_long()); - assertEqual(123456.0, x1.to_double()); - assertEqual(string("123456"), x1.to_string()); - assertEqual(string("123456"), x1.quantity_string()); + 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::testFractionalConversion() +void BasicAmountTestCase::testAbs() { - amount_t x1(1234.56); + amount_t x0; + amount_t x1(-1234L); + amount_t x2(1234L); - assertEqual(true, bool(x1)); - assertEqual(1234L, x1.to_long()); - assertEqual(1234.56, x1.to_double()); - assertEqual(string("1234.56"), x1.to_string()); - assertEqual(string("1234.56"), x1.quantity_string()); + 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::testFractionalRound() @@ -490,6 +511,47 @@ void BasicAmountTestCase::testFractionalRound() CPPUNIT_ASSERT(x4.valid()); } +void BasicAmountTestCase::testReduction() +{ + amount_t x1("60s"); + amount_t x2("600s"); + amount_t x3("6000s"); + amount_t x4("360000s"); + amount_t x5("10m"); // 600s + amount_t x6("100m"); // 6000s + amount_t x7("1000m"); // 60000s + amount_t x8("10000m"); // 600000s + amount_t x9("10h"); // 36000s + amount_t x10("100h"); // 360000s + amount_t x11("1000h"); // 3600000s + amount_t x12("10000h"); // 36000000s + + assertEqual(x2, x5); + assertEqual(x3, x6); + assertEqual(x4, x10); +} + +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::testTruth() { amount_t x0; @@ -532,92 +594,30 @@ void BasicAmountTestCase::testForZero() 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() +void BasicAmountTestCase::testIntegerConversion() { - amount_t x0; - amount_t x1("0.0000000000000000000000000000000000001"); - amount_t x2("-0.0000000000000000000000000000000000001"); - amount_t x3("1"); - amount_t x4("-1"); + amount_t x1(123456L); - CPPUNIT_ASSERT(! x0.sign()); - CPPUNIT_ASSERT(x1.sign() > 0); - CPPUNIT_ASSERT(x2.sign() < 0); - CPPUNIT_ASSERT(x3.sign() > 0); - CPPUNIT_ASSERT(x4.sign() < 0); + assertEqual(true, bool(x1)); + assertEqual(123456L, x1.to_long()); + assertEqual(123456.0, x1.to_double()); + assertEqual(string("123456"), x1.to_string()); + assertEqual(string("123456"), x1.quantity_string()); - CPPUNIT_ASSERT(x0.valid()); CPPUNIT_ASSERT(x1.valid()); - CPPUNIT_ASSERT(x2.valid()); - CPPUNIT_ASSERT(x3.valid()); - CPPUNIT_ASSERT(x4.valid()); } -void BasicAmountTestCase::testAbs() +void BasicAmountTestCase::testFractionalConversion() { - amount_t x0; - amount_t x1(-1234L); - amount_t x2(1234L); + amount_t x1(1234.56); - assertEqual(amount_t(), x0.abs()); - assertEqual(amount_t(1234L), x1.abs()); - assertEqual(amount_t(1234L), x2.abs()); + assertEqual(true, bool(x1)); + assertEqual(1234L, x1.to_long()); + assertEqual(1234.56, x1.to_double()); + assertEqual(string("1234.56"), x1.to_string()); + assertEqual(string("1234.56"), x1.quantity_string()); - CPPUNIT_ASSERT(x0.valid()); CPPUNIT_ASSERT(x1.valid()); - CPPUNIT_ASSERT(x2.valid()); -} - -void BasicAmountTestCase::testReduction() -{ - amount_t x1("60s"); - amount_t x2("600s"); - amount_t x3("6000s"); - amount_t x4("360000s"); - amount_t x5("10m"); // 600s - amount_t x6("100m"); // 6000s - amount_t x7("1000m"); // 60000s - amount_t x8("10000m"); // 600000s - amount_t x9("10h"); // 36000s - amount_t x10("100h"); // 360000s - amount_t x11("1000h"); // 3600000s - amount_t x12("10000h"); // 36000000s - - assertEqual(x2, x5); - assertEqual(x3, x6); - assertEqual(x4, x10); } void BasicAmountTestCase::testPrinting() diff --git a/tests/numerics/BasicAmount.h b/tests/numerics/BasicAmount.h index a6c8aff7..12449e49 100644 --- a/tests/numerics/BasicAmount.h +++ b/tests/numerics/BasicAmount.h @@ -8,9 +8,9 @@ class BasicAmountTestCase : public CPPUNIT_NS::TestCase CPPUNIT_TEST_SUITE(BasicAmountTestCase); CPPUNIT_TEST(testConstructors); - CPPUNIT_TEST(testNegation); CPPUNIT_TEST(testAssignment); CPPUNIT_TEST(testEquality); + CPPUNIT_TEST(testComparisons); CPPUNIT_TEST(testIntegerAddition); CPPUNIT_TEST(testFractionalAddition); CPPUNIT_TEST(testIntegerSubtraction); @@ -19,15 +19,15 @@ class BasicAmountTestCase : public CPPUNIT_NS::TestCase CPPUNIT_TEST(testFractionalMultiplication); CPPUNIT_TEST(testIntegerDivision); CPPUNIT_TEST(testFractionalDivision); - CPPUNIT_TEST(testIntegerConversion); - CPPUNIT_TEST(testFractionalConversion); + CPPUNIT_TEST(testNegation); + CPPUNIT_TEST(testAbs); CPPUNIT_TEST(testFractionalRound); + CPPUNIT_TEST(testReduction); + CPPUNIT_TEST(testSign); CPPUNIT_TEST(testTruth); CPPUNIT_TEST(testForZero); - CPPUNIT_TEST(testComparisons); - CPPUNIT_TEST(testSign); - CPPUNIT_TEST(testAbs); - CPPUNIT_TEST(testReduction); + CPPUNIT_TEST(testIntegerConversion); + CPPUNIT_TEST(testFractionalConversion); CPPUNIT_TEST(testPrinting); CPPUNIT_TEST_SUITE_END(); @@ -40,9 +40,9 @@ public: virtual void tearDown(); void testConstructors(); - void testNegation(); void testAssignment(); void testEquality(); + void testComparisons(); void testIntegerAddition(); void testFractionalAddition(); void testIntegerSubtraction(); @@ -51,15 +51,15 @@ public: void testFractionalMultiplication(); void testIntegerDivision(); void testFractionalDivision(); - void testIntegerConversion(); - void testFractionalConversion(); + void testNegation(); + void testAbs(); void testFractionalRound(); + void testReduction(); + void testSign(); void testTruth(); void testForZero(); - void testComparisons(); - void testSign(); - void testAbs(); - void testReduction(); + void testIntegerConversion(); + void testFractionalConversion(); void testPrinting(); private: |