Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r71295 - trunk/libs/math/test
From: pbristow_at_[hidden]
Date: 2011-04-15 13:30:21


Author: pbristow
Date: 2011-04-15 13:30:20 EDT (Fri, 15 Apr 2011)
New Revision: 71295
URL: http://svn.boost.org/trac/boost/changeset/71295

Log:
Johan Rade tests for nonfinites.
Added:
   trunk/libs/math/test/test_archive.cpp (contents, props changed)
   trunk/libs/math/test/test_nonfinite_trap.cpp (contents, props changed)
   trunk/libs/math/test/test_signed_zero.cpp (contents, props changed)

Added: trunk/libs/math/test/test_archive.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/math/test/test_archive.cpp 2011-04-15 13:30:20 EDT (Fri, 15 Apr 2011)
@@ -0,0 +1,220 @@
+// Copyright (c) 2006 Johan Rade
+
+// 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
+
+#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/auto_unit_test.hpp>
+#include "../../../../boost/math/nonfinite_num_facets.hpp"
+#include "../../../../boost/math/signbit.hpp"
+#include "../../../../boost/math/fpclassify.hpp"
+#include "almost_equal.hpp"
+
+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()
+{
+ 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();
+ ValType a4 = std::numeric_limits<ValType>::quiet_NaN();
+ ValType a5 = std::numeric_limits<ValType>::signaling_NaN();
+ ValType a6 = (changesign)(static_cast<ValType>(0));
+ ValType a7 = static_cast<ValType>(-57.13);
+ ValType a8 = -std::numeric_limits<ValType>::infinity();
+ ValType a9 = -std::numeric_limits<ValType>::quiet_NaN();
+ ValType a10 = -std::numeric_limits<ValType>::signaling_NaN();
+
+ {
+ 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()
+{
+ 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()
+{
+ 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

Added: trunk/libs/math/test/test_nonfinite_trap.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/math/test/test_nonfinite_trap.cpp 2011-04-15 13:30:20 EDT (Fri, 15 Apr 2011)
@@ -0,0 +1,232 @@
+// Copyright (c) 2006 Johan Rade
+
+// 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 : 4702)
+#endif
+
+#include <locale>
+#include <sstream>
+#include <boost/test/auto_unit_test.hpp>
+#include "almost_equal.hpp"
+#include "S_.hpp"
+#include "../../../../boost/math/nonfinite_num_facets.hpp"
+
+namespace {
+
+// the anonymous namespace resolves ambiguities on platforms
+// with fpclassify etc functions at global scope
+
+using namespace boost::math;
+using boost::math::signbit;
+using boost::math::changesign;
+using boost::math::isnan;
+
+//------------------------------------------------------------------------------
+
+void trap_test_finite();
+void trap_test_inf();
+void trap_test_nan();
+
+BOOST_AUTO_TEST_CASE(trap_test)
+{
+ trap_test_finite();
+ trap_test_inf();
+ trap_test_nan();
+}
+
+//------------------------------------------------------------------------------
+
+template<class CharType, class ValType> void trap_test_finite_impl();
+
+void trap_test_finite()
+{
+ trap_test_finite_impl<char, float>();
+ trap_test_finite_impl<char, double>();
+ trap_test_finite_impl<char, long double>();
+ trap_test_finite_impl<wchar_t, float>();
+ trap_test_finite_impl<wchar_t, double>();
+ trap_test_finite_impl<wchar_t, long double>();
+}
+
+template<class CharType, class ValType> void trap_test_finite_impl()
+{
+ std::locale old_locale;
+ std::locale tmp_locale(old_locale,
+ new nonfinite_num_put<CharType>(trap_infinity | trap_nan));
+ std::locale new_locale(tmp_locale,
+ new nonfinite_num_get<CharType>(trap_infinity | trap_nan));
+
+ std::basic_stringstream<CharType> ss;
+ ss.imbue(new_locale);
+
+ ValType a1 = (ValType)1.2;
+ ValType a2 = (ValType)-3.5;
+ ValType a3 = std::numeric_limits<ValType>::max();
+ ValType a4 = -std::numeric_limits<ValType>::max();
+ ss << a1 << ' ' << a2 << ' ' << a3 << ' ' << a4;
+
+ ValType b1, b2, b3, b4;
+ ss >> b1 >> b2 >> b3 >> b4;
+
+ BOOST_CHECK(almost_equal(b1, a1));
+ BOOST_CHECK(almost_equal(b2, a2));
+ BOOST_CHECK(almost_equal(b3, a3));
+ BOOST_CHECK(almost_equal(b4, a4));
+ BOOST_CHECK(b3 != std::numeric_limits<ValType>::infinity());
+ BOOST_CHECK(b4 != -std::numeric_limits<ValType>::infinity());
+ BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit);
+
+ ss.clear();
+ ss.str(S_(""));
+
+ ss << "++5";
+ ValType b5;
+ ss >> b5;
+ BOOST_CHECK(ss.rdstate() == std::ios_base::failbit);
+}
+
+//------------------------------------------------------------------------------
+
+template<class CharType, class ValType> void trap_test_inf_impl();
+template<class CharType, class ValType> void trap_test_put_inf_impl();
+template<class CharType, class ValType> void trap_test_get_inf_impl();
+
+void trap_test_inf()
+{
+ trap_test_inf_impl<char, float>();
+ trap_test_inf_impl<char, double>();
+ trap_test_inf_impl<char, long double>();
+ trap_test_inf_impl<wchar_t, float>();
+ trap_test_inf_impl<wchar_t, double>();
+ trap_test_inf_impl<wchar_t, long double>();
+}
+
+template<class CharType, class ValType> void trap_test_inf_impl()
+{
+ trap_test_put_inf_impl<CharType, ValType>();
+ trap_test_get_inf_impl<CharType, ValType>();
+}
+
+template<class CharType, class ValType> void trap_test_put_inf_impl()
+{
+ std::locale old_locale;
+ std::locale new_locale(old_locale,
+ new nonfinite_num_put<CharType>(trap_infinity));
+
+ std::basic_stringstream<CharType> ss;
+ ss.imbue(new_locale);
+
+ ValType a1 = std::numeric_limits<ValType>::infinity();
+ ss << a1;
+ BOOST_CHECK(ss.rdstate() == std::ios_base::failbit
+ || ss.rdstate() == std::ios_base::badbit);
+ ss.clear();
+
+ ValType a2 = -std::numeric_limits<ValType>::infinity();
+ ss << a2;
+ BOOST_CHECK(ss.rdstate() == std::ios_base::failbit
+ || ss.rdstate() == std::ios_base::badbit);
+}
+
+template<class CharType, class ValType> void trap_test_get_inf_impl()
+{
+ std::locale old_locale;
+ std::locale tmp_locale(old_locale, new nonfinite_num_put<CharType>);
+ std::locale new_locale(tmp_locale,
+ new nonfinite_num_get<CharType>(trap_infinity));
+
+ std::basic_stringstream<CharType> ss;
+ ss.imbue(new_locale);
+
+ ValType a1 = std::numeric_limits<ValType>::infinity();
+ ss << a1;
+ ValType b1;
+ ss >> b1;
+ BOOST_CHECK(ss.rdstate() == std::ios_base::failbit);
+
+ ss.clear();
+ ss.str(S_(""));
+
+ ValType a2 = -std::numeric_limits<ValType>::infinity();
+ ss << a2;
+ ValType b2;
+ ss >> b2;
+ BOOST_CHECK(ss.rdstate() == std::ios_base::failbit);
+}
+
+//------------------------------------------------------------------------------
+
+template<class CharType, class ValType> void trap_test_nan_impl();
+template<class CharType, class ValType> void trap_test_put_nan_impl();
+template<class CharType, class ValType> void trap_test_get_nan_impl();
+
+void trap_test_nan()
+{
+ trap_test_nan_impl<char, float>();
+ trap_test_nan_impl<char, double>();
+ trap_test_nan_impl<char, long double>();
+ trap_test_nan_impl<wchar_t, float>();
+ trap_test_nan_impl<wchar_t, double>();
+ trap_test_nan_impl<wchar_t, long double>();
+}
+
+template<class CharType, class ValType> void trap_test_nan_impl()
+{
+ trap_test_put_nan_impl<CharType, ValType>();
+ trap_test_get_nan_impl<CharType, ValType>();
+}
+
+template<class CharType, class ValType> void trap_test_put_nan_impl()
+{
+ std::locale old_locale;
+ std::locale new_locale(old_locale,
+ new nonfinite_num_put<CharType>(trap_nan));
+
+ std::basic_stringstream<CharType> ss;
+ ss.imbue(new_locale);
+
+ ValType a1 = std::numeric_limits<ValType>::quiet_NaN();
+ ss << a1;
+ BOOST_CHECK(ss.rdstate() == std::ios_base::failbit
+ || ss.rdstate() == std::ios_base::badbit);
+ ss.clear();
+
+ ValType a2 = std::numeric_limits<ValType>::signaling_NaN();
+ ss << a2;
+ BOOST_CHECK(ss.rdstate() == std::ios_base::failbit
+ || ss.rdstate() == std::ios_base::badbit);
+}
+
+template<class CharType, class ValType> void trap_test_get_nan_impl()
+{
+ std::locale old_locale;
+ std::locale tmp_locale(old_locale, new nonfinite_num_put<CharType>);
+ std::locale new_locale(tmp_locale,
+ new nonfinite_num_get<CharType>(trap_nan));
+
+ std::basic_stringstream<CharType> ss;
+ ss.imbue(new_locale);
+
+ ValType a1 = std::numeric_limits<ValType>::quiet_NaN();
+ ss << a1;
+ ValType b1;
+ ss >> b1;
+ BOOST_CHECK(ss.rdstate() == std::ios_base::failbit);
+
+ ss.clear();
+ ss.str(S_(""));
+
+ ValType a2 = std::numeric_limits<ValType>::signaling_NaN();
+ ss << a2;
+ ValType b2;
+ ss >> b2;
+ BOOST_CHECK(ss.rdstate() == std::ios_base::failbit);
+}
+
+//------------------------------------------------------------------------------
+
+} // anonymous namespace

Added: trunk/libs/math/test/test_signed_zero.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/math/test/test_signed_zero.cpp 2011-04-15 13:30:20 EDT (Fri, 15 Apr 2011)
@@ -0,0 +1,73 @@
+// Copyright (c) 2006 Johan Rade
+
+// 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 : 4702)
+#endif
+
+#include <iomanip>
+#include <locale>
+#include <sstream>
+#include <boost/test/auto_unit_test.hpp>
+#include "almost_equal.ipp"
+#include "S_.ipp"
+#include "../../../../boost/math/nonfinite_num_facets.hpp"
+
+namespace {
+
+// the anonymous namespace resolves ambiguities on platforms
+// with fpclassify etc functions at global scope
+
+using namespace boost::math;
+using boost::math::signbit;
+using boost::math::changesign;
+using boost::math::isnan;
+
+//------------------------------------------------------------------------------
+
+template<class CharType, class ValType> void signed_zero_test_impl();
+
+BOOST_AUTO_TEST_CASE(signed_zero_test)
+{
+ signed_zero_test_impl<char, float>();
+ signed_zero_test_impl<char, double>();
+ signed_zero_test_impl<char, long double>();
+ signed_zero_test_impl<wchar_t, float>();
+ signed_zero_test_impl<wchar_t, double>();
+ signed_zero_test_impl<wchar_t, long double>();
+}
+
+template<class CharType, class ValType> void signed_zero_test_impl()
+{
+ std::locale old_locale;
+ std::locale tmp_locale(
+ old_locale, new nonfinite_num_put<CharType>(signed_zero));
+ std::locale new_locale(tmp_locale, new nonfinite_num_get<CharType>);
+
+ std::basic_stringstream<CharType> ss;
+ ss.imbue(new_locale);
+
+ ValType a1 = static_cast<ValType>(0);
+ ValType a2 = (changesign)(static_cast<ValType>(0));
+
+ ss << a1 << ' ' << a2;
+
+ std::basic_string<CharType> s = S_("0 -0");
+ BOOST_CHECK(ss.str() == s);
+
+ ValType b1, b2;
+ ss >> b1 >> b2;
+
+ BOOST_CHECK(b1 == a1);
+ BOOST_CHECK(b2 == a2);
+ BOOST_CHECK(!(signbit)(b1));
+ BOOST_CHECK((signbit)(b2));
+ BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit);
+}
+
+//------------------------------------------------------------------------------
+
+} // anonymous namespace


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk