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 m1(5), m2(3), m3(10);
65 check("months & int multipliable", months(15) == m1 * 3);
66 m1 *= 3;
67 check("months & int multipliable", months(15) == m1);
68 //check("int * months", months(12) == 4 * m2);
69 check("months & int dividable", months(3) == m3 / 3);
70 m3 /= 3;
71 check("months & int dividable", months(3) == m3);
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 y1(5), y2(3), y3(10);
120 check("years & int multipliable", years(15) == y1 * 3);
121 y1 *= 3;
122 check("years & int multipliable", years(15) == y1);
123 //check("int * years", years(12) == 4 * y2);
124 check("years & int dividable", years(3) == y3 / 3);
125 y3 /= 3;
126 check("years & int dividable", years(3) == y3);
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 }
160
161 /*** weeks ***/
162 // shouldn't need many tests, it is nothing more than a date_duration
163 // so all date_duration tests should prove this class
164 {
165 weeks w1(2), w2(4), w3(1), pi(pos_infin);
166 check("add special_values", weeks(pos_infin) == w1 + pi);
167 check("weeks & weeks addable", weeks(3) == w3 + w1);
168 w3 += w1;
169 check("weeks & weeks addable", weeks(3) == w3);
170 check("weeks & weeks subtractable", weeks(-1) == w3 - w2);
171 w3 -= w2;
172 check("weeks & weeks subtractable", weeks(-1) == w3);
173 {
174 days d(10);
175 check("days + weeks", days(31) == d + weeks(3));
176 d += weeks(3);
177 check("days += weeks", days(31) == d);
178 }
179 {
180 days d(10);
181 check("days - weeks", days(-32) == d - weeks(6));
182 d -= weeks(6);
183 check("days -= weeks", days(-32) == d);
184 }
185 {
186 date d(2001, Feb, 28);
187 check("date + weeks", date(2001, Mar, 21) == d + weeks(3));
188 d += weeks(3);
189 check("date += weeks", date(2001, Mar, 21) == d);
190 }
191 {
192 date d(2001, Feb, 28);
193 check("date - weeks", date(2001, Jan, 17) == d - weeks(6));
194 d -= weeks(6);
195 check("date -= weeks", date(2001, Jan, 17) == d);
196 }
197 }
198
199 {
200 date d(2000, Oct, 31);
201 date d2 = d + months(4) + years(2);
202 date d3 = d + years(2) + months(4);
203 check("date + years + months", date(2003,Feb,28) == d2);
204 check("date + years + months", date(2003,Feb,28) == d3);
205 months m = years(2) + months(4) - months(4) - years(2);
206 check("sanity check", m.number_of_months() == 0);
207 }
208 /*{
209 date d(2001, Mar, 31);
210 date d1 = (d - months(1)) + months(1); //Mar 28, right? WRONG
211 // Mar31 - 1 month is Feb28 (last day of month) so Feb28 + 1 month
212 // will be Mar31 (last day of month)
213 check("date + 1 months - 1 months", date(2001,Mar,28) == d1);
214 std::cout << d1 << std::endl;
215 //date d2 = (d - months(1)) + d; //compile error, right? RIGHT
216 //weeks w1 = weeks(1) + months(1); //compiler error, right? RIGHT
217 }*/
218
219 #endif // BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES
220
221 return printTestStats();
222
223 }
224