testgenerators.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Copyright (c) 2002,2003,2005 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. */
  7. #include "boost/date_time/gregorian/gregorian.hpp"
  8. #include "../testfrmwk.hpp"
  9. #include <iostream>
  10. #include <string>
  11. #include <sstream>
  12. int
  13. main()
  14. {
  15. using namespace boost::gregorian;
  16. partial_date pd1(1,Jan);
  17. date d = pd1.get_date(2000);
  18. check("Partial date to_string()", pd1.to_string() == std::string("0"));
  19. check("Partial date getdate", date(2000,1,1) == d);
  20. d = pd1.get_date(2001);
  21. check("Partial date getdate", date(2001,1,1) == d);
  22. partial_date pd2(1,Feb);
  23. check("Partial date to_string()", pd2.to_string() == std::string("31"));
  24. check("Partial date operator==", pd1 == pd1);
  25. check("Partial date operator==", !(pd1 == pd2));
  26. check("Partial date operator==", !(pd2 == pd1));
  27. check("Partial date operator<", !(pd1 < pd1));
  28. check("Partial date operator<", pd1 < pd2);
  29. check("Partial date operator<", !(pd2 < pd1));
  30. typedef last_day_of_the_week_in_month lastkday;
  31. //Find last Sunday in Feb
  32. lastkday lsif(Sunday, Feb);
  33. std::cout << to_simple_string(lsif.get_date(2002)) << std::endl; //24th
  34. check("Last kday", date(2002,Feb,24) == lsif.get_date(2002));
  35. check("Last kday to_string()", lsif.to_string() == std::string("M2.5.0"));
  36. lastkday ltif(Thursday, Feb);
  37. check("Last kday", date(2002,Feb,28) == ltif.get_date(2002));
  38. check("Last kday to_string()", ltif.to_string() == std::string("M2.5.4"));
  39. lastkday lfif(Friday, Feb);
  40. check("Last kday", date(2002,Feb,22) == lfif.get_date(2002));
  41. check("Last kday to_string()", lfif.to_string() == std::string("M2.5.5"));
  42. typedef first_day_of_the_week_in_month firstkday;
  43. firstkday fsif(Sunday, Feb);
  44. std::cout << to_simple_string(fsif.get_date(2002)) << std::endl; //24th
  45. check("First kday", date(2002,Feb,3) == fsif.get_date(2002));
  46. check("First kday to_string()", fsif.to_string() == std::string("M2.1.0"));
  47. firstkday ftif(Thursday, Feb);
  48. check("First kday", date(2002,Feb,7) == ftif.get_date(2002));
  49. check("First kday to_string()", ftif.to_string() == std::string("M2.1.4"));
  50. firstkday ffif(Friday, Feb);
  51. check("First kday", date(2002,Feb,1) == ffif.get_date(2002));
  52. check("First kday to_string()", ffif.to_string() == std::string("M2.1.5"));
  53. typedef first_day_of_the_week_after firstkdayafter;
  54. firstkdayafter fkaf(Monday);
  55. std::cout << to_simple_string(fkaf.get_date(date(2002,Feb,1)))
  56. << std::endl; //feb 4
  57. check("kday after",date(2002,Feb,4) == fkaf.get_date(date(2002,Feb,1)));
  58. firstkdayafter fkaf2(Thursday);
  59. check("kday after",date(2002,Feb,7) == fkaf2.get_date(date(2002,Feb,1)));
  60. check("kday after",date(2002,Feb,28)== fkaf2.get_date(date(2002,Feb,21)));
  61. typedef first_day_of_the_week_before firstkdaybefore;
  62. firstkdaybefore fkbf(Monday);
  63. std::cout << to_simple_string(fkaf.get_date(date(2002,Feb,10)))
  64. << std::endl; //feb 4
  65. check("kday before",date(2002,Feb,4) == fkbf.get_date(date(2002,Feb,10)));
  66. firstkdaybefore fkbf2(Thursday);
  67. check("kday before",date(2002,Jan,31) == fkbf2.get_date(date(2002,Feb,1)));
  68. check("kday before",date(2002,Feb,7)== fkbf2.get_date(date(2002,Feb,14)));
  69. typedef nth_day_of_the_week_in_month nthkdayofmonth;
  70. nthkdayofmonth nkd1(nthkdayofmonth::third, Sunday, Jul);
  71. check("nth_kday 1", date(1969, Jul, 20) == nkd1.get_date(1969));
  72. check("Nth kday to_string()", nkd1.to_string() == std::string("M7.3.0"));
  73. nthkdayofmonth nkd2(nthkdayofmonth::second, Monday, Dec);
  74. check("nth_kday 2", date(1980, Dec, 8) == nkd2.get_date(1980));
  75. check("Nth kday to_string()", nkd2.to_string() == std::string("M12.2.1"));
  76. nthkdayofmonth nkd3(nthkdayofmonth::fifth, Wednesday, Jan);
  77. check("nth_kday fifth wed jan 2003 2003-Jan-29",
  78. date(2003, Jan, 29) == nkd3.get_date(2003));
  79. check("Nth kday to_string()", nkd3.to_string() == std::string("M1.5.3"));
  80. nthkdayofmonth nkd4(nthkdayofmonth::fifth, Monday, Jan);
  81. check("nth_kday fifth mon jan 2003 (actaully 4th) 2003-Jan-27",
  82. date(2003, Jan, 27) == nkd4.get_date(2003));
  83. check("Nth kday to_string()", nkd4.to_string() == std::string("M1.5.1"));
  84. // greg date_generator functions tests
  85. {
  86. date sunday(2003,Feb,2),tuesday(2003,Feb,4);
  87. date friday(2003,Feb,7),saturday(2003,Feb,8);
  88. greg_weekday sat(Saturday), tue(Tuesday), fri(Friday), sund(Sunday);
  89. check("Days until weekday" , days_until_weekday(saturday, sund) == days(1));
  90. check("Days until weekday" , days_until_weekday(friday, tue) == days(4));
  91. check("Days until weekday" , days_until_weekday(tuesday, fri) == days(3));
  92. check("Days until weekday" , days_until_weekday(sunday, sat) == days(6));
  93. check("Days until weekday" , days_until_weekday(sunday, sund) == days(0));
  94. check("Days until weekday" , days_until_weekday(tuesday, tue) == days(0));
  95. check("Days before weekday" , days_before_weekday(saturday, sund) == days(6));
  96. check("Days before weekday" , days_before_weekday(friday, tue) == days(3));
  97. check("Days before weekday" , days_before_weekday(tuesday, fri) == days(4));
  98. check("Days before weekday" , days_before_weekday(sunday, sat) == days(1));
  99. check("Days before weekday" , days_before_weekday(sunday, sund) == days(0));
  100. check("Days before weekday" , days_before_weekday(tuesday, tue) == days(0));
  101. check("Date of next weekday", next_weekday(saturday, sund)== date(2003,Feb,9));
  102. check("Date of next weekday", next_weekday(friday, tue) == date(2003,Feb,11));
  103. check("Date of next weekday", next_weekday(tuesday, fri) == date(2003,Feb,7));
  104. check("Date of next weekday", next_weekday(sunday, sat) == date(2003,Feb,8));
  105. check("Date of next weekday", next_weekday(sunday, sund) == sunday);
  106. check("Date of next weekday", next_weekday(tuesday, tue) == tuesday);
  107. check("Date of previous weekday", previous_weekday(saturday, sund)== date(2003,Feb,2));
  108. check("Date of previous weekday", previous_weekday(friday, tue) == date(2003,Feb,4));
  109. check("Date of previous weekday", previous_weekday(tuesday, fri) == date(2003,Jan,31));
  110. check("Date of previous weekday", previous_weekday(sunday, sat) == date(2003,Feb,1));
  111. check("Date of previous weekday", previous_weekday(sunday, sund) == sunday);
  112. check("Date of previous weekday", previous_weekday(tuesday, tue) == tuesday);
  113. }
  114. #ifndef BOOST_DATE_TIME_NO_LOCALE
  115. #if !defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
  116. //TODO: this is temporary condition -- don't force a failure...
  117. // check("no streaming implemented for new facet", false);
  118. #else
  119. // streaming tests...
  120. std::stringstream ss("");
  121. std::string s("");
  122. ss.str("");
  123. ss << pd1;
  124. s = "01 Jan";
  125. check("streaming partial_date", ss.str() == s);
  126. std::cout << ss.str() << std::endl;
  127. ss.str("");
  128. ss << lsif;
  129. s = "last Sun of Feb";
  130. check("streaming last_kday_of_month", ss.str() == s);
  131. ss.str("");
  132. ss << fsif;
  133. s = "first Sun of Feb";
  134. check("streaming first_kday_of_month", ss.str() == s);
  135. ss.str("");
  136. ss << fkaf;
  137. s = "Mon after";
  138. check("streaming first_kday_after", ss.str() == s);
  139. ss.str("");
  140. ss << fkbf;
  141. s = "Mon before";
  142. check("streaming first_kday_before", ss.str() == s);
  143. ss.str("");
  144. ss << nkd1;
  145. s = "third Sun of Jul";
  146. check("streaming nth_kday", ss.str() == s);
  147. #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
  148. #endif // NO_LOCAL
  149. return printTestStats();
  150. }