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