summaryrefslogtreecommitdiff
path: root/test/unit
diff options
context:
space:
mode:
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/t_amount.cc1329
-rw-r--r--test/unit/t_amount.h105
-rw-r--r--test/unit/t_balance.cc29
-rw-r--r--test/unit/t_balance.h28
-rw-r--r--test/unit/t_commodity.cc106
-rw-r--r--test/unit/t_commodity.h28
-rw-r--r--test/unit/t_expr.cc332
-rw-r--r--test/unit/t_expr.h60
-rw-r--r--test/unit/t_times.cc166
-rw-r--r--test/unit/t_times.h28
-rw-r--r--test/unit/t_utils.cc6
-rw-r--r--test/unit/t_utils.h28
12 files changed, 2245 insertions, 0 deletions
diff --git a/test/unit/t_amount.cc b/test/unit/t_amount.cc
new file mode 100644
index 00000000..2c91ee98
--- /dev/null
+++ b/test/unit/t_amount.cc
@@ -0,0 +1,1329 @@
+#include <system.hh>
+
+#include "t_amount.h"
+
+#include "amount.h"
+#include "commodity.h"
+
+using namespace ledger;
+
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(AmountTestCase, "math");
+
+void AmountTestCase::setUp()
+{
+ times_initialize();
+ amount_t::initialize();
+
+ // Cause the display precision for dollars to be initialized to 2.
+ amount_t x1("$1.00");
+ assertTrue(x1);
+
+ amount_t::stream_fullstrings = true; // make reports from UnitTests accurate
+}
+
+void AmountTestCase::tearDown()
+{
+ amount_t::stream_fullstrings = false;
+ amount_t::shutdown();
+ times_shutdown();
+}
+
+void AmountTestCase::testParser()
+{
+ amount_t x0;
+ amount_t x1;
+ amount_t x2;
+ amount_t x3;
+ amount_t x4("123.456");
+ amount_t x5(x4);
+ amount_t x6(x4);
+ amount_t x7(x4);
+ amount_t x8("$123.45");
+ amount_t x9(x8);
+ amount_t x10(x8);
+ amount_t x11(x8);
+ amount_t x12("$100");
+
+ 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);
+
+ amount_t x15("$1.000.000,00"); // parsing this switches us to European
+
+ amount_t x16("$2000");
+ assertEqual(string("$2.000,00"), x16.to_string());
+ x16.parse("$2000,00");
+ assertEqual(string("$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);
+
+ amount_t x17("$1,000,000.00"); // parsing this switches back to American
+
+ amount_t x18("$2000");
+ assertEqual(string("$2,000.00"), x18.to_string());
+ x18.parse("$2,000");
+ assertEqual(string("$2,000.00"), x18.to_string());
+
+ assertEqual(x15, x17);
+
+ amount_t x19("EUR 1000");
+ amount_t x20("EUR 1000");
+
+ assertEqual(string("EUR 1000"), x19.to_string());
+ assertEqual(string("EUR 1000"), x20.to_string());
+
+ x1.parse("$100.0000", PARSE_NO_MIGRATE);
+ assertEqual(amount_t::precision_t(2), x12.commodity().precision());
+ assertEqual(x1.commodity(), x12.commodity());
+ assertEqual(x1, x12);
+
+ x0.parse("$100.0000");
+ assertEqual(amount_t::precision_t(4), x12.commodity().precision());
+ assertEqual(x0.commodity(), x12.commodity());
+ assertEqual(x0, x12);
+
+ x2.parse("$100.00", PARSE_NO_REDUCE);
+ assertEqual(x2, x12);
+ x3.parse("$100.00", PARSE_NO_MIGRATE | PARSE_NO_REDUCE);
+ assertEqual(x3, x12);
+
+ x4.parse("$100.00");
+ assertEqual(x4, x12);
+ x5.parse("$100.00", PARSE_NO_MIGRATE);
+ assertEqual(x5, x12);
+ x6.parse("$100.00", PARSE_NO_REDUCE);
+ assertEqual(x6, x12);
+ x7.parse("$100.00", PARSE_NO_MIGRATE | PARSE_NO_REDUCE);
+ assertEqual(x7, x12);
+
+ x8.parse("$100.00");
+ assertEqual(x8, x12);
+ x9.parse("$100.00", PARSE_NO_MIGRATE);
+ assertEqual(x9, x12);
+ x10.parse("$100.00", PARSE_NO_REDUCE);
+ assertEqual(x10, x12);
+ x11.parse("$100.00", PARSE_NO_MIGRATE | PARSE_NO_REDUCE);
+ assertEqual(x11, x12);
+
+ assertValid(x0);
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x5);
+ assertValid(x6);
+ assertValid(x7);
+ assertValid(x8);
+ assertValid(x9);
+ assertValid(x10);
+ assertValid(x11);
+ assertValid(x12);
+}
+
+void AmountTestCase::testConstructors()
+{
+ amount_t x0;
+ amount_t x1(123456L);
+ amount_t x2(123456UL);
+ amount_t x3("123.456");
+ amount_t x5("123456");
+ amount_t x6("123.456");
+ amount_t x7(string("123456"));
+ amount_t x8(string("123.456"));
+ amount_t x9(x3);
+ amount_t x10(x6);
+ amount_t x11(x8);
+
+ assertEqual(amount_t(), x0);
+ assertNotEqual(amount_t("0"), x0);
+ assertNotEqual(amount_t("0.0"), x0);
+ assertEqual(x2, x1);
+ assertEqual(x5, x1);
+ assertEqual(x7, x1);
+ assertEqual(x6, x3);
+ assertEqual(x8, x3);
+ assertEqual(x10, x3);
+ assertEqual(x10, x9);
+
+ assertValid(x0);
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x5);
+ assertValid(x6);
+ assertValid(x7);
+ assertValid(x8);
+ assertValid(x9);
+ assertValid(x10);
+ assertValid(x11);
+}
+
+void AmountTestCase::testCommodityConstructors()
+{
+ 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€");
+
+ assertEqual(amount_t("$123.45"), x1);
+ assertEqual(amount_t("-$123.45"), x2);
+ assertEqual(amount_t("$-123.45"), x3);
+ assertEqual(amount_t("DM 123.45"), x4);
+ assertEqual(amount_t("-DM 123.45"), x5);
+ assertEqual(amount_t("DM -123.45"), x6);
+ assertEqual(amount_t("123.45 euro"), x7);
+ assertEqual(amount_t("-123.45 euro"), x8);
+ assertEqual(amount_t("123.45€"), x9);
+ assertEqual(amount_t("-123.45€"), x10);
+
+ assertEqual(string("$123.45"), x1.to_string());
+ assertEqual(string("$-123.45"), x2.to_string());
+ assertEqual(string("$-123.45"), x3.to_string());
+ assertEqual(string("DM 123.45"), x4.to_string());
+ assertEqual(string("DM -123.45"), x5.to_string());
+ assertEqual(string("DM -123.45"), x6.to_string());
+ assertEqual(string("123.45 euro"), x7.to_string());
+ assertEqual(string("-123.45 euro"), x8.to_string());
+ assertEqual(string("123.45€"), x9.to_string());
+ assertEqual(string("-123.45€"), x10.to_string());
+
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+ assertValid(x5);
+ assertValid(x6);
+ assertValid(x7);
+ assertValid(x8);
+ assertValid(x9);
+ assertValid(x10);
+}
+
+#ifndef NOT_FOR_PYTHON
+
+void AmountTestCase::testAssignment()
+{
+ amount_t x0;
+ amount_t x1;
+ amount_t x2;
+ amount_t x3;
+ amount_t x5;
+ amount_t x6;
+ amount_t x7;
+ amount_t x8;
+ amount_t x9;
+ amount_t x10;
+
+ x1 = 123456L;
+ x2 = 123456UL;
+ x3 = "123.456";
+ x5 = "123456";
+ x6 = "123.456";
+ x7 = string("123456");
+ x8 = string("123.456");
+ x9 = x3;
+ x10 = amount_t(x6);
+
+ assertEqual(x2, x1);
+ assertEqual(x5, x1);
+ assertEqual(x7, x1);
+ assertEqual(x6, x3);
+ assertEqual(x8, x3);
+ assertEqual(x10, x3);
+ assertEqual(x10, x9);
+
+ assertFalse(x1.is_null());
+ x1 = x0; // sets x1 back to uninitialized state
+ assertTrue(x0.is_null());
+ assertTrue(x1.is_null());
+
+ assertValid(x0);
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x5);
+ assertValid(x6);
+ assertValid(x7);
+ assertValid(x8);
+ assertValid(x9);
+ assertValid(x10);
+}
+
+void AmountTestCase::testCommodityAssignment()
+{
+ 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€";
+
+ assertEqual(amount_t("$123.45"), x1);
+ assertEqual(amount_t("-$123.45"), x2);
+ assertEqual(amount_t("$-123.45"), x3);
+ assertEqual(amount_t("DM 123.45"), x4);
+ assertEqual(amount_t("-DM 123.45"), x5);
+ assertEqual(amount_t("DM -123.45"), x6);
+ assertEqual(amount_t("123.45 euro"), x7);
+ assertEqual(amount_t("-123.45 euro"), x8);
+ assertEqual(amount_t("123.45€"), x9);
+ assertEqual(amount_t("-123.45€"), x10);
+
+ assertEqual(string("$123.45"), x1.to_string());
+ assertEqual(string("$-123.45"), x2.to_string());
+ assertEqual(string("$-123.45"), x3.to_string());
+ assertEqual(string("DM 123.45"), x4.to_string());
+ assertEqual(string("DM -123.45"), x5.to_string());
+ assertEqual(string("DM -123.45"), x6.to_string());
+ assertEqual(string("123.45 euro"), x7.to_string());
+ assertEqual(string("-123.45 euro"), x8.to_string());
+ assertEqual(string("123.45€"), x9.to_string());
+ assertEqual(string("-123.45€"), x10.to_string());
+
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+ assertValid(x5);
+ assertValid(x6);
+ assertValid(x7);
+ assertValid(x8);
+ assertValid(x9);
+ assertValid(x10);
+}
+
+#endif // NOT_FOR_PYTHON
+
+void AmountTestCase::testEquality()
+{
+ amount_t x1(123456L);
+ amount_t x2(456789L);
+ amount_t x3(333333L);
+ amount_t x4("123456.0");
+ amount_t x5("123456.0");
+ amount_t x6("123456.0");
+
+ assertTrue(x1 == 123456L);
+ assertTrue(x1 != x2);
+ assertTrue(x1 == (x2 - x3));
+ assertTrue(x1 == x4);
+ assertTrue(x4 == x5);
+ assertTrue(x4 == x6);
+
+ assertTrue(x1 == 123456L);
+ assertTrue(123456L == x1);
+ assertTrue(x1 == 123456UL);
+ assertTrue(123456UL == x1);
+ assertTrue(x1 == amount_t("123456.0"));
+ assertTrue(amount_t("123456.0") == x1);
+
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+ assertValid(x5);
+ assertValid(x6);
+}
+
+void AmountTestCase::testCommodityEquality()
+{
+ amount_t x0;
+ 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(x0.sign(), amount_error);
+ assertThrow(x0.compare(x1), amount_error);
+ assertThrow(x0.compare(x2), amount_error);
+ assertThrow(x0.compare(x0), amount_error);
+
+ assertTrue(x1 != x2);
+ assertTrue(x1 != x4);
+ assertTrue(x1 != x7);
+ assertTrue(x1 != x9);
+ assertTrue(x2 == x3);
+ assertTrue(x4 != x5);
+ assertTrue(x5 == x6);
+ assertTrue(x7 == - x8);
+ assertTrue(x9 == - x10);
+
+ assertValid(x0);
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+ assertValid(x5);
+ assertValid(x6);
+ assertValid(x7);
+ assertValid(x8);
+ assertValid(x9);
+ assertValid(x10);
+}
+
+void AmountTestCase::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");
+
+ assertThrow(x0 > x1, amount_error);
+ assertThrow(x0 < x2, amount_error);
+ assertThrow(x0 > x3, amount_error);
+ assertThrow(x0 < x4, amount_error);
+ assertThrow(x0 > x5, amount_error);
+ assertThrow(x0 < x6, amount_error);
+
+ assertTrue(x1 > x3);
+ assertTrue(x3 <= x5);
+ assertTrue(x3 >= x5);
+ assertTrue(x3 < x1);
+ assertTrue(x3 < x4);
+
+ assertTrue(x1 < 100L);
+ assertTrue(100L > x1);
+ assertTrue(x1 < 100UL);
+ assertTrue(100UL > x1);
+#ifndef NOT_FOR_PYTHON
+ assertTrue(x1 < 100.0);
+ assertTrue(100.0 > x1);
+#endif // NOT_FOR_PYTHON
+
+ assertValid(x0);
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+ assertValid(x5);
+ assertValid(x6);
+}
+
+void AmountTestCase::testCommodityComparisons()
+{
+ amount_t x1("$-123");
+ amount_t x2("$123.00");
+ amount_t x3(internalAmount("$-123.4544"));
+ amount_t x4(internalAmount("$123.4544"));
+ amount_t x5("$-123.45");
+ amount_t x6("$123.45");
+ amount_t x7("DM 123.45");
+
+ assertTrue(x1 > x3);
+ assertTrue(x3 <= x5);
+ assertTrue(x3 < x5);
+ assertTrue(x3 <= x5);
+ assertFalse(x3 == x5);
+ assertTrue(x3 < x1);
+ assertTrue(x3 < x4);
+ assertFalse(x6 == x7);
+ assertThrow(x6 < x7, amount_error);
+
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+ assertValid(x5);
+ assertValid(x6);
+}
+
+void AmountTestCase::testIntegerAddition()
+{
+ amount_t x0;
+ amount_t x1(123L);
+ amount_t y1(456L);
+
+ assertEqual(amount_t(579L), x1 + y1);
+ assertEqual(amount_t(579L), x1 + 456L);
+ assertEqual(amount_t(579L), 456L + x1);
+
+ x1 += amount_t(456L);
+ assertEqual(amount_t(579L), x1);
+ x1 += 456L;
+ assertEqual(amount_t(1035L), x1);
+
+ amount_t x4("123456789123456789123456789");
+
+ assertEqual(amount_t("246913578246913578246913578"), x4 + x4);
+
+ assertValid(x0);
+ assertValid(x1);
+ assertValid(y1);
+ assertValid(x4);
+}
+
+void AmountTestCase::testFractionalAddition()
+{
+ amount_t x1("123.123");
+ amount_t y1("456.456");
+
+ assertEqual(amount_t("579.579"), x1 + y1);
+ assertEqual(amount_t("579.579"), x1 + amount_t("456.456"));
+ assertEqual(amount_t("579.579"), amount_t("456.456") + x1);
+
+ x1 += amount_t("456.456");
+ assertEqual(amount_t("579.579"), x1);
+ x1 += amount_t("456.456");
+ assertEqual(amount_t("1036.035"), x1);
+ x1 += 456L;
+ assertEqual(amount_t("1492.035"), x1);
+
+ amount_t x2("123456789123456789.123456789123456789");
+
+ assertEqual(amount_t("246913578246913578.246913578246913578"), x2 + x2);
+
+ assertValid(x1);
+ assertValid(y1);
+ assertValid(x2);
+}
+
+void AmountTestCase::testCommodityAddition()
+{
+ amount_t x0;
+ amount_t x1("$123.45");
+ amount_t x2(internalAmount("$123.456789"));
+ amount_t x3("DM 123.45");
+ amount_t x4("123.45 euro");
+ amount_t x5("123.45€");
+ amount_t x6("123.45");
+
+ assertEqual(amount_t("$246.90"), x1 + x1);
+ assertNotEqual(amount_t("$246.91"), x1 + x2);
+ assertEqual(internalAmount("$246.906789"), x1 + x2);
+
+ // Converting to string drops internal precision
+ assertEqual(string("$246.90"), (x1 + x1).to_string());
+ assertEqual(string("$246.91"), (x1 + x2).to_string());
+
+ assertThrow(x1 + x0, amount_error);
+ assertThrow(x0 + x1, amount_error);
+ assertThrow(x0 + x0, amount_error);
+ assertThrow(x1 + x3, amount_error);
+ assertThrow(x1 + x4, amount_error);
+ assertThrow(x1 + x5, amount_error);
+ assertEqual(string("$246.90"), (x1 + x6).to_string());
+#ifndef NOT_FOR_PYTHON
+ assertEqual(string("$246.90"), (x1 + 123.45).to_string());
+#endif // NOT_FOR_PYTHON
+ assertEqual(string("$246.45"), (x1 + 123L).to_string());
+
+ assertEqual(amount_t("DM 246.90"), x3 + x3);
+ assertEqual(amount_t("246.90 euro"), x4 + x4);
+ assertEqual(amount_t("246.90€"), x5 + x5);
+
+ assertEqual(string("DM 246.90"), (x3 + x3).to_string());
+ assertEqual(string("246.90 euro"), (x4 + x4).to_string());
+ assertEqual(string("246.90€"), (x5 + x5).to_string());
+
+ x1 += amount_t("$456.45");
+ assertEqual(amount_t("$579.90"), x1);
+ x1 += amount_t("$456.45");
+ assertEqual(amount_t("$1036.35"), x1);
+ x1 += amount_t("$456");
+ assertEqual(amount_t("$1492.35"), x1);
+
+ amount_t x7(internalAmount("$123456789123456789.123456789123456789"));
+
+ assertEqual(internalAmount("$246913578246913578.246913578246913578"), x7 + x7);
+
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+ assertValid(x5);
+ assertValid(x6);
+ assertValid(x7);
+}
+
+void AmountTestCase::testIntegerSubtraction()
+{
+ amount_t x1(123L);
+ amount_t y1(456L);
+
+ assertEqual(amount_t(333L), y1 - x1);
+ assertEqual(amount_t(-333L), x1 - y1);
+ assertEqual(amount_t(23L), x1 - 100L);
+ assertEqual(amount_t(-23L), 100L - x1);
+
+ x1 -= amount_t(456L);
+ assertEqual(amount_t(-333L), x1);
+ x1 -= 456L;
+ assertEqual(amount_t(-789L), x1);
+
+ amount_t x4("123456789123456789123456789");
+ amount_t y4("8238725986235986");
+
+ assertEqual(amount_t("123456789115218063137220803"), x4 - y4);
+ assertEqual(amount_t("-123456789115218063137220803"), y4 - x4);
+
+ assertValid(x1);
+ assertValid(y1);
+ assertValid(x4);
+ assertValid(y4);
+}
+
+void AmountTestCase::testFractionalSubtraction()
+{
+ amount_t x1("123.123");
+ amount_t y1("456.456");
+
+ assertEqual(amount_t("-333.333"), x1 - y1);
+ assertEqual(amount_t("333.333"), y1 - x1);
+
+ x1 -= amount_t("456.456");
+ assertEqual(amount_t("-333.333"), x1);
+ x1 -= amount_t("456.456");
+ assertEqual(amount_t("-789.789"), x1);
+ x1 -= 456L;
+ assertEqual(amount_t("-1245.789"), x1);
+
+ amount_t x2("123456789123456789.123456789123456789");
+ amount_t y2("9872345982459.248974239578");
+
+ assertEqual(amount_t("123446916777474329.874482549545456789"), x2 - y2);
+ assertEqual(amount_t("-123446916777474329.874482549545456789"), y2 - x2);
+
+ assertValid(x1);
+ assertValid(y1);
+ assertValid(x2);
+ assertValid(y2);
+}
+
+void AmountTestCase::testCommoditySubtraction()
+{
+ amount_t x0;
+ amount_t x1("$123.45");
+ amount_t x2(internalAmount("$123.456789"));
+ amount_t x3("DM 123.45");
+ amount_t x4("123.45 euro");
+ amount_t x5("123.45€");
+ amount_t x6("123.45");
+
+ assertNotEqual(amount_t(), x1 - x1);
+ assertEqual(amount_t("$0"), x1 - x1);
+ assertEqual(amount_t("$23.45"), x1 - amount_t("$100.00"));
+ assertEqual(amount_t("$-23.45"), amount_t("$100.00") - x1);
+ assertNotEqual(amount_t("$-0.01"), x1 - x2);
+ assertEqual(internalAmount("$-0.006789"), x1 - x2);
+
+ // Converting to string drops internal precision. If an amount is
+ // zero, it drops the commodity as well.
+ assertEqual(string("$0.00"), (x1 - x1).to_string());
+ assertEqual(string("$-0.01"), (x1 - x2).to_string());
+
+ assertThrow(x1 - x0, amount_error);
+ assertThrow(x0 - x1, amount_error);
+ assertThrow(x0 - x0, amount_error);
+ assertThrow(x1 - x3, amount_error);
+ assertThrow(x1 - x4, amount_error);
+ assertThrow(x1 - x5, amount_error);
+ assertEqual(string("$0.00"), (x1 - x6).to_string());
+#ifndef NOT_FOR_PYTHON
+ assertEqual(string("$-0.00"), (x1 - 123.45).to_string());
+#endif // NOT_FOR_PYTHON
+ assertEqual(string("$0.45"), (x1 - 123L).to_string());
+
+ assertEqual(amount_t("DM 0.00"), x3 - x3);
+ assertEqual(amount_t("DM 23.45"), x3 - amount_t("DM 100.00"));
+ assertEqual(amount_t("DM -23.45"), amount_t("DM 100.00") - x3);
+ assertEqual(amount_t("0.00 euro"), x4 - x4);
+ assertEqual(amount_t("23.45 euro"), x4 - amount_t("100.00 euro"));
+ assertEqual(amount_t("-23.45 euro"), amount_t("100.00 euro") - x4);
+ assertEqual(amount_t("0.00€"), x5 - x5);
+ assertEqual(amount_t("23.45€"), x5 - amount_t("100.00€"));
+ assertEqual(amount_t("-23.45€"), amount_t("100.00€") - x5);
+
+ assertEqual(string("DM 0.00"), (x3 - x3).to_string());
+ assertEqual(string("DM 23.45"), (x3 - amount_t("DM 100.00")).to_string());
+ assertEqual(string("DM -23.45"), (amount_t("DM 100.00") - x3).to_string());
+ assertEqual(string("0.00 euro"), (x4 - x4).to_string());
+ assertEqual(string("23.45 euro"), (x4 - amount_t("100.00 euro")).to_string());
+ assertEqual(string("-23.45 euro"), (amount_t("100.00 euro") - x4).to_string());
+ assertEqual(string("0.00€"), (x5 - x5).to_string());
+ assertEqual(string("23.45€"), (x5 - amount_t("100.00€")).to_string());
+ assertEqual(string("-23.45€"), (amount_t("100.00€") - x5).to_string());
+
+ x1 -= amount_t("$456.45");
+ assertEqual(amount_t("$-333.00"), x1);
+ x1 -= amount_t("$456.45");
+ assertEqual(amount_t("$-789.45"), x1);
+ x1 -= amount_t("$456");
+ assertEqual(amount_t("$-1245.45"), x1);
+
+ amount_t x7(internalAmount("$123456789123456789.123456789123456789"));
+ amount_t x8(internalAmount("$2354974984698.98459845984598"));
+
+ assertEqual(internalAmount("$123454434148472090.138858329277476789"), x7 - x8);
+ assertEqual(string("$123454434148472090.138858329277476789"), (x7 - x8).to_string());
+ assertEqual(string("$123454434148472090.14"),
+ (amount_t("$1.00") * (x7 - x8)).to_string());
+ assertEqual(internalAmount("$-123454434148472090.138858329277476789"), x8 - x7);
+ assertEqual(string("$-123454434148472090.138858329277476789"), (x8 - x7).to_string());
+ assertEqual(string("$-123454434148472090.14"),
+ (amount_t("$1.00") * (x8 - x7)).to_string());
+
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+ assertValid(x5);
+ assertValid(x6);
+ assertValid(x7);
+ assertValid(x8);
+}
+
+void AmountTestCase::testIntegerMultiplication()
+{
+ amount_t x1(123L);
+ amount_t y1(456L);
+
+ assertEqual(amount_t(0L), x1 * 0L);
+ assertEqual(amount_t(0L), amount_t(0L) * x1);
+ assertEqual(amount_t(0L), 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(56088L), x1 * y1);
+ assertEqual(amount_t(56088L), y1 * x1);
+ assertEqual(amount_t(56088L), x1 * 456L);
+ assertEqual(amount_t(56088L), amount_t(456L) * x1);
+ assertEqual(amount_t(56088L), 456L * x1);
+
+ x1 *= amount_t(123L);
+ assertEqual(amount_t(15129L), x1);
+ x1 *= 123L;
+ assertEqual(amount_t(1860867L), x1);
+
+ amount_t x4("123456789123456789123456789");
+
+ assertEqual(amount_t("15241578780673678546105778281054720515622620750190521"),
+ x4 * x4);
+
+ assertValid(x1);
+ assertValid(y1);
+ assertValid(x4);
+}
+
+void AmountTestCase::testFractionalMultiplication()
+{
+ amount_t x1("123.123");
+ amount_t y1("456.456");
+
+ assertEqual(amount_t(0L), x1 * 0L);
+ assertEqual(amount_t(0L), amount_t(0L) * x1);
+ assertEqual(amount_t(0L), 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 * amount_t("456.456"));
+ assertEqual(amount_t("56200.232088"), amount_t("456.456") * x1);
+ assertEqual(amount_t("56200.232088"), amount_t("456.456") * x1);
+
+ x1 *= amount_t("123.123");
+ assertEqual(amount_t("15159.273129"), x1);
+ x1 *= amount_t("123.123");
+ assertEqual(amount_t("1866455.185461867"), x1);
+ x1 *= 123L;
+ assertEqual(amount_t("229573987.811809641"), x1);
+
+ amount_t x2("123456789123456789.123456789123456789");
+
+ assertEqual(amount_t("15241578780673678546105778311537878.046486820281054720515622620750190521"),
+ x2 * x2);
+
+ assertValid(x1);
+ assertValid(y1);
+ assertValid(x2);
+}
+
+void AmountTestCase::testCommodityMultiplication()
+{
+ amount_t x0;
+ 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("$0.00"), x1 * 0L);
+ assertEqual(amount_t("$0.00"), 0L * x1);
+ assertEqual(x1, x1 * 1L);
+ assertEqual(x1, 1L * x1);
+ assertEqual(- x1, x1 * -1L);
+ assertEqual(- x1, -1L * x1);
+ assertEqual(internalAmount("$56198.124"), x1 * y1);
+ assertEqual(string("$56198.12"), (x1 * y1).to_string());
+ assertEqual(internalAmount("$56198.124"), y1 * x1);
+ assertEqual(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(string("$15200.00"), (x1 * x2).to_string());
+ assertEqual(string("$15199.99986168"), (x2 * x1).to_string());
+
+ assertThrow(x1 * x0, amount_error);
+ assertThrow(x0 * x1, amount_error);
+ assertThrow(x0 * x0, amount_error);
+ //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(string("$15158.53"), x1.to_string());
+ x1 *= amount_t("123.12");
+ assertEqual(internalAmount("$1866318.755328"), x1);
+ assertEqual(string("$1866318.76"), x1.to_string());
+ x1 *= 123L;
+ assertEqual(internalAmount("$229557206.905344"), x1);
+ assertEqual(string("$229557206.91"), x1.to_string());
+
+ amount_t x7(internalAmount("$123456789123456789.123456789123456789"));
+
+ assertEqual(internalAmount("$15241578780673678546105778311537878.046486820281054720515622620750190521"),
+ x7 * x7);
+
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+ assertValid(x5);
+ assertValid(x7);
+}
+
+void AmountTestCase::testIntegerDivision()
+{
+ amount_t x1(123L);
+ amount_t y1(456L);
+
+ assertThrow(x1 / 0L, amount_error);
+ assertEqual(amount_t(0L), amount_t(0L) / x1);
+ assertEqual(amount_t(0L), 0L / x1);
+ assertEqual(x1, x1 / 1L);
+ assertEqual(string("0.00813"), (amount_t(1L) / x1).to_string());
+ assertEqual(string("0.00813"), (1L / x1).to_string());
+ assertEqual(- x1, x1 / -1L);
+ assertEqual(string("-0.00813"), (amount_t(-1L) / x1).to_string());
+ assertEqual(string("-0.00813"), (-1L / x1).to_string());
+ assertEqual(string("0.269737"), (x1 / y1).to_string());
+ assertEqual(string("3.707317"), (y1 / x1).to_string());
+ assertEqual(string("0.269737"), (x1 / 456L).to_string());
+ assertEqual(string("3.707317"), (amount_t(456L) / x1).to_string());
+ assertEqual(string("3.707317"), (456L / x1).to_string());
+
+ x1 /= amount_t(456L);
+ assertEqual(string("0.269737"), x1.to_string());
+ x1 /= 456L;
+ assertEqual(string("0.000591528163"), x1.to_string());
+
+ amount_t x4("123456789123456789123456789");
+ amount_t y4("56");
+
+ assertEqual(amount_t(1L), x4 / x4);
+ assertEqual(string("2204585520061728377204585.517857"), (x4 / y4).to_string());
+
+ assertEqual(amount_t("0.000000000000000000000000000001"),
+ amount_t("10") / amount_t("10000000000000000000000000000000"));
+
+ assertValid(x1);
+ assertValid(y1);
+ assertValid(x4);
+ assertValid(y4);
+}
+
+void AmountTestCase::testFractionalDivision()
+{
+ amount_t x1("123.123");
+ amount_t y1("456.456");
+
+ assertThrow(x1 / 0L, amount_error);
+ assertEqual(string("0.0081219593"), (amount_t("1.0") / x1).to_string());
+ assertEqual(string("0.0081219593"), (amount_t("1.0") / x1).to_string());
+ assertEqual(x1, x1 / amount_t("1.0"));
+ assertEqual(string("0.0081219593"), (amount_t("1.0") / x1).to_string());
+ assertEqual(string("0.0081219593"), (amount_t("1.0") / x1).to_string());
+ assertEqual(- x1, x1 / amount_t("-1.0"));
+ assertEqual(string("-0.0081219593"), (amount_t("-1.0") / x1).to_string());
+ assertEqual(string("-0.0081219593"), (amount_t("-1.0") / x1).to_string());
+ assertEqual(string("0.269736842105"), (x1 / y1).to_string());
+ assertEqual(string("3.707317073171"), (y1 / x1).to_string());
+ assertEqual(string("0.269736842105"), (x1 / amount_t("456.456")).to_string());
+ assertEqual(string("3.707317073171"), (amount_t("456.456") / x1).to_string());
+ assertEqual(string("3.707317073171"), (amount_t("456.456") / x1).to_string());
+
+ x1 /= amount_t("456.456");
+ assertEqual(string("0.269736842105"), x1.to_string());
+ x1 /= amount_t("456.456");
+ assertEqual(string("0.000590937225286255757"), x1.to_string());
+ x1 /= 456L;
+ assertEqual(string("0.000001295914967733017011337"), x1.to_string());
+
+ amount_t x4("1234567891234567.89123456789");
+ amount_t y4("56.789");
+
+ assertEqual(amount_t("1.0"), x4 / x4);
+ assertEqual(string("21739560323910.75544972737484371973"), (x4 / y4).to_string());
+
+ assertValid(x1);
+ assertValid(y1);
+ assertValid(x4);
+ assertValid(y4);
+}
+
+void AmountTestCase::testCommodityDivision()
+{
+ amount_t x0;
+ 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.00"), 0L / x1);
+ assertEqual(x1, x1 / 1L);
+ assertEqual(string("$0.00812216"), (1L / x1).to_fullstring());
+ assertEqual(- x1, x1 / -1L);
+ assertEqual(string("$-0.00812216"), (-1L / x1).to_fullstring());
+ assertEqual(string("$0.26973382"), (x1 / y1).to_fullstring());
+ assertEqual(string("$0.27"), (x1 / y1).to_string());
+ assertEqual(string("$3.70735867"), (y1 / x1).to_fullstring());
+ assertEqual(string("$3.71"), (y1 / x1).to_string());
+
+ // Internal amounts retain their precision, even when being
+ // converted to strings
+ assertEqual(string("$0.99727201"), (x1 / x2).to_fullstring());
+ assertEqual(string("$1.00273545321637"), (x2 / x1).to_fullstring());
+ assertEqual(string("$1.00"), (x1 / x2).to_string());
+ assertEqual(string("$1.00273545321637"), (x2 / x1).to_string());
+
+ assertThrow(x1 / x0, amount_error);
+ assertThrow(x0 / x1, amount_error);
+ assertThrow(x0 / x0, amount_error);
+ //assertThrow(x1 / x3, amount_error);
+ //assertThrow(x1 / x4, amount_error);
+ //assertThrow(x1 / x5, amount_error);
+
+ x1 /= amount_t("123.12");
+ assertEqual(string("$1.00"), x1.to_string());
+ x1 /= amount_t("123.12");
+ assertEqual(string("$0.00812216"), x1.to_fullstring());
+ assertEqual(string("$0.01"), x1.to_string());
+ x1 /= 123L;
+ assertEqual(string("$0.00006603"), x1.to_fullstring());
+ assertEqual(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(string("$0.0019216115121765559608381226612019501"),
+ (x6 / x7).to_fullstring());
+ assertEqual(string("$520.39654928343335571379527154924040947272"),
+ (x7 / x6).to_fullstring());
+
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+ assertValid(x5);
+ assertValid(x6);
+ assertValid(x7);
+}
+
+void AmountTestCase::testNegation()
+{
+ amount_t x0;
+ amount_t x1(-123456L);
+ amount_t x3("-123.456");
+ amount_t x5("-123456");
+ amount_t x6("-123.456");
+ amount_t x7(string("-123456"));
+ amount_t x8(string("-123.456"));
+ amount_t x9(- x3);
+
+ assertThrow(x0.negated(), amount_error);
+ assertEqual(x5, x1);
+ assertEqual(x7, x1);
+ assertEqual(x6, x3);
+ assertEqual(x8, x3);
+ assertEqual(- x6, x9);
+ assertEqual(x3.negated(), x9);
+
+ amount_t x10(x9.negated());
+
+ assertEqual(x3, x10);
+
+ assertValid(x1);
+ assertValid(x3);
+ assertValid(x5);
+ assertValid(x6);
+ assertValid(x7);
+ assertValid(x8);
+ assertValid(x9);
+ assertValid(x10);
+}
+
+void AmountTestCase::testCommodityNegation()
+{
+ 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€");
+
+ assertEqual(amount_t("$-123.45"), - x1);
+ assertEqual(amount_t("$123.45"), - x2);
+ assertEqual(amount_t("$123.45"), - x3);
+ assertEqual(amount_t("DM -123.45"), - x4);
+ assertEqual(amount_t("DM 123.45"), - x5);
+ assertEqual(amount_t("DM 123.45"), - x6);
+ assertEqual(amount_t("-123.45 euro"), - x7);
+ assertEqual(amount_t("123.45 euro"), - x8);
+ assertEqual(amount_t("-123.45€"), - x9);
+ assertEqual(amount_t("123.45€"), - x10);
+
+ assertEqual(amount_t("$-123.45"), x1.negated());
+ assertEqual(amount_t("$123.45"), x2.negated());
+ assertEqual(amount_t("$123.45"), x3.negated());
+
+ assertEqual(string("$-123.45"), (- x1).to_string());
+ assertEqual(string("$123.45"), (- x2).to_string());
+ assertEqual(string("$123.45"), (- x3).to_string());
+ assertEqual(string("DM -123.45"), (- x4).to_string());
+ assertEqual(string("DM 123.45"), (- x5).to_string());
+ assertEqual(string("DM 123.45"), (- x6).to_string());
+ assertEqual(string("-123.45 euro"), (- x7).to_string());
+ assertEqual(string("123.45 euro"), (- x8).to_string());
+ assertEqual(string("-123.45€"), (- x9).to_string());
+ assertEqual(string("123.45€"), (- x10).to_string());
+
+ assertEqual(amount_t("$-123.45"), x1.negated());
+ assertEqual(amount_t("$123.45"), x2.negated());
+ assertEqual(amount_t("$123.45"), x3.negated());
+
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+ assertValid(x5);
+ assertValid(x6);
+ assertValid(x7);
+ assertValid(x8);
+ assertValid(x9);
+ assertValid(x10);
+}
+
+void AmountTestCase::testAbs()
+{
+ amount_t x0;
+ amount_t x1(-1234L);
+ amount_t x2(1234L);
+
+ assertThrow(x0.abs(), amount_error);
+ assertEqual(amount_t(1234L), x1.abs());
+ assertEqual(amount_t(1234L), x2.abs());
+
+ assertValid(x0);
+ assertValid(x1);
+ assertValid(x2);
+}
+
+void AmountTestCase::testCommodityAbs()
+{
+ amount_t x1("$-1234.56");
+ amount_t x2("$1234.56");
+
+ assertEqual(amount_t("$1234.56"), x1.abs());
+ assertEqual(amount_t("$1234.56"), x2.abs());
+
+ assertValid(x1);
+ assertValid(x2);
+}
+
+#ifndef NOT_FOR_PYTHON
+#if 0
+void AmountTestCase::testReduction()
+{
+ amount_t x0;
+ amount_t x1("60s");
+ amount_t x2("600s");
+ amount_t x3("6000s");
+ amount_t x4("360000s");
+ amount_t x5("10m"); // 600s
+ amount_t x6("100m"); // 6000s
+ amount_t x7("1000m"); // 60000s
+ amount_t x8("10000m"); // 600000s
+ amount_t x9("10h"); // 36000s
+ amount_t x10("100h"); // 360000s
+ amount_t x11("1000h"); // 3600000s
+ amount_t x12("10000h"); // 36000000s
+
+ assertThrow(x0.reduce(), amount_error);
+ assertThrow(x0.unreduce(), amount_error);
+ assertEqual(x2, x5.reduce());
+ assertEqual(x3, x6.reduce());
+ assertEqual(x10, x4.reduce());
+ assertEqual(string("100.0h"), x4.unreduce().to_string());
+}
+#endif
+#endif // NOT_FOR_PYTHON
+
+void AmountTestCase::testSign()
+{
+ amount_t x0;
+ amount_t x1("0.0000000000000000000000000000000000001");
+ amount_t x2("-0.0000000000000000000000000000000000001");
+ amount_t x3("1");
+ amount_t x4("-1");
+
+ assertThrow(x0.sign(), amount_error);
+ assertTrue(x1.sign() > 0);
+ assertTrue(x2.sign() < 0);
+ assertTrue(x3.sign() > 0);
+ assertTrue(x4.sign() < 0);
+
+ assertValid(x0);
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+}
+
+void AmountTestCase::testCommoditySign()
+{
+ amount_t x1(internalAmount("$0.0000000000000000000000000000000000001"));
+ amount_t x2(internalAmount("$-0.0000000000000000000000000000000000001"));
+ amount_t x3("$1");
+ amount_t x4("$-1");
+
+ assertTrue(x1.sign() != 0);
+ assertTrue(x2.sign() != 0);
+ assertTrue(x3.sign() > 0);
+ assertTrue(x4.sign() < 0);
+
+ assertValid(x1);
+ assertValid(x2);
+ assertValid(x3);
+ assertValid(x4);
+}
+
+void AmountTestCase::testTruth()
+{
+ amount_t x0;
+ amount_t x1("1234");
+ amount_t x2("1234.56");
+
+#ifndef NOT_FOR_PYTHON
+ assertThrow(x0.operator bool(), amount_error);
+#endif // NOT_FOR_PYTHON
+
+ assertTrue(x1);
+ assertTrue(x2);
+
+ assertValid(x0);
+ assertValid(x1);
+ assertValid(x2);
+}
+
+void AmountTestCase::testCommodityTruth()
+{
+ amount_t x1("$1234");
+ amount_t x2("$1234.56");
+
+ if (x1)
+ assertTrue(true);
+
+ if (x2)
+ assertTrue(true);
+
+ assertValid(x1);
+ assertValid(x2);
+}
+
+void AmountTestCase::testForZero()
+{
+ amount_t x0;
+ amount_t x1("0.000000000000000000001");
+
+ assertTrue(x1);
+ assertThrow(x0.is_zero(), amount_error);
+ assertThrow(x0.is_realzero(), amount_error);
+ assertFalse(x1.is_zero());
+ assertFalse(x1.is_realzero());
+
+ assertValid(x0);
+ assertValid(x1);
+}
+
+void AmountTestCase::testCommodityForZero()
+{
+ amount_t x1(internalAmount("$0.000000000000000000001"));
+
+ assertTrue(x1); // an internal amount never betrays its precision
+ assertFalse(x1.is_zero());
+ assertFalse(x1.is_realzero());
+
+ assertValid(x1);
+}
+
+void AmountTestCase::testIntegerConversion()
+{
+ amount_t x0;
+ amount_t x1(123456L);
+ amount_t x2("12345682348723487324");
+
+ assertThrow(x0.to_long(), amount_error);
+ assertThrow(x0.to_double(), amount_error);
+ assertFalse(x2.fits_in_long());
+ assertEqual(123456L, x1.to_long());
+ assertEqual(123456.0, x1.to_double());
+ assertEqual(string("123456"), x1.to_string());
+ assertEqual(string("123456"), x1.quantity_string());
+
+ assertValid(x1);
+}
+
+void AmountTestCase::testFractionalConversion()
+{
+ amount_t x1("1234.56");
+ amount_t x2("1234.5683787634678348734");
+
+ assertEqual(1235L, x1.to_long());
+ assertEqual(1234.56, x1.to_double());
+ assertEqual(string("1234.56"), x1.to_string());
+ assertEqual(string("1234.56"), x1.quantity_string());
+
+ assertValid(x1);
+}
+
+void AmountTestCase::testCommodityConversion()
+{
+ amount_t x1("$1234.56");
+
+ assertEqual(1235L, x1.to_long());
+ assertEqual(1234.56, x1.to_double());
+ assertEqual(string("$1234.56"), x1.to_string());
+ assertEqual(string("1234.56"), x1.quantity_string());
+
+ assertValid(x1);
+}
+
+#ifndef NOT_FOR_PYTHON
+
+void AmountTestCase::testPrinting()
+{
+ amount_t x0;
+ amount_t x1("982340823.380238098235098235098235098");
+
+ {
+ std::ostringstream bufstr;
+ x0.print(bufstr);
+ assertEqual(std::string("<null>"), bufstr.str());
+ }
+
+ {
+ std::ostringstream bufstr;
+ x1.print(bufstr);
+
+ assertEqual(std::string("982340823.380238098235098235098235098"),
+ bufstr.str());
+ }
+
+ assertValid(x0);
+ assertValid(x1);
+}
+
+void AmountTestCase::testCommodityPrinting()
+{
+ amount_t x1(internalAmount("$982340823.386238098235098235098235098"));
+ amount_t x2("$982340823.38");
+
+ {
+ std::ostringstream bufstr;
+ x1.print(bufstr);
+
+ assertEqual(std::string("$982340823.386238098235098235098235098"),
+ bufstr.str());
+ }
+
+ {
+ std::ostringstream bufstr;
+ (x1 * x2).print(bufstr);
+
+ assertEqual(std::string("$964993493285024293.18099172508158508135413499124"),
+ bufstr.str());
+ }
+
+ {
+ std::ostringstream bufstr;
+ (x2 * x1).print(bufstr);
+
+ assertEqual(std::string("$964993493285024293.18"), bufstr.str());
+ }
+
+ assertValid(x1);
+ assertValid(x2);
+}
+
+#endif // NOT_FOR_PYTHON
diff --git a/test/unit/t_amount.h b/test/unit/t_amount.h
new file mode 100644
index 00000000..3d63b2d7
--- /dev/null
+++ b/test/unit/t_amount.h
@@ -0,0 +1,105 @@
+#ifndef _T_AMOUNT_H
+#define _T_AMOUNT_H
+
+#include "UnitTests.h"
+
+class AmountTestCase : public CPPUNIT_NS::TestCase
+{
+ CPPUNIT_TEST_SUITE(AmountTestCase);
+
+ CPPUNIT_TEST(testConstructors);
+ CPPUNIT_TEST(testCommodityConstructors);
+ CPPUNIT_TEST(testParser);
+ CPPUNIT_TEST(testAssignment);
+ CPPUNIT_TEST(testCommodityAssignment);
+ CPPUNIT_TEST(testEquality);
+ CPPUNIT_TEST(testCommodityEquality);
+ CPPUNIT_TEST(testComparisons);
+ CPPUNIT_TEST(testCommodityComparisons);
+ CPPUNIT_TEST(testIntegerAddition);
+ CPPUNIT_TEST(testFractionalAddition);
+ CPPUNIT_TEST(testCommodityAddition);
+ CPPUNIT_TEST(testIntegerSubtraction);
+ CPPUNIT_TEST(testFractionalSubtraction);
+ CPPUNIT_TEST(testCommoditySubtraction);
+ CPPUNIT_TEST(testIntegerMultiplication);
+ CPPUNIT_TEST(testFractionalMultiplication);
+ CPPUNIT_TEST(testCommodityMultiplication);
+ CPPUNIT_TEST(testIntegerDivision);
+ CPPUNIT_TEST(testFractionalDivision);
+ CPPUNIT_TEST(testCommodityDivision);
+ CPPUNIT_TEST(testNegation);
+ CPPUNIT_TEST(testCommodityNegation);
+ CPPUNIT_TEST(testAbs);
+ CPPUNIT_TEST(testCommodityAbs);
+#if 0
+ CPPUNIT_TEST(testReduction);
+#endif
+ CPPUNIT_TEST(testSign);
+ CPPUNIT_TEST(testCommoditySign);
+ CPPUNIT_TEST(testTruth);
+ CPPUNIT_TEST(testCommodityTruth);
+ CPPUNIT_TEST(testForZero);
+ CPPUNIT_TEST(testCommodityForZero);
+ CPPUNIT_TEST(testIntegerConversion);
+ CPPUNIT_TEST(testFractionalConversion);
+ CPPUNIT_TEST(testCommodityConversion);
+ CPPUNIT_TEST(testPrinting);
+ CPPUNIT_TEST(testCommodityPrinting);
+
+ CPPUNIT_TEST_SUITE_END();
+
+public:
+ AmountTestCase() {}
+ virtual ~AmountTestCase() {}
+
+ virtual void setUp();
+ virtual void tearDown();
+
+ void testConstructors();
+ void testCommodityConstructors();
+ void testParser();
+ void testAssignment();
+ void testCommodityAssignment();
+ void testEquality();
+ void testCommodityEquality();
+ void testComparisons();
+ void testCommodityComparisons();
+ void testIntegerAddition();
+ void testFractionalAddition();
+ void testCommodityAddition();
+ void testIntegerSubtraction();
+ void testFractionalSubtraction();
+ void testCommoditySubtraction();
+ void testIntegerMultiplication();
+ void testFractionalMultiplication();
+ void testCommodityMultiplication();
+ void testIntegerDivision();
+ void testFractionalDivision();
+ void testCommodityDivision();
+ void testNegation();
+ void testCommodityNegation();
+ void testAbs();
+ void testCommodityAbs();
+ void testFractionalRound();
+ void testCommodityRound();
+ void testCommodityDisplayRound();
+ void testReduction();
+ void testSign();
+ void testCommoditySign();
+ void testTruth();
+ void testCommodityTruth();
+ void testForZero();
+ void testCommodityForZero();
+ void testIntegerConversion();
+ void testFractionalConversion();
+ void testCommodityConversion();
+ void testPrinting();
+ void testCommodityPrinting();
+
+private:
+ AmountTestCase(const AmountTestCase &copy);
+ void operator=(const AmountTestCase &copy);
+};
+
+#endif // _T_AMOUNT_H
diff --git a/test/unit/t_balance.cc b/test/unit/t_balance.cc
new file mode 100644
index 00000000..c7d633b8
--- /dev/null
+++ b/test/unit/t_balance.cc
@@ -0,0 +1,29 @@
+#include <system.hh>
+
+#include "t_balance.h"
+
+#include "utils.h"
+#include "amount.h"
+
+using namespace ledger;
+
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(BalanceTestCase, "math");
+
+void BalanceTestCase::setUp()
+{
+ times_initialize();
+ amount_t::initialize();
+
+ // Cause the display precision for dollars to be initialized to 2.
+ amount_t x1("$1.00");
+ assertTrue(x1);
+
+ amount_t::stream_fullstrings = true; // make reports from UnitTests accurate
+}
+
+void BalanceTestCase::tearDown()
+{
+ amount_t::stream_fullstrings = false;
+ amount_t::shutdown();
+ times_shutdown();
+}
diff --git a/test/unit/t_balance.h b/test/unit/t_balance.h
new file mode 100644
index 00000000..8b8ce1b7
--- /dev/null
+++ b/test/unit/t_balance.h
@@ -0,0 +1,28 @@
+#ifndef _T_BALANCE_H
+#define _T_BALANCE_H
+
+#include "UnitTests.h"
+
+class BalanceTestCase : public CPPUNIT_NS::TestCase
+{
+ CPPUNIT_TEST_SUITE(BalanceTestCase);
+
+ //CPPUNIT_TEST(testConstructors);
+
+ CPPUNIT_TEST_SUITE_END();
+
+public:
+ BalanceTestCase() {}
+ virtual ~BalanceTestCase() {}
+
+ virtual void setUp();
+ virtual void tearDown();
+
+ //void testConstructors();
+
+private:
+ BalanceTestCase(const BalanceTestCase &copy);
+ void operator=(const BalanceTestCase &copy);
+};
+
+#endif // _T_BALANCE_H
diff --git a/test/unit/t_commodity.cc b/test/unit/t_commodity.cc
new file mode 100644
index 00000000..3d84ead6
--- /dev/null
+++ b/test/unit/t_commodity.cc
@@ -0,0 +1,106 @@
+#include <system.hh>
+
+#include "t_commodity.h"
+
+#include "amount.h"
+#include "commodity.h"
+
+using namespace ledger;
+
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CommodityTestCase, "math");
+
+void CommodityTestCase::setUp() {
+ times_initialize();
+ amount_t::initialize();
+ amount_t::stream_fullstrings = true;
+}
+
+void CommodityTestCase::tearDown() {
+ amount_t::shutdown();
+ times_shutdown();
+}
+
+void CommodityTestCase::testPriceHistory()
+{
+#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");
+
+ assertThrow(x0.value(), amount_error);
+ assertFalse(x1.value());
+
+ // Commodities cannot be constructed by themselves, since a great deal
+ // of their state depends on how they were seen to be used.
+ commodity_t& aapl(x1.commodity());
+
+ aapl.add_price(jan17_07, amount_t("$10.20"));
+ aapl.add_price(feb27_07, amount_t("$13.40"));
+ aapl.add_price(feb28_07, amount_t("$18.33"));
+ aapl.add_price(feb28_07sbm, amount_t("$18.30"));
+ aapl.add_price(mar01_07, amount_t("$19.50"));
+ aapl.add_price(apr15_07, amount_t("$21.22"));
+ aapl.add_price(jan17_05, amount_t("EUR 23.00"));
+ aapl.add_price(jan17_06, amount_t("CAD 25.00"));
+
+ 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"));
+
+ 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(false, feb28_07sbm);
+ assertTrue(amt);
+ assertEqual(amount_t("$1831.83"), *amt);
+
+ amt = x1.value(false, CURRENT_TIME());
+ assertTrue(amt);
+ assertEqual(string("$2124.12"), amt->to_string());
+#ifdef INTEGER_MATH
+ assertEqual(string("$2124.12"), amt->to_fullstring());
+#else
+ assertEqual(string("$2124.122"), amt->to_fullstring());
+#endif
+
+ amt = x1.value(false, CURRENT_TIME(), euro);
+ assertTrue(amt);
+ assertEqual(string("EUR 1366.87"), amt->rounded().to_string());
+
+ // Add a newer Euro pricing
+ aapl.add_price(jan17_07, amount_t("EUR 23.00"));
+
+ amt = x1.value(false, CURRENT_TIME(), euro);
+ assertTrue(amt);
+ assertEqual(string("EUR 2302.30"), amt->to_string());
+
+ amt = x1.value(false, CURRENT_TIME(), cad);
+ assertTrue(amt);
+ assertEqual(string("CAD 3223.22"), amt->to_string());
+#endif // NOT_FOR_PYTHON
+
+ assertValid(x1);
+}
diff --git a/test/unit/t_commodity.h b/test/unit/t_commodity.h
new file mode 100644
index 00000000..1dadb450
--- /dev/null
+++ b/test/unit/t_commodity.h
@@ -0,0 +1,28 @@
+#ifndef _T_COMMMODITY_H
+#define _T_COMMMODITY_H
+
+#include "UnitTests.h"
+
+class CommodityTestCase : public CPPUNIT_NS::TestCase
+{
+ CPPUNIT_TEST_SUITE(CommodityTestCase);
+
+ CPPUNIT_TEST(testPriceHistory);
+
+ CPPUNIT_TEST_SUITE_END();
+
+public:
+ CommodityTestCase() {}
+ virtual ~CommodityTestCase() {}
+
+ virtual void setUp();
+ virtual void tearDown();
+
+ void testPriceHistory();
+
+private:
+ CommodityTestCase(const CommodityTestCase &copy);
+ void operator=(const CommodityTestCase &copy);
+};
+
+#endif // _T_COMMMODITY_H
diff --git a/test/unit/t_expr.cc b/test/unit/t_expr.cc
new file mode 100644
index 00000000..b5865948
--- /dev/null
+++ b/test/unit/t_expr.cc
@@ -0,0 +1,332 @@
+#include <system.hh>
+
+#include "t_expr.h"
+
+#include "expr.h"
+#include "predicate.h"
+#include "query.h"
+#include "op.h"
+
+using namespace ledger;
+
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ValueExprTestCase, "expr");
+
+void ValueExprTestCase::setUp()
+{
+ times_initialize();
+ amount_t::initialize();
+}
+
+void ValueExprTestCase::tearDown()
+{
+ amount_t::shutdown();
+ times_shutdown();
+}
+
+// 1. foo and bar
+// 2. 'foo and bar'
+// 3. (foo and bar)
+// 4. ( foo and bar )
+// 5. '( foo and' bar)
+// 6. =foo and bar
+// 7. ='foo and bar'
+// 8. 'expr foo and bar'
+// 9. expr 'foo and bar'
+// 10. expr foo and bar
+// 11. foo and bar or baz
+// 12. foo and bar | baz
+// 13. foo and bar |baz
+// 14. foo and bar| baz
+// 15. foo and bar|baz
+// 16. foo 'and bar|baz'
+
+void ValueExprTestCase::testPredicateTokenizer1()
+{
+ value_t args;
+ args.push_back(string_value("foo"));
+ args.push_back(string_value("and"));
+ args.push_back(string_value("bar"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer2()
+{
+ value_t args;
+ args.push_back(string_value("foo and bar"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer3()
+{
+ value_t args;
+ args.push_back(string_value("(foo"));
+ args.push_back(string_value("and"));
+ args.push_back(string_value("bar)"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::LPAREN, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::RPAREN, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer4()
+{
+ value_t args;
+ args.push_back(string_value("("));
+ args.push_back(string_value("foo"));
+ args.push_back(string_value("and"));
+ args.push_back(string_value("bar"));
+ args.push_back(string_value(")"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::LPAREN, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::RPAREN, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer5()
+{
+ value_t args;
+ args.push_back(string_value("( foo and"));
+ args.push_back(string_value("bar)"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::LPAREN, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::RPAREN, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer6()
+{
+ value_t args;
+ args.push_back(string_value("=foo"));
+ args.push_back(string_value("and"));
+ args.push_back(string_value("bar"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TOK_EQ, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer7()
+{
+ value_t args;
+ args.push_back(string_value("=foo and bar"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TOK_EQ, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer8()
+{
+ value_t args;
+ args.push_back(string_value("expr foo and bar"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TOK_EXPR, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer9()
+{
+ value_t args;
+ args.push_back(string_value("expr"));
+ args.push_back(string_value("foo and bar"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TOK_EXPR, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer10()
+{
+ value_t args;
+ args.push_back(string_value("expr"));
+ args.push_back(string_value("foo"));
+ args.push_back(string_value("and"));
+ args.push_back(string_value("bar"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TOK_EXPR, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer11()
+{
+ value_t args;
+ args.push_back(string_value("foo"));
+ args.push_back(string_value("and"));
+ args.push_back(string_value("bar"));
+ args.push_back(string_value("or"));
+ args.push_back(string_value("baz"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_OR, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer12()
+{
+ value_t args;
+ args.push_back(string_value("foo"));
+ args.push_back(string_value("and"));
+ args.push_back(string_value("bar"));
+ args.push_back(string_value("|"));
+ args.push_back(string_value("baz"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_OR, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer13()
+{
+ value_t args;
+ args.push_back(string_value("foo"));
+ args.push_back(string_value("and"));
+ args.push_back(string_value("bar"));
+ args.push_back(string_value("|baz"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_OR, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer14()
+{
+ value_t args;
+ args.push_back(string_value("foo"));
+ args.push_back(string_value("and"));
+ args.push_back(string_value("bar|"));
+ args.push_back(string_value("baz"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_OR, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer15()
+{
+ value_t args;
+ args.push_back(string_value("foo"));
+ args.push_back(string_value("and"));
+ args.push_back(string_value("bar|baz"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_OR, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
+
+void ValueExprTestCase::testPredicateTokenizer16()
+{
+ value_t args;
+ args.push_back(string_value("foo"));
+ args.push_back(string_value("and bar|baz"));
+
+#ifndef NOT_FOR_PYTHON
+ query_t::lexer_t tokens(args.begin(), args.end());
+
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_AND, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TOK_OR, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::TERM, tokens.next_token().kind);
+ assertEqual(query_t::lexer_t::token_t::END_REACHED, tokens.next_token().kind);
+#endif
+}
diff --git a/test/unit/t_expr.h b/test/unit/t_expr.h
new file mode 100644
index 00000000..2f22db3d
--- /dev/null
+++ b/test/unit/t_expr.h
@@ -0,0 +1,60 @@
+#ifndef _T_EXPR_H
+#define _T_EXPR_H
+
+#include "UnitTests.h"
+
+class ValueExprTestCase : public CPPUNIT_NS::TestCase
+{
+ CPPUNIT_TEST_SUITE(ValueExprTestCase);
+
+ //CPPUNIT_TEST(testConstructors);
+ CPPUNIT_TEST(testPredicateTokenizer1);
+ CPPUNIT_TEST(testPredicateTokenizer2);
+ CPPUNIT_TEST(testPredicateTokenizer3);
+ CPPUNIT_TEST(testPredicateTokenizer4);
+ CPPUNIT_TEST(testPredicateTokenizer5);
+ CPPUNIT_TEST(testPredicateTokenizer6);
+ CPPUNIT_TEST(testPredicateTokenizer7);
+ CPPUNIT_TEST(testPredicateTokenizer8);
+ CPPUNIT_TEST(testPredicateTokenizer9);
+ CPPUNIT_TEST(testPredicateTokenizer10);
+ CPPUNIT_TEST(testPredicateTokenizer11);
+ CPPUNIT_TEST(testPredicateTokenizer12);
+ CPPUNIT_TEST(testPredicateTokenizer13);
+ CPPUNIT_TEST(testPredicateTokenizer14);
+ CPPUNIT_TEST(testPredicateTokenizer15);
+ CPPUNIT_TEST(testPredicateTokenizer16);
+
+ CPPUNIT_TEST_SUITE_END();
+
+public:
+ ValueExprTestCase() {}
+ virtual ~ValueExprTestCase() {}
+
+ virtual void setUp();
+ virtual void tearDown();
+
+ //void testConstructors();
+ void testPredicateTokenizer1();
+ void testPredicateTokenizer2();
+ void testPredicateTokenizer3();
+ void testPredicateTokenizer4();
+ void testPredicateTokenizer5();
+ void testPredicateTokenizer6();
+ void testPredicateTokenizer7();
+ void testPredicateTokenizer8();
+ void testPredicateTokenizer9();
+ void testPredicateTokenizer10();
+ void testPredicateTokenizer11();
+ void testPredicateTokenizer12();
+ void testPredicateTokenizer13();
+ void testPredicateTokenizer14();
+ void testPredicateTokenizer15();
+ void testPredicateTokenizer16();
+
+private:
+ ValueExprTestCase(const ValueExprTestCase &copy);
+ void operator=(const ValueExprTestCase &copy);
+};
+
+#endif // _T_EXPR_H
diff --git a/test/unit/t_times.cc b/test/unit/t_times.cc
new file mode 100644
index 00000000..d8a67b43
--- /dev/null
+++ b/test/unit/t_times.cc
@@ -0,0 +1,166 @@
+#include <system.hh>
+
+#include "t_times.h"
+
+#include "utils.h"
+#include "times.h"
+
+using namespace ledger;
+
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(DateTimeTestCase, "util");
+
+void DateTimeTestCase::setUp()
+{
+ times_initialize();
+}
+
+void DateTimeTestCase::tearDown()
+{
+ times_shutdown();
+}
+
+void DateTimeTestCase::testConstructors()
+{
+#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
+
+#ifndef NOT_FOR_PYTHON
+ date_t d0;
+ date_t d1;
+ datetime_t d3;
+ date_t d4;
+ date_t d5;
+ date_t d6;
+ date_t d7;
+ date_t d8;
+ date_t d9;
+
+#if 0
+ date_t d10;
+ date_t d11;
+ date_t d12;
+ date_t d13;
+ date_t d14;
+ datetime_t d15;
+#endif
+#endif // NOT_FOR_PYTHON
+
+ d1 = parse_date("1990/01/01");
+#ifndef NOT_FOR_PYTHON
+ d3 = boost::posix_time::from_time_t(localMoment);
+#endif // NOT_FOR_PYTHON
+ d4 = parse_date("2006/12/25");
+ d5 = parse_date("12/25");
+ d6 = parse_date("2006.12.25");
+ d7 = parse_date("12.25");
+ d8 = parse_date("2006-12-25");
+ d9 = parse_date("12-25");
+
+#ifndef NOT_FOR_PYTHON
+#if 0
+ d10 = parse_date("tue");
+ d11 = parse_date("tuesday");
+ d12 = parse_date("feb");
+ d13 = parse_date("february");
+ d14 = parse_date("2006");
+ d15 = d3;
+#endif
+#endif // NOT_FOR_PYTHON
+
+#ifndef NOT_FOR_PYTHON
+ assertTrue(d0.is_not_a_date());
+ assertFalse(d1.is_not_a_date());
+ assertFalse(d4.is_not_a_date());
+#endif // NOT_FOR_PYTHON
+
+ assertTrue(CURRENT_DATE() > d1);
+ assertTrue(CURRENT_DATE() > d4);
+
+#ifndef NOT_FOR_PYTHON
+#if 0
+ assertEqual(d3, d15);
+#endif
+#endif // NOT_FOR_PYTHON
+ assertEqual(d4, d6);
+ assertEqual(d4, d8);
+ assertEqual(d5, d7);
+ assertEqual(d5, d9);
+#ifndef NOT_FOR_PYTHON
+#if 0
+ assertEqual(d10, d11);
+ assertEqual(d12, d13);
+
+ assertThrow(parse_date("2007/02/29"), boost::gregorian::bad_day_of_month);
+ //assertThrow(parse_date("2007/13/01"), datetime_error);
+ //assertThrow(parse_date("2007/00/01"), datetime_error);
+ assertThrow(parse_date("2007/01/00"), boost::gregorian::bad_day_of_month);
+ //assertThrow(parse_date("2007/00/00"), boost::gregorian::bad_day_of_month);
+ //assertThrow(parse_date("2007/05/32"), boost::gregorian::bad_day_of_month);
+
+ assertThrow(parse_date("2006x/12/25"), datetime_error);
+ assertThrow(parse_date("2006/12x/25"), datetime_error);
+ assertThrow(parse_date("2006/12/25x"), datetime_error);
+
+ assertThrow(parse_date("feb/12/25"), datetime_error);
+ assertThrow(parse_date("2006/mon/25"), datetime_error);
+ assertThrow(parse_date("2006/12/web"), datetime_error);
+
+ assertThrow(parse_date("12*25"), datetime_error);
+
+ assertThrow(parse_date("tuf"), datetime_error);
+ assertThrow(parse_date("tufsday"), datetime_error);
+ assertThrow(parse_date("fec"), datetime_error);
+ assertThrow(parse_date("fecruary"), datetime_error);
+ assertThrow(parse_date("207x"), datetime_error);
+ assertThrow(parse_date("hello"), datetime_error);
+
+ d1 = parse_date("2002-02-02");
+ d1 = parse_date("2002/02/02");
+ d1 = parse_date("2002.02.02");
+ d1 = parse_date("02-02-2002");
+ d1 = parse_date("02/02/2002");
+ d1 = parse_date("02.02.2002");
+ d1 = parse_date("02-02-02");
+ d1 = parse_date("02/02/02");
+ d1 = parse_date("02.02.02");
+ d1 = parse_date("02-02");
+ d1 = parse_date("02/02");
+ d1 = parse_date("02.02");
+ d1 = parse_date("20020202");
+ d1 = parse_date("20020202T023318");
+ d1 = parse_date("20020202T023318-0700");
+ d1 = parse_date("20020202T023318-0100");
+ d1 = parse_date("02-Feb-2002");
+ d1 = parse_date("2002-Feb-02");
+ d1 = parse_date("02 Feb 2002");
+ d1 = parse_date("02-Feb-2002");
+ d1 = parse_date("02 February 2002");
+ d1 = parse_date("02-February-2002");
+ d1 = parse_date("2002 Feb 02");
+ d1 = parse_date("2002-Feb-02");
+ d1 = parse_date("2002 February 02");
+ d1 = parse_date("2002-February-02");
+ d1 = parse_date("02 Feb");
+ d1 = parse_date("02-Feb");
+ d1 = parse_date("02 February");
+ d1 = parse_date("02-February");
+ d1 = parse_date("Feb 02");
+ d1 = parse_date("Feb-02");
+ d1 = parse_date("February 02");
+ d1 = parse_date("February-02");
+ d1 = parse_date("Feb 02, 2002");
+ d1 = parse_date("February 02, 2002");
+ d1 = parse_date("2002-02-02 12:00:00");
+ d1 = parse_date("2002-02-02 12:00:00 AM");
+ d1 = parse_date("2002-02-02 12:00 AM");
+ d1 = parse_date("2002-02-02 12:00AM");
+ d1 = parse_date("2002-02-02 12p");
+ d1 = parse_date("2002-02-02 12a");
+
+ assertValid(d1);
+#endif // NOT_FOR_PYTHON
+#endif
+}
diff --git a/test/unit/t_times.h b/test/unit/t_times.h
new file mode 100644
index 00000000..5bbadf21
--- /dev/null
+++ b/test/unit/t_times.h
@@ -0,0 +1,28 @@
+#ifndef _T_TIMES_H
+#define _T_TIMES_H
+
+#include "UnitTests.h"
+
+class DateTimeTestCase : public CPPUNIT_NS::TestCase
+{
+ CPPUNIT_TEST_SUITE(DateTimeTestCase);
+
+ CPPUNIT_TEST(testConstructors);
+
+ CPPUNIT_TEST_SUITE_END();
+
+public:
+ DateTimeTestCase() {}
+ virtual ~DateTimeTestCase() {}
+
+ virtual void setUp();
+ virtual void tearDown();
+
+ void testConstructors();
+
+private:
+ DateTimeTestCase(const DateTimeTestCase &copy);
+ void operator=(const DateTimeTestCase &copy);
+};
+
+#endif /* _T_TIMES_H */
diff --git a/test/unit/t_utils.cc b/test/unit/t_utils.cc
new file mode 100644
index 00000000..6c63359c
--- /dev/null
+++ b/test/unit/t_utils.cc
@@ -0,0 +1,6 @@
+#include <system.hh>
+
+#include "t_utils.h"
+
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(UtilitiesTestCase, "util");
+
diff --git a/test/unit/t_utils.h b/test/unit/t_utils.h
new file mode 100644
index 00000000..4520c580
--- /dev/null
+++ b/test/unit/t_utils.h
@@ -0,0 +1,28 @@
+#ifndef _T_UTILS_H
+#define _T_UTILS_H
+
+#include "UnitTests.h"
+
+class UtilitiesTestCase : public CPPUNIT_NS::TestCase
+{
+ CPPUNIT_TEST_SUITE(UtilitiesTestCase);
+
+ //CPPUNIT_TEST(testConstructors);
+
+ CPPUNIT_TEST_SUITE_END();
+
+public:
+ UtilitiesTestCase() {}
+ virtual ~UtilitiesTestCase() {}
+
+ //virtual void setUp();
+ //virtual void tearDown();
+
+ //void testConstructors();
+
+private:
+ UtilitiesTestCase(const UtilitiesTestCase &copy);
+ void operator=(const UtilitiesTestCase &copy);
+};
+
+#endif /* _T_UTILS_H */