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