123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- // Copyright (c) 2006 Johan Rade
- // Copyright (c) 2011 Paul A. Bristow - filename changes for boost-trunk.
- // Distributed under the Boost Software License, Version 1.0.
- // (See accompanying file LICENSE_1_0.txt
- // or copy at http://www.boost.org/LICENSE_1_0.txt)
- //#ifdef _MSC_VER
- //# pragma warning(disable : 4511 4512 4702)
- //#endif
- #define BOOST_TEST_MAIN
- #include <limits>
- #include <locale>
- #include <sstream>
- #include <boost/archive/text_iarchive.hpp>
- #include <boost/archive/text_oarchive.hpp>
- #include <boost/archive/text_wiarchive.hpp>
- #include <boost/archive/text_woarchive.hpp>
- #include <boost/archive/codecvt_null.hpp>
- #include <boost/test/unit_test.hpp>
- #include <boost/math/special_functions/nonfinite_num_facets.hpp>
- #include <boost/math/special_functions/sign.hpp>
- #include <boost/math/special_functions/fpclassify.hpp>
- #include "almost_equal.ipp"
- namespace {
- // The anonymous namespace resolves ambiguities on platforms
- // with fpclassify etc functions at global scope.
- using namespace boost::archive;
- using namespace boost::math;
- using boost::math::signbit;
- using boost::math::changesign;
- using (boost::math::isnan)(;
- //------------------------------------------------------------------------------
- void archive_basic_test();
- void archive_put_trap_test();
- void archive_get_trap_test();
- BOOST_AUTO_TEST_CASE(archive_test)
- {
- archive_basic_test();
- archive_put_trap_test();
- archive_get_trap_test();
- }
- //------------------------------------------------------------------------------
- template<class CharType, class OArchiveType, class IArchiveType, class ValType>
- void archive_basic_test_impl();
- void archive_basic_test()
- {
- archive_basic_test_impl<char, text_oarchive, text_iarchive, float>();
- archive_basic_test_impl<char, text_oarchive, text_iarchive, double>();
- archive_basic_test_impl<
- char, text_oarchive, text_iarchive, long double>();
- archive_basic_test_impl<
- wchar_t, text_woarchive, text_wiarchive, float>();
- archive_basic_test_impl<
- wchar_t, text_woarchive, text_wiarchive, double>();
- archive_basic_test_impl<
- wchar_t, text_woarchive, text_wiarchive, long double>();
- }
- template<class CharType, class OArchiveType, class IArchiveType, class ValType>
- void archive_basic_test_impl()
- {
- if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
- return;
- std::locale default_locale(std::locale::classic(),
- new boost::archive::codecvt_null<CharType>);
- std::locale tmp_locale(default_locale, new nonfinite_num_put<CharType>);
- std::locale my_locale(tmp_locale, new nonfinite_num_get<CharType>);
- std::basic_stringstream<CharType> ss;
- ss.imbue(my_locale);
- ValType a1 = static_cast<ValType>(0);
- ValType a2 = static_cast<ValType>(2307.35);
- ValType a3 = std::numeric_limits<ValType>::infinity();
- BOOST_CHECK((boost::math::isinf)(a3));
- ValType a4 = std::numeric_limits<ValType>::quiet_NaN();
- BOOST_CHECK(((boost::math::isnan)()(a4));
- ValType a5 = std::numeric_limits<ValType>::signaling_NaN();
- BOOST_CHECK(((boost::math::isnan)()(a5));
- ValType a6 = (changesign)(static_cast<ValType>(0));
- ValType a7 = static_cast<ValType>(-57.13);
- ValType a8 = -std::numeric_limits<ValType>::infinity();
- BOOST_CHECK((boost::math::isinf)(a8));
- ValType a9 = -std::numeric_limits<ValType>::quiet_NaN();
- BOOST_CHECK(((boost::math::isnan)()(a9));
- ValType a10 = -std::numeric_limits<ValType>::signaling_NaN();
- BOOST_CHECK(((boost::math::isnan)()(a10));
- {
- OArchiveType oa(ss, no_codecvt);
- oa & a1 & a2 & a3 & a4 & a5 & a6 & a7 & a8 & a9 & a10;
- }
- ValType b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
- {
- IArchiveType ia(ss, no_codecvt);
- ia & b1 & b2 & b3 & b4 & b5 & b6 & b7 & b8 & b9 & b10;
- }
- BOOST_CHECK(a1 == b1);
- BOOST_CHECK(almost_equal(a2, b2));
- BOOST_CHECK(a3 == b3);
- BOOST_CHECK((isnan)(b4));
- BOOST_CHECK(!(signbit)(b4));
- BOOST_CHECK((isnan)(b5));
- BOOST_CHECK(!(signbit)(b5));
- BOOST_CHECK(a6 == b6);
- BOOST_CHECK(almost_equal(a7, b7));
- BOOST_CHECK(a8 == b8);
- BOOST_CHECK((isnan)(b9));
- BOOST_CHECK((signbit)(b9));
- BOOST_CHECK((isnan)(b10));
- BOOST_CHECK((signbit)(b10));
- }
- //------------------------------------------------------------------------------
- template<class CharType, class OArchiveType, class IArchiveType, class ValType>
- void archive_put_trap_test_impl();
- void archive_put_trap_test()
- {
- archive_put_trap_test_impl<char, text_oarchive, text_iarchive, float>();
- archive_put_trap_test_impl<char, text_oarchive, text_iarchive, double>();
- archive_put_trap_test_impl<
- char, text_oarchive, text_iarchive, long double>();
- archive_put_trap_test_impl<
- wchar_t, text_woarchive, text_wiarchive, float>();
- archive_put_trap_test_impl<
- wchar_t, text_woarchive, text_wiarchive, double>();
- archive_put_trap_test_impl<
- wchar_t, text_woarchive, text_wiarchive, long double>();
- }
- template<class CharType, class OArchiveType, class IArchiveType, class ValType>
- void archive_put_trap_test_impl()
- {
- if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
- return;
- std::locale default_locale(std::locale::classic(),
- new boost::archive::codecvt_null<CharType>);
- std::locale new_locale(default_locale,
- new nonfinite_num_put<CharType>(trap_infinity));
- std::basic_stringstream<CharType> ss;
- ss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
- ss.imbue(new_locale);
- ValType a = std::numeric_limits<ValType>::infinity();
- OArchiveType oa(ss, no_codecvt);
- try {
- oa & a;
- }
- catch(std::exception&) {
- ss.clear();
- return;
- }
- BOOST_CHECK(false);
- }
- //------------------------------------------------------------------------------
- template<class CharType, class OArchiveType, class IArchiveType, class ValType>
- void archive_get_trap_test_impl();
- void archive_get_trap_test()
- {
- archive_get_trap_test_impl<char, text_oarchive, text_iarchive, float>();
- archive_get_trap_test_impl<char, text_oarchive, text_iarchive, double>();
- archive_get_trap_test_impl<
- char, text_oarchive, text_iarchive, long double>();
- archive_get_trap_test_impl<
- wchar_t, text_woarchive, text_wiarchive, float>();
- archive_get_trap_test_impl<
- wchar_t, text_woarchive, text_wiarchive, double>();
- archive_get_trap_test_impl<
- wchar_t, text_woarchive, text_wiarchive, long double>();
- }
- template<class CharType, class OArchiveType, class IArchiveType, class ValType>
- void archive_get_trap_test_impl()
- {
- if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
- return;
- std::locale default_locale(std::locale::classic(),
- new boost::archive::codecvt_null<CharType>);
- std::locale tmp_locale(default_locale, new nonfinite_num_put<CharType>);
- std::locale my_locale(tmp_locale,
- new nonfinite_num_get<CharType>(trap_nan));
- std::basic_stringstream<CharType> ss;
- ss.exceptions(std::ios_base::failbit);
- ss.imbue(my_locale);
- ValType a = -std::numeric_limits<ValType>::quiet_NaN();
- {
- OArchiveType oa(ss, no_codecvt);
- oa & a;
- }
- ValType b;
- {
- IArchiveType ia(ss, no_codecvt);
- try {
- ia & b;
- }
- catch(std::exception&) {
- return;
- }
- }
- BOOST_CHECK(false);
- }
- //------------------------------------------------------------------------------
- } // anonymous namespace
|