testperiod.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  6. */
  7. #include "boost/date_time/gregorian/gregorian.hpp"
  8. #include "../testfrmwk.hpp"
  9. #include <iostream>
  10. int main()
  11. {
  12. using namespace boost::gregorian;
  13. date d1(2000,Jan,1),d2(2000,Jan,4);
  14. date d3 = d2;
  15. check("assignment", d3 == d2);
  16. date_period p1(d1,d2);
  17. date_period p2(d1,date_duration(3));
  18. check("construction and ==", p1 == p2);
  19. check("begin", p1.begin() == d1);
  20. check("last", p1.last() == d2-date_duration(1) );
  21. check("end", p1.end() == d2);
  22. check("length", p2.length() == date_duration(3));
  23. check("contains begin", p1.contains(d1));
  24. check("contains last", !p1.contains(d2));
  25. date_period p3(date(2000,Jan,4),date(2000,Feb,1));
  26. check("operator== not equal case", !(p1 == p3));
  27. check("less than order", p1 < p3);
  28. check("greater than order", p3 > p1);
  29. check("not equal", p3 != p1);
  30. check("intersects with myself", p1.intersects(p1));
  31. check("not intersects", !(p1.intersects(p3)));
  32. check("not intersects", !(p3.intersects(p1)));
  33. date_period p4(date(1999,Dec,1), d2);
  34. check("intersects", p1.intersects(p4));
  35. check("intersects", p4.intersects(p1));
  36. date_period p5(date(1999,Dec,1), date(2000,Dec,31));
  37. check("intersects", p1.intersects(p5));
  38. check("intersects", p5.intersects(p1));
  39. date_period p6(date(2000,Jan,1),date(2000,Dec,31));
  40. check("contains period", p5.contains(p6));
  41. check("contains period equal", p6.contains(p6));
  42. check("not contains period", !p6.contains(p5));
  43. //shift test
  44. date_duration fourDays(4);
  45. p1.shift(fourDays); //from 2000-Jan-01--2000-Jan-04
  46. date_period shifted(date(2000,Jan,5),date(2000,Jan,8));
  47. // std::cout << to_string(p1.begin()) <<"--"
  48. // << to_string(p1.last()) << std::endl;
  49. check("shift", p1 == shifted);
  50. //expand the date period
  51. date_period p10(date(2000,Jan,5),date(2000,Jan,8));
  52. p10.expand(days(2)); //from 2000-Jan-01--2000-Jan-04
  53. check("expand", p10 == date_period(date(2000,Jan,3),date(2000,Jan,10)));
  54. //intersection tests
  55. date_period i1(date(2000,Jan,5), date(2000,Jan,10));
  56. date_period i2(date(2000,Jan,1), date(2000,Jan,7));
  57. date_period r1(date(2000,Jan,5), date(2000,Jan,7));
  58. //case 1 [5 -10) intersect [1-7) -> [5-7)
  59. std::cout << to_simple_string(i1.intersection(i2)) << std::endl;
  60. check("intersect case1", i1.intersection(i2) == r1);
  61. check("intersect case1", i2.intersection(i1) == r1);
  62. //case 2 [5 -10) intersect [1-15) -> [5-10)
  63. date_period i3(date(2000,Jan,1), date(2000,Jan,15));
  64. check("intersect case2", i1.intersection(i3) == i1);
  65. check("intersect case2", i3.intersection(i1) == i1);
  66. //case 3 [5-10) intersect [7-15) -> [7-10)
  67. date_period i4(date(2000,Jan,7), date(2000,Jan,10));
  68. date_period r2(date(2000,Jan,7), date(2000,Jan,10));
  69. check("intersect case3", i1.intersection(i4) == r2);
  70. check("intersect case3", i4.intersection(i1) == r2);
  71. //case 4 [5-10) intersect [6-9) -> [6-9)
  72. date_period i5(date(2000,Jan,6), date(2000,Jan,9));
  73. check("intersect case4", i1.intersection(i5) == i5);
  74. check("intersect case4", i5.intersection(i1) == i5);
  75. //case 5 no intersection [1-7) intersect [7-10)
  76. check("no intersection", i2.intersection(i4).is_null());
  77. //case 1 [5 -10) merge [1-7) -> [1-10)
  78. date_period r3(date(2000,Jan,1), date(2000,Jan,10));
  79. // std::cout << to_iso_string(i1.merge(i2).begin()) << "/" << to_iso_string(i1.merge(i2).last()) << std::endl;
  80. check("[5 -10) merge [1-7) -> [1-10)", i1.merge(i2) == r3);
  81. check("[1 -7) merge [7-10) -> null", i2.merge(i4).is_null());
  82. date_period r4(date(2000,Jan,5), date(2000,Jan,10));
  83. check("[5 -10) merge [6-9) -> [5-10)", i1.merge(i5) == r4);
  84. check("[5-10) span [1-7) -> [1-10)", i1.span(i2) == r3);
  85. check("[1-7) span [7-10) -> [1-10)", i2.span(i4) == r3);
  86. check("[7-10) span [1-7) -> [1-10)", i4.span(i2) == r3);
  87. check("[1-15) span [1-7) -> [1-15)", i3.span(i2) == i3);
  88. date_period i6(date(2000,Jan,1), date(2000,Jan,2));
  89. check("[1-2) span [7-10) -> [1-10)", i6.span(i4) == r3);
  90. check("[7-10) span [1-2) -> [1-10)", i4.span(i6) == r3);
  91. date bf_start(2000,Jan,5);
  92. date bf_end(2000,Jan,10);
  93. date bf_before(2000,Jan,4); //is before the period
  94. date bf_after(2000,Jan,11); //is really after
  95. date bf_during(2000, Jan, 7);
  96. date_period bfp1(bf_start, bf_end); //[2000-Jan-5 - 2000-Jan10)
  97. check("is before -- start boundary", !bfp1.is_before(bf_start));
  98. check("is before -- end boundary", bfp1.is_before(bf_end));
  99. check("is before -- last boundary", !bfp1.is_before(bfp1.last()));
  100. check("is before -- false", !bfp1.is_before(bf_before));
  101. check("is before -- false", !bfp1.is_before(bf_during));
  102. check("is before -- true", bfp1.is_before(bf_after));
  103. check("is after -- start boundary", !bfp1.is_after(bf_start));
  104. check("is after -- end boundary", !bfp1.is_after(bf_end));
  105. check("is after -- last boundary", !bfp1.is_after(bfp1.last()));
  106. check("is after -- true", bfp1.is_after(bf_before));
  107. check("is after -- false", !bfp1.is_after(bf_during));
  108. check("is after -- false", !bfp1.is_after(bf_after));
  109. //adjacent tests
  110. /*
  111. [5-----10) adj1
  112. [1----5) adj2
  113. [7-----12) adj3
  114. [12----15) adj4
  115. [1-3) adj5
  116. [7-9) adj6
  117. */
  118. date_period adj1(date(2000,Jan,5), date(2000,Jan,10));
  119. date_period adj2(date(2000,Jan,1), date(2000,Jan,5));
  120. date_period adj3(date(2000,Jan,7), date(2000,Jan,12));
  121. date_period adj4(date(2000,Jan,12), date(2000,Jan,15));
  122. date_period adj5(date(2000,Jan,1), date(2000,Jan,3));
  123. date_period adj6(date(2000,Jan,7), date(2000,Jan,9));
  124. check("is adjacent -- adj1-->adj2", adj1.is_adjacent(adj2));
  125. check("is adjacent -- adj2-->adj1", adj2.is_adjacent(adj1));
  126. check("is adjacent -- adj1-->adj3", !adj1.is_adjacent(adj3));
  127. check("is adjacent -- adj3-->adj1", !adj3.is_adjacent(adj1));
  128. check("is adjacent -- adj1-->adj4", !adj1.is_adjacent(adj4));
  129. check("is adjacent -- adj4-->adj1", !adj4.is_adjacent(adj1));
  130. check("is adjacent -- adj1-->adj5", !adj1.is_adjacent(adj5));
  131. check("is adjacent -- adj5-->adj1", !adj5.is_adjacent(adj1));
  132. check("is adjacent -- adj1-->adj6", !adj1.is_adjacent(adj6));
  133. check("is adjacent -- adj6-->adj1", !adj6.is_adjacent(adj1));
  134. printTestStats();
  135. return 0;
  136. }