File: testgreg_durations.cpp

package info (click to toggle)
boost 1.32.0-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 93,952 kB
  • ctags: 128,458
  • sloc: cpp: 492,477; xml: 52,125; python: 13,519; ansic: 13,013; sh: 1,773; yacc: 853; makefile: 526; perl: 418; lex: 110; csh: 6
file content (192 lines) | stat: -rw-r--r-- 6,884 bytes parent folder | download
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
/* 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 or http://www.boost.org/LICENSE-1.0)
 * Author: Jeff Garland, Bart Garst
 */

#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/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);
    {
      months m1(5), m2(3), m3(10);
      check("months & int multipliable", months(15) == m1 * 3);
      m1 *= 3;
      check("months & int multipliable", months(15) == m1);
      //check("int * months", months(12) == 4 * m2);
      check("months & int dividable", months(3) == m3 / 3);
      m3 /= 3;
      check("months & int dividable", months(3) == m3);
    }
    {
      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 y1(5), y2(3), y3(10);
      check("years & int multipliable", years(15) == y1 * 3);
      y1 *= 3;
      check("years & int multipliable", years(15) == y1);
      //check("int * years", years(12) == 4 * y2);
      check("years & int dividable", years(3) == y3 / 3);
      y3 /= 3;
      check("years & int dividable", years(3) == y3);
    }
    {
      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);
    }

  }
  
  /*** 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();
  
}