File: testdate_iterator.cpp

package info (click to toggle)
boost 1.33.1-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 100,948 kB
  • ctags: 145,103
  • sloc: cpp: 573,492; xml: 49,055; python: 15,626; ansic: 13,588; sh: 2,099; yacc: 858; makefile: 660; perl: 427; lex: 111; csh: 6
file content (367 lines) | stat: -rw-r--r-- 12,317 bytes parent folder | download | duplicates (3)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* 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"
#include <iostream>


void test_month_decrement_iterator(const boost::gregorian::date *Answers, int array_len){
  using namespace boost::gregorian;
  typedef boost::date_time::month_functor<date> mfg;
    
  boost::date_time::date_itr<mfg, date> ditr(Answers[array_len-1]);
  int i = array_len-1;
  std::cout << "month iter decrement test..." << std::endl;
  try { 
    for (; ditr > Answers[0] - date_duration(1); --ditr) {
      check("month iterator: " + to_iso_string(*ditr), Answers[i] == *ditr);
      i--;
    }
    check("month iterator iteration count", i == -1);
  }
  catch(std::exception& e) 
  {
    check("month iterator: exception failure", false);      
    std::cout << e.what() << std::endl;
  }
}

void test_base_iterator(boost::gregorian::date end,
                        boost::gregorian::date_iterator& di,
                        std::string& data)
{
  using namespace boost::gregorian;
  for (; di < end; ++di) {
    data += to_iso_string(*di) + " ";
  }
}

int
main() 
{
  using namespace boost::gregorian;

  day_iterator di(date(2002,Jan,1));
  std::string data;
  test_base_iterator(date(2002,Jan,3),di,data);
  month_iterator di2(date(2002,Jan,3));
  test_base_iterator(date(2002,Mar,1),di2,data);
  std::string result("20020101 20020102 20020103 20020203 ");
  check("base iterator", data == result); 
  std::cout << data << std::endl;


  typedef boost::date_time::day_functor<date> dfg;
  
  {
    const date DayAnswers[] = {date(2000,Jan,20),date(2000,Jan,22),date(2000,Jan,24)};
    boost::date_time::date_itr<dfg,date> ditr(date(2000,Jan,20),2);
    int i=0;
    for (; ditr < date(2000,Jan,25); ++ditr) {
      //std::cout << *ditr << " ";
      check("day iterator -- 2 days", DayAnswers[i] == *ditr);
      i++;
    }
    check("day iterator -- 2 days", i == 3); // check the number of iterations
    // decrement
    i = 2;
    --ditr;
    for (; ditr > date(2000,Jan,19); --ditr) {
      //std::cout << *ditr << " ";
      check("day iterator decrement -- 2 days", DayAnswers[i] == *ditr);
      i--;
    }
    check("day iterator decrement -- 2 days", i == -1); // check the number of iterations
  }

  typedef boost::date_time::week_functor<date> wfg;
  {
    const date WeekAnswers[] = {date(2000,Jan,20),date(2000,Jan,27),date(2000,Feb,3)};
    boost::date_time::date_itr<wfg, date> ditr(date(2000,Jan,20));
    int i=0;
    for (; ditr < date(2000,Feb,6); ++ditr) {
      //std::cout << *ditr << " ";
      check("week iterator", WeekAnswers[i] == *ditr);
      i++;
    }
    check("week iterator", i == 3);
    // decrement
    i=2;
    --ditr;
    for (; ditr > date(2000,Jan,19); --ditr) {
      //std::cout << *ditr << " ";
      check("week iterator", WeekAnswers[i] == *ditr);
      i--;
    }
    check("week iterator", i == -1);
  }

  {
    const date WeekAnswers[] = {date(2000,Jan,20),date(2000,Feb,3)};
    boost::date_time::date_itr<wfg, date> ditr(date(2000,Jan,20),2);
    int i=0;
    for (; ditr < date(2000,Feb,6); ++ditr) {
      //std::cout << *ditr << " ";
      check("week iterator", WeekAnswers[i] == *ditr);
      i++;
    }
    check("week iterator", i == 2);
    // decrement
    i=1;
    --ditr;
    for (; ditr > date(2000,Jan,19); --ditr) {
      //std::cout << *ditr << " ";
      check("week iterator", WeekAnswers[i] == *ditr);
      i--;
    }
    check("week iterator", i == -1);
  }

  {
    const date WeekAnswers[] = {date(2000,Jan,20),date(2000,Feb,3), date(2000,Feb,17)};
    boost::date_time::date_itr<wfg, date> ditr(date(2000,Jan,20),2);
    int i=0;
    for (; ditr < date(2000,Feb,20); ++ditr) {
      //std::cout << *ditr << " ";
      check("week iterator -- 2 weeks", WeekAnswers[i] == *ditr);
      i++;
    }
    check("week iterator -- 2 weeks", i == 3);
    // decrement
    i=2;
    --ditr;
    for (; ditr > date(2000,Jan,19); --ditr) {
      //std::cout << *ditr << " ";
      check("week iterator -- 2 weeks", WeekAnswers[i] == *ditr);
      i--;
    }
    check("week iterator -- 2 weeks", i == -1);
  }

  typedef boost::date_time::month_functor<date> mfg;
  {
    const date MonthAnswers[] = {
      date(2000,Jan,1),date(2000,Feb,1),date(2000,Mar,1),date(2000,Apr,1),
      date(2000,May,1),date(2000,Jun,1),date(2000,Jul,1),date(2000,Aug,1),
      date(2000,Sep,1),date(2000,Oct,1),date(2000,Nov,1),date(2000,Dec,1),
      date(2001,Jan,1)
    };
    test_month_decrement_iterator(MonthAnswers, 13);
    
    boost::date_time::date_itr<mfg, date> ditr(date(2000,Jan,1));
    int i = 0;
    try { 
      for (; ditr < date(2001,Jan,2); ++ditr) {
        check("month iterator: " + to_iso_string(*ditr), MonthAnswers[i] == *ditr);
        i++;
      }
      check("month iterator iteration count", i == 13);
    }
    catch(std::exception& e) 
    {
      check("month iterator: exception failure", false);      
      std::cout << e.what() << std::endl;
    }
  }

  {
    const date MonthAnswers[] = {
      date(2000,Jan,31),date(2000,Feb,29),date(2000,Mar,31),date(2000,Apr,30),
      date(2000,May,31),date(2000,Jun,30),date(2000,Jul,31),date(2000,Aug,31),
      date(2000,Sep,30),date(2000,Oct,31),date(2000,Nov,30),date(2000,Dec,31),
      date(2001,Jan,31)
    };
    test_month_decrement_iterator(MonthAnswers, 13);
    
    boost::date_time::date_itr<mfg, date> ditr(date(2000,Jan,31));
    int i = 0;
    try { 
      for (; ditr < date(2001,Feb,1); ++ditr) {
        //      std::cout << *ditr << " ";
        check("last day of month iterator: " + to_iso_string(*ditr), 
              MonthAnswers[i] == *ditr);
        //check("last day of month iterator", MonthAnswers[i] == *ditr);
        i++;
      }
      check("last day of month iterator", i == 13);
    }
    catch(std::exception& e) 
    {
      check("last day of month iterator: exception failure", false);      
      std::cout << e.what() << std::endl;
    }
  }

  {
    const date MonthAnswers[] = {
      date(2000,Feb,29),date(2000,Mar,31),date(2000,Apr,30),
      date(2000,May,31),date(2000,Jun,30),date(2000,Jul,31),date(2000,Aug,31),
      date(2000,Sep,30),date(2000,Oct,31),date(2000,Nov,30),date(2000,Dec,31),
      date(2001,Jan,31),date(2001,Feb,28)
    };
    test_month_decrement_iterator(MonthAnswers, 13);
    
    boost::date_time::date_itr<mfg, date> ditr(date(2000,Feb,29));
    int i = 0;
    try { 
      for (; ditr < date(2001,Mar,1); ++ditr) {
        //      std::cout << *ditr << " ";
        check("last day of month iterator2: " + to_iso_string(*ditr), 
              MonthAnswers[i] == *ditr);
        //check("last day of month iterator", MonthAnswers[i] == *ditr);
        i++;
      }
      check("last day of month iterator2", i == 13);
    }
    catch(std::exception& e) 
    {
      check("last day of month iterator: exception failure", false);      
      std::cout << e.what() << std::endl;
    }
  }

  { // running a span of 5 years to verify snap to end doesn't occur at next leap year
    const date MonthAnswers[] = {
      date(2000,Feb,28),date(2000,Mar,28),date(2000,Apr,28),date(2000,May,28),
      date(2000,Jun,28),date(2000,Jul,28),date(2000,Aug,28),date(2000,Sep,28),
      date(2000,Oct,28),date(2000,Nov,28),date(2000,Dec,28),date(2001,Jan,28),
      date(2001,Feb,28),date(2001,Mar,28),date(2001,Apr,28),date(2001,May,28),
      date(2001,Jun,28),date(2001,Jul,28),date(2001,Aug,28),date(2001,Sep,28),
      date(2001,Oct,28),date(2001,Nov,28),date(2001,Dec,28),date(2002,Jan,28),
      date(2002,Feb,28),date(2002,Mar,28),date(2002,Apr,28),date(2002,May,28),
      date(2002,Jun,28),date(2002,Jul,28),date(2002,Aug,28),date(2002,Sep,28),
      date(2002,Oct,28),date(2002,Nov,28),date(2002,Dec,28),date(2003,Jan,28),
      date(2003,Feb,28),date(2003,Mar,28),date(2003,Apr,28),date(2003,May,28),
      date(2003,Jun,28),date(2003,Jul,28),date(2003,Aug,28),date(2003,Sep,28),
      date(2003,Oct,28),date(2003,Nov,28),date(2003,Dec,28),date(2004,Jan,28),
      date(2004,Feb,28),date(2004,Mar,28),date(2004,Apr,28),date(2004,May,28),
      date(2004,Jun,28),date(2004,Jul,28),date(2004,Aug,28),date(2004,Sep,28),
      date(2004,Oct,28),date(2004,Nov,28),date(2004,Dec,28),date(2005,Jan,28),
    };
    test_month_decrement_iterator(MonthAnswers, 60);
    
    boost::date_time::date_itr<mfg, date> ditr(date(2000,Feb,28));
    int i = 0;
    try { 
      for (; ditr < date(2005,Feb,1); ++ditr) {
        //      std::cout << *ditr << " ";
        check("last day of month iterator3: " + to_iso_string(*ditr), 
              MonthAnswers[i] == *ditr);
        //check("last day of month iterator", MonthAnswers[i] == *ditr);
        i++;
      }
      check("last day of month iterator3", i == 60);
    }
    catch(std::exception& e) 
    {
      check("last day of month iterator: exception failure", false);      
      std::cout << e.what() << std::endl;
    }
  }

  typedef boost::date_time::year_functor<date> yfg;
  {
    const date YearAnswers[] = {
      date(2000,Jan,1),date(2001,Jan,1),date(2002,Jan,1),date(2003,Jan,1),
      date(2004,Jan,1),date(2005,Jan,1),date(2006,Jan,1),date(2007,Jan,1),
      date(2008,Jan,1),date(2009,Jan,1),date(2010,Jan,1)
    };
    
    boost::date_time::date_itr<yfg, date> d3(date(2000,Jan,1));
    int i = 0;
    for (; d3 < date(2010,Jan,2); ++d3) {
      //std::cout << *d3 << " ";
      check("year iterator: " + to_iso_string(*d3), YearAnswers[i] == *d3);
      i++;
    }
    std::cout << "Decrementing...." << std::endl;
    i = 10;
    --d3;
    for (; d3 > date(1999,Dec,31); --d3) {
      //std::cout << *d3 << " ";
      check("year iterator: " + to_iso_string(*d3), YearAnswers[i] == *d3);
      i--;
    }
 }
  { // WON'T snap top end of month
    const date YearAnswers[] = {
      date(2000,Feb,28),date(2001,Feb,28),date(2002,Feb,28),date(2003,Feb,28),
      date(2004,Feb,28),date(2005,Feb,28),date(2006,Feb,28),date(2007,Feb,28),
      date(2008,Feb,28),date(2009,Feb,28),date(2010,Feb,28)
    };
    
    boost::date_time::date_itr<yfg, date> d3(date(2000,Feb,28));
    int i = 0;
    for (; d3 < date(2010,Mar,1); ++d3) {
      //std::cout << *d3 << " ";
      check("year iterator: " + to_iso_string(*d3), YearAnswers[i] == *d3);
      i++;
    }
    std::cout << "Decrementing...." << std::endl;
    i = 10;
    --d3;
    for (; d3 > date(2000,Feb,27); --d3) {
      //std::cout << *d3 << " ";
      check("year iterator: " + to_iso_string(*d3), YearAnswers[i] == *d3);
      i--;
    }
 }
  {// WILL snap top end of month
    const date YearAnswers[] = {
      date(2000,Feb,29),date(2001,Feb,28),date(2002,Feb,28),date(2003,Feb,28),
      date(2004,Feb,29),date(2005,Feb,28),date(2006,Feb,28),date(2007,Feb,28),
      date(2008,Feb,29),date(2009,Feb,28),date(2010,Feb,28)
    };
    
    boost::date_time::date_itr<yfg, date> d3(date(2000,Feb,29));
    int i = 0;
    for (; d3 < date(2010,Mar,1); ++d3) {
      //std::cout << *d3 << " ";
      check("year iterator: " + to_iso_string(*d3), YearAnswers[i] == *d3);
      i++;
    }
    std::cout << "Decrementing...." << std::endl;
    i = 10;
    --d3;
    for (; d3 > date(2000,Feb,27); --d3) {
      //std::cout << *d3 << " ";
      check("year iterator: " + to_iso_string(*d3), YearAnswers[i] == *d3);
      i--;
    }
 }

  {
    std::cout << "Increment by 2 years...." << std::endl;
    const date YearAnswers[] = {
      date(2000,Jan,1),date(2002,Jan,1),
      date(2004,Jan,1),date(2006,Jan,1),
      date(2008,Jan,1),date(2010,Jan,1)
    };
    
    boost::date_time::date_itr<yfg, date> d3(date(2000,Jan,1),2);
    int i = 0;
    for (; d3 < date(2010,Jan,2); ++d3) {
      //std::cout << *d3 << " ";
      check("year iterator: " + to_iso_string(*d3), YearAnswers[i] == *d3);
      i++;
    }
    // decrement
    std::cout << "Decrementing...." << std::endl;
    i = 5;
    --d3;
    for (; d3 > date(1999,Dec,31); --d3) {
      //std::cout << *d3 << " ";
      check("year iterator: " + to_iso_string(*d3), YearAnswers[i] == *d3);
      i--;
    }
  }


  return printTestStats();
}