t_commodity.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #define BOOST_TEST_DYN_LINK
  2. //#define BOOST_TEST_MODULE commodity
  3. #include <boost/test/unit_test.hpp>
  4. #include <system.hh>
  5. #include "amount.h"
  6. #include "commodity.h"
  7. using namespace ledger;
  8. struct commodity_fixture {
  9. commodity_fixture() {
  10. times_initialize();
  11. amount_t::initialize();
  12. amount_t::stream_fullstrings = true;
  13. }
  14. ~commodity_fixture() {
  15. amount_t::shutdown();
  16. times_shutdown();
  17. }
  18. };
  19. BOOST_FIXTURE_TEST_SUITE(commodity, commodity_fixture)
  20. BOOST_AUTO_TEST_CASE(testPriceHistory)
  21. {
  22. #ifndef NOT_FOR_PYTHON
  23. datetime_t jan17_05;
  24. datetime_t jan17_06;
  25. datetime_t jan17_07;
  26. datetime_t feb27_07;
  27. datetime_t feb28_07;
  28. datetime_t feb28_07sbm;
  29. datetime_t mar01_07;
  30. datetime_t apr15_07;
  31. #endif // NOT_FOR_PYTHON
  32. jan17_05 = parse_datetime("2005/01/17 00:00:00");
  33. jan17_06 = parse_datetime("2006/01/17 00:00:00");
  34. jan17_07 = parse_datetime("2007/01/17 00:00:00");
  35. feb27_07 = parse_datetime("2007/02/27 18:00:00");
  36. feb28_07 = parse_datetime("2007/02/28 06:00:00");
  37. feb28_07sbm = parse_datetime("2007/02/28 11:59:59");
  38. mar01_07 = parse_datetime("2007/03/01 00:00:00");
  39. apr15_07 = parse_datetime("2007/04/15 13:00:00");
  40. amount_t x0;
  41. amount_t x1("100.10 AAPL");
  42. BOOST_CHECK_THROW(x0.value(), amount_error);
  43. #ifndef NOT_FOR_PYTHON
  44. BOOST_CHECK(! x1.value());
  45. #endif
  46. // Commodities cannot be constructed by themselves, since a great deal
  47. // of their state depends on how they were seen to be used.
  48. commodity_t& aapl(x1.commodity());
  49. aapl.add_price(jan17_07, amount_t("$10.20"));
  50. aapl.add_price(feb27_07, amount_t("$13.40"));
  51. aapl.add_price(feb28_07, amount_t("$18.33"));
  52. aapl.add_price(feb28_07sbm, amount_t("$18.30"));
  53. aapl.add_price(mar01_07, amount_t("$19.50"));
  54. aapl.add_price(apr15_07, amount_t("$21.22"));
  55. aapl.add_price(jan17_05, amount_t("EUR 23.00"));
  56. aapl.add_price(jan17_06, amount_t("CAD 25.00"));
  57. amount_t one_euro("EUR 1.00");
  58. commodity_t& euro(one_euro.commodity());
  59. euro.add_price(feb27_07, amount_t("CAD 1.40"));
  60. euro.add_price(jan17_05, amount_t("$0.78"));
  61. amount_t one_cad("CAD 1.00");
  62. commodity_t& cad(one_cad.commodity());
  63. cad.add_price(jan17_06, amount_t("$1.11"));
  64. #ifndef NOT_FOR_PYTHON
  65. optional<amount_t> amt = x1.value(feb28_07sbm);
  66. BOOST_CHECK(amt);
  67. BOOST_CHECK_EQUAL(amount_t("$1831.83"), *amt);
  68. amt = x1.value(CURRENT_TIME());
  69. BOOST_CHECK(amt);
  70. BOOST_CHECK_EQUAL(string("$2124.12"), amt->to_string());
  71. #ifdef INTEGER_MATH
  72. BOOST_CHECK_EQUAL(string("$2124.12"), amt->to_fullstring());
  73. #else
  74. BOOST_CHECK_EQUAL(string("$2124.122"), amt->to_fullstring());
  75. #endif
  76. amt = x1.value(CURRENT_TIME(), &euro);
  77. BOOST_CHECK(amt);
  78. BOOST_CHECK_EQUAL(string("EUR 1787.50"), amt->rounded().to_string());
  79. // Add a newer Euro pricing
  80. aapl.add_price(jan17_07, amount_t("EUR 23.00"));
  81. amt = x1.value(CURRENT_TIME(), &euro);
  82. BOOST_CHECK(amt);
  83. BOOST_CHECK_EQUAL(string("EUR 2302.30"), amt->to_string());
  84. amt = x1.value(CURRENT_TIME(), &cad);
  85. BOOST_CHECK(amt);
  86. BOOST_CHECK_EQUAL(string("CAD 3223.22"), amt->to_string());
  87. #endif // NOT_FOR_PYTHON
  88. BOOST_CHECK(x1.valid());
  89. }
  90. BOOST_AUTO_TEST_SUITE_END()