Files
date_time/test/posix_time/testtime_serialize.cpp
Daniel James c9f634b1d4 Merged revisions 43211,43214-43219,43222-43225,43227-43238,43242,43244-43245,43249-43250,43257-43259,43261,43263,43265,43267-43268,43270-43271,43273,43275-43279,43284-43289,43291,43295,43297-43298,43304-43305,43307,43313,43315,43324,43326-43327,43331,43333,43339-43343,43345,43348,43350,43352-43353,43355-43356,43358,43360,43366-43367,43369-43370,43372-43376,43378-43389,43394,43396-43398,43400-43401,43403-43404,43406-43408,43413-43415,43417-43418,43420,43422-43423 via svnmerge from
https://svn.boost.org/svn/boost/trunk

........
  r43417 | danieljames | 2008-02-26 22:04:55 +0000 (Tue, 26 Feb 2008) | 2 lines
  
  Fix a link to Boost.Bimap.
........
  r43418 | danieljames | 2008-02-26 22:07:25 +0000 (Tue, 26 Feb 2008) | 2 lines
  
  Change another link that's no longer in the repository to link to the website.
........
  r43422 | danieljames | 2008-02-27 18:51:14 +0000 (Wed, 27 Feb 2008) | 1 line
  
  Fix broken copyright urls. Fixes #1573.
........
  r43423 | danieljames | 2008-02-27 19:22:01 +0000 (Wed, 27 Feb 2008) | 1 line
  
  Fix incorrect links to copyright of the form 'http:#www.boost.org
........


[SVN r43425]
2008-02-27 20:00:24 +00:00

128 lines
3.6 KiB
C++

/* Copyright (c) 2002-2005 CrystalClear Software, Inc.
* Use, modification and distribution is subject to the
* Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
* Author: Jeff Garland, Bart Garst
*/
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/time_serialize.hpp>
#include <boost/date_time/testfrmwk.hpp>
#include <fstream>
using namespace boost;
using namespace posix_time;
using namespace gregorian;
template<class archive_type, class temporal_type>
void save_to(archive_type& ar, const temporal_type& tt)
{
ar << tt;
}
int main(){
// originals
date d(2002, Feb, 14);
#if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
time_duration td(12,13,52,123456789);
#else
time_duration td(12,13,52,123456);
#endif
ptime pt(d, td);
time_period tp(pt, ptime(date(2002, Oct, 31), hours(19)));
ptime sv_pt1(not_a_date_time);
ptime sv_pt2(pos_infin);
time_duration sv_td(neg_infin);
// for loading in from archive
date d2(not_a_date_time);
time_duration td2(1,0,0);
ptime pt2(d2, td2);
time_period tp2(pt2, hours(1));
ptime sv_pt3(min_date_time);
ptime sv_pt4(min_date_time);
time_duration sv_td2(0,0,0);
std::ofstream ofs("tmp_file");
// NOTE: DATE_TIME_XML_SERIALIZE is only used in testing and is
// defined in the testing Jamfile
#if defined(DATE_TIME_XML_SERIALIZE)
std::cout << "Running xml archive tests" << std::endl;
archive::xml_oarchive oa(ofs);
#else
std::cout << "Running text archive tests" << std::endl;
archive::text_oarchive oa(ofs);
#endif // DATE_TIME_XML_SERIALIZE
try{
#if defined(DATE_TIME_XML_SERIALIZE)
save_to(oa, BOOST_SERIALIZATION_NVP(pt));
save_to(oa, BOOST_SERIALIZATION_NVP(sv_pt1));
save_to(oa, BOOST_SERIALIZATION_NVP(sv_pt2));
save_to(oa, BOOST_SERIALIZATION_NVP(tp));
save_to(oa, BOOST_SERIALIZATION_NVP(td));
save_to(oa, BOOST_SERIALIZATION_NVP(sv_td));
#else
save_to(oa, pt);
save_to(oa, sv_pt1);
save_to(oa, sv_pt2);
save_to(oa, tp);
save_to(oa, td);
save_to(oa, sv_td);
#endif // DATE_TIME_XML_SERIALIZE
}catch(archive::archive_exception ae){
std::string s(ae.what());
check("Error writing to archive: " + s, false);
ofs.close();
return printTestStats();
}
ofs.close();
std::ifstream ifs("tmp_file");
#if defined(DATE_TIME_XML_SERIALIZE)
archive::xml_iarchive ia(ifs);
#else
archive::text_iarchive ia(ifs);
#endif // DATE_TIME_XML_SERIALIZE
try{
#if defined(DATE_TIME_XML_SERIALIZE)
ia >> BOOST_SERIALIZATION_NVP(pt2);
ia >> BOOST_SERIALIZATION_NVP(sv_pt3);
ia >> BOOST_SERIALIZATION_NVP(sv_pt4);
ia >> BOOST_SERIALIZATION_NVP(tp2);
ia >> BOOST_SERIALIZATION_NVP(td2);
ia >> BOOST_SERIALIZATION_NVP(sv_td2);
#else
ia >> pt2;
ia >> sv_pt3;
ia >> sv_pt4;
ia >> tp2;
ia >> td2;
ia >> sv_td2;
#endif // DATE_TIME_XML_SERIALIZE
}catch(archive::archive_exception ae){
std::string s(ae.what());
check("Error readng from archive: " + s, false);
ifs.close();
return printTestStats();
}
ifs.close();
check("ptime", pt == pt2);
check("special_values ptime (nadt)", sv_pt1 == sv_pt3);
check("special_values ptime (pos_infin)", sv_pt2 == sv_pt4);
check("time_period", tp == tp2);
check("time_duration", td == td2);
check("special_values time_duration (neg_infin)", sv_td == sv_td2);
return printTestStats();
}