test_archive.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright (c) 2006 Johan Rade
  2. // Copyright (c) 2011 Paul A. Bristow - filename changes for boost-trunk.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //#ifdef _MSC_VER
  7. //# pragma warning(disable : 4511 4512 4702)
  8. //#endif
  9. #define BOOST_TEST_MAIN
  10. #include <limits>
  11. #include <locale>
  12. #include <sstream>
  13. #include <boost/archive/text_iarchive.hpp>
  14. #include <boost/archive/text_oarchive.hpp>
  15. #include <boost/archive/text_wiarchive.hpp>
  16. #include <boost/archive/text_woarchive.hpp>
  17. #include <boost/archive/codecvt_null.hpp>
  18. #include <boost/test/unit_test.hpp>
  19. #include <boost/math/special_functions/nonfinite_num_facets.hpp>
  20. #include <boost/math/special_functions/sign.hpp>
  21. #include <boost/math/special_functions/fpclassify.hpp>
  22. #include "almost_equal.ipp"
  23. namespace {
  24. // The anonymous namespace resolves ambiguities on platforms
  25. // with fpclassify etc functions at global scope.
  26. using namespace boost::archive;
  27. using namespace boost::math;
  28. using boost::math::signbit;
  29. using boost::math::changesign;
  30. using (boost::math::isnan)(;
  31. //------------------------------------------------------------------------------
  32. void archive_basic_test();
  33. void archive_put_trap_test();
  34. void archive_get_trap_test();
  35. BOOST_AUTO_TEST_CASE(archive_test)
  36. {
  37. archive_basic_test();
  38. archive_put_trap_test();
  39. archive_get_trap_test();
  40. }
  41. //------------------------------------------------------------------------------
  42. template<class CharType, class OArchiveType, class IArchiveType, class ValType>
  43. void archive_basic_test_impl();
  44. void archive_basic_test()
  45. {
  46. archive_basic_test_impl<char, text_oarchive, text_iarchive, float>();
  47. archive_basic_test_impl<char, text_oarchive, text_iarchive, double>();
  48. archive_basic_test_impl<
  49. char, text_oarchive, text_iarchive, long double>();
  50. archive_basic_test_impl<
  51. wchar_t, text_woarchive, text_wiarchive, float>();
  52. archive_basic_test_impl<
  53. wchar_t, text_woarchive, text_wiarchive, double>();
  54. archive_basic_test_impl<
  55. wchar_t, text_woarchive, text_wiarchive, long double>();
  56. }
  57. template<class CharType, class OArchiveType, class IArchiveType, class ValType>
  58. void archive_basic_test_impl()
  59. {
  60. if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
  61. return;
  62. std::locale default_locale(std::locale::classic(),
  63. new boost::archive::codecvt_null<CharType>);
  64. std::locale tmp_locale(default_locale, new nonfinite_num_put<CharType>);
  65. std::locale my_locale(tmp_locale, new nonfinite_num_get<CharType>);
  66. std::basic_stringstream<CharType> ss;
  67. ss.imbue(my_locale);
  68. ValType a1 = static_cast<ValType>(0);
  69. ValType a2 = static_cast<ValType>(2307.35);
  70. ValType a3 = std::numeric_limits<ValType>::infinity();
  71. BOOST_CHECK((boost::math::isinf)(a3));
  72. ValType a4 = std::numeric_limits<ValType>::quiet_NaN();
  73. BOOST_CHECK(((boost::math::isnan)()(a4));
  74. ValType a5 = std::numeric_limits<ValType>::signaling_NaN();
  75. BOOST_CHECK(((boost::math::isnan)()(a5));
  76. ValType a6 = (changesign)(static_cast<ValType>(0));
  77. ValType a7 = static_cast<ValType>(-57.13);
  78. ValType a8 = -std::numeric_limits<ValType>::infinity();
  79. BOOST_CHECK((boost::math::isinf)(a8));
  80. ValType a9 = -std::numeric_limits<ValType>::quiet_NaN();
  81. BOOST_CHECK(((boost::math::isnan)()(a9));
  82. ValType a10 = -std::numeric_limits<ValType>::signaling_NaN();
  83. BOOST_CHECK(((boost::math::isnan)()(a10));
  84. {
  85. OArchiveType oa(ss, no_codecvt);
  86. oa & a1 & a2 & a3 & a4 & a5 & a6 & a7 & a8 & a9 & a10;
  87. }
  88. ValType b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
  89. {
  90. IArchiveType ia(ss, no_codecvt);
  91. ia & b1 & b2 & b3 & b4 & b5 & b6 & b7 & b8 & b9 & b10;
  92. }
  93. BOOST_CHECK(a1 == b1);
  94. BOOST_CHECK(almost_equal(a2, b2));
  95. BOOST_CHECK(a3 == b3);
  96. BOOST_CHECK((isnan)(b4));
  97. BOOST_CHECK(!(signbit)(b4));
  98. BOOST_CHECK((isnan)(b5));
  99. BOOST_CHECK(!(signbit)(b5));
  100. BOOST_CHECK(a6 == b6);
  101. BOOST_CHECK(almost_equal(a7, b7));
  102. BOOST_CHECK(a8 == b8);
  103. BOOST_CHECK((isnan)(b9));
  104. BOOST_CHECK((signbit)(b9));
  105. BOOST_CHECK((isnan)(b10));
  106. BOOST_CHECK((signbit)(b10));
  107. }
  108. //------------------------------------------------------------------------------
  109. template<class CharType, class OArchiveType, class IArchiveType, class ValType>
  110. void archive_put_trap_test_impl();
  111. void archive_put_trap_test()
  112. {
  113. archive_put_trap_test_impl<char, text_oarchive, text_iarchive, float>();
  114. archive_put_trap_test_impl<char, text_oarchive, text_iarchive, double>();
  115. archive_put_trap_test_impl<
  116. char, text_oarchive, text_iarchive, long double>();
  117. archive_put_trap_test_impl<
  118. wchar_t, text_woarchive, text_wiarchive, float>();
  119. archive_put_trap_test_impl<
  120. wchar_t, text_woarchive, text_wiarchive, double>();
  121. archive_put_trap_test_impl<
  122. wchar_t, text_woarchive, text_wiarchive, long double>();
  123. }
  124. template<class CharType, class OArchiveType, class IArchiveType, class ValType>
  125. void archive_put_trap_test_impl()
  126. {
  127. if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
  128. return;
  129. std::locale default_locale(std::locale::classic(),
  130. new boost::archive::codecvt_null<CharType>);
  131. std::locale new_locale(default_locale,
  132. new nonfinite_num_put<CharType>(trap_infinity));
  133. std::basic_stringstream<CharType> ss;
  134. ss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
  135. ss.imbue(new_locale);
  136. ValType a = std::numeric_limits<ValType>::infinity();
  137. OArchiveType oa(ss, no_codecvt);
  138. try {
  139. oa & a;
  140. }
  141. catch(std::exception&) {
  142. ss.clear();
  143. return;
  144. }
  145. BOOST_CHECK(false);
  146. }
  147. //------------------------------------------------------------------------------
  148. template<class CharType, class OArchiveType, class IArchiveType, class ValType>
  149. void archive_get_trap_test_impl();
  150. void archive_get_trap_test()
  151. {
  152. archive_get_trap_test_impl<char, text_oarchive, text_iarchive, float>();
  153. archive_get_trap_test_impl<char, text_oarchive, text_iarchive, double>();
  154. archive_get_trap_test_impl<
  155. char, text_oarchive, text_iarchive, long double>();
  156. archive_get_trap_test_impl<
  157. wchar_t, text_woarchive, text_wiarchive, float>();
  158. archive_get_trap_test_impl<
  159. wchar_t, text_woarchive, text_wiarchive, double>();
  160. archive_get_trap_test_impl<
  161. wchar_t, text_woarchive, text_wiarchive, long double>();
  162. }
  163. template<class CharType, class OArchiveType, class IArchiveType, class ValType>
  164. void archive_get_trap_test_impl()
  165. {
  166. if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
  167. return;
  168. std::locale default_locale(std::locale::classic(),
  169. new boost::archive::codecvt_null<CharType>);
  170. std::locale tmp_locale(default_locale, new nonfinite_num_put<CharType>);
  171. std::locale my_locale(tmp_locale,
  172. new nonfinite_num_get<CharType>(trap_nan));
  173. std::basic_stringstream<CharType> ss;
  174. ss.exceptions(std::ios_base::failbit);
  175. ss.imbue(my_locale);
  176. ValType a = -std::numeric_limits<ValType>::quiet_NaN();
  177. {
  178. OArchiveType oa(ss, no_codecvt);
  179. oa & a;
  180. }
  181. ValType b;
  182. {
  183. IArchiveType ia(ss, no_codecvt);
  184. try {
  185. ia & b;
  186. }
  187. catch(std::exception&) {
  188. return;
  189. }
  190. }
  191. BOOST_CHECK(false);
  192. }
  193. //------------------------------------------------------------------------------
  194. } // anonymous namespace