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