diff options
author | John Wiegley <johnw@newartisans.com> | 2007-04-15 11:17:00 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 03:35:32 -0400 |
commit | 479dd85da543b7af8e5f888ff2f2beaf6ff6923b (patch) | |
tree | 7a447b52ab366c1f5a9399f9365a45b0879305b4 | |
parent | 0ef82600e5978495cccb4eed63b1de7094deab04 (diff) | |
download | fork-ledger-479dd85da543b7af8e5f888ff2f2beaf6ff6923b.tar.gz fork-ledger-479dd85da543b7af8e5f888ff2f2beaf6ff6923b.tar.bz2 fork-ledger-479dd85da543b7af8e5f888ff2f2beaf6ff6923b.zip |
Miscellaneous changes
-rw-r--r-- | Makefile.am | 8 | ||||
-rwxr-xr-x | acprep | 4 | ||||
-rw-r--r-- | amount.cc | 24 | ||||
-rw-r--r-- | amount.h | 9 | ||||
-rw-r--r-- | py_amount.cc | 24 | ||||
-rw-r--r-- | py_eval.cc | 30 | ||||
-rw-r--r-- | py_eval.h | 1 | ||||
-rw-r--r-- | pyledger.cc | 6 | ||||
-rwxr-xr-x | setup.py | 9 | ||||
-rw-r--r-- | tests/corelib/numerics/BasicAmountTestCase.cc | 120 |
10 files changed, 121 insertions, 114 deletions
diff --git a/Makefile.am b/Makefile.am index 45f4d045..0f2a3737 100644 --- a/Makefile.am +++ b/Makefile.am @@ -143,6 +143,8 @@ if HAVE_BOOST_PYTHON noinst_PROGRAMS = ledger.so +# jww (2007-04-14): This is not passing HAVE_EXPAT! + ledger.so: pyledger.cc libledger.la libpyledger.la CFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS) -L. -L.libs" \ python setup.py build --build-lib=. @@ -156,6 +158,9 @@ endif ###################################################################### TESTS = UnitTests +if HAVE_BOOST_PYTHON +TESTS += PyUnitTests +endif check_PROGRAMS = $(TESTS) @@ -180,6 +185,9 @@ if DEBUG UnitTests_CXXFLAGS += -DDEBUG_LEVEL=4 endif +PyUnitTests: + python tests/python/UnitTests.py + ###################################################################### all: check @@ -31,7 +31,9 @@ if [ $SYSTEM = Linux ]; then elif [ $SYSTEM = Solaris ]; then CXXFLAGS="-pthreads" elif [ $SYSTEM = Darwin ]; then - CXXFLAGS="-Wno-long-double" + #CXXFLAGS="-arch i386 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk" + CXXFLAGS="$CXXFLAGS -Wno-long-double" + #LIBDIRS="$LIBDIRS -arch i386 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk" else CXXFLAGS="" fi @@ -216,13 +216,33 @@ namespace { mpf_init_set_d(temp, val); mp_exp_t exp; - char * buf = mpf_get_str(NULL, &exp, 10, 10, temp); + char * buf = mpf_get_str(NULL, &exp, 10, 1000, temp); int len = std::strlen(buf); if (len > 0 && buf[0] == '-') exp++; - exp = len - exp; + if (exp <= len) { + exp = len - exp; + } else { + // There were trailing zeros, which we have to put back on in + // order to convert this buffer into an integer. + + int zeroes = exp - len; + + char * newbuf = (char *)std::malloc(len + zeroes); + std::strcpy(newbuf, buf); + + int i; + for (i = 0; i < zeroes; i++) + newbuf[len + i] = '0'; + newbuf[len + i] = '\0'; + + free(buf); + buf = newbuf; + + exp = (len - exp) + zeroes; + } mpz_set_str(dest, buf, 10); free(buf); @@ -95,7 +95,8 @@ class amount_t return ! quantity && ! has_commodity(); } - std::string quantity_string() const; + std::string to_string() const; + std::string quantity_string() const {} // assignment operator amount_t& operator=(const amount_t& amt); @@ -306,6 +307,12 @@ class amount_t void read_quantity(char *& data); }; +inline std::string amount_t::to_string() const { + std::ostringstream bufstream; + print(bufstream); + return bufstream.str(); +} + inline amount_t abs(const amount_t& amt) { return amt < 0 ? amt.negated() : amt; } diff --git a/py_amount.cc b/py_amount.cc index b4ff21d1..c310bda2 100644 --- a/py_amount.cc +++ b/py_amount.cc @@ -41,8 +41,8 @@ commodity_t * py_find_commodity(const std::string& symbol) } #define EXC_TRANSLATOR(type) \ - void exc_translate_ ## type(const type& err) { \ - PyErr_SetString(PyExc_RuntimeError, err.what()); \ + void exc_translate_ ## type(const type * const err) { \ + PyErr_SetString(PyExc_ArithmeticError, err->what()); \ } EXC_TRANSLATOR(amount_error) @@ -59,48 +59,34 @@ void export_amount() .def(init<char *>()) .def(init<bool>()) .def(init<long>()) - .def(init<unsigned long>()) - .def(init<double>()) .def(self += self) .def(self += long()) - .def(self += double()) .def(self + self) .def(self + long()) .def(long() + self) - .def(self + double()) - .def(double() + self) .def(self -= self) .def(self -= long()) - .def(self -= double()) .def(self - self) .def(self - long()) .def(long() - self) - .def(self - double()) - .def(double() - self) .def(self *= self) .def(self *= long()) - .def(self *= double()) .def(self * self) .def(self * long()) .def(long() * self) - .def(self * double()) - .def(double() * self) .def(self /= self) .def(self /= long()) - .def(self /= double()) .def(self / self) .def(self / long()) .def(long() / self) - .def(self / double()) - .def(double() / self) .def(- self) @@ -135,7 +121,8 @@ void export_amount() .def(self_ns::str(self)) .def(abs(self)) -#if 0 + .def("__repr__", &amount_t::to_string) + .def("has_commodity", &amount_t::has_commodity) .add_property("commodity", @@ -165,7 +152,6 @@ void export_amount() .def("value", &amount_t::value) .def("valid", &amount_t::valid) -#endif ; class_< commodity_base_t::updater_t, commodity_updater_wrap, @@ -220,7 +206,7 @@ void export_amount() ; #define EXC_TRANSLATE(type) \ - register_exception_translator<type>(&exc_translate_ ## type); + register_exception_translator<type *>(&exc_translate_ ## type); EXC_TRANSLATE(amount_error); } @@ -23,24 +23,22 @@ void shutdown_option(); namespace ledger { -namespace { - void initialize_ledger_for_python() - { - export_amount(); +void initialize_ledger_for_python() +{ + export_amount(); #if 0 - export_balance(); - export_value(); - export_datetime(); - - export_journal(); - export_parser(); - export_option(); - export_walk(); - export_format(); - export_report(); - export_valexpr(); + export_balance(); + export_value(); + export_datetime(); + + export_journal(); + export_parser(); + export_option(); + export_walk(); + export_format(); + export_report(); + export_valexpr(); #endif - } } void shutdown_ledger_for_python() @@ -13,6 +13,7 @@ using namespace boost::python; namespace ledger { +void initialize_ledger_for_python(); void shutdown_ledger_for_python(); class python_interpreter_t : public xml::xpath_t::scope_t diff --git a/pyledger.cc b/pyledger.cc index 27d06776..8c9a249d 100644 --- a/pyledger.cc +++ b/pyledger.cc @@ -1,10 +1,10 @@ #include <boost/python.hpp> -using namespace boost::python; +#include "py_eval.h" -void initialize_ledger_for_python(); +using namespace boost::python; BOOST_PYTHON_MODULE(ledger) { - initialize_ledger_for_python(); + ledger::initialize_ledger_for_python(); } @@ -4,7 +4,11 @@ from distutils.core import setup, Extension import os -libs = ["ledger", "pyledger", "boost_python", "gmp", "pcre"] +defines = [('PYTHON_MODULE', 1)] + +defines.extend ([('DEBUG_LEVEL', 4)]) + +libs = ["pyledger", "ledger", "boost_python", "gmp", "pcre"] if os.environ.has_key ("HAVE_EXPAT") and\ os.environ["HAVE_EXPAT"] == "true": @@ -26,5 +30,4 @@ setup(name = "Ledger", url = "http://johnwiegley.com/", ext_modules = [ Extension("ledger", ["pyledger.cc"], - define_macros = [('PYTHON_MODULE', 1)], - libraries = libs)]) + define_macros = defines, libraries = libs)]) diff --git a/tests/corelib/numerics/BasicAmountTestCase.cc b/tests/corelib/numerics/BasicAmountTestCase.cc index e82f5d19..5f634cdf 100644 --- a/tests/corelib/numerics/BasicAmountTestCase.cc +++ b/tests/corelib/numerics/BasicAmountTestCase.cc @@ -13,13 +13,11 @@ void BasicAmountTestCase::testConstructors() amount_t x0; amount_t x1(123456L); amount_t x2(123456UL); - amount_t x3(123.456); amount_t x4(true); 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); @@ -27,36 +25,33 @@ void BasicAmountTestCase::testConstructors() assertEqual(x2, x1); assertEqual(x5, x1); assertEqual(x7, x1); - assertEqual(x6, x3); - assertEqual(x8, x3); - assertEqual(x10, x3); + assertEqual(x6, x8); + assertEqual(x10, x6); + assertEqual(x11, x10); assertEqual(amount_t(1L), x4); - assertEqual(x10, x9); } 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); + amount_t x9(- x6); assertEqual(amount_t(0L), x0); assertEqual(x5, x1); assertEqual(x7, x1); - assertEqual(x6, x3); - assertEqual(x8, x3); + assertEqual(x6, x8); assertEqual(- x6, x9); - assertEqual(x3.negated(), x9); + assertEqual(x6.negated(), x9); amount_t x10(x9); x10.negate(); - assertEqual(x3, x10); + assertEqual(x6, x10); } void BasicAmountTestCase::testAssignment() @@ -64,44 +59,40 @@ void BasicAmountTestCase::testAssignment() amount_t x0; amount_t x1 = 123456L; amount_t x2 = 123456UL; - amount_t x3 = 123.456; amount_t x4 = true; 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 x9 = x6; 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(x8, x6); + assertEqual(x10, x6); assertEqual(amount_t(1L), x4); assertEqual(x10, x9); x0 = amount_t(); x1 = 123456L; x2 = 123456UL; - x3 = 123.456; x4 = true; x5 = "123456"; x6 = "123.456"; x7 = std::string("123456"); x8 = std::string("123.456"); - x9 = x3; + x9 = x6; 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(x8, x6); + assertEqual(x10, x6); assertEqual(amount_t(1L), x4); assertEqual(x10, x9); } @@ -111,16 +102,12 @@ 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 == x5); } void BasicAmountTestCase::testIntegerAddition() @@ -150,19 +137,19 @@ void BasicAmountTestCase::testIntegerAddition() void BasicAmountTestCase::testFractionalAddition() { - amount_t x1(123.123); - amount_t y1(456.456); + 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); + assertEqual(amount_t("579.579"), x1 + y1); + assertEqual(amount_t("579.579"), x1 + amount_t("456.456")); + assertEqual(amount_t("579.579"), amount_t("456.456") + x1); - x1 += amount_t(456.456); - assertEqual(amount_t(579.579), x1); - x1 += 456.456; - assertEqual(amount_t(1036.035), x1); + x1 += amount_t("456.456"); + assertEqual(amount_t("579.579"), x1); + x1 += amount_t("456.456"); + assertEqual(amount_t("1036.035"), x1); x1 += 456L; - assertEqual(amount_t(1492.035), x1); + assertEqual(amount_t("1492.035"), x1); amount_t x2("123456789123456789.123456789123456789"); @@ -198,18 +185,18 @@ void BasicAmountTestCase::testIntegerSubtraction() void BasicAmountTestCase::testFractionalSubtraction() { - amount_t x1(123.123); - amount_t y1(456.456); + amount_t x1("123.123"); + amount_t y1("456.456"); - assertEqual(amount_t(-333.333), x1 - y1); - assertEqual(amount_t(333.333), y1 - x1); + 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 -= amount_t("456.456"); + assertEqual(amount_t("-333.333"), x1); + x1 -= amount_t("456.456"); + assertEqual(amount_t("-789.789"), x1); x1 -= 456L; - assertEqual(amount_t(-1245.789), x1); + assertEqual(amount_t("-1245.789"), x1); amount_t x2("123456789123456789.123456789123456789"); amount_t y2("9872345982459.248974239578"); @@ -256,8 +243,8 @@ void BasicAmountTestCase::testIntegerMultiplication() void BasicAmountTestCase::testFractionalMultiplication() { - amount_t x1(123.123); - amount_t y1(456.456); + amount_t x1("123.123"); + amount_t y1("456.456"); assertEqual(amount_t(0L), x1 * 0L); assertEqual(amount_t(0L), amount_t(0L) * x1); @@ -270,13 +257,12 @@ void BasicAmountTestCase::testFractionalMultiplication() 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); + assertEqual(amount_t("56200.232088"), x1 * amount_t("456.456")); + assertEqual(amount_t("56200.232088"), amount_t("456.456") * x1); - x1 *= amount_t(123.123); + x1 *= amount_t("123.123"); assertEqual(amount_t("15159.273129"), x1); - x1 *= 123.123; + x1 *= amount_t("123.123"); assertEqual(amount_t("1866455.185461867"), x1); x1 *= 123L; assertEqual(amount_t("229573987.811809641"), x1); @@ -321,27 +307,23 @@ void BasicAmountTestCase::testIntegerDivision() void BasicAmountTestCase::testFractionalDivision() { - amount_t x1(123.123); - amount_t y1(456.456); + amount_t x1("123.123"); + amount_t y1("456.456"); assertThrow(x1 / 0L, amount_error *); - assertEqual(amount_t("0.008121"), amount_t(1.0) / x1); - assertEqual(amount_t("0.008121"), 1.0 / x1); - assertEqual(x1, x1 / 1.0); - assertEqual(amount_t("0.008121"), amount_t(1.0) / x1); - assertEqual(amount_t("0.008121"), 1.0 / x1); - assertEqual(- x1, x1 / -1.0); - assertEqual(- amount_t("0.008121"), amount_t(-1.0) / x1); - assertEqual(- amount_t("0.008121"), -1.0 / x1); + assertEqual(amount_t("0.00812195"), amount_t("1.0") / x1); + assertEqual(x1, x1 / amount_t("1.0")); + assertEqual(amount_t("0.00812195"), amount_t("1.0") / x1); + assertEqual(- x1, x1 / amount_t("-1.0")); + assertEqual(- amount_t("0.00812195"), amount_t("-1.0") / x1); assertEqual(amount_t("0.269736842105"), x1 / y1); assertEqual(amount_t("3.707317073170"), y1 / x1); - assertEqual(amount_t("0.269736842105"), x1 / 456.456); - assertEqual(amount_t("3.707317073170"), amount_t(456.456) / x1); - assertEqual(amount_t("3.707317073170"), 456.456 / x1); + assertEqual(amount_t("0.269736842105"), x1 / amount_t("456.456")); + assertEqual(amount_t("3.707317073170"), amount_t("456.456") / x1); - x1 /= amount_t(456.456); + x1 /= amount_t("456.456"); assertEqual(amount_t("0.269736842105"), x1); - x1 /= 456.456; + x1 /= amount_t("456.456"); assertEqual(amount_t("0.0005909372252856792330476541"), x1); x1 /= 456L; assertEqual(amount_t("0.00000129591496773175270405187302631578947368421052631578947368421"), x1); @@ -349,7 +331,7 @@ void BasicAmountTestCase::testFractionalDivision() amount_t x4("1234567891234567.89123456789"); amount_t y4("56.789"); - assertEqual(amount_t(1.0), x4 / x4); + assertEqual(amount_t("1.0"), x4 / x4); assertEqual(amount_t("21739560323910.7554497273748437197344556164"), x4 / y4); } |