diff options
author | John Wiegley <johnw@newartisans.com> | 2009-01-28 20:49:26 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-01-28 20:49:26 -0400 |
commit | e851c02d2747be0acc336bc278da9e0460b75738 (patch) | |
tree | df813c5c572922cf05a6317b7eb1d6062dedac05 | |
parent | 3020f0f8513d8cef04d4a291dc9b5fa50e1b832c (diff) | |
download | fork-ledger-e851c02d2747be0acc336bc278da9e0460b75738.tar.gz fork-ledger-e851c02d2747be0acc336bc278da9e0460b75738.tar.bz2 fork-ledger-e851c02d2747be0acc336bc278da9e0460b75738.zip |
Unit test for Python are now auto-generated from the C++ tests.
-rw-r--r-- | Makefile.am | 36 | ||||
-rw-r--r-- | python/py_amount.cc | 58 | ||||
-rw-r--r-- | python/py_commodity.cc | 11 | ||||
-rw-r--r-- | python/py_times.cc | 22 | ||||
-rwxr-xr-x | test/PyUnitTests.py | 2 | ||||
-rw-r--r-- | test/UnitTests.py | 9 | ||||
-rwxr-xr-x | test/convert.py | 174 | ||||
-rw-r--r-- | test/python/__init__.py | 0 | ||||
-rw-r--r-- | test/python/math/__init__.py | 0 | ||||
-rw-r--r-- | test/python/math/t_amount.py | 1469 | ||||
-rw-r--r-- | test/unit/t_amount.cc | 134 | ||||
-rw-r--r-- | test/unit/t_amount.h | 2 | ||||
-rw-r--r-- | test/unit/t_balance.cc | 1 | ||||
-rw-r--r-- | test/unit/t_commodity.cc | 38 | ||||
-rw-r--r-- | test/unit/t_expr.cc | 5 | ||||
-rw-r--r-- | test/unit/t_times.cc | 142 | ||||
-rw-r--r-- | test/unit/t_utils.cc | 9 |
17 files changed, 479 insertions, 1633 deletions
diff --git a/Makefile.am b/Makefile.am index c36701a2..5383ec8b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -437,10 +437,42 @@ extra_tests_SOURCES = \ extra_tests_CPPFLAGS = -I$(srcdir)/test $(lib_cppflags) extra_tests_LDADD = $(math_tests_LDADD) libledger_extra.la +all_tests_sources = \ + $(util_tests_SOURCES) \ + $(math_tests_SOURCES) \ + $(expr_tests_SOURCES) \ + $(data_tests_SOURCES) \ + $(parse_tests_SOURCES) \ + $(report_tests_SOURCES) \ + $(extra_tests_SOURCES) + EXTRA_DIST += test/python PyUnitTests_SOURCES = test/__init__.py test/PyUnitTests.py test/UnitTests.py +all_py_tests_sources = \ + $(patsubst test/unit/%.cc,test/python/%.py, \ + $(filter test/unit/t_%.cc,$(all_tests_sources))) + +test/python/%.py: test/unit/%.cc test/convert.py + $(PYTHON) $(srcdir)/test/convert.py $< $@ + +test/python/UnitTests.py: $(all_py_tests_sources) + @echo "from unittest import TextTestRunner, TestSuite" > $@ + @for file in $$(ls $(srcdir)/test/unit/*.cc); do \ + base=$$(basename $$file); \ + base=$$(echo $$base | sed 's/\.cc//'); \ + echo "import $$base" >> $@; \ + done + @echo "suites = [" >> $@ + @for file in $$(ls $(srcdir)/test/unit/*.cc); do \ + base=$$(basename $$file); \ + base=$$(echo $$base | sed 's/\.cc//'); \ + echo " $$base.suite()," >> $@; \ + done + @echo "]" >> $@ + @echo "TextTestRunner().run(TestSuite(suites))" >> $@ + ESC_python=`echo "$(PYTHON)" | sed 's/\//\\\\\//g'` ESC_srcdir=`echo "$(srcdir)" | sed 's/\//\\\\\//g'` ESC_builddir=`echo "$(top_builddir)" | sed 's/\//\\\\\//g'` @@ -448,7 +480,9 @@ ESC_distdir=`echo "$(distdir)" | sed 's/\//\\\\\//g'` # jww (2007-05-10): This rule will not be triggered on systems that # define an EXEEXT. -PyUnitTests: $(srcdir)/test/PyUnitTests.py +PyUnitTests: $(srcdir)/test/PyUnitTests.py test/python/UnitTests.py + @echo all_test_sources: $(all_tests_sources) + @echo all_py_test_sources: $(all_py_tests_sources) cat $(srcdir)/test/PyUnitTests.py \ | sed "s/%python%/$(ESC_python)/" \ | sed "s/%srcdir%/$(ESC_srcdir)/g" \ diff --git a/python/py_amount.cc b/python/py_amount.cc index 5a68b6d5..195429a0 100644 --- a/python/py_amount.cc +++ b/python/py_amount.cc @@ -97,13 +97,23 @@ void py_parse_str_2(amount_t& amount, const string& str, unsigned char flags) { amount.parse(str, flags); } +void py_print(amount_t& amount, object out) { + if (PyFile_Check(out.ptr())) { + pyofstream outstr(reinterpret_cast<PyFileObject *>(out.ptr())); + amount.print(outstr); + } else { + PyErr_SetString(PyExc_IOError, + "Argument to amount.print_(file) is not a file object"); + } +} + void py_read_1(amount_t& amount, object in) { if (PyFile_Check(in.ptr())) { pyifstream instr(reinterpret_cast<PyFileObject *>(in.ptr())); amount.read(instr); } else { PyErr_SetString(PyExc_IOError, - "Argument to amount.parse(file) is not a file object"); + "Argument to amount.read(file) is not a file object"); } } void py_read_2(amount_t& amount, const std::string& str) { @@ -111,6 +121,25 @@ void py_read_2(amount_t& amount, const std::string& str) { amount.read(p); } +void py_write_xml_1(amount_t& amount, object out) { + if (PyFile_Check(out.ptr())) { + pyofstream outstr(reinterpret_cast<PyFileObject *>(out.ptr())); + amount.write_xml(outstr); + } else { + PyErr_SetString(PyExc_IOError, + "Argument to amount.write_xml(file) is not a file object"); + } +} + void py_write_xml_2(amount_t& amount, object out, const int depth) { + if (PyFile_Check(out.ptr())) { + pyofstream outstr(reinterpret_cast<PyFileObject *>(out.ptr())); + amount.write_xml(outstr, depth); + } else { + PyErr_SetString(PyExc_IOError, + "Argument to amount.write_xml(file, depth) is not a file object"); + } +} + #define EXC_TRANSLATOR(type) \ void exc_translate_ ## type(const type& err) { \ PyErr_SetString(PyExc_ArithmeticError, err.what()); \ @@ -124,12 +153,10 @@ void export_amount() scope().attr("AMOUNT_PARSE_NO_REDUCE") = AMOUNT_PARSE_NO_REDUCE; class_< amount_t > ("Amount") -#if 0 .def("initialize", &amount_t::initialize) .staticmethod("initialize") .def("shutdown", &amount_t::shutdown) .staticmethod("shutdown") -#endif .add_static_property("current_pool", make_getter(&amount_t::current_pool, @@ -266,7 +293,7 @@ internal precision.") .def(double() / self) #endif - .add_property("precision", &amount_t::precision) + .def("precision", &amount_t::precision) .def("negate", &amount_t::negate) .def("in_place_negate", &amount_t::in_place_negate, @@ -317,22 +344,21 @@ internal precision.") #endif .def("fits_in_long", &amount_t::fits_in_long) - .add_property("quantity_string", &amount_t::quantity_string) + .def("quantity_string", &amount_t::quantity_string) - .add_property("commodity", - make_function(&amount_t::commodity, - return_value_policy<reference_existing_object>()), - make_function(&amount_t::set_commodity, - with_custodian_and_ward<1, 2>())) + .def("commodity", &amount_t::commodity, + return_value_policy<reference_existing_object>()) + .def("set_commodity", &amount_t::set_commodity, + with_custodian_and_ward<1, 2>()) .def("has_commodity", &amount_t::has_commodity) .def("clear_commodity", &amount_t::clear_commodity) - .add_property("number", &amount_t::number) + .def("number", &amount_t::number) .def("annotate", &amount_t::annotate) .def("is_annotated", &amount_t::is_annotated) #if 0 - .add_property("annotation", &amount_t::annotation) + .def("annotation", &amount_t::annotation) #endif .def("strip_annotations", &amount_t::strip_annotations) @@ -344,10 +370,18 @@ internal precision.") .def("parse_conversion", &amount_t::parse_conversion) .staticmethod("parse_conversion") + .def("print_", py_print) + .def("read", py_read_1) .def("read", py_read_2) .def("write", &amount_t::write) + .def("read_xml", &amount_t::read_xml) + .def("write_xml", py_write_xml_1) + .def("write_xml", py_write_xml_2) + + .def("dump", &amount_t::dump) + .def("valid", &amount_t::valid) ; diff --git a/python/py_commodity.cc b/python/py_commodity.cc index 6611a900..df9cca18 100644 --- a/python/py_commodity.cc +++ b/python/py_commodity.cc @@ -40,6 +40,13 @@ namespace ledger { using namespace boost::python; +void py_add_price(commodity_t& commodity, + const datetime_t& date, + const amount_t& price) +{ + commodity.add_price(date, price); +} + void export_commodity() { scope().attr("COMMODITY_STYLE_DEFAULTS") = COMMODITY_STYLE_DEFAULTS; @@ -57,7 +64,9 @@ void export_commodity() .def("drop_flags", &commodity_t::drop_flags) - .add_property("precision", &commodity_t::precision) + .def("add_price", py_add_price) + + .def("precision", &commodity_t::precision) ; } diff --git a/python/py_times.cc b/python/py_times.cc index 0fc62941..23532cc2 100644 --- a/python/py_times.cc +++ b/python/py_times.cc @@ -32,12 +32,6 @@ #include "pyinterp.h" #include "pyutils.h" -#include <boost/cast.hpp> -#include <boost/python/module.hpp> -#include <boost/python/def.hpp> -#include <boost/python/to_python_converter.hpp> - -#include <Python.h> #include <datetime.h> // jww (2007-05-04): Convert time duration objects to PyDelta @@ -121,12 +115,26 @@ struct datetime_from_python typedef register_python_conversion<datetime_t, datetime_to_python, datetime_from_python> datetime_python_conversion; +datetime_t py_parse_datetime(const string& str) { + return parse_datetime(str); +} + +date_t py_parse_date(const string& str) { + return parse_date(str); +} + void export_times() { - date_python_conversion(); datetime_python_conversion(); + date_python_conversion(); register_optional_to_python<datetime_t>(); + register_optional_to_python<date_t>(); + + scope().attr("parse_datetime") = &py_parse_datetime; + scope().attr("parse_date") = &py_parse_date; + scope().attr("current_time") = current_time; + scope().attr("current_date") = current_date; } } // namespace ledger diff --git a/test/PyUnitTests.py b/test/PyUnitTests.py index 41054faf..acae7781 100755 --- a/test/PyUnitTests.py +++ b/test/PyUnitTests.py @@ -3,4 +3,4 @@ PYTHONPATH="%builddir%":"%srcdir%":$PYTHONPATH \ LD_LIBRARY_PATH="%builddir%/.libs":$LD_LIBRARY_PATH \ DYLD_LIBRARY_PATH="%builddir%/.libs":$DYLD_LIBRARY_PATH \ - %python% "%srcdir%"/test/UnitTests.py + %python% "%builddir%"/test/python/UnitTests.py diff --git a/test/UnitTests.py b/test/UnitTests.py deleted file mode 100644 index 6e75a147..00000000 --- a/test/UnitTests.py +++ /dev/null @@ -1,9 +0,0 @@ -from unittest import TextTestRunner, TestSuite - -import test.python.math.t_amount as t_amount - -suites = [ - t_amount.suite(), -] - -TextTestRunner().run(TestSuite(suites)) diff --git a/test/convert.py b/test/convert.py new file mode 100755 index 00000000..54118720 --- /dev/null +++ b/test/convert.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python + +# convert.py: This script converts a C++ Ledger unit test into an equivalent +# Python unit test. +# +# Copyright (c) 2003-2009, John Wiegley. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# - Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# - Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# - Neither the name of New Artisans LLC nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import re +import sys +import os + +source = os.path.abspath(sys.argv[1]) +base = os.path.splitext(source)[0] +target = os.path.abspath(sys.argv[2]) + +dirname = os.path.dirname(target) +if not os.path.isdir(dirname): + os.makedirs(dirname) + +fd = open(source, "r") +fo = open(target, "w") + +fo.write('''# -*- coding: utf-8 -*- + +import unittest +import exceptions +import operator + +from ledger import * +from StringIO import * +from datetime import * + +internalAmount = Amount.exact + +class %sTestCase(unittest.TestCase): + testSession = None + + def assertValid(self, amt): + self.assertTrue(amt.valid())''' % os.path.basename(base)) + +not_for_python = 0 + +for line in fd.readlines(): + if re.match('^#ifndef NOT_FOR_PYTHON', line): + not_for_python += 1 + continue + elif not_for_python > 0: + if re.match('^#endif // NOT_FOR_PYTHON', line): + not_for_python -= 1 + continue + + if re.match('^(using|CPP|[#{}/])', line): + continue + if re.match('^\s+[{}]\s+$', line): + continue + + if not re.search('assert', line): + match = re.match('void [^:]+::(test[^(]+|setUp|tearDown)\(\)', line) + if match: + fo.write(' def %s(self):\n' % match.group(1)) + continue + + match = re.search(' ([a-z:_<>]+?)&?\s+([a-z0-9_]+)(\((.+?)\))?;', line) + if match: + if match.group(1) != "std::string": + line = ' %s = %s(%s)\n' % (match.group(2), match.group(1), + match.group(4) or "") + else: + line = '' + + match = re.search(' ([a-z:_<>]+?)&?\s+([a-z0-9]+)\s*=\s*([^(]+);', line) + if match: + line = ' %s = %s(%s)\n' % (match.group(2), match.group(1), + match.group(3)) + + match = re.search(' ([a-z:_<>]+?)\s+([a-z0-9]+)\s*=\s*(.+?)$', line) + if match: + line = ' %s = %s\n' % (match.group(2), match.group(3)) + + line = re.sub('CPPUNIT_ASSERT', 'self.assertTrue', line) + line = re.sub('assertValid', 'self.assertValid', line) + line = re.sub('assertTrue', 'self.assertTrue', line) + line = re.sub('assertFalse', 'self.assertFalse', line) + line = re.sub('assertNotEqual', 'self.assertNotEqual', line) + line = re.sub('assertEqual', 'self.assertEqual', line) + line = re.sub('assertThrow\(([^,]+), ([^,)]+?)\)', + 'self.assertRaises(\\2, lambda: \\1)', line) + #line = re.sub('optional<([^>]+?)>', '\\1', line) + line = re.sub('amount_t::precision_t\(([^)]+?)\)', '\\1', line) + + # Determine this list automatically by scanning the class_ lines in + # python/*.cc + line = re.sub('amount_t::', 'Amount.', line) + line = re.sub('commodity_t\(([^)]+?)\)', '\\1', line) + line = re.sub('commodity_t::', 'Commodity.', line) + line = re.sub('balance_t::', 'Balance.', line) + line = re.sub('balance_pair_t::', 'BalancePair.', line) + line = re.sub('value_t::', 'Value.', line) + + line = re.sub('amount_t', 'Amount', line) + line = re.sub('commodity_t', 'Commodity', line) + line = re.sub('balance_t', 'Balance', line) + line = re.sub('balance_pair_t', 'BalancePair', line) + line = re.sub('value_t', 'Value', line) + + line = re.sub('ledger::', '', line) + line = re.sub('std::istringstream', 'StringIO', line) + line = re.sub('std::ostringstream', 'StringIO', line) + line = re.sub('set_session_context\(&session\)', + 'self.testSession = session()\n set_session_context(self.testSession)', line) + line = re.sub('set_session_context\(\)', + 'set_session_context()\n self.testSession = None', line) + line = re.sub('([a-z_]+?)_t\b', '\\1', line) + line = re.sub('std::string\(([^)]+?)\)', '\\1', line) + line = re.sub('string\(([^)]+?)\)', '\\1', line) + line = re.sub('\.print\(([^)]+?)\)', '.print_(\\1)', line) + line = re.sub('true', 'True', line) + line = re.sub('false', 'False', line) + line = re.sub('([0-9]+)[FL]', '\\1', line) + line = re.sub('([0-9]+)UL', '\\1L', line) + line = re.sub(';', '', line) + line = re.sub('//', '#', line) + line = re.sub('->', '.', line) + line = re.sub('(\s+|\()(\S+?) \? (.+?) : (.+?)\)', + '\\1\\3 if \\2 else \\4)', line) + line = re.sub('if \((.+?)\)( {)?$', 'if \\1:', line) + line = re.sub('(} )?else( {)?$', 'else:', line) + + line = re.sub('amount_error', 'exceptions.ArithmeticError', line) + + match = re.match('^ ', line) + if match: + fo.write(' ' + line) + else: + fo.write(line) + +fo.write(''' + +def suite(): + return unittest.TestLoader().loadTestsFromTestCase(%sTestCase) + +if __name__ == '__main__': + unittest.main() +''' % os.path.basename(base)) + +fo.close() +fd.close() diff --git a/test/python/__init__.py b/test/python/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/test/python/__init__.py +++ /dev/null diff --git a/test/python/math/__init__.py b/test/python/math/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/test/python/math/__init__.py +++ /dev/null diff --git a/test/python/math/t_amount.py b/test/python/math/t_amount.py deleted file mode 100644 index 0aabbec4..00000000 --- a/test/python/math/t_amount.py +++ /dev/null @@ -1,1469 +0,0 @@ -# -*- 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/unit/t_amount.cc b/test/unit/t_amount.cc index c95384a8..6f181555 100644 --- a/test/unit/t_amount.cc +++ b/test/unit/t_amount.cc @@ -42,11 +42,13 @@ void AmountTestCase::testParser() assertEqual(amount_t::precision_t(2), x12.commodity().precision()); +#ifndef NOT_FOR_PYTHON string buf("$100..."); std::istringstream input(buf); amount_t x13; x13.parse(input); assertEqual(x12, x13); +#endif // NOT_FOR_PYTHON amount_t x14; assertThrow(x14.parse("DM"), amount_error); @@ -210,6 +212,7 @@ void AmountTestCase::testCommodityConstructors() assertValid(x10); } +#ifndef NOT_FOR_PYTHON void AmountTestCase::testAssignment() { amount_t x0; @@ -315,6 +318,7 @@ void AmountTestCase::testCommodityAssignment() assertValid(x9); assertValid(x10); } +#endif // NOT_FOR_PYTHON void AmountTestCase::testEquality() { @@ -350,35 +354,24 @@ void AmountTestCase::testEquality() void AmountTestCase::testCommodityEquality() { amount_t x0; - amount_t x1; - amount_t x2; - amount_t x3; - amount_t x4; - amount_t x5; - amount_t x6; - amount_t x7; - amount_t x8; - amount_t x9; - amount_t x10; - - x1 = "$123.45"; - x2 = "-$123.45"; - x3 = "$-123.45"; - x4 = "DM 123.45"; - x5 = "-DM 123.45"; - x6 = "DM -123.45"; - x7 = "123.45 euro"; - x8 = "-123.45 euro"; - x9 = "123.45€"; - x10 = "-123.45€"; + amount_t x1("$123.45"); + amount_t x2("-$123.45"); + amount_t x3("$-123.45"); + amount_t x4("DM 123.45"); + amount_t x5("-DM 123.45"); + amount_t x6("DM -123.45"); + amount_t x7("123.45 euro"); + amount_t x8("-123.45 euro"); + amount_t x9("123.45€"); + amount_t x10("-123.45€"); assertTrue(x0.is_null()); assertThrow(x0.is_zero(), amount_error); assertThrow(x0.is_realzero(), amount_error); - assertThrow(assert(x0.sign() == 0), amount_error); - assertThrow(assert(x0.compare(x1) < 0), amount_error); - assertThrow(assert(x0.compare(x2) > 0), amount_error); - assertThrow(assert(x0.compare(x0) == 0), amount_error); + assertThrow(x0.sign() == 0, amount_error); + assertThrow(x0.compare(x1) < 0, amount_error); + assertThrow(x0.compare(x2) > 0, amount_error); + assertThrow(x0.compare(x0) == 0, amount_error); assertTrue(x1 != x2); assertTrue(x1 != x4); @@ -1335,7 +1328,7 @@ void AmountTestCase::testTruth() amount_t x1("1234"); amount_t x2("1234.56"); - assertThrow(assert(x0 ? 1 : 0), amount_error); + assertThrow(x0 ? 1 : 0, amount_error); assertTrue(x1); assertTrue(x2); @@ -1442,24 +1435,24 @@ void AmountTestCase::testCommodityConversion() assertValid(x1); } +#ifndef NOT_FOR_PYTHON void AmountTestCase::testPrinting() { amount_t x0; amount_t x1("982340823.380238098235098235098235098"); -#if 0 { - std::ostringstream bufstr; - assertThrow(bufstr << x0, amount_error); + std::ostringstream bufstr; + x0.print(bufstr); + assertEqual(std::string("<null>"), bufstr.str()); } -#endif { - std::ostringstream bufstr; - bufstr << x1; + std::ostringstream bufstr; + x1.print(bufstr); - assertEqual(std::string("982340823.380238098235098235098235098"), - bufstr.str()); + assertEqual(std::string("982340823.380238098235098235098235098"), + bufstr.str()); } assertValid(x0); @@ -1472,26 +1465,26 @@ void AmountTestCase::testCommodityPrinting() amount_t x2("$982340823.38"); { - std::ostringstream bufstr; - bufstr << x1; + std::ostringstream bufstr; + x1.print(bufstr); - assertEqual(std::string("$982340823.386238098235098235098235098"), - bufstr.str()); + assertEqual(std::string("$982340823.386238098235098235098235098"), + bufstr.str()); } { - std::ostringstream bufstr; - bufstr << (x1 * x2).to_string(); + std::ostringstream bufstr; + (x1 * x2).print(bufstr); - assertEqual(std::string("$964993493285024293.18099172508158508135413499124"), - bufstr.str()); + assertEqual(std::string("$964993493285024293.18099172508158508135413499124"), + bufstr.str()); } { - std::ostringstream bufstr; - bufstr << (x2 * x1).to_string(); + std::ostringstream bufstr; + (x2 * x1).print(bufstr); - assertEqual(std::string("$964993493285024293.18"), bufstr.str()); + assertEqual(std::string("$964993493285024293.18"), bufstr.str()); } assertValid(x1); @@ -1512,14 +1505,14 @@ void AmountTestCase::testSerialization() std::string buf; { - std::ostringstream storage; - assertThrow(x0.write(storage), amount_error); - x1.write(storage); - x2.write(storage); - x3.write(storage); - x4.write(storage); - x5.write(storage); - buf = storage.str(); + std::ostringstream storage; + assertThrow(x0.write(storage), amount_error); + x1.write(storage); + x2.write(storage); + x3.write(storage); + x4.write(storage); + x5.write(storage); + buf = storage.str(); } amount_t x1b; @@ -1528,12 +1521,12 @@ void AmountTestCase::testSerialization() amount_t x4b; amount_t x5b; { - std::istringstream storage(buf); - x1b.read(storage); - x2b.read(storage); - x3b.read(storage); - x4b.read(storage); - x5b.read(storage); + std::istringstream storage(buf); + x1b.read(storage); + x2b.read(storage); + x3b.read(storage); + x4b.read(storage); + x5b.read(storage); } assertEqual(x1, x1b); @@ -1541,6 +1534,7 @@ void AmountTestCase::testSerialization() assertEqual(x3, x3b); assertEqual(x4, x4b); +#ifndef NOT_FOR_PYTHON const char * ptr = buf.c_str(); amount_t x1c; @@ -1549,17 +1543,18 @@ void AmountTestCase::testSerialization() amount_t x4c; amount_t x5c; { - x1c.read(ptr); - x2c.read(ptr); - x3c.read(ptr); - x4c.read(ptr); - x5c.read(ptr); + x1c.read(ptr); + x2c.read(ptr); + x3c.read(ptr); + x4c.read(ptr); + x5c.read(ptr); } assertEqual(x1, x1b); assertEqual(x2, x2b); assertEqual(x3, x3b); assertEqual(x4, x4b); +#endif // NOT_FOR_PYTHON assertValid(x1); assertValid(x2); @@ -1574,3 +1569,14 @@ void AmountTestCase::testSerialization() assertValid(x3c); assertValid(x4c); } + +void AmountTestCase::testXmlSerialization() +{ + amount_t x1("$8,192.34"); + + std::ostringstream storage; + x1.write_xml(storage); + + assertEqual(std::string("<amount>\n <commodity flags=\"PT\">\n <symbol>$</symbol>\n </commodity>\n <quantity>8192.34</quantity>\n</amount>\n"), storage.str()); +} +#endif // NOT_FOR_PYTHON diff --git a/test/unit/t_amount.h b/test/unit/t_amount.h index a8310673..21224a09 100644 --- a/test/unit/t_amount.h +++ b/test/unit/t_amount.h @@ -48,6 +48,7 @@ class AmountTestCase : public CPPUNIT_NS::TestCase CPPUNIT_TEST(testPrinting); CPPUNIT_TEST(testCommodityPrinting); CPPUNIT_TEST(testSerialization); + CPPUNIT_TEST(testXmlSerialization); CPPUNIT_TEST_SUITE_END(); @@ -99,6 +100,7 @@ public: void testPrinting(); void testCommodityPrinting(); void testSerialization(); + void testXmlSerialization(); private: AmountTestCase(const AmountTestCase ©); diff --git a/test/unit/t_balance.cc b/test/unit/t_balance.cc index 93d97d6a..78a3a3fc 100644 --- a/test/unit/t_balance.cc +++ b/test/unit/t_balance.cc @@ -26,4 +26,5 @@ void BalanceTestCase::tearDown() void BalanceTestCase::testConstructors() { + int x = 1; } diff --git a/test/unit/t_commodity.cc b/test/unit/t_commodity.cc index 62ec557e..a75fb727 100644 --- a/test/unit/t_commodity.cc +++ b/test/unit/t_commodity.cc @@ -17,14 +17,25 @@ void CommodityTestCase::tearDown() { void CommodityTestCase::testPriceHistory() { - datetime_t jan17_05 = parse_datetime("2005/01/17 00:00:00"); - datetime_t jan17_06 = parse_datetime("2006/01/17 00:00:00"); - datetime_t jan17_07 = parse_datetime("2007/01/17 00:00:00"); - datetime_t feb27_07 = parse_datetime("2007/02/27 18:00:00"); - datetime_t feb28_07 = parse_datetime("2007/02/28 06:00:00"); - datetime_t feb28_07sbm = parse_datetime("2007/02/28 11:59:59"); - datetime_t mar01_07 = parse_datetime("2007/03/01 00:00:00"); - datetime_t apr15_07 = parse_datetime("2007/04/15 13:00:00"); +#ifndef NOT_FOR_PYTHON + datetime_t jan17_05; + datetime_t jan17_06; + datetime_t jan17_07; + datetime_t feb27_07; + datetime_t feb28_07; + datetime_t feb28_07sbm; + datetime_t mar01_07; + datetime_t apr15_07; +#endif // NOT_FOR_PYTHON + + jan17_05 = parse_datetime("2005/01/17 00:00:00"); + jan17_06 = parse_datetime("2006/01/17 00:00:00"); + jan17_07 = parse_datetime("2007/01/17 00:00:00"); + feb27_07 = parse_datetime("2007/02/27 18:00:00"); + feb28_07 = parse_datetime("2007/02/28 06:00:00"); + feb28_07sbm = parse_datetime("2007/02/28 11:59:59"); + mar01_07 = parse_datetime("2007/03/01 00:00:00"); + apr15_07 = parse_datetime("2007/04/15 13:00:00"); amount_t x0; amount_t x1("100.10 AAPL"); @@ -45,15 +56,18 @@ void CommodityTestCase::testPriceHistory() aapl.add_price(jan17_05, amount_t("EUR 23.00")); aapl.add_price(jan17_06, amount_t("CAD 25.00")); - commodity_t& euro(amount_t("EUR 1.00").commodity()); + amount_t one_euro("EUR 1.00"); + commodity_t& euro(one_euro.commodity()); euro.add_price(feb27_07, amount_t("CAD 1.40")); euro.add_price(jan17_05, amount_t("$0.78")); - commodity_t& cad(amount_t("CAD 1.00").commodity()); + amount_t one_cad("CAD 1.00"); + commodity_t& cad(one_cad.commodity()); cad.add_price(jan17_06, amount_t("$1.11")); +#ifndef NOT_FOR_PYTHON optional<amount_t> amt = x1.value(feb28_07sbm); assertTrue(amt); assertEqual(amount_t("$1831.83"), *amt); @@ -76,6 +90,7 @@ void CommodityTestCase::testPriceHistory() amt = x1.value(current_time, cad); assertTrue(amt); assertEqual(amount_t("CAD 3223.22"), *amt); +#endif // NOT_FOR_PYTHON assertValid(x1); } @@ -83,15 +98,18 @@ void CommodityTestCase::testPriceHistory() void CommodityTestCase::testLots() { // jww (2007-04-17): tbd + int x = 1; } void CommodityTestCase::testScalingBase() { // jww (2007-04-17): tbd + int x = 1; } void CommodityTestCase::testReduction() { // jww (2007-04-17): tbd + int x = 1; } diff --git a/test/unit/t_expr.cc b/test/unit/t_expr.cc index a6655f6a..209bb934 100644 --- a/test/unit/t_expr.cc +++ b/test/unit/t_expr.cc @@ -9,15 +9,20 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ValueExprTestCase, "expr"); void ValueExprTestCase::setUp() { amount_t::initialize(); +#ifndef NOT_FOR_PYTHON expr_t::initialize(); +#endif // NOT_FOR_PYTHON } void ValueExprTestCase::tearDown() { +#ifndef NOT_FOR_PYTHON expr_t::shutdown(); +#endif // NOT_FOR_PYTHON amount_t::shutdown(); } void ValueExprTestCase::testConstructors() { + int x = 1; } diff --git a/test/unit/t_times.cc b/test/unit/t_times.cc index 7f35bf50..a249ad3b 100644 --- a/test/unit/t_times.cc +++ b/test/unit/t_times.cc @@ -1,81 +1,109 @@ #include "t_times.h" +#include "utils.h" + +using namespace ledger; + CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(DateTimeTestCase, "util"); -void DateTimeTestCase::setUp() {} -void DateTimeTestCase::tearDown() {} +void DateTimeTestCase::setUp() { + int x = 1; +} +void DateTimeTestCase::tearDown() { + int x = 1; +} void DateTimeTestCase::testConstructors() { -#if 0 - std::time_t time_t_now = std::time(NULL); - struct tm * moment = std::localtime(&time_t_now); - +#ifndef NOT_FOR_PYTHON + std::time_t now = std::time(NULL); + struct tm * moment = std::localtime(&now); std::time_t localMoment = std::mktime(moment); +#endif // NOT_FOR_PYTHON - ptime d0; - ptime d1(parse_datetime("1990/01/01")); - ptime d3(boost::posix_time::from_time_t(localMoment)); - ptime d4(parse_datetime("2006/12/25")); - //ptime d5(parse_datetime("12/25")); - ptime d6(parse_datetime("2006.12.25")); - //ptime d7(parse_datetime("12.25")); - ptime d8(parse_datetime("2006-12-25")); - //ptime d9(parse_datetime("12-25")); -#if 0 - ptime d10(parse_datetime("tue")); - ptime d11(parse_datetime("tuesday")); - ptime d12(parse_datetime("feb")); - ptime d13(parse_datetime("february")); - ptime d14(parse_datetime("2006")); -#endif - ptime d15(d3); +#ifndef NOT_FOR_PYTHON + datetime_t d0; + datetime_t d1; + datetime_t d3; + datetime_t d4; + datetime_t d5; + datetime_t d6; + datetime_t d7; + datetime_t d8; + datetime_t d9; + + datetime_t d10; + datetime_t d11; + datetime_t d12; + datetime_t d13; + datetime_t d14; + datetime_t d15; +#endif // NOT_FOR_PYTHON + d1 = parse_datetime("1990/01/01"); +#ifndef NOT_FOR_PYTHON + d3 = boost::posix_time::from_time_t(localMoment); +#endif // NOT_FOR_PYTHON + d4 = parse_datetime("2006/12/25"); + d5 = parse_datetime("12/25"); + d6 = parse_datetime("2006.12.25"); + d7 = parse_datetime("12.25"); + d8 = parse_datetime("2006-12-25"); + d9 = parse_datetime("12-25"); + + d10 = parse_datetime("tue"); + d11 = parse_datetime("tuesday"); + d12 = parse_datetime("feb"); + d13 = parse_datetime("february"); + d14 = parse_datetime("2006"); +#ifndef NOT_FOR_PYTHON + d15 = d3; +#endif // NOT_FOR_PYTHON + +#ifndef NOT_FOR_PYTHON assertTrue(d0.is_not_a_date_time()); assertFalse(d1.is_not_a_date_time()); assertFalse(d4.is_not_a_date_time()); +#endif // NOT_FOR_PYTHON - assertTrue(now > d1); - //assertTrue(now <= d3); - assertTrue(now > d4); + assertTrue(current_time > d1); + assertTrue(current_time > d4); +#ifndef NOT_FOR_PYTHON assertEqual(d3, d15); +#endif // NOT_FOR_PYTHON assertEqual(d4, d6); assertEqual(d4, d8); - //assertEqual(d5, d7); - //assertEqual(d5, d9); -#if 0 + assertEqual(d5, d7); + assertEqual(d5, d9); assertEqual(d10, d11); assertEqual(d12, d13); -#endif #if 0 - assertThrow(parse_datetime("2007/02/29"), datetime_error *); - assertThrow(parse_datetime("2007/13/01"), datetime_error *); - assertThrow(parse_datetime("2007/00/01"), datetime_error *); - assertThrow(parse_datetime("2007/01/00"), datetime_error *); - assertThrow(parse_datetime("2007/00/00"), datetime_error *); - assertThrow(parse_datetime("2007/05/32"), datetime_error *); - - assertThrow(parse_datetime("2006x/12/25"), datetime_error *); - assertThrow(parse_datetime("2006/12x/25"), datetime_error *); - //assertThrow(parse_datetime("2006/12/25x"), datetime_error *); - - assertThrow(parse_datetime("feb/12/25"), datetime_error *); - assertThrow(parse_datetime("2006/mon/25"), datetime_error *); - assertThrow(parse_datetime("2006/12/web"), datetime_error *); - - assertThrow(parse_datetime("12*25"), datetime_error *); - - assertThrow(parse_datetime("tuf"), datetime_error *); - assertThrow(parse_datetime("tufsday"), datetime_error *); - assertThrow(parse_datetime("fec"), datetime_error *); - assertThrow(parse_datetime("fecruary"), datetime_error *); - assertThrow(parse_datetime("207x"), datetime_error *); - assertThrow(parse_datetime("hello"), datetime_error *); - - interval_t i1; - interval_t i2; -#endif +#ifndef NOT_FOR_PYTHON + assertThrow(parse_datetime("2007/02/29"), boost::gregorian::bad_day_of_month); + //assertThrow(parse_datetime("2007/13/01"), datetime_error); + //assertThrow(parse_datetime("2007/00/01"), datetime_error); + assertThrow(parse_datetime("2007/01/00"), boost::gregorian::bad_day_of_month); + //assertThrow(parse_datetime("2007/00/00"), boost::gregorian::bad_day_of_month); + //assertThrow(parse_datetime("2007/05/32"), boost::gregorian::bad_day_of_month); + + assertThrow(parse_datetime("2006x/12/25"), datetime_error); + assertThrow(parse_datetime("2006/12x/25"), datetime_error); + assertThrow(parse_datetime("2006/12/25x"), datetime_error); + + assertThrow(parse_datetime("feb/12/25"), datetime_error); + assertThrow(parse_datetime("2006/mon/25"), datetime_error); + assertThrow(parse_datetime("2006/12/web"), datetime_error); + + assertThrow(parse_datetime("12*25"), datetime_error); + + assertThrow(parse_datetime("tuf"), datetime_error); + assertThrow(parse_datetime("tufsday"), datetime_error); + assertThrow(parse_datetime("fec"), datetime_error); + assertThrow(parse_datetime("fecruary"), datetime_error); + assertThrow(parse_datetime("207x"), datetime_error); + assertThrow(parse_datetime("hello"), datetime_error); +#endif // NOT_FOR_PYTHON #endif } diff --git a/test/unit/t_utils.cc b/test/unit/t_utils.cc index 57c2a5d4..3c32cf78 100644 --- a/test/unit/t_utils.cc +++ b/test/unit/t_utils.cc @@ -2,9 +2,14 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(UtilitiesTestCase, "util"); -void UtilitiesTestCase::setUp() {} -void UtilitiesTestCase::tearDown() {} +void UtilitiesTestCase::setUp() { + int x = 1; +} +void UtilitiesTestCase::tearDown() { + int x = 1; +} void UtilitiesTestCase::testConstructors() { + int x = 1; } |