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: Jeff Garland
6 */
7
8 #include "boost/date_time/gregorian/gregorian.hpp"
9 #include "../testfrmwk.hpp"
10 #include <iostream>
11
main()12 int main()
13 {
14 using namespace boost::gregorian;
15 date d1(2000,Jan,1),d2(2000,Jan,4);
16 date d3 = d2;
17 check("assignment", d3 == d2);
18 date_period p1(d1,d2);
19 date_period p2(d1,date_duration(3));
20 check("construction and ==", p1 == p2);
21 check("begin", p1.begin() == d1);
22 check("last", p1.last() == d2-date_duration(1) );
23 check("end", p1.end() == d2);
24 check("length", p2.length() == date_duration(3));
25 check("contains begin", p1.contains(d1));
26 check("contains last", !p1.contains(d2));
27 date_period p3(date(2000,Jan,4),date(2000,Feb,1));
28 check("operator== not equal case", !(p1 == p3));
29 check("less than order", p1 < p3);
30 check("greater than order", p3 > p1);
31 check("not equal", p3 != p1);
32 check("intersects with myself", p1.intersects(p1));
33 check("not intersects", !(p1.intersects(p3)));
34 check("not intersects", !(p3.intersects(p1)));
35 date_period p4(date(1999,Dec,1), d2);
36 check("intersects", p1.intersects(p4));
37 check("intersects", p4.intersects(p1));
38 date_period p5(date(1999,Dec,1), date(2000,Dec,31));
39 check("intersects", p1.intersects(p5));
40 check("intersects", p5.intersects(p1));
41 date_period p6(date(2000,Jan,1),date(2000,Dec,31));
42 check("contains period", p5.contains(p6));
43 check("contains period equal", p6.contains(p6));
44 check("not contains period", !p6.contains(p5));
45
46 //shift test
47 date_duration fourDays(4);
48 p1.shift(fourDays); //from 2000-Jan-01--2000-Jan-04
49 date_period shifted(date(2000,Jan,5),date(2000,Jan,8));
50 // std::cout << to_string(p1.begin()) <<"--"
51 // << to_string(p1.last()) << std::endl;
52 check("shift", p1 == shifted);
53
54 //expand the date period
55 date_period p10(date(2000,Jan,5),date(2000,Jan,8));
56 p10.expand(days(2)); //from 2000-Jan-01--2000-Jan-04
57 check("expand", p10 == date_period(date(2000,Jan,3),date(2000,Jan,10)));
58
59 //intersection tests
60 date_period i1(date(2000,Jan,5), date(2000,Jan,10));
61 date_period i2(date(2000,Jan,1), date(2000,Jan,7));
62 date_period r1(date(2000,Jan,5), date(2000,Jan,7));
63 //case 1 [5 -10) intersect [1-7) -> [5-7)
64 std::cout << i1.intersection(i2) << std::endl;
65 check("intersect case1", i1.intersection(i2) == r1);
66 check("intersect case1", i2.intersection(i1) == r1);
67 //case 2 [5 -10) intersect [1-15) -> [5-10)
68 date_period i3(date(2000,Jan,1), date(2000,Jan,15));
69 check("intersect case2", i1.intersection(i3) == i1);
70 check("intersect case2", i3.intersection(i1) == i1);
71 //case 3 [5-10) intersect [7-15) -> [7-10)
72 date_period i4(date(2000,Jan,7), date(2000,Jan,10));
73 date_period r2(date(2000,Jan,7), date(2000,Jan,10));
74 check("intersect case3", i1.intersection(i4) == r2);
75 check("intersect case3", i4.intersection(i1) == r2);
76 //case 4 [5-10) intersect [6-9) -> [6-9)
77 date_period i5(date(2000,Jan,6), date(2000,Jan,9));
78 check("intersect case4", i1.intersection(i5) == i5);
79 check("intersect case4", i5.intersection(i1) == i5);
80 //case 5 no intersection [1-7) intersect [7-10)
81 check("no intersection", i2.intersection(i4).is_null());
82
83 //case 1 [5 -10) merge [1-7) -> [1-10)
84 date_period r3(date(2000,Jan,1), date(2000,Jan,10));
85 // std::cout << to_iso_string(i1.merge(i2).begin()) << "/" << to_iso_string(i1.merge(i2).last()) << std::endl;
86 check("[5 -10) merge [1-7) -> [1-10)", i1.merge(i2) == r3);
87 check("[1 -7) merge [7-10) -> null", i2.merge(i4).is_null());
88 date_period r4(date(2000,Jan,5), date(2000,Jan,10));
89 check("[5 -10) merge [6-9) -> [5-10)", i1.merge(i5) == r4);
90
91 check("[5-10) span [1-7) -> [1-10)", i1.span(i2) == r3);
92 check("[1-7) span [7-10) -> [1-10)", i2.span(i4) == r3);
93 check("[7-10) span [1-7) -> [1-10)", i4.span(i2) == r3);
94 check("[1-15) span [1-7) -> [1-15)", i3.span(i2) == i3);
95
96 date_period i6(date(2000,Jan,1), date(2000,Jan,2));
97 check("[1-2) span [7-10) -> [1-10)", i6.span(i4) == r3);
98 check("[7-10) span [1-2) -> [1-10)", i4.span(i6) == r3);
99
100
101 date bf_start(2000,Jan,5);
102 date bf_end(2000,Jan,10);
103 date bf_before(2000,Jan,4); //is before the period
104 date bf_after(2000,Jan,11); //is really after
105 date bf_during(2000, Jan, 7);
106 date_period bfp1(bf_start, bf_end); //[2000-Jan-5 - 2000-Jan10)
107
108 check("is before -- start boundary", !bfp1.is_before(bf_start));
109 check("is before -- end boundary", bfp1.is_before(bf_end));
110 check("is before -- last boundary", !bfp1.is_before(bfp1.last()));
111 check("is before -- false", !bfp1.is_before(bf_before));
112 check("is before -- false", !bfp1.is_before(bf_during));
113 check("is before -- true", bfp1.is_before(bf_after));
114
115 check("is after -- start boundary", !bfp1.is_after(bf_start));
116 check("is after -- end boundary", !bfp1.is_after(bf_end));
117 check("is after -- last boundary", !bfp1.is_after(bfp1.last()));
118 check("is after -- true", bfp1.is_after(bf_before));
119 check("is after -- false", !bfp1.is_after(bf_during));
120 check("is after -- false", !bfp1.is_after(bf_after));
121
122 //adjacent tests
123 /*
124 [5-----10) adj1
125 [1----5) adj2
126 [7-----12) adj3
127 [12----15) adj4
128 [1-3) adj5
129 [7-9) adj6
130 */
131 date_period adj1(date(2000,Jan,5), date(2000,Jan,10));
132 date_period adj2(date(2000,Jan,1), date(2000,Jan,5));
133 date_period adj3(date(2000,Jan,7), date(2000,Jan,12));
134 date_period adj4(date(2000,Jan,12), date(2000,Jan,15));
135 date_period adj5(date(2000,Jan,1), date(2000,Jan,3));
136 date_period adj6(date(2000,Jan,7), date(2000,Jan,9));
137
138 check("is adjacent -- adj1-->adj2", adj1.is_adjacent(adj2));
139 check("is adjacent -- adj2-->adj1", adj2.is_adjacent(adj1));
140 check("is adjacent -- adj1-->adj3", !adj1.is_adjacent(adj3));
141 check("is adjacent -- adj3-->adj1", !adj3.is_adjacent(adj1));
142 check("is adjacent -- adj1-->adj4", !adj1.is_adjacent(adj4));
143 check("is adjacent -- adj4-->adj1", !adj4.is_adjacent(adj1));
144 check("is adjacent -- adj1-->adj5", !adj1.is_adjacent(adj5));
145 check("is adjacent -- adj5-->adj1", !adj5.is_adjacent(adj1));
146 check("is adjacent -- adj1-->adj6", !adj1.is_adjacent(adj6));
147 check("is adjacent -- adj6-->adj1", !adj6.is_adjacent(adj1));
148
149 printTestStats();
150
151 return 0;
152 }
153
154