diff options
30 files changed, 1808 insertions, 259 deletions
@@ -79,3 +79,10 @@ /system.hh.gch /texinfo.tex /version.m4 +/data_tests +/expr_tests +/extra_tests +/math_tests +/parse_tests +/report_tests +/util_tests diff --git a/Makefile.am b/Makefile.am index 0c00d947..502e263c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,153 +3,193 @@ SUBDIRS = lib/gdtoa endif EXTRA_DIST = autogen.sh contrib -lib_LTLIBRARIES = libamounts.la libledger.la - -libamounts_la_CPPFLAGS = -I$(srcdir)/src -I$(srcdir)/lib \ - -I$(srcdir)/lib/utfcpp/source \ - -I$(srcdir)/lib/irrxml/src +lib_LTLIBRARIES = \ + libledger_util.la \ + libledger_math.la \ + libledger_expr.la \ + libledger_data.la \ + libledger_parse.la \ + libledger_report.la \ + libledger_extra.la + +lib_cppflags = -I$(srcdir)/src -I$(srcdir)/lib \ + -I$(srcdir)/lib/utfcpp/source \ + -I$(srcdir)/lib/irrxml/src if HAVE_GDTOA -libamounts_la_CPPFLAGS += -I$(top_builddir)/lib/gdtoa -I$(srcdir)/lib/gdtoa +lib_cppflags += -I$(top_builddir)/lib/gdtoa -I$(srcdir)/lib/gdtoa +endif +if HAVE_LIBOFX +lib_cppflags += -DHAVE_LIBOFX=1 endif +if DEBUG +lib_cppflags += -DDEBUG_MODE +endif + +libledger_util_la_SOURCES = \ + src/utils.cc \ + src/times.cc \ + src/mask.cc \ + src/binary.cc \ + lib/sha1.cpp -libamounts_la_SOURCES = \ - src/utils.cc \ - src/times.cc \ - src/mask.cc \ - src/binary.cc \ - \ +libledger_util_la_CPPFLAGS = $(lib_cppflags) +libledger_util_la_LDFLAGS = -release 3.0.0 + +libledger_math_la_SOURCES = \ src/amount.cc \ src/commodity.cc \ src/balance.cc \ src/balpair.cc \ - src/value.cc \ - \ + src/value.cc + +libledger_math_la_CPPFLAGS = $(lib_cppflags) +libledger_math_la_LDFLAGS = -release 3.0.0 + +libledger_expr_la_SOURCES = \ src/token.cc \ src/parser.cc \ src/op.cc \ src/expr.cc \ src/scope.cc \ src/format.cc \ - src/option.cc \ - \ - lib/sha1.cpp \ - \ + src/option.cc + +libledger_expr_la_CPPFLAGS = $(lib_cppflags) +libledger_expr_la_LDFLAGS = -release 3.0.0 + +libledger_data_la_SOURCES = \ + src/item.cc \ + src/xact.cc \ + src/entry.cc \ + src/account.cc \ + src/journal.cc \ + src/iterators.cc \ + src/compare.cc + +libledger_data_la_CPPFLAGS = $(lib_cppflags) +libledger_data_la_LDFLAGS = -release 3.0.0 + +libledger_parse_la_SOURCES = \ + src/textual.cc \ + src/cache.cc \ + src/xml.cc \ + src/csv.cc \ + src/emacs.cc \ + src/qif.cc \ + src/gnucash.cc \ lib/irrxml/src/irrXML.cpp - if HAVE_LIBOFX -libamounts_la_CPPFLAGS += -DHAVE_LIBOFX=1 -endif -if DEBUG -libamounts_la_CPPFLAGS += -DDEBUG_MODE +libledger_parse_la_SOURCES += src/ofx.cc endif -libledger_la_CPPFLAGS = $(libamounts_la_CPPFLAGS) -libledger_la_LDFLAGS = -release 3.0.0 -libledger_la_SOURCES = \ - src/journal.cc \ - src/item.cc \ - src/entry.cc \ - src/xact.cc \ - src/account.cc \ - src/iterators.cc \ - src/compare.cc \ - \ - src/textual.cc \ - src/cache.cc \ - src/emacs.cc \ - src/qif.cc \ - src/xml.cc \ - src/csv.cc \ - src/gnucash.cc \ - \ - src/session.cc \ - src/report.cc \ - src/filters.cc \ - src/output.cc \ - src/help.cc \ - \ - src/derive.cc \ - src/reconcile.cc \ - src/quotes.cc +libledger_parse_la_CPPFLAGS = $(lib_cppflags) +libledger_parse_la_LDFLAGS = -release 3.0.0 -if HAVE_LIBOFX -libledger_la_SOURCES += src/ofx.cc -endif +libledger_report_la_SOURCES = \ + src/session.cc \ + src/report.cc \ + src/filters.cc \ + src/output.cc \ + src/help.cc + +libledger_report_la_CPPFLAGS = $(lib_cppflags) +libledger_report_la_LDFLAGS = -release 3.0.0 + +libledger_extra_la_SOURCES = \ + src/derive.cc \ + src/reconcile.cc \ + src/quotes.cc -pkginclude_HEADERS = \ - acconf.h \ - src/system.hh \ - src/utils.h \ - src/flags.h \ - src/hooks.h \ - src/pushvar.h \ - src/error.h \ - src/times.h \ - src/mask.h \ - src/binary.h \ - \ - src/amount.h \ - src/commodity.h \ - src/balance.h \ - src/balpair.h \ - src/value.h \ - \ - src/token.h \ - src/parser.h \ - src/op.h \ - src/expr.h \ - src/scope.h \ - src/predicate.h \ - src/format.h \ - src/option.h \ - \ - src/journal.h \ - src/item.h \ - src/entry.h \ - src/xact.h \ - src/account.h \ - src/iterators.h \ - src/compare.h \ - \ - src/textual.h \ - src/cache.h \ - src/emacs.h \ - src/qif.h \ - src/xml.h \ - src/csv.h \ - src/gnucash.h \ - src/ofx.h \ - \ - src/session.h \ - src/report.h \ - src/handler.h \ - src/filters.h \ - src/output.h \ - src/help.h \ - \ - src/derive.h \ - src/reconcile.h \ - src/quotes.h \ - \ - src/ledger.h \ - lib/fdstream.h \ - lib/sha1.h \ - \ - python/pyledger.h \ - python/pyinterp.h +libledger_extra_la_CPPFLAGS = $(lib_cppflags) +libledger_extra_la_LDFLAGS = -release 3.0.0 + +pkginclude_HEADERS = \ + acconf.h \ + src/system.hh \ + src/utils.h \ + src/flags.h \ + src/hooks.h \ + src/pushvar.h \ + src/error.h \ + src/times.h \ + src/mask.h \ + src/binary.h \ + \ + src/amount.h \ + src/commodity.h \ + src/balance.h \ + src/balpair.h \ + src/value.h \ + \ + src/token.h \ + src/parser.h \ + src/op.h \ + src/expr.h \ + src/scope.h \ + src/predicate.h \ + src/format.h \ + src/option.h \ + \ + src/item.h \ + src/xact.h \ + src/entry.h \ + src/account.h \ + src/journal.h \ + src/iterators.h \ + src/compare.h \ + \ + src/textual.h \ + src/cache.h \ + src/xml.h \ + src/csv.h \ + src/emacs.h \ + src/qif.h \ + src/gnucash.h \ + src/ofx.h \ + \ + src/session.h \ + src/report.h \ + src/handler.h \ + src/filters.h \ + src/output.h \ + src/help.h \ + \ + src/derive.h \ + src/reconcile.h \ + src/quotes.h \ + \ + src/ledger.h \ + \ + python/pyledger.h \ + python/pyinterp.h \ + \ + lib/fdstream.h \ + lib/sha1.h \ + \ + lib/utfcpp/source/utf8.h \ + lib/utfcpp/source/utf8/checked.h \ + lib/utfcpp/source/utf8/core.h \ + lib/utfcpp/source/utf8/unchecked.h \ + \ + lib/irrxml/src/CXMLReaderImpl.h \ + lib/irrxml/src/fast_atof.h \ + lib/irrxml/src/heapsort.h \ + lib/irrxml/src/irrArray.h \ + lib/irrxml/src/irrString.h \ + lib/irrxml/src/irrTypes.h \ + lib/irrxml/src/irrXML.h CLEANFILES = if USE_PCH -nodist_libledger_la_SOURCES = src/system.hh.gch +nodist_libledger_util_la_SOURCES = src/system.hh.gch BUILT_SOURCES = src/system.hh.gch CLEANFILES += src/system.hh.gch $(srcdir)/src/system.hh.gch: $(srcdir)/src/system.hh $(top_builddir)/acconf.h $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ - $(libledger_la_CPPFLAGS) \ - $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) \ + $(lib_cppflags) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) \ -g -o $@ $(srcdir)/src/system.hh endif @@ -157,17 +197,17 @@ endif bin_PROGRAMS = ledger -ledger_CPPFLAGS = $(libledger_la_CPPFLAGS) +ledger_CPPFLAGS = $(lib_cppflags) if HAVE_BOOST_PYTHON ledger_CPPFLAGS += -DHAVE_BOOST_PYTHON=1 -I$(srcdir)/python endif ledger_SOURCES = src/main.cc -ledger_LDADD = $(LIBOBJS) libamounts.la libledger.la +ledger_LDADD = $(LIBOBJS) $(lib_LTLIBRARIES) if HAVE_GDTOA ledger_LDADD += lib/gdtoa/libgdtoa.la endif if HAVE_BOOST_PYTHON -ledger_LDADD += libpyledger.la +ledger_LDADD += libledger_python.la endif ledger_LDFLAGS = @@ -178,11 +218,16 @@ ELCFILES = DISTCLEANFILES = ledger.elc timeclock.elc CLEANFILES += TAGS -all_sources = $(libamounts_la_SOURCES) \ - $(libledger_la_SOURCES) \ - $(libpyledger_la_SOURCES) \ - python/pyledger.cc \ - $(pkginclude_HEADERS) +all_sources = $(libledger_util_la_SOURCES) \ + $(libledger_math_la_SOURCES) \ + $(libledger_expr_la_SOURCES) \ + $(libledger_data_la_SOURCES) \ + $(libledger_parse_la_SOURCES) \ + $(libledger_report_la_SOURCES) \ + $(libledger_extra_la_SOURCES) \ + $(libledger_python_la_SOURCES) \ + python/pyledger.cc +all_files = $(all_sources) $(pkginclude_HEADERS) TAGS: $(all_sources) @etags $(all_sources) @@ -192,9 +237,9 @@ TAGS: $(all_sources) if HAVE_BOOST_PYTHON -lib_LTLIBRARIES += libpyledger.la +lib_LTLIBRARIES += libledger_python.la -libpyledger_la_SOURCES = \ +libledger_python_la_SOURCES = \ python/pyutils.h \ python/pyfstream.h \ python/py_utils.cc \ @@ -205,7 +250,7 @@ libpyledger_la_SOURCES = \ python/pyinterp.cc \ python/setup.py -libpyledger_la_CPPFLAGS = $(libledger_la_CPPFLAGS) -I$(srcdir)/python +libledger_python_la_CPPFLAGS = $(lib_cppflags) -I$(srcdir)/python pyexec_PROGRAMS = ledger.so @@ -225,15 +270,18 @@ install-exec-hook: --prefix=$(prefix) --root=$(DESTDIR)/ endif -ledger_so_SOURCES = $(libamounts_la_SOURCES) $(libledger_la_SOURCES) \ - $(libpyledger_la_SOURCES) python/pyledger.cc +ledger_so_SOURCES = $(all_sources) ledger_so_DEPENDENCIES = $(lib_LTLIBRARIES) -PYLIBS = ledger amounts pyledger gmp \ - boost_system$(BOOST_SUFFIX) \ - boost_date_time$(BOOST_SUFFIX) \ - boost_filesystem$(BOOST_SUFFIX) \ - boost_regex$(BOOST_SUFFIX) \ +PYLIBS = ledger_util ledger_math ledger_expr \ + ledger_data ledger_parse \ + ledger_report ledger_extra \ + ledger_python \ + gmp \ + boost_system$(BOOST_SUFFIX) \ + boost_date_time$(BOOST_SUFFIX) \ + boost_filesystem$(BOOST_SUFFIX) \ + boost_regex$(BOOST_SUFFIX) \ boost_python$(BOOST_SUFFIX) if HAVE_GDTOA @@ -247,7 +295,7 @@ if HAVE_GDTOA ledger.so: $(ledger_so_SOURCES) $(ledger_so_DEPENDENCIES) BUILD_DIR=`cd $(top_builddir); pwd`; \ (cd $(srcdir); \ - CFLAGS="$(CPPFLAGS) -I$(srcdir) $(libpyledger_la_CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)" \ + CFLAGS="$(CPPFLAGS) -I$(srcdir) $(libledger_python_la_CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)" \ LDFLAGS="$(LDFLAGS) -L$$BUILD_DIR -L$$BUILD_DIR/.libs -L$$BUILD_DIR/lib/gdtoa -L$$BUILD_DIR/lib/gdtoa/.libs" \ ARCHFLAGS="$(ARCHFLAGS)" PYLIBS="$(PYLIBS)" \ $(PYTHON) python/setup.py build \ @@ -256,7 +304,7 @@ else ledger.so: $(ledger_so_SOURCES) $(ledger_so_DEPENDENCIES) BUILD_DIR=`cd $(top_builddir); pwd`; \ (cd $(srcdir); \ - CFLAGS="$(CPPFLAGS) -I$(srcdir) $(libpyledger_la_CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)" \ + CFLAGS="$(CPPFLAGS) -I$(srcdir) $(libledger_python_la_CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)" \ LDFLAGS="$(LDFLAGS) -L$$BUILD_DIR -L$$BUILD_DIR/.libs" \ ARCHFLAGS="$(ARCHFLAGS)" PYLIBS="$(PYLIBS)" \ $(PYTHON) python/setup.py build \ @@ -274,7 +322,14 @@ TESTS += RegressionTests endif if HAVE_CPPUNIT -TESTS += UnitTests +TESTS += \ + util_tests \ + math_tests \ + expr_tests \ + data_tests \ + parse_tests \ + report_tests \ + extra_tests endif if HAVE_BOOST_PYTHON @@ -283,29 +338,84 @@ endif check_PROGRAMS = $(TESTS) -UnitTests_SOURCES = \ +util_tests_SOURCES = \ test/UnitTests.cc \ test/UnitTests.h \ + test/util_tests.cc \ + test/util_tests.h \ test/unit/t_utils.cc \ test/unit/t_utils.h \ test/unit/t_times.cc \ - test/unit/t_times.h \ + test/unit/t_times.h + +util_tests_CPPFLAGS = -I$(srcdir)/test $(lib_cppflags) +util_tests_LDADD = libledger_util.la -lcppunit + +math_tests_SOURCES = \ + test/UnitTests.cc \ + test/UnitTests.h \ + test/math_tests.cc \ + test/math_tests.h \ test/unit/t_commodity.cc \ test/unit/t_commodity.h \ test/unit/t_amount.cc \ test/unit/t_amount.h \ test/unit/t_balance.cc \ - test/unit/t_balance.h \ - test/unit/t_expr.cc \ - test/unit/t_expr.h + test/unit/t_balance.h -UnitTests_CPPFLAGS = -I$(srcdir)/test $(libledger_la_CPPFLAGS) -UnitTests_LDFLAGS = $(LIBADD_DL) -UnitTests_LDADD = $(lib_LTLIBRARIES) -lcppunit +math_tests_CPPFLAGS = -I$(srcdir)/test $(lib_cppflags) +math_tests_LDADD = $(util_tests_LDADD) libledger_math.la if HAVE_GDTOA -UnitTests_LDADD += lib/gdtoa/libgdtoa.la +math_tests_LDADD += lib/gdtoa/libgdtoa.la endif +expr_tests_SOURCES = \ + test/UnitTests.cc \ + test/UnitTests.h \ + test/expr_tests.cc \ + test/expr_tests.h \ + test/unit/t_expr.cc \ + test/unit/t_expr.h + +expr_tests_CPPFLAGS = -I$(srcdir)/test $(lib_cppflags) +expr_tests_LDADD = $(math_tests_LDADD) libledger_expr.la + +data_tests_SOURCES = \ + test/UnitTests.cc \ + test/UnitTests.h \ + test/data_tests.cc \ + test/data_tests.h + +data_tests_CPPFLAGS = -I$(srcdir)/test $(lib_cppflags) +data_tests_LDADD = $(math_tests_LDADD) libledger_data.la + +parse_tests_SOURCES = \ + test/UnitTests.cc \ + test/UnitTests.h \ + test/parse_tests.cc \ + test/parse_tests.h + +parse_tests_CPPFLAGS = -I$(srcdir)/test $(lib_cppflags) +parse_tests_LDADD = $(math_tests_LDADD) libledger_parse.la + +report_tests_SOURCES = \ + test/UnitTests.cc \ + test/UnitTests.h \ + test/report_tests.cc \ + test/report_tests.h + +report_tests_CPPFLAGS = -I$(srcdir)/test $(lib_cppflags) +report_tests_LDADD = $(math_tests_LDADD) libledger_report.la + +extra_tests_SOURCES = \ + test/UnitTests.cc \ + test/UnitTests.h \ + test/extra_tests.cc \ + test/extra_tests.h + +extra_tests_CPPFLAGS = -I$(srcdir)/test $(lib_cppflags) +extra_tests_LDADD = $(math_tests_LDADD) libledger_extra.la + EXTRA_DIST += test/python PyUnitTests_SOURCES = test/__init__.py test/PyUnitTests.py test/UnitTests.py @@ -329,7 +439,7 @@ RegressionTests_SOURCES = test/regress.py EXTRA_DIST += test/regress RegressionTests: $(srcdir)/test/regress.py - echo "python $(srcdir)/test/regress.py $(top_builddir)/ledger$(EXEEXT) $(srcdir)/test/regress" > $@ + echo "$(PYTHON) $(srcdir)/test/regress.py $(top_builddir)/ledger$(EXEEXT) $(srcdir)/test/regress" > $@ chmod 755 $@ if HAVE_VALGRIND @@ -290,7 +290,7 @@ PATH="$PYTHON_HOME/bin:$PATH" \ # and warnings much easier to spot. if [ -f Makefile ]; then - perl -i -pe 's/^\t(\$\((LIBTOOL|CXX)\).*?\.cc)$/\t\@echo " " CXX \$\@;$1 > \/dev\/null/;' Makefile + perl -i -pe 's/^\t(\$\((LIBTOOL|CXX)\).*?\.(cc|cpp))$/\t\@echo " " CXX \$\@;$1 > \/dev\/null/;' Makefile perl -i -pe 's/^\tmv -f/\t\@mv -f/;' Makefile perl -i -pe 's/^\t(\$\((.*?)LINK\).*)/\t\@echo " " LD \$\@;$1 > \/dev\/null/;' Makefile fi diff --git a/src/amount.h b/src/amount.h index 8679d665..a9e60be3 100644 --- a/src/amount.h +++ b/src/amount.h @@ -88,9 +88,9 @@ class amount_t { public: /** - * The initialize and shutdown methods ready the amount subsystem - * for use. Normally they are called by `ledger::initialize' and - * `ledger::shutdown'. + * The initialize and shutdown methods ready the amount subsystem for + * use. Normally they are called by `session_t::initialize' and + * `session_t::shutdown'. */ static void initialize(); static void shutdown(); @@ -62,12 +62,15 @@ private: string str; bool compiled; +public: + /** + * The initialize and shutdown methods ready the amount subsystem for + * use. Normally they are called by `session_t::initialize' and + * `session_t::shutdown'. + */ static void initialize(); static void shutdown(); - friend class session_t; - -public: expr_t(); expr_t(const expr_t& other); expr_t(const ptr_op_t& _ptr, const string& _str = ""); diff --git a/test/UnitTests.cc b/test/UnitTests.cc index f6a27d3e..9095da85 100644 --- a/test/UnitTests.cc +++ b/test/UnitTests.cc @@ -8,15 +8,12 @@ #include <cppunit/extensions/TestFactoryRegistry.h> #include "UnitTests.h" - +#include "utils.h" // Create the CppUnit registry CPPUNIT_REGISTRY_ADD_TO_DEFAULT("Framework"); -CPPUNIT_REGISTRY_ADD_TO_DEFAULT("numerics"); -CPPUNIT_REGISTRY_ADD_TO_DEFAULT("utility"); - // Create a sample test, which acts both as a template, and a // verification that the basic framework is functioning. @@ -45,7 +42,6 @@ private: CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(UnitTests, "framework"); - // Create the various runners and commence running the tests! int main(int argc, char* argv[]) @@ -82,12 +78,12 @@ int main(int argc, char* argv[]) runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest()); try { IF_VERIFY() - initialize_memory_tracing(); + ledger::initialize_memory_tracing(); runner.run(controller, testPath); IF_VERIFY() - shutdown_memory_tracing(); + ledger::shutdown_memory_tracing(); // Print test in a compiler compatible format. CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut()); @@ -103,9 +99,7 @@ int main(int argc, char* argv[]) #endif } catch (std::invalid_argument &e) { // Test path not resolved - CPPUNIT_NS::stdCOut() << "\n" - << "ERROR: " << e.what() - << "\n"; + CPPUNIT_NS::stdCOut() << "\nERROR: " << e.what() << "\n"; return 0; } diff --git a/test/UnitTests.h b/test/UnitTests.h index e7027cf4..d9314ddf 100644 --- a/test/UnitTests.h +++ b/test/UnitTests.h @@ -1,10 +1,6 @@ #ifndef _UNITTESTS_H #define _UNITTESTS_H -#include "ledger.h" - -using namespace ledger; - #include <cppunit/extensions/HelperMacros.h> #include <cppunit/Exception.h> #include <cppunit/Portability.h> @@ -19,6 +15,6 @@ using namespace ledger; #define assertMessage(x,y) CPPUNIT_ASSERT_MESSAGE(x,y) #define assertThrow(x,y) CPPUNIT_ASSERT_THROW(x,y) -#define internalAmount(x) amount_t::exact(x) +#define internalAmount(x) amount_t::exact(x) #endif /* _UNITTESTS_H */ diff --git a/test/UnitTests.py b/test/UnitTests.py index a51300a3..6e75a147 100644 --- a/test/UnitTests.py +++ b/test/UnitTests.py @@ -1,6 +1,6 @@ from unittest import TextTestRunner, TestSuite -import test.python.numerics.t_amount as t_amount +import test.python.math.t_amount as t_amount suites = [ t_amount.suite(), diff --git a/test/data_tests.cc b/test/data_tests.cc new file mode 100644 index 00000000..8fa1db8a --- /dev/null +++ b/test/data_tests.cc @@ -0,0 +1,3 @@ +#include <cppunit/extensions/HelperMacros.h> + +CPPUNIT_REGISTRY_ADD_TO_DEFAULT("data"); diff --git a/test/expr_tests.cc b/test/expr_tests.cc new file mode 100644 index 00000000..d3f0a734 --- /dev/null +++ b/test/expr_tests.cc @@ -0,0 +1,3 @@ +#include <cppunit/extensions/HelperMacros.h> + +CPPUNIT_REGISTRY_ADD_TO_DEFAULT("expr"); diff --git a/test/extra_tests.cc b/test/extra_tests.cc new file mode 100644 index 00000000..4d8d6a9e --- /dev/null +++ b/test/extra_tests.cc @@ -0,0 +1,3 @@ +#include <cppunit/extensions/HelperMacros.h> + +CPPUNIT_REGISTRY_ADD_TO_DEFAULT("extra"); diff --git a/test/math_tests.cc b/test/math_tests.cc new file mode 100644 index 00000000..c1e8c839 --- /dev/null +++ b/test/math_tests.cc @@ -0,0 +1,3 @@ +#include <cppunit/extensions/HelperMacros.h> + +CPPUNIT_REGISTRY_ADD_TO_DEFAULT("math"); diff --git a/test/parse_tests.cc b/test/parse_tests.cc new file mode 100644 index 00000000..f741fd98 --- /dev/null +++ b/test/parse_tests.cc @@ -0,0 +1,3 @@ +#include <cppunit/extensions/HelperMacros.h> + +CPPUNIT_REGISTRY_ADD_TO_DEFAULT("parse"); diff --git a/test/python/numerics/__init__.py b/test/python/math/__init__.py index e69de29b..e69de29b 100644 --- a/test/python/numerics/__init__.py +++ b/test/python/math/__init__.py diff --git a/test/python/numerics/t_amount.py b/test/python/math/t_amount.py index 0aabbec4..0aabbec4 100644 --- a/test/python/numerics/t_amount.py +++ b/test/python/math/t_amount.py diff --git a/test/python/math/t_amount_flymake.py b/test/python/math/t_amount_flymake.py new file mode 100644 index 00000000..0aabbec4 --- /dev/null +++ b/test/python/math/t_amount_flymake.py @@ -0,0 +1,1469 @@ +# -*- coding: utf-8 -*- + +import unittest +import exceptions +import operator + +from ledger import * +from StringIO import * + +internalAmount = amount.exact + +class t_amountTestCase(unittest.TestCase): + testSession = None + + def assertValid(self, amt): + self.assertTrue(amt.valid()) + + def setUp(self): + #self.testSession = session() + #set_session_context(self.testSession) + + # Cause the display precision for dollars to be initialized to 2. + x1 = amount("$1.00") + self.assertTrue(x1) + + amount.stream_fullstrings = True # make reports from UnitTests accurate + + def tearDown(self): + amount.stream_fullstrings = False + + #set_session_context() + #self.testSession = None + + 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.45") + x9 = amount(x8) + x10 = amount(x8) + x11 = amount(x8) + x12 = amount("$100") + + self.assertEqual(2, x12.commodity.precision) + + #buf = "$100..." + #input = StringIO(buf) + #x13 = amount() + #x13.parse(input) + #self.assertEqual(x12, x13) + + x14 = amount() + self.assertRaises(exceptions.ArithmeticError, lambda: x14.parse("DM")) + + x15 = amount("$1.000.000,00") + + x16 = amount("$2000") + self.assertEqual("$2.000,00", x16.to_string()) + x16.parse("$2000,00") + self.assertEqual("$2.000,00", x16.to_string()) + + # Since European-ness is an additive quality, we must switch back + # to American-ness manually + x15.commodity.drop_flags(COMMODITY_STYLE_EUROPEAN) + + x17 = amount("$1,000,000.00") + + x18 = amount("$2000") + self.assertEqual("$2,000.00", x18.to_string()) + x18.parse("$2,000") + self.assertEqual("$2,000.00", x18.to_string()) + + self.assertEqual(x15, x17) + + x19 = amount("EUR 1000") + x20 = amount("EUR 1000") + + self.assertEqual("EUR 1000", x19.to_string()) + self.assertEqual("EUR 1000", x20.to_string()) + + x1.parse("$100.0000", AMOUNT_PARSE_NO_MIGRATE) + self.assertEqual(2, 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) + x2 = amount(123456L) + x3 = amount("123.456") + x5 = amount("123456") + x6 = amount("123.456") + x7 = amount("123456") + x8 = amount("123.456") + x9 = amount(x3) + x10 = amount(x6) + x11 = amount(x8) + + self.assertRaises(exceptions.ArithmeticError, lambda: amount(0) == x0) + self.assertRaises(exceptions.ArithmeticError, lambda: amount() == x0) + self.assertRaises(exceptions.ArithmeticError, lambda: amount("0") == x0) + self.assertRaises(exceptions.ArithmeticError, lambda: amount("0.0") == x0) + self.assertEqual(x2, x1) + self.assertEqual(x5, x1) + self.assertEqual(x7, x1) + self.assertEqual(x6, x3) + self.assertEqual(x8, x3) + self.assertEqual(x10, x3) + self.assertEqual(x10, x9) + + 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) + + def testCommodityConstructors(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 testAssignment(self): + x0 = amount() + x1 = amount(123456) + x2 = amount(123456L) + x3 = amount("123.456") + x5 = amount("123456") + x6 = amount("123.456") + x7 = "123456" + x8 = "123.456" + x9 = amount(x3) + x10 = amount(x6) + + self.assertEqual(x2, x1) + self.assertEqual(x5, x1) + self.assertEqual(x7, x1) + self.assertEqual(x6, x3) + self.assertEqual(x8, x3) + self.assertEqual(x10, x3) + self.assertEqual(x10, x9) + + x1 = amount(123456) + x2 = amount(123456L) + x3 = amount("123.456") + x5 = amount("123456") + x6 = amount("123.456") + x7 = amount("123456") + x8 = amount("123.456") + x9 = x3 + x10 = amount(x6) + + self.assertEqual(x2, x1) + self.assertEqual(x5, x1) + self.assertEqual(x7, x1) + self.assertEqual(x6, x3) + self.assertEqual(x8, x3) + 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) + self.assertValid(x5) + self.assertValid(x6) + self.assertValid(x7) + self.assertValid(x8) + self.assertValid(x9) + self.assertValid(x10) + + def testCommodityAssignment(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): + x1 = amount(123456) + x2 = amount(456789) + x3 = amount(333333) + x4 = amount("123456.0") + x5 = amount("123456.0") + x6 = 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(x4 == x6) + + self.assertTrue(x1 == 123456) + self.assertTrue(123456 == x1) + self.assertTrue(x1 == 123456L) + self.assertTrue(123456L == x1) + self.assertTrue(x1 == amount("123456.0")) + self.assertTrue(amount("123456.0") == x1) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + self.assertValid(x6) + + def testCommodityEquality(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.is_null()) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.is_zero()) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.is_realzero()) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.sign() == 0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.compare(x1) < 0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.compare(x2) > 0) + self.assertRaises(exceptions.ArithmeticError, lambda: 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 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.assertRaises(exceptions.ArithmeticError, lambda: x0 > x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 < x2) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 > x3) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 < x4) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 > x5) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 < x6) + + self.assertTrue(x1 > x3) + self.assertTrue(x3 <= x5) + self.assertTrue(x3 >= x5) + self.assertTrue(x3 < x1) + self.assertTrue(x3 < x4) + + self.assertTrue(x1 < 100) + self.assertTrue(100 > x1) + self.assertTrue(x1 < 100L) + self.assertTrue(100L > x1) + self.assertTrue(x1 < amount("100.0")) + self.assertTrue(amount("100.0") > x1) + + self.assertValid(x0) + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + self.assertValid(x6) + + def testCommodityComparisons(self): + 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") + x7 = amount("DM 123.45") + + 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.assertFalse(x6 == x7) + self.assertRaises(exceptions.ArithmeticError, lambda: x6 < x7) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + self.assertValid(x6) + + def testIntegerAddition(self): + x0 = amount() + 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.assertValid(x0) + self.assertValid(x1) + self.assertValid(y1) + self.assertValid(x4) + + def testFractionalAddition(self): + x1 = amount("123.123") + y1 = amount("456.456") + + self.assertEqual(amount("579.579"), x1 + y1) + self.assertEqual(amount("579.579"), x1 + amount("456.456")) + self.assertEqual(amount("579.579"), amount("456.456") + x1) + + x1 += amount("456.456") + self.assertEqual(amount("579.579"), x1) + x1 += amount("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.assertValid(x1) + self.assertValid(y1) + self.assertValid(x2) + + def testCommodityAddition(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, lambda: x1 + x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 + x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 + x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 + x3) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 + x4) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 + x5) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 + x6) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 + amount("123.45")) + self.assertRaises(exceptions.ArithmeticError, lambda: 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 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.assertValid(x1) + self.assertValid(y1) + self.assertValid(x4) + self.assertValid(y4) + + 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 -= amount("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.assertValid(x1) + self.assertValid(y1) + self.assertValid(x2) + self.assertValid(y2) + + def testCommoditySubtraction(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, lambda: x1 - x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 - x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 - x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 - x3) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 - x4) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 - x5) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 - x6) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 - amount("123.45")) + self.assertRaises(exceptions.ArithmeticError, lambda: 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 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.assertValid(x1) + self.assertValid(y1) + self.assertValid(x4) + + 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 * amount("456.456")) + self.assertEqual(amount("56200.232088"), amount("456.456") * x1) + self.assertEqual(amount("56200.232088"), amount("456.456") * x1) + + x1 *= amount("123.123") + self.assertEqual(amount("15159.273129"), x1) + x1 *= amount("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.assertValid(x1) + self.assertValid(y1) + self.assertValid(x2) + + def testCommodityMultiplication(self): + x0 = amount() + 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, lambda: x1 * x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 * x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 * x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 * x3) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 * x4) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 * x5) + + x1 *= amount("123.12") + self.assertEqual(internalAmount("$15158.5344"), x1) + self.assertEqual("$15158.53", x1.to_string()) + x1 *= amount("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 testIntegerDivision(self): + x1 = amount(123) + y1 = amount(456) + + self.assertRaises(exceptions.ArithmeticError, lambda: x1 / 0) + 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.assertValid(x1) + self.assertValid(y1) + self.assertValid(x4) + self.assertValid(y4) + + def testFractionalDivision(self): + x1 = amount("123.123") + y1 = amount("456.456") + + self.assertRaises(exceptions.ArithmeticError, lambda: x1 / 0) + self.assertEqual(amount("0.00812195934"), amount("1.0") / x1) + self.assertEqual(amount("0.00812195934"), amount("1.0") / x1) + self.assertEqual(x1, x1 / amount("1.0")) + self.assertEqual(amount("0.00812195934"), amount("1.0") / x1) + self.assertEqual(amount("0.00812195934"), amount("1.0") / x1) + self.assertEqual(- x1, x1 / amount("-1.0")) + self.assertEqual(- amount("0.00812195934"), amount("-1.0") / x1) + self.assertEqual(- amount("0.00812195934"), amount("-1.0") / x1) + self.assertEqual(amount("0.269736842105263"), x1 / y1) + self.assertEqual(amount("3.707317073170732"), y1 / x1) + self.assertEqual(amount("0.269736842105263"), x1 / amount("456.456")) + self.assertEqual(amount("3.707317073170732"), amount("456.456") / x1) + self.assertEqual(amount("3.707317073170732"), amount("456.456") / x1) + + x1 /= amount("456.456") + self.assertEqual(amount("0.269736842105263"), x1) + x1 /= amount("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.assertValid(x1) + self.assertValid(y1) + self.assertValid(x4) + self.assertValid(y4) + + def testCommodityDivision(self): + x0 = amount() + 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, lambda: 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, lambda: x1 / x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 / x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 / x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 / x3) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 / x4) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 / x5) + + x1 /= amount("123.12") + self.assertEqual(internalAmount("$1.00"), x1) + self.assertEqual("$1.00", x1.to_string()) + x1 /= amount("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 testNegation(self): + x0 = amount() + x1 = amount(-123456) + x3 = amount("-123.456") + x5 = amount("-123456") + x6 = amount("-123.456") + x7 = amount("-123456") + x8 = amount("-123.456") + x9 = amount(- x3) + + self.assertRaises(exceptions.ArithmeticError, lambda: x0.negate()) + self.assertEqual(x5, x1) + self.assertEqual(x7, x1) + self.assertEqual(x6, x3) + self.assertEqual(x8, x3) + self.assertEqual(- x6, x9) + self.assertEqual(x3.negate(), x9) + + x10 = amount(x9.negate()) + + self.assertEqual(x3, x10) + + self.assertValid(x1) + self.assertValid(x3) + self.assertValid(x5) + self.assertValid(x6) + self.assertValid(x7) + self.assertValid(x8) + self.assertValid(x9) + self.assertValid(x10) + + def testCommodityNegation(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 testAbs(self): + x0 = amount() + x1 = amount(-1234) + x2 = amount(1234) + + self.assertRaises(exceptions.ArithmeticError, lambda: x0.abs()) + self.assertEqual(amount(1234), x1.abs()) + self.assertEqual(amount(1234), x2.abs()) + + self.assertValid(x0) + self.assertValid(x1) + self.assertValid(x2) + + def testCommodityAbs(self): + x1 = amount("$-1234.56") + x2 = amount("$1234.56") + + self.assertEqual(amount("$1234.56"), x1.abs()) + self.assertEqual(amount("$1234.56"), x2.abs()) + + self.assertValid(x1) + self.assertValid(x2) + + def testFractionalRound(self): + x0 = amount() + x1 = amount("1234.567890") + + self.assertRaises(exceptions.ArithmeticError, lambda: x0.precision) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.round()) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.round(2)) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.unround()) + self.assertEqual(6, x1.precision) + + x1b = amount(x1.unround()) + + self.assertEqual(x1b.precision, x1b.unround().precision) + + y7 = amount(x1.round(7)) + y6 = amount(x1.round(6)) + y5 = amount(x1.round(5)) + y4 = amount(x1.round(4)) + y3 = amount(x1.round(3)) + y2 = amount(x1.round(2)) + y1 = amount(x1.round(1)) + y0 = amount(x1.round(0)) + + self.assertEqual(6, y7.precision) + self.assertEqual(6, y6.precision) + self.assertEqual(5, y5.precision) + self.assertEqual(4, y4.precision) + self.assertEqual(3, y3.precision) + self.assertEqual(2, y2.precision) + self.assertEqual(1, y1.precision) + self.assertEqual(0, y0.precision) + + self.assertEqual(amount("1234.56789"), y7) + self.assertEqual(amount("1234.56789"), y6) + self.assertEqual(amount("1234.56789"), y5) + self.assertEqual(amount("1234.5679"), y4) + self.assertEqual(amount("1234.568"), y3) + self.assertEqual(amount("1234.57"), y2) + self.assertEqual(amount("1234.6"), y1) + self.assertEqual(amount("1235"), y0) + + 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)) + + x5 = amount("0.0000000000000000000000000000000000001") + + self.assertEqual(amount("0.0000000000000000000000000000000000001"), + x5.round(37)) + self.assertEqual(amount(0), x5.round(36)) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + + def testCommodityRound(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)) + + x5 = amount("$123.45") + + x5 *= amount("100.12") + + self.assertEqual(internalAmount("$12359.814"), x5) + self.assertEqual("$12359.81", x5.to_string()) + self.assertEqual("$12359.814", x5.to_fullstring()) + self.assertEqual("$12359.814", x5.unround().to_string()) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + self.assertValid(x5) + + def testCommodityDisplayRound(self): + x1 = amount("$0.85") + x2 = amount("$0.1") + + x1 *= amount("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 testReduction(self): + x0 = amount() + x1 = amount("60s") + x2 = amount("600s") + x3 = amount("6000s") + x4 = amount("360000s") + x5 = amount("10m") + x6 = amount("100m") + x7 = amount("1000m") + x8 = amount("10000m") + x9 = amount("10h") + x10 = amount("100h") + x11 = amount("1000h") + x12 = amount("10000h") + + self.assertRaises(exceptions.ArithmeticError, lambda: x0.reduce()) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.unreduce()) + self.assertEqual(x2, x5) + self.assertEqual(x3, x6) + self.assertEqual(x4, x10) + self.assertEqual("100.0h", x4.unreduce().to_string()) + + def testSign(self): + x0 = amount() + x1 = amount("0.0000000000000000000000000000000000001") + x2 = amount("-0.0000000000000000000000000000000000001") + x3 = amount("1") + x4 = amount("-1") + + self.assertRaises(exceptions.ArithmeticError, lambda: 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 testCommoditySign(self): + x1 = amount(internalAmount("$0.0000000000000000000000000000000000001")) + x2 = amount(internalAmount("$-0.0000000000000000000000000000000000001")) + x3 = amount("$1") + x4 = amount("$-1") + + self.assertTrue(x1.sign() != 0) + self.assertTrue(x2.sign() != 0) + self.assertTrue(x3.sign() > 0) + self.assertTrue(x4.sign() < 0) + + self.assertValid(x1) + self.assertValid(x2) + self.assertValid(x3) + self.assertValid(x4) + + def testTruth(self): + x0 = amount() + x1 = amount("1234") + x2 = amount("1234.56") + + self.assertRaises(exceptions.ArithmeticError, lambda: 1 if x0 else 0) + + self.assertTrue(x1) + self.assertTrue(x2) + + self.assertValid(x0) + self.assertValid(x1) + self.assertValid(x2) + + def testCommodityTruth(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): + x0 = amount() + x1 = amount("0.000000000000000000001") + + self.assertTrue(x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.is_zero()) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.is_realzero()) + self.assertFalse(x1.is_zero()) + self.assertFalse(x1.is_realzero()) + + self.assertValid(x0) + self.assertValid(x1) + + def testCommodityForZero(self): + x1 = amount(internalAmount("$0.000000000000000000001")) + + self.assertTrue(x1) + self.assertFalse(x1.is_zero()) + self.assertFalse(x1.is_realzero()) + + self.assertValid(x1) + + def testIntegerConversion(self): + x0 = amount() + x1 = amount(123456) + x2 = amount("12345682348723487324") + + self.assertRaises(exceptions.ArithmeticError, lambda: x0.to_long()) + #self.assertRaises(exceptions.ArithmeticError, lambda: x0.to_double()) + self.assertFalse(x2.fits_in_long()) + self.assertEqual(123456, x1.to_long()) + #self.assertEqual(123456.0, x1.to_double()) + self.assertEqual("123456", x1.to_string()) + self.assertEqual("123456", x1.quantity_string) + + self.assertValid(x1) + + def testFractionalConversion(self): + x1 = amount("1234.56") + x2 = amount("1234.5683787634678348734") + + self.assertRaises(exceptions.ArithmeticError, lambda: x1.to_long()) # loses precision + #self.assertRaises(exceptions.ArithmeticError, lambda: x2.to_double()) # loses precision + #self.assertFalse(x2.fits_in_double()) + self.assertEqual(1234, x1.to_long(True)) + #self.assertEqual(1234.56, x1.to_double()) + self.assertEqual("1234.56", x1.to_string()) + self.assertEqual("1234.56", x1.quantity_string) + + self.assertValid(x1) + + def testCommodityConversion(self): + x1 = amount("$1234.56") + + self.assertRaises(exceptions.ArithmeticError, lambda: x1.to_long()) # loses precision + self.assertEqual(1234, x1.to_long(True)) + #self.assertEqual(1234.56, x1.to_double()) + self.assertEqual("$1234.56", x1.to_string()) + self.assertEqual("1234.56", x1.quantity_string) + + self.assertValid(x1) + + def testPrinting(self): + pass + #x0 = amount() + #x1 = amount("982340823.380238098235098235098235098") + # + #bufstr = StringIO() + #self.assertRaises(exceptions.ArithmeticError, lambda: bufstr.write(x0)) + # + #bufstr = StringIO() + #bufstr.write(x1) + # + #self.assertEqual("982340823.380238098235098235098235098", + # bufstr.getvalue()) + # + #self.assertValid(x0) + #self.assertValid(x1) + + def testCommodityPrinting(self): + pass + #x1 = amount(internalAmount("$982340823.386238098235098235098235098")) + #x2 = amount("$982340823.38") + # + #bufstr = StringIO() + #bufstr.write(x1) + # + #self.assertEqual("$982340823.386238098235098235098235098", + # bufstr.getvalue()) + # + #bufstr = StringIO() + #bufstr.write((x1 * x2).to_string()) + # + #self.assertEqual("$964993493285024293.18099172508158508135413499124", + # bufstr.getvalue()) + # + #bufstr = StringIO() + #bufstr.write((x2 * x1).to_string()) + # + #self.assertEqual("$964993493285024293.18", bufstr.getvalue()) + # + #self.assertValid(x1) + #self.assertValid(x2) + + def testSerialization(self): + pass + #x0 = amount() + #x1 = amount("$8,192.34") + #x2 = amount("8192.34") + #x3 = amount("8192.34") + #x4 = amount("-8192.34") + #x5 = amount(x4) + # + ## Force x3's pointer to actually be set to null_commodity + ##x3.set_commodity(*x3.current_pool.null_commodity) + # + #buf = "" + #storage = StringIO() + #self.assertRaises(exceptions.ArithmeticError, lambda: x0.write(storage)) + #x1.write(storage) + #x2.write(storage) + #x3.write(storage) + #x4.write(storage) + #x5.write(storage) + #buf = storage.getvalue() + # + #x1b = amount() + #x2b = amount() + #x3b = amount() + #x4b = amount() + #x5b = amount() + # + #storage = StringIO(buf) + #x1b.read(storage) + #x2b.read(storage) + #x3b.read(storage) + #x4b.read(storage) + #x5b.read(storage) + # + #self.assertEqual(x1, x1b) + #self.assertEqual(x2, x2b) + #self.assertEqual(x3, x3b) + #self.assertEqual(x4, x4b) + # + #ptr = buf.c_str() + # + #x1c = amount() + #x2c = amount() + #x3c = amount() + #x4c = amount() + #x5c = amount() + # + #x1c.read(ptr) + #x2c.read(ptr) + #x3c.read(ptr) + #x4c.read(ptr) + #x5c.read(ptr) + # + #self.assertEqual(x1, x1b) + #self.assertEqual(x2, x2b) + #self.assertEqual(x3, x3b) + #self.assertEqual(x4, x4b) + # + #self.assertValid(x1) + #self.assertValid(x2) + #self.assertValid(x3) + #self.assertValid(x4) + #self.assertValid(x1b) + #self.assertValid(x2b) + #self.assertValid(x3b) + #self.assertValid(x4b) + #self.assertValid(x1c) + #self.assertValid(x2c) + #self.assertValid(x3c) + #self.assertValid(x4c) + + +def suite(): + return unittest.TestLoader().loadTestsFromTestCase(t_amountTestCase) + +if __name__ == '__main__': + unittest.main() diff --git a/test/report_tests.cc b/test/report_tests.cc new file mode 100644 index 00000000..2fabbe9a --- /dev/null +++ b/test/report_tests.cc @@ -0,0 +1,3 @@ +#include <cppunit/extensions/HelperMacros.h> + +CPPUNIT_REGISTRY_ADD_TO_DEFAULT("report"); diff --git a/test/unit/t_amount.cc b/test/unit/t_amount.cc index 6fd78fa6..c95384a8 100644 --- a/test/unit/t_amount.cc +++ b/test/unit/t_amount.cc @@ -1,10 +1,15 @@ #include "t_amount.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(AmountTestCase, "numerics"); +#include "utils.h" +#include "amount.h" + +using namespace ledger; + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(AmountTestCase, "math"); void AmountTestCase::setUp() { - ledger::set_session_context(&session); + amount_t::initialize(); // Cause the display precision for dollars to be initialized to 2. amount_t x1("$1.00"); @@ -16,8 +21,7 @@ void AmountTestCase::setUp() void AmountTestCase::tearDown() { amount_t::stream_fullstrings = false; - - ledger::set_session_context(); + amount_t::shutdown(); } void AmountTestCase::testParser() diff --git a/test/unit/t_amount.h b/test/unit/t_amount.h index 2d5a327a..a8310673 100644 --- a/test/unit/t_amount.h +++ b/test/unit/t_amount.h @@ -52,8 +52,6 @@ class AmountTestCase : public CPPUNIT_NS::TestCase CPPUNIT_TEST_SUITE_END(); public: - ledger::session_t session; - AmountTestCase() {} virtual ~AmountTestCase() {} diff --git a/test/unit/t_balance.cc b/test/unit/t_balance.cc index ca759836..93d97d6a 100644 --- a/test/unit/t_balance.cc +++ b/test/unit/t_balance.cc @@ -1,10 +1,15 @@ #include "t_balance.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(BalanceTestCase, "numerics"); +#include "utils.h" +#include "amount.h" + +using namespace ledger; + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(BalanceTestCase, "math"); void BalanceTestCase::setUp() { - ledger::set_session_context(&session); + amount_t::initialize(); // Cause the display precision for dollars to be initialized to 2. amount_t x1("$1.00"); @@ -16,8 +21,7 @@ void BalanceTestCase::setUp() void BalanceTestCase::tearDown() { amount_t::stream_fullstrings = false; - - ledger::set_session_context(); + amount_t::shutdown(); } void BalanceTestCase::testConstructors() diff --git a/test/unit/t_balance.h b/test/unit/t_balance.h index 7c27f7e8..fea68b8a 100644 --- a/test/unit/t_balance.h +++ b/test/unit/t_balance.h @@ -12,8 +12,6 @@ class BalanceTestCase : public CPPUNIT_NS::TestCase CPPUNIT_TEST_SUITE_END(); public: - ledger::session_t session; - BalanceTestCase() {} virtual ~BalanceTestCase() {} diff --git a/test/unit/t_commodity.cc b/test/unit/t_commodity.cc index 925b2cee..62ec557e 100644 --- a/test/unit/t_commodity.cc +++ b/test/unit/t_commodity.cc @@ -1,12 +1,18 @@ #include "t_commodity.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CommodityTestCase, "numerics"); +#include "utils.h" +#include "amount.h" + +using namespace ledger; + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CommodityTestCase, "math"); void CommodityTestCase::setUp() { - ledger::set_session_context(&session); + amount_t::initialize(); } + void CommodityTestCase::tearDown() { - ledger::set_session_context(); + amount_t::shutdown(); } void CommodityTestCase::testPriceHistory() diff --git a/test/unit/t_commodity.h b/test/unit/t_commodity.h index ed739751..67903628 100644 --- a/test/unit/t_commodity.h +++ b/test/unit/t_commodity.h @@ -15,8 +15,6 @@ class CommodityTestCase : public CPPUNIT_NS::TestCase CPPUNIT_TEST_SUITE_END(); public: - ledger::session_t session; - CommodityTestCase() {} virtual ~CommodityTestCase() {} diff --git a/test/unit/t_expr.cc b/test/unit/t_expr.cc index 58a60e23..a6655f6a 100644 --- a/test/unit/t_expr.cc +++ b/test/unit/t_expr.cc @@ -1,23 +1,21 @@ #include "t_expr.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ValueExprTestCase, "numerics"); +#include "expr.h" -void ValueExprTestCase::setUp() -{ - ledger::set_session_context(&session); +using namespace ledger; - // Cause the display precision for dollars to be initialized to 2. - amount_t x1("$1.00"); - assertTrue(x1); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ValueExprTestCase, "expr"); - amount_t::stream_fullstrings = true; // make reports from UnitTests accurate +void ValueExprTestCase::setUp() +{ + amount_t::initialize(); + expr_t::initialize(); } void ValueExprTestCase::tearDown() { - amount_t::stream_fullstrings = false; - - ledger::set_session_context(); + expr_t::shutdown(); + amount_t::shutdown(); } void ValueExprTestCase::testConstructors() diff --git a/test/unit/t_expr.h b/test/unit/t_expr.h index bb03ba56..50813ea3 100644 --- a/test/unit/t_expr.h +++ b/test/unit/t_expr.h @@ -12,8 +12,6 @@ class ValueExprTestCase : public CPPUNIT_NS::TestCase CPPUNIT_TEST_SUITE_END(); public: - ledger::session_t session; - ValueExprTestCase() {} virtual ~ValueExprTestCase() {} diff --git a/test/unit/t_times.cc b/test/unit/t_times.cc index 05fd34ce..7f35bf50 100644 --- a/test/unit/t_times.cc +++ b/test/unit/t_times.cc @@ -1,6 +1,6 @@ #include "t_times.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(DateTimeTestCase, "utility"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(DateTimeTestCase, "util"); void DateTimeTestCase::setUp() {} void DateTimeTestCase::tearDown() {} diff --git a/test/unit/t_utils.cc b/test/unit/t_utils.cc index eda84a3a..57c2a5d4 100644 --- a/test/unit/t_utils.cc +++ b/test/unit/t_utils.cc @@ -1,6 +1,6 @@ #include "t_utils.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(UtilitiesTestCase, "utility"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(UtilitiesTestCase, "util"); void UtilitiesTestCase::setUp() {} void UtilitiesTestCase::tearDown() {} diff --git a/test/unit/t_valexpr.cc b/test/unit/t_valexpr.cc deleted file mode 100644 index 026b4eec..00000000 --- a/test/unit/t_valexpr.cc +++ /dev/null @@ -1,25 +0,0 @@ -#include "t_valexpr.h" - -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ValueExprTestCase, "numerics"); - -void ValueExprTestCase::setUp() -{ - ledger::set_session_context(&session); - - // Cause the display precision for dollars to be initialized to 2. - amount_t x1("$1.00"); - assertTrue(x1); - - amount_t::stream_fullstrings = true; // make reports from UnitTests accurate -} - -void ValueExprTestCase::tearDown() -{ - amount_t::stream_fullstrings = false; - - ledger::set_session_context(); -} - -void ValueExprTestCase::testConstructors() -{ -} diff --git a/test/unit/t_valexpr.h b/test/unit/t_valexpr.h deleted file mode 100644 index 3cac4ed6..00000000 --- a/test/unit/t_valexpr.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef _T_VALEXPR_H -#define _T_VALEXPR_H - -#include "UnitTests.h" - -class ValueExprTestCase : public CPPUNIT_NS::TestCase -{ - CPPUNIT_TEST_SUITE(ValueExprTestCase); - - CPPUNIT_TEST(testConstructors); - - CPPUNIT_TEST_SUITE_END(); - -public: - ledger::session_t session; - - ValueExprTestCase() {} - virtual ~ValueExprTestCase() {} - - virtual void setUp(); - virtual void tearDown(); - - void testConstructors(); - -private: - ValueExprTestCase(const ValueExprTestCase ©); - void operator=(const ValueExprTestCase ©); -}; - -#endif // _T_VALEXPR_H diff --git a/test/util_tests.cc b/test/util_tests.cc new file mode 100644 index 00000000..f584db89 --- /dev/null +++ b/test/util_tests.cc @@ -0,0 +1,3 @@ +#include <cppunit/extensions/HelperMacros.h> + +CPPUNIT_REGISTRY_ADD_TO_DEFAULT("util"); |