testtime_input_facet.cpp

来自「Boost provides free peer-reviewed portab」· C++ 代码 · 共 436 行 · 第 1/2 页

CPP
436
字号
    try {      facet->time_duration_format("%H%M%S%F");      ss.str("not-a-date-time");      ++count;      ss >> td;      check("special value w/ hours flag", td == nadt);      ss.str("");      facet->time_duration_format("%M%S%F");      ss.str("not-a-date-time");      ++count;      ss >> td;      check("special value w/ minutes flag", td == nadt);      ss.str("");      facet->time_duration_format("%S%F");      ss.str("not-a-date-time");      ++count;      ss >> td;      check("special value w/ seconds flag", td == nadt);      ss.str("");      facet->time_duration_format("%s");      ss.str("not-a-date-time");      ++count;      ss >> td;      check("special value w/ sec w/frac_sec (always) flag", td == nadt);      ss.str("");      facet->time_duration_format("%f");      ss.str("not-a-date-time");      ++count;      ss >> td;      check("special value w/ frac_sec (always) flag", td == nadt);      ss.str("");    }    catch(...) {       // any exception is a failure      std::stringstream msg;      msg << "special_values with scrunched formats failed at test" << count;      check(msg.str(), false);    }    // exception tests    std::ios_base::failure exc("failure");    check("failure test w/ hours flag",        failure_test(td, "bad_input", exc, new time_input_facet("%H%M%S%F")));    check("silent failure test w/ hours flag",        failure_test(td, "bad_input", new time_input_facet("%H%M%S%F")));    check("failure test w/ minute flag",        failure_test(td, "bad_input", exc, new time_input_facet("%M%S%F")));    check("silent failure test w/ minute flag",        failure_test(td, "bad_input", new time_input_facet("%M%S%F")));    check("failure test w/ second flag",        failure_test(td, "bad_input", exc, new time_input_facet("%S%F")));    check("silent failure test w/ second flag",        failure_test(td, "bad_input", new time_input_facet("%S%F")));    check("failure test w/ sec w/frac (always) flag",        failure_test(td, "bad_input", exc, new time_input_facet("%s")));    check("silent failure test w/ sec w/frac (always) flag",        failure_test(td, "bad_input", new time_input_facet("%s")));    check("failure test w/ frac_sec flag",        failure_test(td, "bad_input", exc, new time_input_facet("%f")));    check("silent failure test w/ frac_sec flag",        failure_test(td, "bad_input", new time_input_facet("%f")));      }  // special_values tests. prove the individual flags catch special_values  // NOTE: these flags all by themselves will not parse a complete ptime,  // these are *specific* special_values tests  iss.str("+infinity -infinity");  facet->format("%H");  facet->time_duration_format("%H");  iss >> pt;  iss >> td;  check("Special value: ptime %H flag", pt == ptime(pos_infin));  check("Special value: time_duration %H flag", td == time_duration(neg_infin));    iss.str("not-a-date-time +infinity");  facet->format("%M");  facet->time_duration_format("%M");  iss >> pt;  iss >> td;  check("Special value: ptime %M flag", pt == ptime(not_a_date_time));  check("Special value: time_duration %M flag", td == time_duration(pos_infin));    iss.str("-infinity not-a-date-time ");  facet->format("%S");  facet->time_duration_format("%S");  iss >> pt;  iss >> td;  check("Special value: ptime %S flag", pt == ptime(neg_infin));  check("Special value: time_duration %S flag", td == time_duration(not_a_date_time));    iss.str("+infinity -infinity");  facet->format("%s");  facet->time_duration_format("%s");  iss >> pt;  iss >> td;  check("Special value: ptime %s flag", pt == ptime(pos_infin));  check("Special value: time_duration %s flag", td == time_duration(neg_infin));    iss.str("not-a-date-time +infinity");  facet->format("%j");  facet->time_duration_format("%f");  iss >> pt;  iss >> td;  check("Special value: ptime %j flag", pt == ptime(not_a_date_time));  check("Special value: time_duration %f flag", td == time_duration(pos_infin));   // time_period tests - the time_period_parser is thoroughly tested in gregorian tests  // default period format is closed range so last ptime is included in peiod  iss.str("[2005-Jan-01 00:00:00/2005-Dec-31 23:59:59]");  facet->format(time_input_facet::default_time_input_format); // reset format  iss >> tp;  check("Time period, default formats",       (tp.begin() == ptime(date(2005,1,1),hours(0))) &&      (tp.last() == ptime(date(2005,12,31),time_duration(23,59,59))) &&      (tp.end() == ptime(date(2005,12,31),time_duration(23,59,59,1))) );  {    std::stringstream ss;    ptime pt(not_a_date_time);    ptime pt2 = second_clock::local_time();    ptime pt3(neg_infin);    ptime pt4(pos_infin);    time_period tp(pt2, pt); // ptime/nadt    time_period tp2(pt, pt); // nadt/nadt    time_period tp3(pt3, pt4);    ss << tp;    ss >> tp2;    check("Special values period (reversibility test)", tp == tp2);    ss.str("[-infinity/+infinity]");    ss >> tp2;    check("Special values period (infinities)", tp3 == tp2);  }  // Failure tests  // faliure tests for date elements tested in gregorian tests  time_input_facet* facet2 = new time_input_facet();  facet2->time_duration_format("%H:%M:%S%f");  check("Failure test: Missing frac_sec with %f flag (w/exceptions)",         failure_test(td, "14:13:12 extra stuff", e_failure, facet2));  time_input_facet* facet3 = new time_input_facet();  facet3->time_duration_format("%H:%M:%S%f");  check("Failure test: Missing frac_sec with %f flag (no exceptions)",         failure_test(td, "14:13:12 extra stuff", facet3));  // Reversable format tests  time_duration td_io(10,11,12,1234567);  ptime pt_io(date(2004,03,15), td_io);  time_facet* otp_facet = new time_facet();  time_input_facet* inp_facet = new time_input_facet();  std::stringstream ss;  std::locale loc2(std::locale::classic(), otp_facet);  ss.imbue(std::locale(loc2, inp_facet)); // imbue locale containing both facets  ss.str("");  ss << pt_io; // stream out pt_io  ss >> pt;  check("Stream out one ptime then into another: default format", pt_io == pt);  ss.str("");  ss << td_io; // stream out td_io  ss >> td;  check("Stream out one time_duration then into another: default format", td_io == td);  td_io = time_duration(1,29,59); // no frac_sec, default format has %F  pt_io = ptime(date(2004,2,29), td_io); // leap year  ss.str("");  ss << pt_io; // stream out pt_io  ss >> pt;  check("Stream out one ptime then into another: default format", pt_io == pt);  ss.str("");  ss << td_io; // stream out td_io  ss >> td;  check("Stream out one time_duration then into another: default format", td_io == td);  td_io = time_duration(13,05,0); // no seconds as the next formats won't use them  pt_io = ptime(date(2004,2,29), td_io); // leap year  otp_facet->format("%Y-%jT%H:%M"); // extended ordinal  inp_facet->format("%Y-%jT%H:%M");  ss.str("");  ss << pt_io; // stream out pt_io  ss >> pt;  check("Stream out one ptime then into another: extended ordinal format", pt_io == pt);  otp_facet->format("Time: %H:%M, Date: %B %d, %Y"); // custom format with extra words  inp_facet->format("Time: %H:%M, Date: %B %d, %Y");  ss.str("");  ss << pt_io; // stream out pt_io  ss >> pt;  check("Stream out one ptime then into another: custom format (" + ss.str() + ")", pt_io == pt);  {    // fully parameterized constructor - compile only test, all other features tested in gregorian    boost::date_time::format_date_parser<date, char> fdp("%Y-%b-%d", std::locale::classic());    special_values_parser svp; // default constructor    period_parser pp; // default constructor    boost::date_time::date_generator_parser<date, char> dgp; // default constructor    time_input_facet tif("%Y-%m-%d %H:%M:%s", fdp, svp, pp, dgp);  }#endif // USE_DATE_TIME_PRE_1_33_FACET_IO }int main(){  try {  //wrap all the tests -- we don't expect an exception    do_all_tests();  }  catch(std::exception& e) {    std::string failure("std::exception caught: ");    failure += e.what();    check(failure, false);  }  catch(...) {    check("Unknown exception caught -- failing", false);  }  return printTestStats();}                      

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?