testgreg_duration_operators.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
  2. * Use, modification and distribution is subject to the
  3. * Boost Software License, Version 1.0. (See accompanying
  4. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  5. * Author: Jeff Garland, Bart Garst
  6. */
  7. #include "boost/date_time/gregorian/gregorian.hpp"
  8. #include "boost/date_time/posix_time/posix_time.hpp"
  9. #include "../testfrmwk.hpp"
  10. int main(){
  11. #if !defined(BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES)
  12. // do not set this test to return fail -
  13. // this is not necessarily a compiler problem
  14. check("Optional gregorian types not selected - no tests run", true);
  15. #else
  16. using namespace boost::gregorian;
  17. using namespace boost::posix_time;
  18. /*** months ***/
  19. {
  20. ptime p(date(2001, Oct, 31), hours(5));
  21. check("ptime + months",
  22. ptime(date(2002, Feb, 28), hours(5)) == p + months(4));
  23. p += months(4);
  24. check("ptime += months",
  25. ptime(date(2002, Feb, 28), hours(5)) == p);
  26. }
  27. {
  28. ptime p(date(2001, Oct, 31), hours(5));
  29. check("ptime - months",
  30. ptime(date(2001, Apr, 30), hours(5)) == p - months(6));
  31. p -= months(6);
  32. check("ptime -= months",
  33. ptime(date(2001, Apr, 30), hours(5)) == p);
  34. }
  35. /*** years ***/
  36. {
  37. ptime p(date(2001, Feb, 28), hours(5));
  38. check("ptime + years",
  39. ptime(date(2004, Feb, 29), hours(5)) == p + years(3));
  40. p += years(3);
  41. check("ptime += years",
  42. ptime(date(2004, Feb, 29), hours(5)) == p);
  43. }
  44. {
  45. ptime p(date(2000, Feb, 29), hours(5));
  46. check("ptime - years",
  47. ptime(date(1998, Feb, 28), hours(5)) == p - years(2));
  48. p -= years(2);
  49. check("ptime -= years",
  50. ptime(date(1998, Feb, 28), hours(5)) == p);
  51. }
  52. /*** weeks ***/
  53. // shouldn't need many tests, it is nothing more than a date_duration
  54. // so all date_duration tests should prove this class
  55. {
  56. ptime p(date(2001, Feb, 28), hours(5));
  57. check("ptime + weeks",
  58. ptime(date(2001, Mar, 21), hours(5)) == p + weeks(3));
  59. p += weeks(3);
  60. check("ptime += weeks",
  61. ptime(date(2001, Mar, 21), hours(5)) == p);
  62. }
  63. {
  64. ptime p(date(2001, Feb, 28), hours(5));
  65. check("ptime - weeks",
  66. ptime(date(2001, Feb, 14), hours(5)) == p - weeks(2));
  67. p -= weeks(2);
  68. check("ptime -= weeks",
  69. ptime(date(2001, Feb, 14), hours(5)) == p);
  70. }
  71. #endif // BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES
  72. return printTestStats();
  73. }