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