testdate_duration.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 "../testfrmwk.hpp"
  9. #include <iostream>
  10. void test_date_duration()
  11. {
  12. using namespace boost::gregorian;
  13. date_duration threeDays(3);
  14. date_duration twoDays(2);
  15. //date_duration zeroDays(0);
  16. check("Self equal case", threeDays == threeDays);
  17. check("Not equal case", !(threeDays == twoDays));
  18. check("Less case succeed", twoDays < threeDays);
  19. check("Not less case", !(threeDays < twoDays));
  20. check("Not less case - equal", !(threeDays < threeDays));
  21. check("Greater than ", !(threeDays > threeDays));
  22. check("Greater equal ", threeDays >= threeDays);
  23. check("Greater equal - false", !(twoDays >= threeDays));
  24. check("add", twoDays + threeDays == date_duration(5));
  25. date_duration fiveDays = threeDays;
  26. fiveDays += twoDays;
  27. check("add", fiveDays == date_duration(5));
  28. date_duration tenDays = fiveDays;
  29. tenDays += date_duration(5);
  30. check("add", tenDays.days() == 10);
  31. date_duration derivedOneDay = threeDays - twoDays;
  32. check("Subtraction - neg result", twoDays - threeDays == date_duration(-1));
  33. date_duration oneDay(1);
  34. check("Subtraction", oneDay == derivedOneDay);
  35. date_duration fiveDaysDerived = tenDays;
  36. fiveDaysDerived -= fiveDays;
  37. check("Subtraction", fiveDaysDerived == fiveDays);
  38. oneDay = twoDays / 2;
  39. check("Division", oneDay.days() == 1);
  40. date_duration oneDayDivide = threeDays / 2;
  41. check("Division", oneDayDivide.days() == 1);
  42. date_duration hundred(100);
  43. hundred /= -10;
  44. check("Division", hundred.days() == -10 && hundred.is_negative());
  45. date_duration pos_dur(123);
  46. date_duration neg_dur(-pos_dur);
  47. check("unary-", neg_dur.days() == -123);
  48. // special values tests
  49. date_duration pi_dur(pos_infin);
  50. date_duration ni_dur(neg_infin);
  51. date_duration nd_dur(not_a_date_time);
  52. check("pos_inf + neg_inf", (pi_dur + ni_dur) == nd_dur);
  53. //check("inf * integer", (pi_dur * 2) == pi_dur); // not implemented
  54. check("neg_inf / integer", (ni_dur / 3) == ni_dur);
  55. check("inf + dur", (pi_dur + hundred) == pi_dur);
  56. check("unary-", date_duration(-pi_dur) == ni_dur);
  57. // date_duration dd(1);
  58. // dd++;
  59. // check("Increment", dd == twoDays);
  60. }
  61. int main() {
  62. test_date_duration();
  63. return printTestStats();
  64. }