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
|
/* Copyright (c) 2002,2003,2005 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: Jeff Garland, Bart Garst
*/
#include "boost/date_time/gregorian/gregorian.hpp"
#include "../testfrmwk.hpp"
int main(){
#if !defined(BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES)
// do not set this test to return fail -
// this is not necessarily a compiler problem
check("Optional gregorian types not selected - no tests run", true);
#else
using namespace boost::gregorian;
/*** months ***/
{
months m1(5), m2(3), m3(1);
check("months & months addable", months(8) == m1 + m2);
m1 += m2;
check("months & months addable", months(8) == m1);
check("months & months subtractable", months(-5) == m2 - m1);
m2 -= m1;
check("months & months subtractable", months(-5) == m2);
{
// adding and subtracting negative values
date d1(2005, Jan, 1);
date d2(2005, Feb, 1);
check("add neg months (year wrap under)",
d1 + months(-1) == date(2004,Dec,1));
check("add neg months (no year wrap under)",
d2 + months(-1) == date(2005,Jan,1));
check("add neg months (year wrap under)",
d2 + months(-2) == date(2004,Dec,1));
check("add neg months (year wrap under)",
d2 + months(-12) == date(2004,Feb,1));
check("add neg months (year wrap under)",
d2 + months(-13) == date(2004,Jan,1));
check("add neg months (year wrap under)",
d2 + months(-14) == date(2003,Dec,1));
date d3(2005, Dec, 1);
date d4(2005, Nov, 1);
check("subtract neg months (year wrap over)",
d3 - months(-1) == date(2006,Jan,1));
check("subtract neg months (no year wrap over)",
d4 - months(-1) == date(2005,Dec,1));
check("subtract neg months (year wrap over)",
d4 - months(-2) == date(2006,Jan,1));
check("subtract neg months (year wrap over)",
d4 - months(-12) == date(2006,Nov,1));
check("subtract neg months (year wrap over)",
d4 - months(-13) == date(2006,Dec,1));
check("subtract neg months (year wrap over)",
d4 - months(-14) == date(2007,Jan,1));
}
{
months m1x(5), m3x(10);
check("months & int multipliable", months(15) == m1x * 3);
m1x *= 3;
check("months & int multipliable", months(15) == m1x);
//check("int * months", months(12) == 4 * m2x);
check("months & int dividable", months(3) == m3x / 3);
m3x /= 3;
check("months & int dividable", months(3) == m3x);
}
{
months m(-5), m_pos(pos_infin), m_neg(neg_infin), m_nadt(not_a_date_time);
check("months add special_values", m + m_pos == m_pos);
check("months add special_values", m + m_neg == m_neg);
check("months add special_values", m_pos + m_neg == m_nadt);
check("months add special_values", m_neg + m_neg == m_neg);
check("months subtract special_values", m - m_pos == m_neg);
check("months subtract special_values", m - m_neg == m_pos);
check("months subtract special_values", m_pos - m_neg == m_pos);
check("months special_values & int multipliable", m_pos * -1 == m_neg);
check("months special_values & int multipliable", m_pos * 0 == m_nadt);
check("months special_values & int dividable", m_neg / 3 == m_neg);
}
years y1(2), y2(4);
check("months & years addable", months(25) == m3 + y1);
m3 += y1;
check("months & years addable", months(25) == m3);
check("months & years subtractable", months(-23) == m3 - y2);
m3 -= y2;
check("months & years subtractable", months(-23) == m3);
{
date d(2001, Oct, 31);
check("date + months", date(2002, Feb, 28) == d + months(4));
d += months(4);
check("date += months", date(2002, Feb, 28) == d);
}
{
date d(2001, Oct, 31);
check("date - months", date(2001, Apr, 30) == d - months(6));
d -= months(6);
check("date -= months", date(2001, Apr, 30) == d);
}
}
/*** years ***/
{
years y1(2), y2(4), y3(1);
check("years & years addable", years(3) == y3 + y1);
y3 += y1;
check("years & years addable", years(3) == y3);
check("years & years subtractable", years(-1) == y3 - y2);
y3 -= y2;
check("years & years subtractable", years(-1) == y3);
{
years y1x(5), y3x(10);
check("years & int multipliable", years(15) == y1x * 3);
y1x *= 3;
check("years & int multipliable", years(15) == y1x);
//check("int * years", years(12) == 4 * y2x);
check("years & int dividable", years(3) == y3x / 3);
y3x /= 3;
check("years & int dividable", years(3) == y3x);
}
{
years m(15), y_pos(pos_infin), y_neg(neg_infin), y_nadt(not_a_date_time);
check("years add special_values", m + y_pos == y_pos);
check("years add special_values", m + y_neg == y_neg);
check("years add special_values", y_pos + y_neg == y_nadt);
check("years add special_values", y_neg + y_neg == y_neg);
check("years subtract special_values", m - y_pos == y_neg);
check("years subtract special_values", m - y_neg == y_pos);
check("years subtract special_values", y_pos - y_neg == y_pos);
check("years special_values & int multipliable", y_pos * -1 == y_neg);
check("years special_values & int multipliable", y_pos * 0 == y_nadt);
check("years special_values & int dividable", y_neg / 3 == y_neg);
}
months m1(5), m2(3);
check("years & months addable", months(51) == y2 + m2);
check("years & months subtractable", months(43) == y2 - m1);
{
date d(2001, Feb, 28); // not a leap year
check("date + years", date(2004, Feb, 29) == d + years(3));
d += years(3);
check("date += years", date(2004, Feb, 29) == d);
}
{
date d(2000, Feb, 29);
check("date - years", date(1994, Feb, 28) == d - years(6));
d -= years(6);
check("date -= years", date(1994, Feb, 28) == d);
}
try {
date d1(1400, 6, 1);
const date d2 = d1 + years(8599);
check("date + many years != overflow", d2 == date(9999, 6, 1));
}
catch (...) {
check("date + many years != overflow", false);
}
try {
date d1(9999, 1, 1);
const date d2 = d1 - years(8599);
check("date - many years != overflow", d2 == date(1400, 1, 1));
}
catch (...) {
check("date - many years != overflow", false);
}
}
/*** weeks ***/
// shouldn't need many tests, it is nothing more than a date_duration
// so all date_duration tests should prove this class
{
weeks w1(2), w2(4), w3(1), pi(pos_infin);
check("add special_values", weeks(pos_infin) == w1 + pi);
check("weeks & weeks addable", weeks(3) == w3 + w1);
w3 += w1;
check("weeks & weeks addable", weeks(3) == w3);
check("weeks & weeks subtractable", weeks(-1) == w3 - w2);
w3 -= w2;
check("weeks & weeks subtractable", weeks(-1) == w3);
{
days d(10);
check("days + weeks", days(31) == d + weeks(3));
d += weeks(3);
check("days += weeks", days(31) == d);
}
{
days d(10);
check("days - weeks", days(-32) == d - weeks(6));
d -= weeks(6);
check("days -= weeks", days(-32) == d);
}
{
date d(2001, Feb, 28);
check("date + weeks", date(2001, Mar, 21) == d + weeks(3));
d += weeks(3);
check("date += weeks", date(2001, Mar, 21) == d);
}
{
date d(2001, Feb, 28);
check("date - weeks", date(2001, Jan, 17) == d - weeks(6));
d -= weeks(6);
check("date -= weeks", date(2001, Jan, 17) == d);
}
}
{
date d(2000, Oct, 31);
date d2 = d + months(4) + years(2);
date d3 = d + years(2) + months(4);
check("date + years + months", date(2003,Feb,28) == d2);
check("date + years + months", date(2003,Feb,28) == d3);
months m = years(2) + months(4) - months(4) - years(2);
check("sanity check", m.number_of_months() == 0);
}
/*{
date d(2001, Mar, 31);
date d1 = (d - months(1)) + months(1); //Mar 28, right? WRONG
// Mar31 - 1 month is Feb28 (last day of month) so Feb28 + 1 month
// will be Mar31 (last day of month)
check("date + 1 months - 1 months", date(2001,Mar,28) == d1);
std::cout << d1 << std::endl;
//date d2 = (d - months(1)) + d; //compile error, right? RIGHT
//weeks w1 = weeks(1) + months(1); //compiler error, right? RIGHT
}*/
#endif // BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES
return printTestStats();
}
|