]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/date_time/test/testgeneric_period.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / date_time / test / testgeneric_period.cpp
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
8 #include <iostream>
9 #include "boost/date_time/period.hpp"
10 #include "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 //! this operator is needed because period adds a duration_rep to a point_rep
42 template<class int_type>
43 inline
44 int_type operator-(int i, duration_type<int_type> dt){
45 return i - dt.get_rep();
46 }
47
48
49 int main(){
50 using namespace boost::date_time;
51 typedef period<int, duration_type<int> > a_period;
52
53 /*** check all functions - normal periods ***/
54
55 a_period p1(1, duration_type<int>(9));
56 check("Different constructors", p1 == a_period(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 a_period p2(5, 30);
64 check("First", p2.begin() == 5);
65 check("Last", p2.last() == 29);
66 check("End", p2.end() == 30);
67 check("Length", p2.length() == 25);
68 check("is_null (not)", !p2.is_null());
69
70 a_period p3(35, 81);
71 check("Operator ==", p1 == a_period(1,10));
72 check("Operator !=", p1 != p2);
73 check("Operator <", p1 < p3);
74 check("Operator >", p3 > p2);
75
76 {
77 a_period p(1,10);
78 p.shift(5);
79 check("Shift (right)", p == a_period(6,15));
80 p.shift(-15);
81 check("Shift (left)", p == a_period(-9,0));
82 }
83
84 check("Contains rep", p2.contains(20));
85 check("Contains rep (not)", !p2.contains(2));
86 check("Contains period", p1.contains(a_period(2,8)));
87 check("Contains period (not)", !p1.contains(p3));
88
89 check("Intersects", p1.intersects(p2));
90 check("Intersects", p2.intersects(p1));
91
92 check("Adjacent", p1.is_adjacent(a_period(-5,1)));
93 check("Adjacent", p1.is_adjacent(a_period(10,20)));
94 check("Adjacent (not)", !p1.is_adjacent(p3));
95
96 check("Is before", p1.is_before(15));
97 check("Is after", p3.is_after(15));
98
99 check("Intersection", (p1.intersection(p2) == a_period(5,10)));
100 check("Intersection", (p1.intersection(p3).is_null()));
101
102 check("Merge", p1.merge(p2) == a_period(1,30) );
103 check("Merge", p1.merge(p3).is_null());
104
105 check("Span", p3.span(p1) == a_period(1, 81));
106
107 /*** zero length period ***/
108
109 // treat a zero length period as a point
110 a_period zero_len(3,duration_type<int>(0));
111 check("Same beg & end == zero_length",
112 a_period(1,1) == a_period(1, duration_type<int>(0)));
113 check("2 point (zero length) == 1 point zero duration",
114 a_period(3,3) == zero_len);
115
116 // zero_length period always returns false for is_before & is_after
117 check("Is Before zero period", !zero_len.is_before(5));
118 check("Is After zero period (not)", !zero_len.is_after(5));
119 check("Is Before zero period (not)", !zero_len.is_before(-5));
120 check("Is After zero period", !zero_len.is_after(-5));
121
122 check("is_null", zero_len.is_null());
123 check("Contains rep (not)", !zero_len.contains(20));
124 // a null_period cannot contain any points
125 check("Contains rep", !zero_len.contains(3));
126 check("Contains period (not)", !zero_len.contains(a_period(5,8)));
127 check("Contains period", p1.contains(zero_len));
128 check("Intersects", zero_len.intersects(p1));
129 check("Intersects", p1.intersects(zero_len));
130 check("Adjacent", zero_len.is_adjacent(a_period(-10,3)));
131 check("Adjacent", a_period(-10,3).is_adjacent(zero_len));
132 check("Intersection", (zero_len.intersection(p1) == zero_len));
133 check("Span", zero_len.span(p2) == a_period(3,30));
134
135 /*** invalid period ***/
136
137 a_period null_per(5,1);
138
139 check("Is Before invalid period (always false)", !null_per.is_before(7));
140 check("Is After invalid period (always false)", !null_per.is_after(7));
141 check("Is Before invalid period (always false)", !null_per.is_before(-5));
142 check("Is After invalid period (always false)", !null_per.is_after(-5));
143
144 check("is_null", null_per.is_null());
145 check("Contains rep larger (always false)", !null_per.contains(20));
146 check("Contains rep in-between (always false)", !null_per.contains(3));
147 check("Contains period (not)", !null_per.contains(a_period(7,9)));
148 check("Contains period", p1.contains(null_per));
149 check("Intersects", null_per.intersects(p1));
150 check("Intersects", p1.intersects(null_per));
151 check("Adjacent", null_per.is_adjacent(a_period(-10,5)));
152 check("Adjacent", null_per.is_adjacent(a_period(1,10)));
153
154 // what should this next one do?
155 //check("Intersection", (null_per.intersection(p1) == zero_len));
156 check("Span", null_per.span(p3) == a_period(5,81));
157
158 {
159 std::cout << std::endl;
160 a_period p1x(0, -2);
161 check("First", p1x.begin() == 0);
162 check("Last", p1x.last() == -3);
163 check("End", p1x.end() == -2);
164 check("Length", p1x.length() == -2);
165 check("is_null", p1x.is_null());
166 }
167 {
168 std::cout << std::endl;
169 a_period p1x(0, -1);
170 check("First", p1x.begin() == 0);
171 check("Last", p1x.last() == -2);
172 check("End", p1x.end() == -1);
173 check("Length", p1x.length() == -1);
174 check("is_null", p1x.is_null());
175 }
176 {
177 std::cout << std::endl;
178 a_period p1x(0, 0);
179 check("First", p1x.begin() == 0);
180 check("Last", p1x.last() == -1);
181 check("End", p1x.end() == 0);
182 check("Length", p1x.length() == 0);
183 check("is_null", p1x.is_null());
184 }
185 {
186 std::cout << std::endl;
187 a_period p1x(0, 1);
188 check("First", p1x.begin() == 0);
189 check("Last", p1x.last() == 0);
190 check("End", p1x.end() == 1);
191 check("Length", p1x.length() == 1);
192 check("is_null", !p1x.is_null());
193 }
194 {
195 std::cout << std::endl;
196 a_period p1x(0, 2);
197 check("First", p1x.begin() == 0);
198 check("Last", p1x.last() == 1);
199 check("End", p1x.end() == 2);
200 check("Length", p1x.length() == 2);
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() == -2);
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() == -3);
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(0, duration_type<int>(0));
224 check("First", p1x.begin() == 0);
225 check("Last", p1x.last() == -1);
226 check("End", p1x.end() == 0);
227 check("Length", p1x.length() == 0);
228 check("is_null", p1x.is_null());
229 }
230 {
231 std::cout << std::endl;
232 a_period p1x(0, duration_type<int>(1));
233 check("First", p1x.begin() == 0);
234 check("Last", p1x.last() == 0);
235 check("End", p1x.end() == 1);
236 check("Length", p1x.length() == 1);
237 check("is_null", !p1x.is_null());
238 }
239 {
240 std::cout << std::endl;
241 a_period p1x(0, duration_type<int>(2));
242 check("First", p1x.begin() == 0);
243 check("Last", p1x.last() == 1);
244 check("End", p1x.end() == 2);
245 check("Length", p1x.length() == 2);
246 check("is_null", !p1x.is_null());
247 }
248 {
249 std::cout << std::endl;
250 a_period p1x(1,1); // length should be 0
251 a_period p2x(1,2); // length should be 1
252 a_period p3x(1,3); // length should be 2
253 check("Length p1", p1x.length() == 0);
254 check("Length p2", p2x.length() == 1);
255 check("Length p3", p3x.length() == 2);
256 check("is_null p1 (not)", p1x.is_null());
257 check("is_null p2 (not)", !p2x.is_null());
258 }
259
260 {
261 a_period p1x(1,2); // length should be 1
262 p1x.shift(duration_type<int>(1));
263 a_period p2x(2,3); // shifted result
264 check("shift", p1x == p2x);
265 }
266 {
267 a_period p1x(5,duration_type<int>(3));
268 a_period p2x(3,10); // expanded result
269 p1x.expand(duration_type<int>(2)); //from 2000-Jan-01--2000-Jan-04
270 check("expand", p1x == p2x);
271 }
272 return printTestStats();
273 }