testgreg_durations.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Copyright (c) 2002,2003,2005 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. int main(){
  10. #if !defined(BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES)
  11. // do not set this test to return fail -
  12. // this is not necessarily a compiler problem
  13. check("Optional gregorian types not selected - no tests run", true);
  14. #else
  15. using namespace boost::gregorian;
  16. /*** months ***/
  17. {
  18. months m1(5), m2(3), m3(1);
  19. check("months & months addable", months(8) == m1 + m2);
  20. m1 += m2;
  21. check("months & months addable", months(8) == m1);
  22. check("months & months subtractable", months(-5) == m2 - m1);
  23. m2 -= m1;
  24. check("months & months subtractable", months(-5) == m2);
  25. {
  26. // adding and subtracting negative values
  27. date d1(2005, Jan, 1);
  28. date d2(2005, Feb, 1);
  29. check("add neg months (year wrap under)",
  30. d1 + months(-1) == date(2004,Dec,1));
  31. check("add neg months (no year wrap under)",
  32. d2 + months(-1) == date(2005,Jan,1));
  33. check("add neg months (year wrap under)",
  34. d2 + months(-2) == date(2004,Dec,1));
  35. check("add neg months (year wrap under)",
  36. d2 + months(-12) == date(2004,Feb,1));
  37. check("add neg months (year wrap under)",
  38. d2 + months(-13) == date(2004,Jan,1));
  39. check("add neg months (year wrap under)",
  40. d2 + months(-14) == date(2003,Dec,1));
  41. date d3(2005, Dec, 1);
  42. date d4(2005, Nov, 1);
  43. check("subtract neg months (year wrap over)",
  44. d3 - months(-1) == date(2006,Jan,1));
  45. check("subtract neg months (no year wrap over)",
  46. d4 - months(-1) == date(2005,Dec,1));
  47. check("subtract neg months (year wrap over)",
  48. d4 - months(-2) == date(2006,Jan,1));
  49. check("subtract neg months (year wrap over)",
  50. d4 - months(-12) == date(2006,Nov,1));
  51. check("subtract neg months (year wrap over)",
  52. d4 - months(-13) == date(2006,Dec,1));
  53. check("subtract neg months (year wrap over)",
  54. d4 - months(-14) == date(2007,Jan,1));
  55. }
  56. {
  57. months m1x(5), m2x(3), m3x(10);
  58. check("months & int multipliable", months(15) == m1x * 3);
  59. m1x *= 3;
  60. check("months & int multipliable", months(15) == m1x);
  61. //check("int * months", months(12) == 4 * m2x);
  62. check("months & int dividable", months(3) == m3x / 3);
  63. m3x /= 3;
  64. check("months & int dividable", months(3) == m3x);
  65. }
  66. {
  67. months m(-5), m_pos(pos_infin), m_neg(neg_infin), m_nadt(not_a_date_time);
  68. check("months add special_values", m + m_pos == m_pos);
  69. check("months add special_values", m + m_neg == m_neg);
  70. check("months add special_values", m_pos + m_neg == m_nadt);
  71. check("months add special_values", m_neg + m_neg == m_neg);
  72. check("months subtract special_values", m - m_pos == m_neg);
  73. check("months subtract special_values", m - m_neg == m_pos);
  74. check("months subtract special_values", m_pos - m_neg == m_pos);
  75. check("months special_values & int multipliable", m_pos * -1 == m_neg);
  76. check("months special_values & int multipliable", m_pos * 0 == m_nadt);
  77. check("months special_values & int dividable", m_neg / 3 == m_neg);
  78. }
  79. years y1(2), y2(4);
  80. check("months & years addable", months(25) == m3 + y1);
  81. m3 += y1;
  82. check("months & years addable", months(25) == m3);
  83. check("months & years subtractable", months(-23) == m3 - y2);
  84. m3 -= y2;
  85. check("months & years subtractable", months(-23) == m3);
  86. {
  87. date d(2001, Oct, 31);
  88. check("date + months", date(2002, Feb, 28) == d + months(4));
  89. d += months(4);
  90. check("date += months", date(2002, Feb, 28) == d);
  91. }
  92. {
  93. date d(2001, Oct, 31);
  94. check("date - months", date(2001, Apr, 30) == d - months(6));
  95. d -= months(6);
  96. check("date -= months", date(2001, Apr, 30) == d);
  97. }
  98. }
  99. /*** years ***/
  100. {
  101. years y1(2), y2(4), y3(1);
  102. check("years & years addable", years(3) == y3 + y1);
  103. y3 += y1;
  104. check("years & years addable", years(3) == y3);
  105. check("years & years subtractable", years(-1) == y3 - y2);
  106. y3 -= y2;
  107. check("years & years subtractable", years(-1) == y3);
  108. {
  109. years y1x(5), y2x(3), y3x(10);
  110. check("years & int multipliable", years(15) == y1x * 3);
  111. y1x *= 3;
  112. check("years & int multipliable", years(15) == y1x);
  113. //check("int * years", years(12) == 4 * y2x);
  114. check("years & int dividable", years(3) == y3x / 3);
  115. y3x /= 3;
  116. check("years & int dividable", years(3) == y3x);
  117. }
  118. {
  119. years m(15), y_pos(pos_infin), y_neg(neg_infin), y_nadt(not_a_date_time);
  120. check("years add special_values", m + y_pos == y_pos);
  121. check("years add special_values", m + y_neg == y_neg);
  122. check("years add special_values", y_pos + y_neg == y_nadt);
  123. check("years add special_values", y_neg + y_neg == y_neg);
  124. check("years subtract special_values", m - y_pos == y_neg);
  125. check("years subtract special_values", m - y_neg == y_pos);
  126. check("years subtract special_values", y_pos - y_neg == y_pos);
  127. check("years special_values & int multipliable", y_pos * -1 == y_neg);
  128. check("years special_values & int multipliable", y_pos * 0 == y_nadt);
  129. check("years special_values & int dividable", y_neg / 3 == y_neg);
  130. }
  131. months m1(5), m2(3);
  132. check("years & months addable", months(51) == y2 + m2);
  133. check("years & months subtractable", months(43) == y2 - m1);
  134. {
  135. date d(2001, Feb, 28); // not a leap year
  136. check("date + years", date(2004, Feb, 29) == d + years(3));
  137. d += years(3);
  138. check("date += years", date(2004, Feb, 29) == d);
  139. }
  140. {
  141. date d(2000, Feb, 29);
  142. check("date - years", date(1994, Feb, 28) == d - years(6));
  143. d -= years(6);
  144. check("date -= years", date(1994, Feb, 28) == d);
  145. }
  146. try {
  147. date d1(1400, 6, 1);
  148. const date d2 = d1 + years(8599);
  149. check("date + many years != overflow", d2 == date(9999, 6, 1));
  150. }
  151. catch (...) {
  152. check("date + many years != overflow", false);
  153. }
  154. try {
  155. date d1(9999, 1, 1);
  156. const date d2 = d1 - years(8599);
  157. check("date - many years != overflow", d2 == date(1400, 1, 1));
  158. }
  159. catch (...) {
  160. check("date - many years != overflow", false);
  161. }
  162. }
  163. /*** weeks ***/
  164. // shouldn't need many tests, it is nothing more than a date_duration
  165. // so all date_duration tests should prove this class
  166. {
  167. weeks w1(2), w2(4), w3(1), pi(pos_infin);
  168. check("add special_values", weeks(pos_infin) == w1 + pi);
  169. check("weeks & weeks addable", weeks(3) == w3 + w1);
  170. w3 += w1;
  171. check("weeks & weeks addable", weeks(3) == w3);
  172. check("weeks & weeks subtractable", weeks(-1) == w3 - w2);
  173. w3 -= w2;
  174. check("weeks & weeks subtractable", weeks(-1) == w3);
  175. {
  176. days d(10);
  177. check("days + weeks", days(31) == d + weeks(3));
  178. d += weeks(3);
  179. check("days += weeks", days(31) == d);
  180. }
  181. {
  182. days d(10);
  183. check("days - weeks", days(-32) == d - weeks(6));
  184. d -= weeks(6);
  185. check("days -= weeks", days(-32) == d);
  186. }
  187. {
  188. date d(2001, Feb, 28);
  189. check("date + weeks", date(2001, Mar, 21) == d + weeks(3));
  190. d += weeks(3);
  191. check("date += weeks", date(2001, Mar, 21) == d);
  192. }
  193. {
  194. date d(2001, Feb, 28);
  195. check("date - weeks", date(2001, Jan, 17) == d - weeks(6));
  196. d -= weeks(6);
  197. check("date -= weeks", date(2001, Jan, 17) == d);
  198. }
  199. }
  200. {
  201. date d(2000, Oct, 31);
  202. date d2 = d + months(4) + years(2);
  203. date d3 = d + years(2) + months(4);
  204. check("date + years + months", date(2003,Feb,28) == d2);
  205. check("date + years + months", date(2003,Feb,28) == d3);
  206. months m = years(2) + months(4) - months(4) - years(2);
  207. check("sanity check", m.number_of_months() == 0);
  208. }
  209. /*{
  210. date d(2001, Mar, 31);
  211. date d1 = (d - months(1)) + months(1); //Mar 28, right? WRONG
  212. // Mar31 - 1 month is Feb28 (last day of month) so Feb28 + 1 month
  213. // will be Mar31 (last day of month)
  214. check("date + 1 months - 1 months", date(2001,Mar,28) == d1);
  215. std::cout << d1 << std::endl;
  216. //date d2 = (d - months(1)) + d; //compile error, right? RIGHT
  217. //weeks w1 = weeks(1) + months(1); //compiler error, right? RIGHT
  218. }*/
  219. #endif // BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES
  220. return printTestStats();
  221. }