diff options
Diffstat (limited to 'tests/corelib')
-rw-r--r-- | tests/corelib/numerics/BasicAmount.cc | 30 | ||||
-rw-r--r-- | tests/corelib/numerics/Commodity.cc | 16 | ||||
-rw-r--r-- | tests/corelib/numerics/DateTime.cc | 101 | ||||
-rw-r--r-- | tests/corelib/numerics/DateTimeTest.h | 28 |
4 files changed, 137 insertions, 38 deletions
diff --git a/tests/corelib/numerics/BasicAmount.cc b/tests/corelib/numerics/BasicAmount.cc index f4259843..84692a90 100644 --- a/tests/corelib/numerics/BasicAmount.cc +++ b/tests/corelib/numerics/BasicAmount.cc @@ -14,7 +14,6 @@ void BasicAmountTestCase::testConstructors() amount_t x1(123456L); amount_t x2(123456UL); amount_t x3(123.456); - amount_t x4(true); amount_t x5("123456"); amount_t x6("123.456"); amount_t x7(std::string("123456")); @@ -33,14 +32,12 @@ void BasicAmountTestCase::testConstructors() assertEqual(x6, x3); assertEqual(x8, x3); 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()); @@ -91,7 +88,6 @@ void BasicAmountTestCase::testAssignment() amount_t x1 = 123456L; amount_t x2 = 123456UL; amount_t x3 = 123.456; - amount_t x4 = true; amount_t x5 = "123456"; amount_t x6 = "123.456"; amount_t x7 = std::string("123456"); @@ -106,14 +102,12 @@ void BasicAmountTestCase::testAssignment() assertEqual(x6, x3); assertEqual(x8, x3); assertEqual(x10, x3); - assertEqual(amount_t(1L), x4); assertEqual(x10, x9); x0 = amount_t(); x1 = 123456L; x2 = 123456UL; x3 = 123.456; - x4 = true; x5 = "123456"; x6 = "123.456"; x7 = std::string("123456"); @@ -128,14 +122,12 @@ void BasicAmountTestCase::testAssignment() assertEqual(x6, x3); assertEqual(x8, x3); 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()); @@ -182,20 +174,12 @@ void BasicAmountTestCase::testIntegerAddition() x1 += 456L; assertEqual(amount_t(1035L), x1); - amount_t x3(true); - amount_t y3(true); - - assertEqual(amount_t(2L), x3 + y3); - assertEqual(amount_t(2L), x3 + true); - amount_t x4("123456789123456789123456789"); assertEqual(amount_t("246913578246913578246913578"), x4 + x4); CPPUNIT_ASSERT(x1.valid()); CPPUNIT_ASSERT(y1.valid()); - CPPUNIT_ASSERT(x3.valid()); - CPPUNIT_ASSERT(y3.valid()); CPPUNIT_ASSERT(x4.valid()); } @@ -239,11 +223,6 @@ void BasicAmountTestCase::testIntegerSubtraction() x1 -= 456L; assertEqual(amount_t(-789L), x1); - amount_t x3(true); - amount_t y3(true); - - assertEqual(amount_t(false), x3 - y3); - amount_t x4("123456789123456789123456789"); amount_t y4("8238725986235986"); @@ -252,8 +231,6 @@ void BasicAmountTestCase::testIntegerSubtraction() CPPUNIT_ASSERT(x1.valid()); CPPUNIT_ASSERT(y1.valid()); - CPPUNIT_ASSERT(x3.valid()); - CPPUNIT_ASSERT(y3.valid()); CPPUNIT_ASSERT(x4.valid()); CPPUNIT_ASSERT(y4.valid()); } @@ -310,11 +287,6 @@ void BasicAmountTestCase::testIntegerMultiplication() x1 *= 123L; assertEqual(amount_t(1860867L), x1); - amount_t x3(true); - amount_t y3(true); - - assertEqual(amount_t(true), x3 * y3); - amount_t x4("123456789123456789123456789"); assertEqual(amount_t("15241578780673678546105778281054720515622620750190521"), @@ -322,8 +294,6 @@ void BasicAmountTestCase::testIntegerMultiplication() CPPUNIT_ASSERT(x1.valid()); CPPUNIT_ASSERT(y1.valid()); - CPPUNIT_ASSERT(x3.valid()); - CPPUNIT_ASSERT(y3.valid()); CPPUNIT_ASSERT(x4.valid()); } diff --git a/tests/corelib/numerics/Commodity.cc b/tests/corelib/numerics/Commodity.cc index b15211f6..9a3ec4c1 100644 --- a/tests/corelib/numerics/Commodity.cc +++ b/tests/corelib/numerics/Commodity.cc @@ -15,12 +15,12 @@ void CommodityTestCase::testConstructors() void CommodityTestCase::testPriceHistory() { - datetime_t jan17_07("2007/01/17 00:00:00"); - datetime_t feb27_07("2007/02/27 18:00:00"); - datetime_t feb28_07("2007/02/28 06:00:00"); - datetime_t feb28_07sbm("2007/02/28 11:59:59"); - datetime_t mar01_07("2007/03/01 00:00:00"); - datetime_t apr15_07("2007/04/15 13:00:00"); + ptime jan17_07 = boost::posix_time::time_from_string("2007/01/17 00:00:00"); + ptime feb27_07 = boost::posix_time::time_from_string("2007/02/27 18:00:00"); + ptime feb28_07 = boost::posix_time::time_from_string("2007/02/28 06:00:00"); + ptime feb28_07sbm = boost::posix_time::time_from_string("2007/02/28 11:59:59"); + ptime mar01_07 = boost::posix_time::time_from_string("2007/03/01 00:00:00"); + ptime apr15_07 = boost::posix_time::time_from_string("2007/04/15 13:00:00"); // jww (2007-04-17): tbd amount_t x1("100.10 AAPL"); @@ -29,9 +29,9 @@ void CommodityTestCase::testPriceHistory() // deal of their state depends on how they were seen to be used. commodity_t& aapl(x1.commodity()); - aapl.add_price(datetime_t(), amount_t("$10.20")); + aapl.add_price(now, amount_t("$10.20")); - assertEqual(amount_t("$1021.02"), x1.value(datetime_t())); + assertEqual(amount_t("$1021.02"), x1.value(now)); assertValid(x1); } diff --git a/tests/corelib/numerics/DateTime.cc b/tests/corelib/numerics/DateTime.cc new file mode 100644 index 00000000..fe5ac68b --- /dev/null +++ b/tests/corelib/numerics/DateTime.cc @@ -0,0 +1,101 @@ +#include "DateTimeTest.h" +#include "ledger.h" +#include "acconf.h" + +#include <ctime> + +using namespace ledger; + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(DateTimeTestCase, "numerics"); + +void DateTimeTestCase::setUp() {} +void DateTimeTestCase::tearDown() {} + +void DateTimeTestCase::testConstructors() +{ + struct tm moment; + std::memset(&moment, INT_MAX, sizeof(struct tm)); + +#ifdef HAVE_STRPTIME + strptime("2006/12/25 00:00:00", "%Y/%m/%d %H:%M:%S", &moment); +#endif + +#ifdef HAVE_TIMEGM + std::time_t gmtMoment = timegm(&moment); +#endif + std::time_t localMoment = std::mktime(&moment); + + date_t d1; +#ifdef HAVE_TIMEGM + date_t d2(gmtMoment); +#endif + date_t d3(localMoment); + date_t d4("2006/12/25"); + date_t d5("12/25"); + date_t d6("2006.12.25"); + date_t d7("12.25"); + date_t d8("2006-12-25"); + date_t d9("12-25"); + date_t d10("tue"); + date_t d11("tuesday"); + date_t d12("feb"); + date_t d13("february"); + date_t d14("2006"); + date_t d15(d3); + +#ifdef HAVE_TIMEGM + if (std::memcmp(&gmtMoment, &localMoment, sizeof(std::time_t)) == 0) + assertEqual(d2, d3); + else + assertNotEqual(d2, d3); +#endif + + assertFalse(d1); + assertTrue(d4); + + assertTrue(date_t::now > d1); + assertTrue(date_t::now > d3); + assertTrue(date_t::now > d4); + + assertEqual(d3, d4); + assertEqual(d3, d15); + assertEqual(d4, d6); + assertEqual(d4, d8); + assertEqual(d5, d7); + assertEqual(d5, d9); + assertEqual(d10, d11); + assertEqual(d12, d13); + + assertThrow(date_t("2007/02/29"), date_error *); + assertThrow(date_t("2007/13/01"), date_error *); + assertThrow(date_t("2007/00/01"), date_error *); + assertThrow(date_t("2007/01/00"), date_error *); + assertThrow(date_t("2007/00/00"), date_error *); + assertThrow(date_t("2007/05/32"), date_error *); + + assertThrow(date_t("2006x/12/25"), date_error *); + assertThrow(date_t("2006/12x/25"), date_error *); + assertThrow(date_t("2006/12/25x"), date_error *); + + assertThrow(date_t("feb/12/25"), date_error *); + assertThrow(date_t("2006/mon/25"), date_error *); + assertThrow(date_t("2006/12/web"), date_error *); + + assertThrow(date_t("12*25"), date_error *); + + assertThrow(date_t("tuf"), date_error *); + assertThrow(date_t("tufsday"), date_error *); + assertThrow(date_t("fec"), date_error *); + assertThrow(date_t("fecruary"), date_error *); + assertThrow(date_t("207x"), date_error *); + assertThrow(date_t("hello"), date_error *); + + datetime_t dt1; + datetime_t dt2; + datetime_t dt3; + datetime_t dt4; + datetime_t dt5; + + interval_t i1; + interval_t i2; +} diff --git a/tests/corelib/numerics/DateTimeTest.h b/tests/corelib/numerics/DateTimeTest.h new file mode 100644 index 00000000..abc914cd --- /dev/null +++ b/tests/corelib/numerics/DateTimeTest.h @@ -0,0 +1,28 @@ +#ifndef _DATETIMETEST_H +#define _DATETIMETEST_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 ©); + void operator=(const DateTimeTestCase ©); +}; + +#endif /* _DATETIMETEST_H */ |