testgeneric_period.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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: Bart Garst
  6. */
  7. #include <iostream>
  8. #include "boost/date_time/period.hpp"
  9. #include "testfrmwk.hpp"
  10. /*! duration_rep parameter for period requires a func unit() that
  11. * returns the smallest unit of measure for this type. This minimal
  12. * class fulfills that, and other, requirements */
  13. template<class int_type>
  14. class duration_type {
  15. public:
  16. duration_type(int_type a = 0) : _val(a) {}
  17. static int_type unit() { return 1; }
  18. int_type get_rep() { return _val; }
  19. bool operator==(duration_type<int_type> rhs) { return _val == rhs._val; }
  20. bool operator<(duration_type<int_type> rhs) { return _val < rhs._val; }
  21. bool operator>(duration_type<int_type> rhs) { return _val > rhs._val; }
  22. private:
  23. int_type _val;
  24. };
  25. //! To enable things like "cout << period.length()"
  26. template<class int_type>
  27. inline
  28. std::ostream& operator<<(std::ostream& os, duration_type<int_type> dt){
  29. os << dt.get_rep();
  30. return os;
  31. }
  32. //! this operator is needed because period adds a duration_rep to a point_rep
  33. template<class int_type>
  34. inline
  35. int_type operator+(int i, duration_type<int_type> dt){
  36. return i + dt.get_rep();
  37. }
  38. //! this operator is needed because period adds a duration_rep to a point_rep
  39. template<class int_type>
  40. inline
  41. int_type operator-(int i, duration_type<int_type> dt){
  42. return i - dt.get_rep();
  43. }
  44. int main(){
  45. using namespace boost::date_time;
  46. typedef period<int, duration_type<int> > a_period;
  47. /*** check all functions - normal periods ***/
  48. a_period p1(1, duration_type<int>(9));
  49. check("Different constructors", p1 == a_period(1, 10));
  50. check("First", p1.begin() == 1);
  51. check("Last", p1.last() == 9);
  52. check("End", p1.end() == 10);
  53. check("Length", p1.length() == 9);
  54. check("is_null (not)", !p1.is_null());
  55. a_period p2(5, 30);
  56. check("First", p2.begin() == 5);
  57. check("Last", p2.last() == 29);
  58. check("End", p2.end() == 30);
  59. check("Length", p2.length() == 25);
  60. check("is_null (not)", !p2.is_null());
  61. a_period p3(35, 81);
  62. check("Operator ==", p1 == a_period(1,10));
  63. check("Operator !=", p1 != p2);
  64. check("Operator <", p1 < p3);
  65. check("Operator >", p3 > p2);
  66. {
  67. a_period p(1,10);
  68. p.shift(5);
  69. check("Shift (right)", p == a_period(6,15));
  70. p.shift(-15);
  71. check("Shift (left)", p == a_period(-9,0));
  72. }
  73. check("Contains rep", p2.contains(20));
  74. check("Contains rep (not)", !p2.contains(2));
  75. check("Contains period", p1.contains(a_period(2,8)));
  76. check("Contains period (not)", !p1.contains(p3));
  77. check("Intersects", p1.intersects(p2));
  78. check("Intersects", p2.intersects(p1));
  79. check("Adjacent", p1.is_adjacent(a_period(-5,1)));
  80. check("Adjacent", p1.is_adjacent(a_period(10,20)));
  81. check("Adjacent (not)", !p1.is_adjacent(p3));
  82. check("Is before", p1.is_before(15));
  83. check("Is after", p3.is_after(15));
  84. check("Intersection", (p1.intersection(p2) == a_period(5,10)));
  85. check("Intersection", (p1.intersection(p3).is_null()));
  86. check("Merge", p1.merge(p2) == a_period(1,30) );
  87. check("Merge", p1.merge(p3).is_null());
  88. check("Span", p3.span(p1) == a_period(1, 81));
  89. /*** zero length period ***/
  90. // treat a zero length period as a point
  91. a_period zero_len(3,duration_type<int>(0));
  92. check("Same beg & end == zero_length",
  93. a_period(1,1) == a_period(1, duration_type<int>(0)));
  94. check("2 point (zero length) == 1 point zero duration",
  95. a_period(3,3) == zero_len);
  96. // zero_length period always returns false for is_before & is_after
  97. check("Is Before zero period", !zero_len.is_before(5));
  98. check("Is After zero period (not)", !zero_len.is_after(5));
  99. check("Is Before zero period (not)", !zero_len.is_before(-5));
  100. check("Is After zero period", !zero_len.is_after(-5));
  101. check("is_null", zero_len.is_null());
  102. check("Contains rep (not)", !zero_len.contains(20));
  103. // a null_period cannot contain any points
  104. check("Contains rep", !zero_len.contains(3));
  105. check("Contains period (not)", !zero_len.contains(a_period(5,8)));
  106. check("Contains period", p1.contains(zero_len));
  107. check("Intersects", zero_len.intersects(p1));
  108. check("Intersects", p1.intersects(zero_len));
  109. check("Adjacent", zero_len.is_adjacent(a_period(-10,3)));
  110. check("Adjacent", a_period(-10,3).is_adjacent(zero_len));
  111. check("Intersection", (zero_len.intersection(p1) == zero_len));
  112. check("Span", zero_len.span(p2) == a_period(3,30));
  113. /*** invalid period ***/
  114. a_period null_per(5,1);
  115. check("Is Before invalid period (always false)", !null_per.is_before(7));
  116. check("Is After invalid period (always false)", !null_per.is_after(7));
  117. check("Is Before invalid period (always false)", !null_per.is_before(-5));
  118. check("Is After invalid period (always false)", !null_per.is_after(-5));
  119. check("is_null", null_per.is_null());
  120. check("Contains rep larger (always false)", !null_per.contains(20));
  121. check("Contains rep in-between (always false)", !null_per.contains(3));
  122. check("Contains period (not)", !null_per.contains(a_period(7,9)));
  123. check("Contains period", p1.contains(null_per));
  124. check("Intersects", null_per.intersects(p1));
  125. check("Intersects", p1.intersects(null_per));
  126. check("Adjacent", null_per.is_adjacent(a_period(-10,5)));
  127. check("Adjacent", null_per.is_adjacent(a_period(1,10)));
  128. // what should this next one do?
  129. //check("Intersection", (null_per.intersection(p1) == zero_len));
  130. check("Span", null_per.span(p3) == a_period(5,81));
  131. {
  132. std::cout << std::endl;
  133. a_period p1x(0, -2);
  134. check("First", p1x.begin() == 0);
  135. check("Last", p1x.last() == -3);
  136. check("End", p1x.end() == -2);
  137. check("Length", p1x.length() == -2);
  138. check("is_null", p1x.is_null());
  139. }
  140. {
  141. std::cout << std::endl;
  142. a_period p1x(0, -1);
  143. check("First", p1x.begin() == 0);
  144. check("Last", p1x.last() == -2);
  145. check("End", p1x.end() == -1);
  146. check("Length", p1x.length() == -1);
  147. check("is_null", p1x.is_null());
  148. }
  149. {
  150. std::cout << std::endl;
  151. a_period p1x(0, 0);
  152. check("First", p1x.begin() == 0);
  153. check("Last", p1x.last() == -1);
  154. check("End", p1x.end() == 0);
  155. check("Length", p1x.length() == 0);
  156. check("is_null", p1x.is_null());
  157. }
  158. {
  159. std::cout << std::endl;
  160. a_period p1x(0, 1);
  161. check("First", p1x.begin() == 0);
  162. check("Last", p1x.last() == 0);
  163. check("End", p1x.end() == 1);
  164. check("Length", p1x.length() == 1);
  165. check("is_null", !p1x.is_null());
  166. }
  167. {
  168. std::cout << std::endl;
  169. a_period p1x(0, 2);
  170. check("First", p1x.begin() == 0);
  171. check("Last", p1x.last() == 1);
  172. check("End", p1x.end() == 2);
  173. check("Length", p1x.length() == 2);
  174. check("is_null", !p1x.is_null());
  175. }
  176. {
  177. std::cout << std::endl;
  178. a_period p1x(0, duration_type<int>(-1));
  179. check("First", p1x.begin() == 0);
  180. check("Last", p1x.last() == -2);
  181. check("End", p1x.end() == -1);
  182. check("Length", p1x.length() == -1);
  183. check("is_null", p1x.is_null());
  184. }
  185. {
  186. std::cout << std::endl;
  187. a_period p1x(0, duration_type<int>(-2));
  188. check("First", p1x.begin() == 0);
  189. check("Last", p1x.last() == -3);
  190. check("End", p1x.end() == -2);
  191. check("Length", p1x.length() == -2);
  192. check("is_null", p1x.is_null());
  193. }
  194. {
  195. std::cout << std::endl;
  196. a_period p1x(0, duration_type<int>(0));
  197. check("First", p1x.begin() == 0);
  198. check("Last", p1x.last() == -1);
  199. check("End", p1x.end() == 0);
  200. check("Length", p1x.length() == 0);
  201. check("is_null", p1x.is_null());
  202. }
  203. {
  204. std::cout << std::endl;
  205. a_period p1x(0, duration_type<int>(1));
  206. check("First", p1x.begin() == 0);
  207. check("Last", p1x.last() == 0);
  208. check("End", p1x.end() == 1);
  209. check("Length", p1x.length() == 1);
  210. check("is_null", !p1x.is_null());
  211. }
  212. {
  213. std::cout << std::endl;
  214. a_period p1x(0, duration_type<int>(2));
  215. check("First", p1x.begin() == 0);
  216. check("Last", p1x.last() == 1);
  217. check("End", p1x.end() == 2);
  218. check("Length", p1x.length() == 2);
  219. check("is_null", !p1x.is_null());
  220. }
  221. {
  222. std::cout << std::endl;
  223. a_period p1x(1,1); // length should be 0
  224. a_period p2x(1,2); // length should be 1
  225. a_period p3x(1,3); // length should be 2
  226. check("Length p1", p1x.length() == 0);
  227. check("Length p2", p2x.length() == 1);
  228. check("Length p3", p3x.length() == 2);
  229. check("is_null p1 (not)", p1x.is_null());
  230. check("is_null p2 (not)", !p2x.is_null());
  231. }
  232. {
  233. a_period p1x(1,2); // length should be 1
  234. p1x.shift(duration_type<int>(1));
  235. a_period p2x(2,3); // shifted result
  236. check("shift", p1x == p2x);
  237. }
  238. {
  239. a_period p1x(5,duration_type<int>(3));
  240. a_period p2x(3,10); // expanded result
  241. p1x.expand(duration_type<int>(2)); //from 2000-Jan-01--2000-Jan-04
  242. check("expand", p1x == p2x);
  243. }
  244. return printTestStats();
  245. }