📄 testdate_input_facet.cpp
字号:
/* Copyright (c) 2005 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
* $Date: 2005/07/01 03:00:34 $
*/
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/testfrmwk.hpp"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#ifndef USE_DATE_TIME_PRE_1_33_FACET_IO
// for tests that are expected to fail and throw exceptions
template<class temporal_type, class exception_type>
bool failure_test(temporal_type component,
const std::string& input,
exception_type /*except*/,
boost::gregorian::date_input_facet* facet)
{
using namespace boost::gregorian;
bool result = false;
std::istringstream iss(input);
iss.exceptions(std::ios_base::failbit); // turn on exceptions
iss.imbue(std::locale(std::locale::classic(), facet));
try {
iss >> component;
}
catch(exception_type e) {
std::cout << "Expected exception caught: \""
<< e.what() << "\"" << std::endl;
result = iss.fail(); // failbit must be set to pass test
}
catch(...) {
result = false;
}
return result;
}
// for tests that are expected to fail quietly
template<class temporal_type>
bool failure_test(temporal_type component,
const std::string& input,
boost::gregorian::date_input_facet* facet)
{
using namespace boost::gregorian;
std::istringstream iss(input);
/* leave exceptions turned off
* iss.exceptions(std::ios_base::failbit); */
iss.imbue(std::locale(std::locale::classic(), facet));
try {
iss >> component;
}
catch(...) {
std::cout << "Caught unexpected exception" << std::endl;
return false;
}
return iss.fail(); // failbit must be set to pass test
}
#endif
int main(){
#ifndef USE_DATE_TIME_PRE_1_33_FACET_IO
using namespace boost::gregorian;
{
// verify no extra character are consumed
greg_month m(1);
std::stringstream ss("Mar.");
std::istreambuf_iterator<char> sitr(ss), str_end;
date_input_facet* f = new date_input_facet();
f->get(sitr, str_end, ss, m);
check("No extra characters consumed", m = greg_month(Mar) && *sitr == '.');
}
// set up initial objects
date d(not_a_date_time);
days dd(not_a_date_time);
greg_month m(1);
greg_weekday gw(0);
greg_day gd(1);
greg_year gy(2000);
// exceptions for failure_tests
std::ios_base::failure e_failure("default");
bad_month e_bad_month;
bad_year e_bad_year;
bad_day_of_month e_bad_day_of_month;
bad_weekday e_bad_weekday;
bad_day_of_year e_bad_day_of_year;
// default format tests: date, days, month, weekday, day, year
std::istringstream iss("2005-Jan-15 21 Feb Tue 4 2002");
iss >> d;
check("Default format date", d == date(2005,Jan,15));
iss >> dd;
check("Default (only) format positive days", dd == days(21));
iss >> m;
check("Default format month", m == greg_month(2));
iss >> gw;
check("Default format weekday", gw == greg_weekday(2));
iss >> gd;
check("Default (only) format day of month", gd == greg_day(4));
iss >> gy;
check("Default format year", gy == greg_year(2002));
// failure tests
check("Input Misspelled in year (date) w/exceptions",
failure_test(d, "205-Jan-15", e_bad_year, new date_input_facet()));
check("Input Misspelled in year (date) no-exceptions",
failure_test(d, "205-Jan-15", new date_input_facet()));
check("Input Misspelled in month (date) w/exceptions",
failure_test(d, "2005-Jsn-15", e_bad_month, new date_input_facet()));
check("Input Misspelled in month (date) no-exceptions",
failure_test(d, "2005-Jsn-15", new date_input_facet()));
check("Input Misspelled in day (date) w/exceptions",
failure_test(d, "2005-Jan-51", e_bad_day_of_month, new date_input_facet()));
check("Input Misspelled in day (date) no-exceptions",
failure_test(d, "2005-Jan-51", new date_input_facet()));
check("Input Misspelled greg_weekday w/exceptions",
failure_test(gw, "San", e_bad_weekday, new date_input_facet()));
check("Input Misspelled greg_weekday no-exceptions",
failure_test(gw, "San", new date_input_facet()));
check("Input Misspelled month w/exceptions",
failure_test(m, "Jsn", e_bad_month, new date_input_facet()));
check("Input Misspelled month no-exceptions",
failure_test(m, "Jsn", new date_input_facet()));
check("Bad Input greg_day w/exceptions",
failure_test(gd, "Sun", e_bad_day_of_month, new date_input_facet()));
check("Bad Input greg_day no-exceptions",
failure_test(gd, "Sun", new date_input_facet()));
check("Input Misspelled greg_year w/exceptions",
failure_test(gy, "205", e_bad_year, new date_input_facet()));
check("Input Misspelled greg_year no-exceptions",
failure_test(gy, "205", new date_input_facet()));
// change to full length names, iso date format, and 2 digit year
date_input_facet* facet = new date_input_facet();
facet->set_iso_format();
facet->month_format("%B");
facet->weekday_format("%A");
facet->year_format("%y");
iss.str("20050115 -55 February Tuesday 02");
iss.imbue(std::locale(std::locale::classic(), facet));
iss >> d;
check("ISO format date", d == date(2005,Jan,15));
iss >> dd;
check("Default (only) format negative days", dd == days(-55));
iss >> m;
check("Full format month", m == greg_month(2));
iss >> gw;
check("Full format weekday", gw == greg_weekday(2));
iss >> gy;
check("2 digit format year", gy == greg_year(2002));
{ // literal % in format tests
date d(not_a_date_time);
greg_month m(1);
greg_weekday gw(0);
greg_year y(1400);
date_input_facet* f = new date_input_facet("%%d %Y-%b-%d");
std::stringstream ss;
ss.imbue(std::locale(ss.getloc(), f));
ss.str("%d 2005-Jun-14");
ss >> d;
check("Literal '%' in date format", d == date(2005,Jun,14));
f->format("%%%d %Y-%b-%d");
ss.str("%14 2005-Jun-14");
ss >> d;
check("Multiple literal '%'s in date format", d == date(2005,Jun,14));
f->month_format("%%b %b");
ss.str("%b Jun");
ss >> m;
check("Literal '%' in month format", m == greg_month(6));
f->month_format("%%%b");
ss.str("%Jun");
ss >> m;
check("Multiple literal '%'s in month format", m == greg_month(6));
f->weekday_format("%%a %a");
ss.str("%a Tue");
ss >> gw;
check("Literal '%' in weekday format", gw == greg_weekday(2));
f->weekday_format("%%%a");
ss.str("%Tue");
ss >> gw;
check("Multiple literal '%'s in weekday format", gw == greg_weekday(2));
f->year_format("%%Y %Y");
ss.str("%Y 2005");
ss >> y;
check("Literal '%' in year format", y == greg_year(2005));
f->year_format("%%%Y");
ss.str("%2005");
ss >> y;
check("Multiple literal '%'s in year format", y == greg_year(2005));
}
// All days, month, weekday, day, and year formats have been tested
// begin testing other date formats
facet->set_iso_extended_format();
iss.str("2005-01-15");
iss >> d;
check("ISO Extended format date", d == date(2005,Jan,15));
facet->format("%B %d, %Y");
iss.str("March 15, 2006");
iss >> d;
check("Custom date format: \"%B %d, %Y\" => 'March 15, 2006'",
d == date(2006,Mar,15));
facet->format("%Y-%j"); // Ordinal format ISO8601(2000 sect 5.2.2.1 extended)
iss.str("2006-074");
iss >> d;
check("Custom date format: \"%Y-%j\" => '2006-074'",
d == date(2006,Mar,15));
check("Bad input Custom date format: \"%Y-%j\" => '2006-74' (w/exceptions)",
failure_test(d, "2006-74", e_bad_day_of_year, facet));
check("Bad input Custom date format: \"%Y-%j\" => '2006-74' (no exceptions)",
failure_test(d, "2006-74", facet));
// date_period tests
// A date_period is constructed with an open range. So the periods
// [2000-07--04/2000-07-25) <-- open range
// And
// [2000-07--04/2000-07-24] <-- closed range
// Are equal
date begin(2002, Jul, 4);
days len(21);
date_period dp(date(2000,Jan,1), days(1));
iss.str("[2002-07-04/2002-07-24]");
facet->set_iso_extended_format();
iss >> dp;
check("Default period (closed range)", dp == date_period(begin,len));
{
std::stringstream ss;
date d(not_a_date_time);
date d2 = day_clock::local_day();
date d3(neg_infin);
date d4(pos_infin);
date_period dp(d2, d); // date/nadt
date_period dp2(d, d); // nadt/nadt
date_period dp3(d3, d4);
ss << dp;
ss >> dp2;
check("Special values period (reversibility test)", dp == dp2);
ss.str("[-infinity/+infinity]");
ss >> dp2;
check("Special values period (infinities)", dp3 == dp2);
}
// open range
period_parser pp(period_parser::AS_OPEN_RANGE);
iss.str("[2002-07-04/2002-07-25)");
facet->period_parser(pp);
iss >> dp;
check("Open range period", dp == date_period(begin,len));
// custom period delimiters
pp.delimiter_strings(" to ", "from ", " exclusive", " inclusive");
iss.str("from 2002-07-04 to 2002-07-25 exclusive");
facet->period_parser(pp);
iss >> dp;
check("Open range period - custom delimiters", dp == date_period(begin,len));
pp.range_option(period_parser::AS_CLOSED_RANGE);
iss.str("from 2002-07-04 to 2002-07-24 inclusive");
facet->period_parser(pp);
iss >> dp;
check("Closed range period - custom delimiters", dp == date_period(begin,len));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -