summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--amount.cc75
-rw-r--r--amount.h24
-rw-r--r--py_amount.cc10
-rw-r--r--tests/corelib/numerics/BasicAmount.cc39
-rw-r--r--tests/corelib/numerics/CommodityAmount.cc162
-rw-r--r--tests/python/corelib/numerics/BasicAmount.py38
-rw-r--r--tests/python/corelib/numerics/CommodityAmount.py905
7 files changed, 747 insertions, 506 deletions
diff --git a/amount.cc b/amount.cc
index f4569571..f143a70c 100644
--- a/amount.cc
+++ b/amount.cc
@@ -50,10 +50,11 @@ namespace ledger {
bool do_cleanup = true;
-bool amount_t::keep_price = false;
-bool amount_t::keep_date = false;
-bool amount_t::keep_tag = false;
-bool amount_t::keep_base = false;
+bool amount_t::keep_price = false;
+bool amount_t::keep_date = false;
+bool amount_t::keep_tag = false;
+bool amount_t::keep_base = false;
+bool amount_t::full_strings = false;
#define BIGINT_BULK_ALLOC 0x0001
#define BIGINT_KEEP_PREC 0x0002
@@ -505,11 +506,12 @@ void amount_t::_clear()
amount_t& amount_t::operator+=(const amount_t& amt)
{
- if (commodity() != amt.commodity())
+ if (commodity() != amt.commodity()) {
throw new amount_error
(std::string("Adding amounts with different commodities: ") +
(has_commodity() ? commodity_->qualified_symbol : "NONE") + " != " +
(amt.has_commodity() ? amt.commodity_->qualified_symbol : "NONE"));
+ }
if (! amt.quantity)
return *this;
@@ -575,23 +577,33 @@ amount_t& amount_t::operator-=(const amount_t& amt)
amount_t& amount_t::operator*=(const amount_t& amt)
{
- if (! amt.quantity)
- return (*this = amt);
- else if (! quantity)
- return *this;
-
if (has_commodity() && amt.has_commodity() &&
- commodity() != amt.commodity())
+ commodity() != amt.commodity()) {
throw new amount_error
(std::string("Multiplying amounts with different commodities: ") +
(has_commodity() ? commodity_->qualified_symbol : "NONE") + " != " +
(amt.has_commodity() ? amt.commodity_->qualified_symbol : "NONE"));
+ }
+
+ if (! amt.quantity) {
+ *this = *this - *this; // preserve our commodity
+ goto finish;
+ }
+ else if (! quantity) {
+ *this = amt;
+ *this = *this - *this; // preserve the foreign commodity
+ goto finish;
+ }
_dup();
mpz_mul(MPZ(quantity), MPZ(quantity), MPZ(amt.quantity));
quantity->prec += amt.quantity->prec;
+ finish:
+ if (! has_commodity())
+ commodity_ = amt.commodity_;
+
if (has_commodity() && ! (quantity->flags & BIGINT_KEEP_PREC)) {
unsigned int comm_prec = commodity().precision();
if (quantity->prec > comm_prec + 6U) {
@@ -605,26 +617,44 @@ amount_t& amount_t::operator*=(const amount_t& amt)
amount_t& amount_t::operator/=(const amount_t& amt)
{
- if (! amt.quantity || ! amt)
- throw new amount_error("Divide by zero");
- else if (! quantity)
- return *this;
-
if (has_commodity() && amt.has_commodity() &&
- commodity() != amt.commodity())
+ commodity() != amt.commodity()) {
throw new amount_error
(std::string("Dividing amounts with different commodities: ") +
(has_commodity() ? commodity_->qualified_symbol : "NONE") + " != " +
(amt.has_commodity() ? amt.commodity_->qualified_symbol : "NONE"));
+ }
+
+ if (! amt.quantity || ! amt) {
+ throw new amount_error("Divide by zero");
+ }
+ else if (! quantity) {
+ *this = amt;
+ *this = *this - *this; // preserve the foreign commodity
+ goto finish;
+ }
_dup();
// Increase the value's precision, to capture fractional parts after
- // the divide.
- mpz_ui_pow_ui(divisor, 10, amt.quantity->prec + quantity->prec + 6U);
+ // the divide. Round up in the last position.
+
+ mpz_ui_pow_ui(divisor, 10, (2 * amt.quantity->prec) + quantity->prec + 7U);
mpz_mul(MPZ(quantity), MPZ(quantity), divisor);
mpz_tdiv_q(MPZ(quantity), MPZ(quantity), MPZ(amt.quantity));
- quantity->prec += quantity->prec + 6U;
+ quantity->prec += amt.quantity->prec + quantity->prec + 7U;
+
+ mpz_round(MPZ(quantity), MPZ(quantity), quantity->prec, quantity->prec - 1);
+ quantity->prec -= 1;
+
+ finish:
+ if (! has_commodity())
+ commodity_ = amt.commodity_;
+
+ // If this amount has a commodity, and we're not dealing with plain
+ // numbers, or internal numbers (which keep full precision at all
+ // times), then round the number to within the commodity's precision
+ // plus six places.
if (has_commodity() && ! (quantity->flags & BIGINT_KEEP_PREC)) {
unsigned int comm_prec = commodity().precision();
@@ -889,7 +919,8 @@ void amount_t::print_quantity(std::ostream& out) const
mpz_clear(remainder);
}
-void amount_t::print(std::ostream& _out, bool omit_commodity) const
+void amount_t::print(std::ostream& _out, bool omit_commodity,
+ bool full_precision) const
{
amount_t base(*this);
if (! amount_t::keep_base && commodity().larger()) {
@@ -922,7 +953,7 @@ void amount_t::print(std::ostream& _out, bool omit_commodity) const
unsigned char precision = 0;
if (quantity) {
- if (! comm || base.quantity->flags & BIGINT_KEEP_PREC) {
+ if (! comm || full_precision || base.quantity->flags & BIGINT_KEEP_PREC) {
mpz_ui_pow_ui(divisor, 10, base.quantity->prec);
mpz_tdiv_qr(quotient, remainder, MPZ(base.quantity), divisor);
precision = base.quantity->prec;
diff --git a/amount.h b/amount.h
index 9f16f305..ddd087f4 100644
--- a/amount.h
+++ b/amount.h
@@ -63,6 +63,7 @@ class amount_t
static bool keep_date;
static bool keep_tag;
static bool keep_base;
+ static bool full_strings;
protected:
void _init();
@@ -238,11 +239,15 @@ class amount_t
operator bool() const {
return ! zero();
}
+ operator std::string() const {
+ return to_string();
+ }
operator long() const;
operator double() const;
std::string to_string() const;
+ std::string to_fullstring() const;
std::string quantity_string() const;
// comparisons between amounts
@@ -311,6 +316,8 @@ class amount_t
bool valid() const;
+ static amount_t exact(const std::string& value);
+
// This function is special, and exists only to support a custom
// optimization in binary.cc (which offers a significant enough gain
// to be worth the trouble).
@@ -332,7 +339,8 @@ class amount_t
#define AMOUNT_PARSE_NO_MIGRATE 0x01
#define AMOUNT_PARSE_NO_REDUCE 0x02
- void print(std::ostream& out, bool omit_commodity = false) const;
+ void print(std::ostream& out, bool omit_commodity = false,
+ bool full_precision = false) const;
void parse(std::istream& in, unsigned char flags = 0);
void parse(const std::string& str, unsigned char flags = 0) {
std::istringstream stream(str);
@@ -350,12 +358,24 @@ class amount_t
void read_quantity(char *& data);
};
+inline amount_t amount_t::exact(const std::string& value) {
+ amount_t temp;
+ temp.parse(value, AMOUNT_PARSE_NO_MIGRATE);
+ return temp;
+}
+
inline std::string amount_t::to_string() const {
std::ostringstream bufstream;
print(bufstream);
return bufstream.str();
}
+inline std::string amount_t::to_fullstring() const {
+ std::ostringstream bufstream;
+ print(bufstream, false, true);
+ return bufstream.str();
+}
+
inline std::string amount_t::quantity_string() const {
std::ostringstream bufstream;
print(bufstream, true);
@@ -425,7 +445,7 @@ inline bool operator!=(const T val, const amount_t& amt) {
}
inline std::ostream& operator<<(std::ostream& out, const amount_t& amt) {
- amt.print(out);
+ amt.print(out, false, amount_t::full_strings);
return out;
}
inline std::istream& operator>>(std::istream& in, amount_t& amt) {
diff --git a/py_amount.cc b/py_amount.cc
index ac36b134..18fd3588 100644
--- a/py_amount.cc
+++ b/py_amount.cc
@@ -138,10 +138,10 @@ void export_amount()
.def(self_ns::int_(self))
.def(self_ns::float_(self))
- .def(self_ns::str(self))
.def(abs(self))
- .def("__repr__", &amount_t::to_string)
+ .def("__str__", &amount_t::to_string)
+ .def("__repr__", &amount_t::to_fullstring)
.def("has_commodity", &amount_t::has_commodity)
@@ -155,9 +155,15 @@ void export_amount()
.def("strip_annotations", &amount_t::strip_annotations)
.def("clear_commodity", &amount_t::clear_commodity)
+ //.add_static_property("full_strings", &amount_t::full_strings)
+
.def("to_string", &amount_t::to_string)
+ .def("to_fullstring", &amount_t::to_fullstring)
.def("quantity_string", &amount_t::quantity_string)
+ .def("exact", &amount_t::exact)
+ .staticmethod("exact")
+
.def("abs", &amount_t::abs)
.def("compare", &amount_t::compare)
.def("date", &amount_t::date)
diff --git a/tests/corelib/numerics/BasicAmount.cc b/tests/corelib/numerics/BasicAmount.cc
index 16d8b1ea..f4259843 100644
--- a/tests/corelib/numerics/BasicAmount.cc
+++ b/tests/corelib/numerics/BasicAmount.cc
@@ -378,16 +378,16 @@ void BasicAmountTestCase::testIntegerDivision()
assertEqual(- x1, x1 / -1L);
assertEqual(- amount_t("0.008130"), amount_t(-1L) / x1);
assertEqual(- amount_t("0.008130"), -1L / x1);
- assertEqual(amount_t("0.269736"), x1 / y1);
+ assertEqual(amount_t("0.269737"), x1 / y1);
assertEqual(amount_t("3.707317"), y1 / x1);
- assertEqual(amount_t("0.269736"), x1 / 456L);
+ assertEqual(amount_t("0.269737"), x1 / 456L);
assertEqual(amount_t("3.707317"), amount_t(456L) / x1);
assertEqual(amount_t("3.707317"), 456L / x1);
x1 /= amount_t(456L);
- assertEqual(amount_t("0.269736"), x1);
+ assertEqual(amount_t("0.269737"), x1);
x1 /= 456L;
- assertEqual(amount_t("0.000591526315789473"), x1);
+ assertEqual(amount_t("0.00059152850877193"), x1);
amount_t x4("123456789123456789123456789");
amount_t y4("56");
@@ -407,33 +407,32 @@ void BasicAmountTestCase::testFractionalDivision()
amount_t y1(456.456);
assertThrow(x1 / 0L, amount_error *);
- assertEqual(amount_t("0.008121"), amount_t(1.0) / x1);
- assertEqual(amount_t("0.008121"), 1.0 / x1);
+ assertEqual(amount_t("0.008121959"), amount_t(1.0) / x1);
+ assertEqual(amount_t("0.008121959"), 1.0 / x1);
assertEqual(x1, x1 / 1.0);
- assertEqual(amount_t("0.008121"), amount_t(1.0) / x1);
- assertEqual(amount_t("0.008121"), 1.0 / x1);
+ assertEqual(amount_t("0.008121959"), amount_t(1.0) / x1);
+ assertEqual(amount_t("0.008121959"), 1.0 / x1);
assertEqual(- x1, x1 / -1.0);
- assertEqual(- amount_t("0.008121"), amount_t(-1.0) / x1);
- assertEqual(- amount_t("0.008121"), -1.0 / x1);
- assertEqual(amount_t("0.269736842105"), x1 / y1);
- assertEqual(amount_t("3.707317073170"), y1 / x1);
- assertEqual(amount_t("0.269736842105"), x1 / 456.456);
- assertEqual(amount_t("3.707317073170"), amount_t(456.456) / x1);
- assertEqual(amount_t("3.707317073170"), 456.456 / x1);
+ assertEqual(- amount_t("0.008121959"), amount_t(-1.0) / x1);
+ assertEqual(- amount_t("0.008121959"), -1.0 / x1);
+ assertEqual(amount_t("0.269736842105263"), x1 / y1);
+ assertEqual(amount_t("3.707317073170732"), y1 / x1);
+ assertEqual(amount_t("0.269736842105263"), x1 / 456.456);
+ assertEqual(amount_t("3.707317073170732"), amount_t(456.456) / x1);
+ assertEqual(amount_t("3.707317073170732"), 456.456 / x1);
x1 /= amount_t(456.456);
- assertEqual(amount_t("0.269736842105"), x1);
+ assertEqual(amount_t("0.269736842105263"), x1);
x1 /= 456.456;
- assertEqual(amount_t("0.0005909372252856792330476541"), x1);
+ assertEqual(amount_t("0.000590937225286255411255411255411255411"), x1);
x1 /= 456L;
- assertEqual(amount_t("0.00000129591496773175270405187302631578947368421052631578947368421"), x1);
+ assertEqual(amount_t("0.000001295914967733016252753094858358016252192982456140350877192982456140350877192982"), x1);
amount_t x4("1234567891234567.89123456789");
amount_t y4("56.789");
assertEqual(amount_t(1.0), x4 / x4);
- assertEqual(amount_t("21739560323910.7554497273748437197344556164"),
- x4 / y4);
+ assertEqual(amount_t("21739560323910.7554497273748437197344556164046"), x4 / y4);
CPPUNIT_ASSERT(x1.valid());
CPPUNIT_ASSERT(y1.valid());
diff --git a/tests/corelib/numerics/CommodityAmount.cc b/tests/corelib/numerics/CommodityAmount.cc
index 111eb339..b2429fc3 100644
--- a/tests/corelib/numerics/CommodityAmount.cc
+++ b/tests/corelib/numerics/CommodityAmount.cc
@@ -3,22 +3,22 @@
using namespace ledger;
-CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CommodityAmountTestCase, "numerics");
+#define internalAmount(x) amount_t::exact(x)
-inline amount_t internalAmount(const std::string& value) {
- amount_t temp;
- temp.parse(value, AMOUNT_PARSE_NO_MIGRATE);
- return temp;
-}
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CommodityAmountTestCase, "numerics");
void CommodityAmountTestCase::setUp()
{
// Cause the display precision for dollars to be initialized to 2.
amount_t x1("$1.00");
assertTrue(x1);
+ amount_t::full_strings = true; // makes error reports from UnitTests accurate
}
-void CommodityAmountTestCase::tearDown() {}
+void CommodityAmountTestCase::tearDown()
+{
+ amount_t::full_strings = false;
+}
void CommodityAmountTestCase::testConstructors()
{
@@ -331,81 +331,115 @@ void CommodityAmountTestCase::testSubtraction()
void CommodityAmountTestCase::testMultiplication()
{
- // jww (2007-04-16): tbd
- amount_t x1(123.123);
- amount_t y1(456.456);
+ amount_t x1("$123.12");
+ amount_t y1("$456.45");
+ amount_t x2(internalAmount("$123.456789"));
+ amount_t x3("DM 123.45");
+ amount_t x4("123.45 euro");
+ amount_t x5("123.45€");
- assertEqual(amount_t(0L), x1 * 0L);
- assertEqual(amount_t(0L), amount_t(0L) * x1);
- assertEqual(amount_t(0L), 0L * x1);
+ assertEqual(amount_t("$0.00"), x1 * 0L);
+ assertEqual(amount_t("$0.00"), 0L * x1);
assertEqual(x1, x1 * 1L);
- assertEqual(x1, amount_t(1L) * x1);
assertEqual(x1, 1L * x1);
assertEqual(- x1, x1 * -1L);
- assertEqual(- x1, amount_t(-1L) * x1);
assertEqual(- x1, -1L * x1);
- assertEqual(amount_t("56200.232088"), x1 * y1);
- assertEqual(amount_t("56200.232088"), y1 * x1);
- assertEqual(amount_t("56200.232088"), x1 * 456.456);
- assertEqual(amount_t("56200.232088"), amount_t(456.456) * x1);
- assertEqual(amount_t("56200.232088"), 456.456 * x1);
-
- x1 *= amount_t(123.123);
- assertEqual(amount_t("15159.273129"), x1);
- x1 *= 123.123;
- assertEqual(amount_t("1866455.185461867"), x1);
+ assertEqual(internalAmount("$56198.124"), x1 * y1);
+ assertEqual(std::string("$56198.12"), (x1 * y1).to_string());
+ assertEqual(internalAmount("$56198.124"), y1 * x1);
+ assertEqual(std::string("$56198.12"), (y1 * x1).to_string());
+
+ // Internal amounts retain their precision, even when being
+ // converted to strings
+ assertEqual(internalAmount("$15199.99986168"), x1 * x2);
+ assertEqual(internalAmount("$15199.99986168"), x2 * x1);
+ assertEqual(std::string("$15200.00"), (x1 * x2).to_string());
+ assertEqual(std::string("$15199.99986168"), (x2 * x1).to_string());
+
+ assertThrow(x1 * x3, amount_error *);
+ assertThrow(x1 * x4, amount_error *);
+ assertThrow(x1 * x5, amount_error *);
+
+ x1 *= amount_t("123.12");
+ assertEqual(internalAmount("$15158.5344"), x1);
+ assertEqual(std::string("$15158.53"), x1.to_string());
+ x1 *= 123.12;
+ assertEqual(internalAmount("$1866318.755328"), x1);
+ assertEqual(std::string("$1866318.76"), x1.to_string());
x1 *= 123L;
- assertEqual(amount_t("229573987.811809641"), x1);
+ assertEqual(internalAmount("$229557206.905344"), x1);
+ assertEqual(std::string("$229557206.91"), x1.to_string());
- amount_t x2("123456789123456789.123456789123456789");
+ amount_t x7(internalAmount("$123456789123456789.123456789123456789"));
- assertEqual(amount_t("15241578780673678546105778311537878.046486820281054720515622620750190521"),
- x2 * x2);
+ assertEqual(internalAmount("$15241578780673678546105778311537878.046486820281054720515622620750190521"),
+ x7 * x7);
assertValid(x1);
- assertValid(y1);
assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+ assertValid(x5);
+ assertValid(x7);
}
void CommodityAmountTestCase::testDivision()
{
- // jww (2007-04-16): tbd
- amount_t x1(123.123);
- amount_t y1(456.456);
+ amount_t x1("$123.12");
+ amount_t y1("$456.45");
+ amount_t x2(internalAmount("$123.456789"));
+ amount_t x3("DM 123.45");
+ amount_t x4("123.45 euro");
+ amount_t x5("123.45€");
assertThrow(x1 / 0L, amount_error *);
- assertEqual(amount_t("0.008121"), amount_t(1.0) / x1);
- assertEqual(amount_t("0.008121"), 1.0 / x1);
- assertEqual(x1, x1 / 1.0);
- assertEqual(amount_t("0.008121"), amount_t(1.0) / x1);
- assertEqual(amount_t("0.008121"), 1.0 / x1);
- assertEqual(- x1, x1 / -1.0);
- assertEqual(- amount_t("0.008121"), amount_t(-1.0) / x1);
- assertEqual(- amount_t("0.008121"), -1.0 / x1);
- assertEqual(amount_t("0.269736842105"), x1 / y1);
- assertEqual(amount_t("3.707317073170"), y1 / x1);
- assertEqual(amount_t("0.269736842105"), x1 / 456.456);
- assertEqual(amount_t("3.707317073170"), amount_t(456.456) / x1);
- assertEqual(amount_t("3.707317073170"), 456.456 / x1);
-
- x1 /= amount_t(456.456);
- assertEqual(amount_t("0.269736842105"), x1);
- x1 /= 456.456;
- assertEqual(amount_t("0.0005909372252856792330476541"), x1);
- x1 /= 456L;
- assertEqual(amount_t("0.00000129591496773175270405187302631578947368421052631578947368421"), x1);
-
- amount_t x4("1234567891234567.89123456789");
- amount_t y4("56.789");
-
- assertEqual(amount_t(1.0), x4 / x4);
- assertEqual(amount_t("21739560323910.7554497273748437197344556164"),
- x4 / y4);
+ assertEqual(amount_t("$0.00"), 0L / x1);
+ assertEqual(x1, x1 / 1L);
+ assertEqual(internalAmount("$0.00812216"), 1L / x1);
+ assertEqual(- x1, x1 / -1L);
+ assertEqual(internalAmount("$-0.00812216"), -1L / x1);
+ assertEqual(internalAmount("$0.26973382"), x1 / y1);
+ assertEqual(std::string("$0.27"), (x1 / y1).to_string());
+ assertEqual(internalAmount("$3.70735867"), y1 / x1);
+ assertEqual(std::string("$3.71"), (y1 / x1).to_string());
+
+ // Internal amounts retain their precision, even when being
+ // converted to strings
+ assertEqual(internalAmount("$0.99727201"), x1 / x2);
+ assertEqual(internalAmount("$1.00273545321637426901"), x2 / x1);
+ assertEqual(std::string("$1.00"), (x1 / x2).to_string());
+ assertEqual(std::string("$1.00273545321637426901"), (x2 / x1).to_string());
+
+ assertThrow(x1 / x3, amount_error *);
+ assertThrow(x1 / x4, amount_error *);
+ assertThrow(x1 / x5, amount_error *);
+
+ x1 /= amount_t("123.12");
+ assertEqual(internalAmount("$1.00"), x1);
+ assertEqual(std::string("$1.00"), x1.to_string());
+ x1 /= 123.12;
+ assertEqual(internalAmount("$0.00812216"), x1);
+ assertEqual(std::string("$0.01"), x1.to_string());
+ x1 /= 123L;
+ assertEqual(internalAmount("$0.00006603"), x1);
+ assertEqual(std::string("$0.00"), x1.to_string());
+
+ amount_t x6(internalAmount("$237235987235987.98723987235978"));
+ amount_t x7(internalAmount("$123456789123456789.123456789123456789"));
+
+ assertEqual(amount_t("$1"), x7 / x7);
+ assertEqual(internalAmount("$0.0019216115121765559608381226612019501046413574469262"),
+ x6 / x7);
+ assertEqual(internalAmount("$520.39654928343335571379527154924040947271699678158689736256"),
+ x7 / x6);
assertValid(x1);
- assertValid(y1);
+ assertValid(x2);
+ assertValid(x3);
assertValid(x4);
- assertValid(y4);
+ assertValid(x5);
+ assertValid(x6);
+ assertValid(x7);
}
void CommodityAmountTestCase::testConversion()
@@ -622,7 +656,7 @@ void CommodityAmountTestCase::testPrinting()
{
std::ostringstream bufstr;
- bufstr << (x1 * x2);
+ bufstr << (x1 * x2).to_string();
assertEqual(std::string("$964993493285024293.18099172508158508135413499124"),
bufstr.str());
@@ -630,7 +664,7 @@ void CommodityAmountTestCase::testPrinting()
{
std::ostringstream bufstr;
- bufstr << (x2 * x1);
+ bufstr << (x2 * x1).to_string();
assertEqual(std::string("$964993493285024293.18"), bufstr.str());
}
diff --git a/tests/python/corelib/numerics/BasicAmount.py b/tests/python/corelib/numerics/BasicAmount.py
index 38cfacca..a485dca4 100644
--- a/tests/python/corelib/numerics/BasicAmount.py
+++ b/tests/python/corelib/numerics/BasicAmount.py
@@ -326,16 +326,16 @@ class BasicAmountTestCase(unittest.TestCase):
self.assertEqual(- x1, x1 / -1)
self.assertEqual(- amount("0.008130"), amount(-1) / x1)
self.assertEqual(- amount("0.008130"), -1 / x1)
- self.assertEqual(amount("0.269736"), x1 / y1)
+ self.assertEqual(amount("0.269737"), x1 / y1)
self.assertEqual(amount("3.707317"), y1 / x1)
- self.assertEqual(amount("0.269736"), x1 / 456)
+ 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.269736"), x1)
+ self.assertEqual(amount("0.269737"), x1)
x1 /= 456
- self.assertEqual(amount("0.000591526315789473"), x1)
+ self.assertEqual(amount("0.00059152850877193"), x1)
x4 = amount("123456789123456789123456789")
y4 = amount("56")
@@ -353,32 +353,32 @@ class BasicAmountTestCase(unittest.TestCase):
y1 = amount(456.456)
self.assertRaises(exceptions.ArithmeticError, self.divideByZero, x1)
- self.assertEqual(amount("0.008121"), amount(1.0) / x1)
- self.assertEqual(amount("0.008121"), 1.0 / x1)
+ self.assertEqual(amount("0.008121959"), amount(1.0) / x1)
+ self.assertEqual(amount("0.008121959"), 1.0 / x1)
self.assertEqual(x1, x1 / 1.0)
- self.assertEqual(amount("0.008121"), amount(1.0) / x1)
- self.assertEqual(amount("0.008121"), 1.0 / x1)
+ self.assertEqual(amount("0.008121959"), amount(1.0) / x1)
+ self.assertEqual(amount("0.008121959"), 1.0 / x1)
self.assertEqual(- x1, x1 / -1.0)
- self.assertEqual(- amount("0.008121"), amount(-1.0) / x1)
- self.assertEqual(- amount("0.008121"), -1.0 / x1)
- self.assertEqual(amount("0.269736842105"), x1 / y1)
- self.assertEqual(amount("3.707317073170"), y1 / x1)
- self.assertEqual(amount("0.269736842105"), x1 / 456.456)
- self.assertEqual(amount("3.707317073170"), amount(456.456) / x1)
- self.assertEqual(amount("3.707317073170"), 456.456 / x1)
+ self.assertEqual(- amount("0.008121959"), amount(-1.0) / x1)
+ self.assertEqual(- amount("0.008121959"), -1.0 / x1)
+ self.assertEqual(amount("0.269736842105263"), x1 / y1)
+ self.assertEqual(amount("3.707317073170732"), y1 / x1)
+ self.assertEqual(amount("0.269736842105263"), x1 / 456.456)
+ self.assertEqual(amount("3.707317073170732"), amount(456.456) / x1)
+ self.assertEqual(amount("3.707317073170732"), 456.456 / x1)
x1 /= amount(456.456)
- self.assertEqual(amount("0.269736842105"), x1)
+ self.assertEqual(amount("0.269736842105263"), x1)
x1 /= 456.456
- self.assertEqual(amount("0.0005909372252856792330476541"), x1)
+ self.assertEqual(amount("0.000590937225286255411255411255411255411"), x1)
x1 /= 456
- self.assertEqual(amount("0.00000129591496773175270405187302631578947368421052631578947368421"), x1)
+ 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.7554497273748437197344556164"),
+ self.assertEqual(amount("21739560323910.7554497273748437197344556164046"),
x4 / y4)
self.assertTrue(x1.valid())
diff --git a/tests/python/corelib/numerics/CommodityAmount.py b/tests/python/corelib/numerics/CommodityAmount.py
index 1a32243f..286418eb 100644
--- a/tests/python/corelib/numerics/CommodityAmount.py
+++ b/tests/python/corelib/numerics/CommodityAmount.py
@@ -1,413 +1,544 @@
+# -*- coding: utf-8 -*-
+
import unittest
import exceptions
+import operator
from ledger import amount
+internalAmount = amount.exact
+
class CommodityAmountTestCase(unittest.TestCase):
- def testConstructors(self):
- x0 = amount()
- x1 = amount(123456)
- x2 = amount(123456L)
- x3 = amount(123.456)
- x4 = amount(True)
- x5 = amount("123456")
- x6 = amount("123.456")
- x9 = amount(x3)
- x10 = amount(x6)
-
- self.assertEqual(amount(0), x0)
- self.assertEqual(x2, x1)
- self.assertEqual(x5, x1)
- self.assertEqual(x6, x3)
- self.assertEqual(x10, x3)
- self.assertEqual(amount(1), x4)
- self.assertEqual(x10, x9)
-
- self.assertTrue(x0.valid())
- self.assertTrue(x1.valid())
- self.assertTrue(x2.valid())
- self.assertTrue(x3.valid())
- self.assertTrue(x4.valid())
- self.assertTrue(x5.valid())
- self.assertTrue(x6.valid())
- self.assertTrue(x9.valid())
- self.assertTrue(x10.valid())
+ def setUp(self):
+ # Cause the display precision for dollars to be initialized to 2.
+ x1 = amount("$1.00")
+ self.assertTrue(x1)
+ amount.full_strings = True # makes error reports from UnitTests accurate
- def testNegation(self):
- x0 = amount()
- x1 = amount(-123456)
- x3 = amount(-123.456)
- x5 = amount("-123456")
- x6 = amount("-123.456")
- x9 = amount(- x3)
+ def tearDown(self):
+ amount.full_strings = False
- self.assertEqual(amount(0), x0)
- self.assertEqual(x5, x1)
- self.assertEqual(x6, x3)
- self.assertEqual(- x6, x9)
- self.assertEqual(x3.negated(), x9)
+ def assertValid(self, amt):
+ self.assertTrue(amt.valid())
- x10 = amount(x9)
- x10.negate()
+ def testConstructors(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)
- self.assertEqual(x3, x10)
+ def testNegation(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)
- x4 = amount(True)
- x5 = amount("123456")
- x6 = amount("123.456")
- x9 = x3
- x10 = amount(x6)
-
- self.assertEqual(amount(0), x0)
- self.assertEqual(x2, x1)
- self.assertEqual(x5, x1)
- self.assertEqual(x6, x3)
- self.assertEqual(x10, x3)
- self.assertEqual(amount(1), x4)
- self.assertEqual(x10, x9)
-
- x0 = amount()
- x1 = amount(123456)
- x2 = amount(123456L)
- x3 = amount(123.456)
- x4 = amount(True)
- x5 = amount("123456")
- x6 = amount("123.456")
- x9 = x3
- x10 = amount(x6)
-
- self.assertEqual(amount(0), x0)
- self.assertEqual(x2, x1)
- self.assertEqual(x5, x1)
- self.assertEqual(x6, x3)
- self.assertEqual(x10, x3)
- self.assertEqual(amount(1), x4)
- self.assertEqual(x10, x9)
+ 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")
+ 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(x1 == 123456)
self.assertTrue(x1 != x2)
- self.assertTrue(x1 == (x2 - x3))
- self.assertTrue(x1 == x4)
- self.assertTrue(x4 == x5)
-
- def testIntegerAddition(self):
- 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)
-
- x3 = amount(True)
- y3 = amount(True)
-
- self.assertEqual(amount(2), x3 + y3)
- self.assertEqual(amount(2), x3 + True)
-
- x4 = amount("123456789123456789123456789")
-
- self.assertEqual(amount("246913578246913578246913578"), x4 + 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 + 456.456)
- self.assertEqual(amount(579.579), 456.456 + x1)
-
- x1 += amount(456.456)
- self.assertEqual(amount(579.579), x1)
- x1 += 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)
-
- 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)
-
- x3 = amount(True)
- y3 = amount(True)
-
- self.assertEqual(amount(False), x3 - y3)
-
- x4 = amount("123456789123456789123456789")
- y4 = amount("8238725986235986")
-
- self.assertEqual(amount("123456789115218063137220803"), x4 - y4)
- self.assertEqual(amount("-123456789115218063137220803"), y4 - x4)
-
- 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 -= 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.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(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 testAddition(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")
- def testIntegerMultiplication(self):
- x1 = amount(123)
- y1 = amount(456)
+ 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, operator.add, x1, x0)
+ self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x3)
+ self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x4)
+ self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x5)
+ self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x6)
+ self.assertRaises(exceptions.ArithmeticError, operator.add, x1, 123.45)
+ self.assertRaises(exceptions.ArithmeticError, operator.add, 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 testSubtraction(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(0), x1 * 0)
- self.assertEqual(amount(0), amount(0) * x1)
- self.assertEqual(amount(0), 0 * x1)
+ 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, operator.sub, x1, x0)
+ self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x3)
+ self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x4)
+ self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x5)
+ self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x6)
+ self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, 123.45)
+ self.assertRaises(exceptions.ArithmeticError, operator.sub, 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 testMultiplication(self):
+ 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, 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)
+ 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, operator.mul, x1, x3)
+ self.assertRaises(exceptions.ArithmeticError, operator.mul, x1, x4)
+ self.assertRaises(exceptions.ArithmeticError, operator.mul, x1, x5)
+
+ x1 *= amount("123.12")
+ self.assertEqual(internalAmount("$15158.5344"), x1)
+ self.assertEqual("$15158.53", x1.to_string())
+ x1 *= 123.12
+ self.assertEqual(internalAmount("$1866318.755328"), x1)
+ self.assertEqual("$1866318.76", x1.to_string())
x1 *= 123
- self.assertEqual(amount(1860867), x1)
+ 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 testDivision(self):
+ 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, operator.div, 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, operator.div, x1, x3)
+ self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x4)
+ self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x5)
+
+ x1 /= amount("123.12")
+ self.assertEqual(internalAmount("$1.00"), x1)
+ self.assertEqual("$1.00", x1.to_string())
+ x1 /= 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 testConversion(self):
+ x1 = amount("$1234.56")
- x3 = amount(True)
- y3 = amount(True)
+ self.assertEqual(True, bool(x1))
+ self.assertEqual(1234, int(x1))
+ self.assertEqual(1234.56, float(x1))
+ self.assertEqual("$1234.56", x1.to_string())
+ self.assertEqual("1234.56", x1.quantity_string())
- self.assertEqual(amount(True), x3 * y3)
+ self.assertValid(x1)
- x4 = amount("123456789123456789123456789")
+ def testRound(self):
+ x1 = amount(internalAmount("$1234.567890"))
- self.assertEqual(amount("15241578780673678546105778281054720515622620750190521"),
- x4 * x4)
+ 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))
- def testFractionalMultiplication(self):
- x1 = amount(123.123)
- y1 = amount(456.456)
+ x2 = amount(internalAmount("$9876.543210"))
- 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 * 456.456)
- self.assertEqual(amount("56200.232088"), amount(456.456) * x1)
- self.assertEqual(amount("56200.232088"), 456.456 * x1)
-
- x1 *= amount(123.123)
- self.assertEqual(amount("15159.273129"), x1)
- x1 *= 123.123
- self.assertEqual(amount("1866455.185461867"), x1)
- x1 *= 123
- self.assertEqual(amount("229573987.811809641"), x1)
+ 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))
- x2 = amount("123456789123456789.123456789123456789")
+ x3 = amount(internalAmount("$-1234.567890"))
- self.assertEqual(amount("15241578780673678546105778311537878.046486820281054720515622620750190521"),
- x2 * x2)
+ 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))
- def divideByZero(self, amt):
- return amt / 0
+ x4 = amount(internalAmount("$-9876.543210"))
- def testIntegerDivision(self):
- x1 = amount(123)
- y1 = amount(456)
+ 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))
- self.assertRaises(exceptions.ArithmeticError, self.divideByZero, x1)
- 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.269736"), x1 / y1)
- self.assertEqual(amount("3.707317"), y1 / x1)
- self.assertEqual(amount("0.269736"), x1 / 456)
- self.assertEqual(amount("3.707317"), amount(456) / x1)
- self.assertEqual(amount("3.707317"), 456 / x1)
-
- x1 /= amount(456)
- self.assertEqual(amount("0.269736"), x1)
- x1 /= 456
- self.assertEqual(amount("0.000591526315789473"), x1)
-
- x4 = amount("123456789123456789123456789")
- y4 = amount("56")
-
- self.assertEqual(amount(1), x4 / x4)
- self.assertEqual(amount("2204585520061728377204585.517857"), x4 / y4)
-
- def testFractionalDivision(self):
- x1 = amount(123.123)
- y1 = amount(456.456)
-
- self.assertRaises(exceptions.ArithmeticError, self.divideByZero, x1)
- self.assertEqual(amount("0.008121"), amount(1.0) / x1)
- self.assertEqual(amount("0.008121"), 1.0 / x1)
- self.assertEqual(x1, x1 / 1.0)
- self.assertEqual(amount("0.008121"), amount(1.0) / x1)
- self.assertEqual(amount("0.008121"), 1.0 / x1)
- self.assertEqual(- x1, x1 / -1.0)
- self.assertEqual(- amount("0.008121"), amount(-1.0) / x1)
- self.assertEqual(- amount("0.008121"), -1.0 / x1)
- self.assertEqual(amount("0.269736842105"), x1 / y1)
- self.assertEqual(amount("3.707317073170"), y1 / x1)
- self.assertEqual(amount("0.269736842105"), x1 / 456.456)
- self.assertEqual(amount("3.707317073170"), amount(456.456) / x1)
- self.assertEqual(amount("3.707317073170"), 456.456 / x1)
-
- x1 /= amount(456.456)
- self.assertEqual(amount("0.269736842105"), x1)
- x1 /= 456.456
- self.assertEqual(amount("0.0005909372252856792330476541"), x1)
- x1 /= 456
- self.assertEqual(amount("0.00000129591496773175270405187302631578947368421052631578947368421"), x1)
-
- x4 = amount("1234567891234567.89123456789")
- y4 = amount("56.789")
-
- self.assertEqual(amount(1.0), x4 / x4)
- self.assertEqual(amount("21739560323910.7554497273748437197344556164"),
- x4 / y4)
-
- def testIntegerConversion(self):
- x1 = amount(123456)
+ self.assertValid(x1)
+ self.assertValid(x2)
+ self.assertValid(x3)
+ self.assertValid(x4)
- self.assertEqual(True, bool(x1))
- self.assertEqual(123456, int(x1))
- self.assertEqual(123456.0, float(x1))
- self.assertEqual("123456", x1.to_string())
- self.assertEqual("123456", x1.quantity_string())
+ def testDisplayRound(self):
+ x1 = amount("$0.85")
+ x2 = amount("$0.1")
- def testFractionalConversion(self):
- x1 = amount(1234.56)
+ x1 *= 0.19
- self.assertEqual(True, not (not x1))
- self.assertEqual(1234, int(x1))
- self.assertEqual(1234.56, float(x1))
- self.assertEqual("1234.56", x1.to_string())
- self.assertEqual("1234.56", x1.quantity_string())
+ 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())
- def testFractionalRound(self):
- x1 = amount("1234.567890")
-
- self.assertEqual(amount("1234.56789"), x1.round(6))
- self.assertEqual(amount("1234.56789"), x1.round(5))
- self.assertEqual(amount("1234.5679"), x1.round(4))
- self.assertEqual(amount("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("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))
+ x1 *= 7
+
+ self.assertNotEqual(amount("$1.13"), x1)
+ self.assertEqual(internalAmount("$1.1305"), x1)
+ self.assertEqual("$1.13", x1.to_string())
def testTruth(self):
- x0 = amount()
- x1 = amount("1234")
- x2 = amount("1234.56")
+ 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.assertTrue(not x0)
- self.assertTrue(x1 )
- self.assertTrue(x2)
+ self.assertValid(x1)
+ self.assertValid(x2)
def testForZero(self):
- x0 = amount()
- x1 = amount("0.000000000000000000001")
+ x1 = amount(internalAmount("$0.000000000000000000001"))
- self.assertTrue(not x0)
- self.assertTrue(x1)
- self.assertTrue(x0.zero())
- self.assertTrue(x0.realzero())
- self.assertTrue(not x1.zero())
- self.assertTrue(not x1.realzero())
+ self.assertFalse(x1)
+ self.assertTrue(x1.zero())
+ self.assertFalse(x1.realzero())
+
+ self.assertValid(x1)
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")
+ 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")
self.assertTrue(x0 > x1)
self.assertTrue(x0 < x2)
@@ -418,39 +549,59 @@ class CommodityAmountTestCase(unittest.TestCase):
self.assertTrue(x1 > x3)
self.assertTrue(x3 <= x5)
- 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.assertValid(x0)
+ self.assertValid(x1)
+ self.assertValid(x2)
+ self.assertValid(x3)
+ self.assertValid(x4)
+ self.assertValid(x5)
+ self.assertValid(x6)
+
def testSign(self):
x0 = amount()
- x1 = amount("0.0000000000000000000000000000000000001")
- x2 = amount("-0.0000000000000000000000000000000000001")
- x3 = amount("1")
- x4 = amount("-1")
-
- self.assertTrue(not x0.sign())
- self.assertTrue(x1.sign() > 0)
- self.assertTrue(x2.sign() < 0)
+ x1 = amount(internalAmount("$0.0000000000000000000000000000000000001"))
+ x2 = amount(internalAmount("$-0.0000000000000000000000000000000000001"))
+ x3 = amount("$1")
+ x4 = amount("$-1")
+
+ self.assertFalse(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 testAbs(self):
x0 = amount()
- x1 = amount(-1234)
- x2 = amount(1234)
+ x1 = amount("$-1234.56")
+ x2 = amount("$1234.56")
self.assertEqual(amount(), abs(x0))
- self.assertEqual(amount(1234), abs(x1))
- self.assertEqual(amount(1234), abs(x2))
+ self.assertEqual(amount("$1234.56"), abs(x1))
+ self.assertEqual(amount("$1234.56"), abs(x2))
x0.abs()
x1.abs()
x2.abs()
self.assertEqual(amount(), x0)
- self.assertEqual(amount(1234), x1)
- self.assertEqual(amount(1234), x2)
+ self.assertEqual(amount("$1234.56"), x1)
+ self.assertEqual(amount("$1234.56"), x2)
+
+ self.assertValid(x0)
+ self.assertValid(x1)
+ self.assertValid(x2)
def testPrinting(self):
pass