summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2007-04-16 04:27:26 +0000
committerJohn Wiegley <johnw@newartisans.com>2008-04-13 03:35:35 -0400
commite7f9486f6e524947a29b8e7d20bc834a240a3b23 (patch)
tree9720b3b7750bffc3cab66a707cac8719371ad857
parent8a63ad1c8d0f03b9be57331f911c0ccd998ece68 (diff)
downloadfork-ledger-e7f9486f6e524947a29b8e7d20bc834a240a3b23.tar.gz
fork-ledger-e7f9486f6e524947a29b8e7d20bc834a240a3b23.tar.bz2
fork-ledger-e7f9486f6e524947a29b8e7d20bc834a240a3b23.zip
Finished uncommoditized amount unit tests.
-rw-r--r--amount.cc34
-rw-r--r--amount.h28
-rw-r--r--py_amount.cc13
-rw-r--r--tests/corelib/numerics/BasicAmount.cc200
-rw-r--r--tests/corelib/numerics/BasicAmount.h18
-rw-r--r--tests/python/corelib/numerics/BasicAmount.py326
6 files changed, 488 insertions, 131 deletions
diff --git a/amount.cc b/amount.cc
index 714749bf..ee654328 100644
--- a/amount.cc
+++ b/amount.cc
@@ -681,23 +681,18 @@ bool amount_t::operator!=(const amount_t& amt) const
return compare(amt) != 0;
}
-amount_t::operator bool() const
+bool amount_t::zero() const
{
if (! quantity)
- return false;
+ return true;
- if (has_commodity() && quantity->prec <= commodity().precision()) {
- return mpz_sgn(MPZ(quantity)) != 0;
- } else {
- mpz_set(temp, MPZ(quantity));
- if (quantity->flags & BIGINT_KEEP_PREC || ! has_commodity())
- mpz_ui_pow_ui(divisor, 10, quantity->prec);
+ if (has_commodity()) {
+ if (quantity->prec <= commodity().precision())
+ return realzero();
else
- mpz_ui_pow_ui(divisor, 10, quantity->prec - commodity().precision());
- mpz_tdiv_q(temp, temp, divisor);
- bool zero = mpz_sgn(temp) == 0;
- return ! zero;
+ return round(commodity().precision()).sign() == 0;
}
+ return realzero();
}
amount_t::operator long() const
@@ -737,13 +732,6 @@ amount_t::operator double() const
return std::atof(num.str().c_str());
}
-bool amount_t::realzero() const
-{
- if (! quantity)
- return true;
- return mpz_sgn(MPZ(quantity)) == 0;
-}
-
amount_t amount_t::value(const datetime_t& moment) const
{
if (quantity) {
@@ -887,7 +875,7 @@ void amount_t::print_quantity(std::ostream& out) const
mpz_clear(remainder);
}
-void amount_t::print(std::ostream& _out) const
+void amount_t::print(std::ostream& _out, bool omit_commodity) const
{
if (! quantity) {
_out << "0";
@@ -967,7 +955,7 @@ void amount_t::print(std::ostream& _out) const
return;
}
- if (! (comm.flags() & COMMODITY_STYLE_SUFFIXED)) {
+ if (! omit_commodity && ! (comm.flags() & COMMODITY_STYLE_SUFFIXED)) {
comm.write(out);
if (comm.flags() & COMMODITY_STYLE_SEPARATED)
@@ -1048,7 +1036,7 @@ void amount_t::print(std::ostream& _out) const
}
}
- if (comm.flags() & COMMODITY_STYLE_SUFFIXED) {
+ if (! omit_commodity && comm.flags() & COMMODITY_STYLE_SUFFIXED) {
if (comm.flags() & COMMODITY_STYLE_SEPARATED)
out << " ";
@@ -1062,7 +1050,7 @@ void amount_t::print(std::ostream& _out) const
// If there are any annotations associated with this commodity,
// output them now.
- if (comm.annotated) {
+ if (! omit_commodity && comm.annotated) {
annotated_commodity_t& ann(static_cast<annotated_commodity_t&>(comm));
assert(&ann.price != this);
ann.write_annotations(out);
diff --git a/amount.h b/amount.h
index 213f6b5c..b47a67fd 100644
--- a/amount.h
+++ b/amount.h
@@ -128,9 +128,6 @@ class amount_t
return ! quantity && ! has_commodity();
}
- std::string to_string() const;
- std::string quantity_string() const {}
-
// assignment operator
amount_t& operator=(const amount_t& amt);
amount_t& operator=(const std::string& val);
@@ -226,12 +223,21 @@ class amount_t
return negated();
}
- // test for non-zero (use ! for zero)
- operator bool() const;
+ // test for zero and non-zero
+ int sign() const;
+ bool zero() const;
+ bool realzero() const {
+ return sign() == 0;
+ }
+ operator bool() const {
+ return ! zero();
+ }
+
operator long() const;
operator double() const;
- bool realzero() const;
+ std::string to_string() const;
+ std::string quantity_string() const;
// comparisons between amounts
int compare(const amount_t& amt) const;
@@ -259,8 +265,6 @@ class amount_t
parse(in);
}
- int sign() const;
-
// POD comparisons
#define AMOUNT_CMP_INT(OP) \
template <typename T> \
@@ -322,7 +326,7 @@ class amount_t
#define AMOUNT_PARSE_NO_MIGRATE 0x01
#define AMOUNT_PARSE_NO_REDUCE 0x02
- void print(std::ostream& out) const;
+ void print(std::ostream& out, bool omit_commodity = 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);
@@ -346,6 +350,12 @@ inline std::string amount_t::to_string() const {
return bufstream.str();
}
+inline std::string amount_t::quantity_string() const {
+ std::ostringstream bufstream;
+ print(bufstream, true);
+ return bufstream.str();
+}
+
inline amount_t abs(const amount_t& amt) {
return amt < 0 ? amt.negated() : amt;
}
diff --git a/py_amount.cc b/py_amount.cc
index d8f42762..ac36b134 100644
--- a/py_amount.cc
+++ b/py_amount.cc
@@ -21,6 +21,13 @@ void py_parse_2(amount_t& amount, const std::string& str) {
amount.parse(str);
}
+amount_t py_round_1(amount_t& amount, unsigned int prec) {
+ return amount.round(prec);
+}
+amount_t py_round_2(amount_t& amount) {
+ return amount.round();
+}
+
struct commodity_updater_wrap : public commodity_base_t::updater_t
{
PyObject * self;
@@ -148,6 +155,7 @@ void export_amount()
.def("strip_annotations", &amount_t::strip_annotations)
.def("clear_commodity", &amount_t::clear_commodity)
+ .def("to_string", &amount_t::to_string)
.def("quantity_string", &amount_t::quantity_string)
.def("abs", &amount_t::abs)
@@ -159,10 +167,15 @@ void export_amount()
.def("parse", py_parse_1)
.def("parse", py_parse_2)
.def("price", &amount_t::price)
+ .def("realzero", &amount_t::realzero)
.def("reduce", &amount_t::reduce)
.def("reduced", &amount_t::reduced)
+ .def("round", py_round_1)
+ .def("round", py_round_2)
.def("sign", &amount_t::sign)
+ .def("unround", &amount_t::unround)
.def("value", &amount_t::value)
+ .def("zero", &amount_t::zero)
.def("valid", &amount_t::valid)
;
diff --git a/tests/corelib/numerics/BasicAmount.cc b/tests/corelib/numerics/BasicAmount.cc
index a3860f4d..44246c40 100644
--- a/tests/corelib/numerics/BasicAmount.cc
+++ b/tests/corelib/numerics/BasicAmount.cc
@@ -32,6 +32,19 @@ void BasicAmountTestCase::testConstructors()
assertEqual(x10, x3);
assertEqual(amount_t(1L), x4);
assertEqual(x10, x9);
+
+ CPPUNIT_ASSERT(x0.valid());
+ CPPUNIT_ASSERT(x1.valid());
+ CPPUNIT_ASSERT(x2.valid());
+ CPPUNIT_ASSERT(x3.valid());
+ CPPUNIT_ASSERT(x4.valid());
+ CPPUNIT_ASSERT(x5.valid());
+ CPPUNIT_ASSERT(x6.valid());
+ CPPUNIT_ASSERT(x7.valid());
+ CPPUNIT_ASSERT(x8.valid());
+ CPPUNIT_ASSERT(x9.valid());
+ CPPUNIT_ASSERT(x10.valid());
+ CPPUNIT_ASSERT(x11.valid());
}
void BasicAmountTestCase::testNegation()
@@ -354,12 +367,181 @@ void BasicAmountTestCase::testFractionalDivision()
x4 / y4);
}
-// round
-// conversion
-// truth tests
-// test for real zero
-// comparison operators
-// sign check
-// abs
-// reduce
-// printing to a string buffer
+void BasicAmountTestCase::testIntegerConversion()
+{
+ amount_t x1(123456L);
+
+ assertEqual(true, bool(x1));
+ assertEqual(123456L, long(x1));
+ assertEqual(123456.0, double(x1));
+ assertEqual(std::string("123456"), x1.to_string());
+ assertEqual(std::string("123456"), x1.quantity_string());
+}
+
+void BasicAmountTestCase::testFractionalConversion()
+{
+ amount_t x1(1234.56);
+
+ assertEqual(true, bool(x1));
+ assertEqual(1234L, long(x1));
+ assertEqual(1234.56, double(x1));
+ assertEqual(std::string("1234.56"), x1.to_string());
+ assertEqual(std::string("1234.56"), x1.quantity_string());
+}
+
+void BasicAmountTestCase::testFractionalRound()
+{
+ amount_t x1("1234.567890");
+
+ assertEqual(amount_t("1234.56789"), x1.round(6));
+ assertEqual(amount_t("1234.56789"), x1.round(5));
+ assertEqual(amount_t("1234.5679"), x1.round(4));
+ assertEqual(amount_t("1234.568"), x1.round(3));
+ assertEqual(amount_t("1234.57"), x1.round(2));
+ assertEqual(amount_t("1234.6"), x1.round(1));
+ assertEqual(amount_t("1235"), x1.round(0));
+
+ amount_t x2("9876.543210");
+
+ assertEqual(amount_t("9876.543210"), x2.round(6));
+ assertEqual(amount_t("9876.54321"), x2.round(5));
+ assertEqual(amount_t("9876.5432"), x2.round(4));
+ assertEqual(amount_t("9876.543"), x2.round(3));
+ assertEqual(amount_t("9876.54"), x2.round(2));
+ assertEqual(amount_t("9876.5"), x2.round(1));
+ assertEqual(amount_t("9877"), x2.round(0));
+
+ amount_t x3("-1234.567890");
+
+ assertEqual(amount_t("-1234.56789"), x3.round(6));
+ assertEqual(amount_t("-1234.56789"), x3.round(5));
+ assertEqual(amount_t("-1234.5679"), x3.round(4));
+ assertEqual(amount_t("-1234.568"), x3.round(3));
+ assertEqual(amount_t("-1234.57"), x3.round(2));
+ assertEqual(amount_t("-1234.6"), x3.round(1));
+ assertEqual(amount_t("-1235"), x3.round(0));
+
+ amount_t x4("-9876.543210");
+
+ assertEqual(amount_t("-9876.543210"), x4.round(6));
+ assertEqual(amount_t("-9876.54321"), x4.round(5));
+ assertEqual(amount_t("-9876.5432"), x4.round(4));
+ assertEqual(amount_t("-9876.543"), x4.round(3));
+ assertEqual(amount_t("-9876.54"), x4.round(2));
+ assertEqual(amount_t("-9876.5"), x4.round(1));
+ assertEqual(amount_t("-9877"), x4.round(0));
+}
+
+void BasicAmountTestCase::testTruth()
+{
+ amount_t x0;
+ amount_t x1("1234");
+ amount_t x2("1234.56");
+
+ if (x0)
+ CPPUNIT_ASSERT(false);
+ else
+ CPPUNIT_ASSERT(true);
+
+ if (x1)
+ CPPUNIT_ASSERT(true);
+ else
+ CPPUNIT_ASSERT(false);
+
+ if (x2)
+ CPPUNIT_ASSERT(true);
+ else
+ CPPUNIT_ASSERT(false);
+}
+
+void BasicAmountTestCase::testForZero()
+{
+ amount_t x0;
+ amount_t x1("0.000000000000000000001");
+
+ CPPUNIT_ASSERT(! x0);
+ CPPUNIT_ASSERT(x1);
+ CPPUNIT_ASSERT(x0.zero());
+ CPPUNIT_ASSERT(x0.realzero());
+ CPPUNIT_ASSERT(! x1.zero());
+ CPPUNIT_ASSERT(! x1.realzero());
+}
+
+void BasicAmountTestCase::testComparisons()
+{
+ amount_t x0;
+ amount_t x1(-123L);
+ amount_t x2(123L);
+ amount_t x3(-123.45);
+ amount_t x4(123.45);
+ amount_t x5("-123.45");
+ amount_t x6("123.45");
+
+ CPPUNIT_ASSERT(x0 > x1);
+ CPPUNIT_ASSERT(x0 < x2);
+ CPPUNIT_ASSERT(x0 > x3);
+ CPPUNIT_ASSERT(x0 < x4);
+ CPPUNIT_ASSERT(x0 > x5);
+ CPPUNIT_ASSERT(x0 < x6);
+
+ CPPUNIT_ASSERT(x1 > x3);
+ CPPUNIT_ASSERT(x3 <= x5);
+ CPPUNIT_ASSERT(x3 >= x5);
+ CPPUNIT_ASSERT(x3 < x1);
+ CPPUNIT_ASSERT(x3 < x4);
+}
+
+void BasicAmountTestCase::testSign()
+{
+ amount_t x0;
+ amount_t x1("0.0000000000000000000000000000000000001");
+ amount_t x2("-0.0000000000000000000000000000000000001");
+ amount_t x3("1");
+ amount_t x4("-1");
+
+ CPPUNIT_ASSERT(! x0.sign());
+ CPPUNIT_ASSERT(x1.sign() > 0);
+ CPPUNIT_ASSERT(x2.sign() < 0);
+ CPPUNIT_ASSERT(x3.sign() > 0);
+ CPPUNIT_ASSERT(x4.sign() < 0);
+}
+
+void BasicAmountTestCase::testAbs()
+{
+ amount_t x0;
+ amount_t x1(-1234L);
+ amount_t x2(1234L);
+
+ assertEqual(amount_t(), abs(x0));
+ assertEqual(amount_t(1234L), abs(x1));
+ assertEqual(amount_t(1234L), abs(x2));
+
+ x0.abs();
+ x1.abs();
+ x2.abs();
+
+ assertEqual(amount_t(), x0);
+ assertEqual(amount_t(1234L), x1);
+ assertEqual(amount_t(1234L), x2);
+}
+
+void BasicAmountTestCase::testPrinting()
+{
+ amount_t x0;
+ amount_t x1("982340823.380238098235098235098235098");
+
+ {
+ std::ostringstream bufstr;
+ bufstr << x0;
+
+ assertEqual(std::string("0"), bufstr.str());
+ }
+
+ {
+ std::ostringstream bufstr;
+ bufstr << x1;
+
+ assertEqual(std::string("982340823.380238098235098235098235098"),
+ bufstr.str());
+ }
+}
diff --git a/tests/corelib/numerics/BasicAmount.h b/tests/corelib/numerics/BasicAmount.h
index 6e2f021e..cff3bed8 100644
--- a/tests/corelib/numerics/BasicAmount.h
+++ b/tests/corelib/numerics/BasicAmount.h
@@ -19,6 +19,15 @@ class BasicAmountTestCase : public CPPUNIT_NS::TestCase
CPPUNIT_TEST(testFractionalMultiplication);
CPPUNIT_TEST(testIntegerDivision);
CPPUNIT_TEST(testFractionalDivision);
+ CPPUNIT_TEST(testIntegerConversion);
+ CPPUNIT_TEST(testFractionalConversion);
+ CPPUNIT_TEST(testFractionalRound);
+ CPPUNIT_TEST(testTruth);
+ CPPUNIT_TEST(testForZero);
+ CPPUNIT_TEST(testComparisons);
+ CPPUNIT_TEST(testSign);
+ CPPUNIT_TEST(testAbs);
+ CPPUNIT_TEST(testPrinting);
CPPUNIT_TEST_SUITE_END();
@@ -41,6 +50,15 @@ public:
void testFractionalMultiplication();
void testIntegerDivision();
void testFractionalDivision();
+ void testIntegerConversion();
+ void testFractionalConversion();
+ void testFractionalRound();
+ void testTruth();
+ void testForZero();
+ void testComparisons();
+ void testSign();
+ void testAbs();
+ void testPrinting();
private:
BasicAmountTestCase(const BasicAmountTestCase &copy);
diff --git a/tests/python/corelib/numerics/BasicAmount.py b/tests/python/corelib/numerics/BasicAmount.py
index 454a9dcc..ce6479a2 100644
--- a/tests/python/corelib/numerics/BasicAmount.py
+++ b/tests/python/corelib/numerics/BasicAmount.py
@@ -3,11 +3,12 @@ import exceptions
from ledger import amount
+
class BasicAmountTestCase(unittest.TestCase):
def testConstructors(self):
x0 = amount()
- x1 = amount(123456L)
- x2 = amount(123456)
+ x1 = amount(123456)
+ x2 = amount(123456L)
x3 = amount(123.456)
x4 = amount(True)
x5 = amount("123456")
@@ -15,23 +16,33 @@ class BasicAmountTestCase(unittest.TestCase):
x9 = amount(x3)
x10 = amount(x6)
- self.assertEqual(amount(0L), x0)
+ self.assertEqual(amount(0), x0)
self.assertEqual(x2, x1)
self.assertEqual(x5, x1)
self.assertEqual(x6, x3)
self.assertEqual(x10, x3)
- self.assertEqual(amount(1L), x4)
+ 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 testNegation(self):
x0 = amount()
- x1 = amount(-123456L)
+ x1 = amount(-123456)
x3 = amount(-123.456)
x5 = amount("-123456")
x6 = amount("-123.456")
x9 = amount(- x3)
- self.assertEqual(amount(0L), x0)
+ self.assertEqual(amount(0), x0)
self.assertEqual(x5, x1)
self.assertEqual(x6, x3)
self.assertEqual(- x6, x9)
@@ -44,8 +55,8 @@ class BasicAmountTestCase(unittest.TestCase):
def testAssignment(self):
x0 = amount()
- x1 = amount(123456L)
- x2 = amount(123456)
+ x1 = amount(123456)
+ x2 = amount(123456L)
x3 = amount(123.456)
x4 = amount(True)
x5 = amount("123456")
@@ -53,17 +64,17 @@ class BasicAmountTestCase(unittest.TestCase):
x9 = x3
x10 = amount(x6)
- self.assertEqual(amount(0L), x0)
+ self.assertEqual(amount(0), x0)
self.assertEqual(x2, x1)
self.assertEqual(x5, x1)
self.assertEqual(x6, x3)
self.assertEqual(x10, x3)
- self.assertEqual(amount(1L), x4)
+ self.assertEqual(amount(1), x4)
self.assertEqual(x10, x9)
x0 = amount()
- x1 = amount(123456L)
- x2 = amount(123456)
+ x1 = amount(123456)
+ x2 = amount(123456L)
x3 = amount(123.456)
x4 = amount(True)
x5 = amount("123456")
@@ -71,45 +82,45 @@ class BasicAmountTestCase(unittest.TestCase):
x9 = x3
x10 = amount(x6)
- self.assertEqual(amount(0L), x0)
+ self.assertEqual(amount(0), x0)
self.assertEqual(x2, x1)
self.assertEqual(x5, x1)
self.assertEqual(x6, x3)
self.assertEqual(x10, x3)
- self.assertEqual(amount(1L), x4)
+ self.assertEqual(amount(1), x4)
self.assertEqual(x10, x9)
def testEquality(self):
- x1 = amount(123456L)
- x2 = amount(456789L)
- x3 = amount(333333L)
+ x1 = amount(123456)
+ x2 = amount(456789)
+ x3 = amount(333333)
x4 = amount(123456.0)
x5 = amount("123456.0")
- self.assertTrue(x1 == 123456L)
+ 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(123L)
- y1 = amount(456L)
+ x1 = amount(123)
+ y1 = amount(456)
- self.assertEqual(amount(579L), x1 + y1)
- self.assertEqual(amount(579L), x1 + 456L)
- self.assertEqual(amount(579L), 456L + x1)
+ self.assertEqual(amount(579), x1 + y1)
+ self.assertEqual(amount(579), x1 + 456)
+ self.assertEqual(amount(579), 456 + x1)
- x1 += amount(456L)
- self.assertEqual(amount(579L), x1)
- x1 += 456L
- self.assertEqual(amount(1035L), 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(2L), x3 + y3)
- self.assertEqual(amount(2L), x3 + True)
+ self.assertEqual(amount(2), x3 + y3)
+ self.assertEqual(amount(2), x3 + True)
x4 = amount("123456789123456789123456789")
@@ -127,7 +138,7 @@ class BasicAmountTestCase(unittest.TestCase):
self.assertEqual(amount(579.579), x1)
x1 += 456.456
self.assertEqual(amount(1036.035), x1)
- x1 += 456L
+ x1 += 456
self.assertEqual(amount(1492.035), x1)
x2 = amount("123456789123456789.123456789123456789")
@@ -135,18 +146,18 @@ class BasicAmountTestCase(unittest.TestCase):
self.assertEqual(amount("246913578246913578.246913578246913578"), x2 + x2)
def testIntegerSubtraction(self):
- x1 = amount(123L)
- y1 = amount(456L)
+ x1 = amount(123)
+ y1 = amount(456)
- self.assertEqual(amount(333L), y1 - x1)
- self.assertEqual(amount(-333L), x1 - y1)
- self.assertEqual(amount(23L), x1 - 100L)
- self.assertEqual(amount(-23L), 100L - x1)
+ 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(456L)
- self.assertEqual(amount(-333L), x1)
- x1 -= 456L
- self.assertEqual(amount(-789L), x1)
+ x1 -= amount(456)
+ self.assertEqual(amount(-333), x1)
+ x1 -= 456
+ self.assertEqual(amount(-789), x1)
x3 = amount(True)
y3 = amount(True)
@@ -170,7 +181,7 @@ class BasicAmountTestCase(unittest.TestCase):
self.assertEqual(amount(-333.333), x1)
x1 -= 456.456
self.assertEqual(amount(-789.789), x1)
- x1 -= 456L
+ x1 -= 456
self.assertEqual(amount(-1245.789), x1)
x2 = amount("123456789123456789.123456789123456789")
@@ -180,28 +191,28 @@ class BasicAmountTestCase(unittest.TestCase):
self.assertEqual(amount("-123446916777474329.874482549545456789"), y2 - x2)
def testIntegerMultiplication(self):
- x1 = amount(123L)
- y1 = amount(456L)
-
- self.assertEqual(amount(0L), x1 * 0L)
- self.assertEqual(amount(0L), amount(0L) * x1)
- self.assertEqual(amount(0L), 0L * x1)
- self.assertEqual(x1, x1 * 1L)
- self.assertEqual(x1, amount(1L) * x1)
- self.assertEqual(x1, 1L * x1)
- self.assertEqual(- x1, x1 * -1L)
- self.assertEqual(- x1, amount(-1L) * x1)
- self.assertEqual(- x1, -1L * x1)
- self.assertEqual(amount(56088L), x1 * y1)
- self.assertEqual(amount(56088L), y1 * x1)
- self.assertEqual(amount(56088L), x1 * 456L)
- self.assertEqual(amount(56088L), amount(456L) * x1)
- self.assertEqual(amount(56088L), 456L * x1)
-
- x1 *= amount(123L)
- self.assertEqual(amount(15129L), x1)
- x1 *= 123L
- self.assertEqual(amount(1860867L), x1)
+ 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)
x3 = amount(True)
y3 = amount(True)
@@ -217,15 +228,15 @@ class BasicAmountTestCase(unittest.TestCase):
x1 = amount(123.123)
y1 = amount(456.456)
- self.assertEqual(amount(0L), x1 * 0L)
- self.assertEqual(amount(0L), amount(0L) * x1)
- self.assertEqual(amount(0L), 0L * x1)
- self.assertEqual(x1, x1 * 1L)
- self.assertEqual(x1, amount(1L) * x1)
- self.assertEqual(x1, 1L * x1)
- self.assertEqual(- x1, x1 * -1L)
- self.assertEqual(- x1, amount(-1L) * x1)
- self.assertEqual(- x1, -1L * x1)
+ 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)
@@ -236,7 +247,7 @@ class BasicAmountTestCase(unittest.TestCase):
self.assertEqual(amount("15159.273129"), x1)
x1 *= 123.123
self.assertEqual(amount("1866455.185461867"), x1)
- x1 *= 123L
+ x1 *= 123
self.assertEqual(amount("229573987.811809641"), x1)
x2 = amount("123456789123456789.123456789123456789")
@@ -248,33 +259,33 @@ class BasicAmountTestCase(unittest.TestCase):
return amt / 0
def testIntegerDivision(self):
- x1 = amount(123L)
- y1 = amount(456L)
+ x1 = amount(123)
+ y1 = amount(456)
self.assertRaises(exceptions.ArithmeticError, self.divideByZero, x1)
- self.assertEqual(amount(0L), amount(0L) / x1)
- self.assertEqual(amount(0L), 0L / x1)
- self.assertEqual(x1, x1 / 1L)
- self.assertEqual(amount("0.008130"), amount(1L) / x1)
- self.assertEqual(amount("0.008130"), 1L / x1)
- self.assertEqual(- x1, x1 / -1L)
- self.assertEqual(- amount("0.008130"), amount(-1L) / x1)
- self.assertEqual(- amount("0.008130"), -1L / 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 / 456L)
- self.assertEqual(amount("3.707317"), amount(456L) / x1)
- self.assertEqual(amount("3.707317"), 456L / 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(456L)
+ x1 /= amount(456)
self.assertEqual(amount("0.269736"), x1)
- x1 /= 456L
+ x1 /= 456
self.assertEqual(amount("0.000591526315789473"), x1)
x4 = amount("123456789123456789123456789")
y4 = amount("56")
- self.assertEqual(amount(1L), x4 / x4)
+ self.assertEqual(amount(1), x4 / x4)
self.assertEqual(amount("2204585520061728377204585.517857"), x4 / y4)
def testFractionalDivision(self):
@@ -300,7 +311,7 @@ class BasicAmountTestCase(unittest.TestCase):
self.assertEqual(amount("0.269736842105"), x1)
x1 /= 456.456
self.assertEqual(amount("0.0005909372252856792330476541"), x1)
- x1 /= 456L
+ x1 /= 456
self.assertEqual(amount("0.00000129591496773175270405187302631578947368421052631578947368421"), x1)
x4 = amount("1234567891234567.89123456789")
@@ -310,6 +321,141 @@ class BasicAmountTestCase(unittest.TestCase):
self.assertEqual(amount("21739560323910.7554497273748437197344556164"),
x4 / y4)
+ def testIntegerConversion(self):
+ x1 = amount(123456)
+
+ 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 testFractionalConversion(self):
+ x1 = amount(1234.56)
+
+ 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())
+
+ 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))
+
+ def testTruth(self):
+ x0 = amount()
+ x1 = amount("1234")
+ x2 = amount("1234.56")
+
+ self.assertTrue(not x0)
+ self.assertTrue(x1 )
+ self.assertTrue(x2)
+
+ def testForZero(self):
+ x0 = amount()
+ x1 = amount("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())
+
+ 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.assertTrue(x0 > x1)
+ self.assertTrue(x0 < x2)
+ self.assertTrue(x0 > x3)
+ self.assertTrue(x0 < x4)
+ self.assertTrue(x0 > x5)
+ self.assertTrue(x0 < x6)
+
+ self.assertTrue(x1 > x3)
+ self.assertTrue(x3 <= x5)
+ self.assertTrue(x3 >= x5)
+ self.assertTrue(x3 < x1)
+ self.assertTrue(x3 < x4)
+
+ 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)
+ self.assertTrue(x3.sign() > 0)
+ self.assertTrue(x4.sign() < 0)
+
+ def testAbs(self):
+ x0 = amount()
+ x1 = amount(-1234)
+ x2 = amount(1234)
+
+ self.assertEqual(amount(), abs(x0))
+ self.assertEqual(amount(1234), abs(x1))
+ self.assertEqual(amount(1234), abs(x2))
+
+ x0.abs()
+ x1.abs()
+ x2.abs()
+
+ self.assertEqual(amount(), x0)
+ self.assertEqual(amount(1234), x1)
+ self.assertEqual(amount(1234), x2)
+
+ def testPrinting(self):
+ pass
+
+
def suite():
return unittest.TestLoader().loadTestsFromTestCase(BasicAmountTestCase)