testiterator.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, Bart Garst
  6. * $Date$
  7. */
  8. #include <iostream>
  9. #include "boost/date_time/posix_time/posix_time.hpp"
  10. #include "../testfrmwk.hpp"
  11. #if defined(BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS)
  12. #include "boost/date_time/gregorian/formatters_limited.hpp"
  13. #else
  14. #include "boost/date_time/gregorian/formatters.hpp"
  15. #endif
  16. void iterate_backward(const boost::posix_time::ptime *answers, int ary_len,
  17. const boost::posix_time::time_duration& td)
  18. {
  19. using namespace boost::posix_time;
  20. using namespace boost::gregorian;
  21. int i = ary_len -1;
  22. ptime end = answers[i];
  23. time_iterator titr(end,td);
  24. std::cout << "counting down by previous duration..." << std::endl;
  25. for (; titr >= answers[0]; --titr) {
  26. std::cout << to_simple_string(*titr) << std::endl;
  27. check("iterating backward", answers[i] == *titr);
  28. --i;
  29. }
  30. check("iterating backward count", i == -1); // check the number of iterations
  31. std::cout << std::endl;
  32. }
  33. int
  34. main()
  35. {
  36. using namespace boost::posix_time;
  37. using namespace boost::gregorian;
  38. date d(2000,Jan,20);
  39. ptime start(d);
  40. const ptime answer1[] = {ptime(d), ptime(d,seconds(1)),
  41. ptime(d,seconds(2)), ptime(d,seconds(3))};
  42. int i=0;
  43. time_iterator titr(start,seconds(1));
  44. for (; titr < ptime(d,seconds(4)); ++titr) {
  45. std::cout << to_simple_string(*titr) << std::endl;
  46. check("iterator -- 1 sec", answer1[i] == *titr);
  47. i++;
  48. }
  49. check("iterator -- 1 sec", i == 4); // check the number of iterations
  50. iterate_backward(answer1, 4, seconds(1));
  51. //iterate by hours
  52. const ptime answer2[] = {ptime(d), ptime(d,hours(1)),
  53. ptime(d,hours(2)), ptime(d,hours(3))};
  54. i=0;
  55. time_iterator titr2(start,hours(1));
  56. for (; titr2 < ptime(d,hours(4)); ++titr2) {
  57. std::cout << to_simple_string(*titr2) << std::endl;
  58. check("iterator -- 1 hour", answer2[i] == *titr2);
  59. i++;
  60. }
  61. check("iterator -- 1 hour", i == 4); // check the number of iterations
  62. iterate_backward(answer2, 4, hours(1));
  63. //iterate by 15 mintues
  64. const ptime answer3[] = {ptime(d), ptime(d,minutes(15)),
  65. ptime(d,minutes(30)), ptime(d,minutes(45)),
  66. ptime(d,minutes(60)), ptime(d,minutes(75))};
  67. i=0;
  68. time_iterator titr3(start,minutes(15));
  69. for (; titr3 < ptime(d,time_duration(1,20,0)); ++titr3) {
  70. std::cout << to_simple_string(*titr3) << std::endl;
  71. check("iterator -- 15 min", answer3[i] == *titr3);
  72. i++;
  73. }
  74. check("iterator -- 15 min", i == 6); // check the number of iterations
  75. iterate_backward(answer3, 6, minutes(15));
  76. //iterate by .1 seconds
  77. const ptime answer4[] = {ptime(d), ptime(d,time_duration(0,0,0,1000)),
  78. ptime(d,time_duration(0,0,0,2000)),
  79. ptime(d,time_duration(0,0,0,3000))};
  80. i=0;
  81. time_iterator titr4(start,time_duration(0,0,0,1000));
  82. for (; titr4 < ptime(d,time_duration(0,0,0,4000)); ++titr4) {
  83. std::cout << to_simple_string(*titr4) << std::endl;
  84. check("iterator -- tenth sec", answer4[i] == *titr4);
  85. i++;
  86. }
  87. check("iterator -- tenth sec", i == 4); // check the number of iterations
  88. iterate_backward(answer4, 4, time_duration(0,0,0,1000));
  89. //iterate by crazy duration
  90. time_duration crzyd = duration_from_string("2:18:32.423");
  91. const ptime answer5[] = {ptime(d), ptime(d,crzyd),
  92. ptime(d, crzyd * 2),
  93. ptime(d, crzyd * 3)};
  94. i=0;
  95. time_iterator titr5(start,crzyd);
  96. for (; titr5 < ptime(d,crzyd * 4); ++titr5) {
  97. std::cout << to_simple_string(*titr5) << std::endl;
  98. check("iterator -- crazy duration", answer5[i] == *titr5);
  99. i++;
  100. }
  101. check("iterator -- crazy duration", i == 4); // check the number of iterations
  102. iterate_backward(answer5, 4, crzyd);
  103. //iterate up by neg_dur
  104. time_duration pos_dur = minutes(3);
  105. time_duration neg_dur = -pos_dur;
  106. time_iterator up_itr(start, neg_dur), dn_itr(start, pos_dur);
  107. for(i=0; i < 10; ++i)
  108. {
  109. check("up-by-neg == backward iterate", *up_itr == *dn_itr);
  110. ++up_itr; // up by -3
  111. --dn_itr; // down by 3
  112. }
  113. return printTestStats();
  114. }