Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r71296 - trunk/libs/math/test
From: pbristow_at_[hidden]
Date: 2011-04-15 13:52:55


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

Log:
More nonfinite tests
Added:
   trunk/libs/math/test/test_basic_nonfinite.cpp (contents, props changed)
   trunk/libs/math/test/test_legacy_nonfinite.cpp (contents, props changed)
   trunk/libs/math/test/test_lexical_cast.cpp (contents, props changed)

Added: trunk/libs/math/test/test_basic_nonfinite.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/math/test/test_basic_nonfinite.cpp 2011-04-15 13:52:54 EDT (Fri, 15 Apr 2011)
@@ -0,0 +1,254 @@
+
+// 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)
+
+// Copyright (c) 2006 Johan Rade
+// Copyright (c) 2011 Paul A. Bristow comments
+
+/*!
+\file
+\brief Basic tests of the nonfinite num facets.
+
+\detail Basic_test outputs using nonfinite_num_put facet
+and reads back in using nonfinite_num_ facet,
+and checks loopback OK.
+
+Also checks that output of infinity, -infinity and NaN are as expected,
+using C99 specification "nan -nan nan -nan" and "inf -inf".
+Also includes a few combinations of display manipulators
+(left, right, internal, showpos)
+and checks that can input C99 infinity and NaN too.
+
+*/
+
+#ifdef _MSC_VER
+# pragma warning(disable : 4702)
+#endif
+
+#include <iomanip>
+#include <locale>
+#include <sstream>
+
+#define BOOST_TEST_MAIN
+
+#include <boost/test/auto_unit_test.hpp>
+
+#include "almost_equal.ipp"
+#include "S_.ipp"
+
+#include <boost/math/special_functions/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::spirit::detail::changesign; // Temporary spirit version.
+using boost::math::changesign;
+using boost::math::isnan;
+
+//------------------------------------------------------------------------------
+
+void basic_test_finite();
+void basic_test_inf();
+void basic_test_nan();
+void basic_test_format();
+
+BOOST_AUTO_TEST_CASE(basic_test)
+{
+ basic_test_finite();
+ basic_test_inf();
+ basic_test_nan();
+ basic_test_format();
+}
+
+//------------------------------------------------------------------------------
+
+template<class CharType, class ValType> void basic_test_finite_impl();
+
+void basic_test_finite()
+{
+ basic_test_finite_impl<char, float>();
+ basic_test_finite_impl<char, double>();
+ basic_test_finite_impl<char, long double>();
+ basic_test_finite_impl<wchar_t, float>();
+ basic_test_finite_impl<wchar_t, double>();
+ basic_test_finite_impl<wchar_t, long double>();
+}
+
+template<class CharType, class ValType> void basic_test_finite_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>);
+
+ 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 basic_test_inf_impl();
+
+void basic_test_inf()
+{
+ basic_test_inf_impl<char, float>();
+ basic_test_inf_impl<char, double>();
+ basic_test_inf_impl<char, long double>();
+ basic_test_inf_impl<wchar_t, float>();
+ basic_test_inf_impl<wchar_t, double>();
+ basic_test_inf_impl<wchar_t, long double>();
+}
+
+template<class CharType, class ValType> void basic_test_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>);
+
+ std::basic_stringstream<CharType> ss;
+ ss.imbue(new_locale);
+
+ ValType a1 = std::numeric_limits<ValType>::infinity();
+ ValType a2 = -std::numeric_limits<ValType>::infinity();
+
+ ss << a1 << ' ' << a2;
+
+ std::basic_string<CharType> s = S_("inf -inf");
+ BOOST_CHECK(ss.str() == s);
+
+ ss << " infinity"; // Alternative C99 representation of infinity.
+
+ ValType b1, b2, b3;
+ ss >> b1;
+ ss >> b2;
+ ss >> b3;
+
+ BOOST_CHECK(b1 == a1);
+ BOOST_CHECK(b2 == a2);
+ BOOST_CHECK(b3 == std::numeric_limits<ValType>::infinity());
+ BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit);
+}
+
+//------------------------------------------------------------------------------
+
+template<class CharType, class ValType> void basic_test_nan_impl();
+
+void basic_test_nan()
+{
+ basic_test_nan_impl<char, float>();
+ basic_test_nan_impl<char, double>();
+ basic_test_nan_impl<char, long double>();
+ basic_test_nan_impl<wchar_t, float>();
+ basic_test_nan_impl<wchar_t, double>();
+ basic_test_nan_impl<wchar_t, long double>();
+}
+
+template<class CharType, class ValType> void basic_test_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>);
+
+ std::basic_stringstream<CharType> ss;
+ ss.imbue(new_locale);
+
+ ValType a1 = std::numeric_limits<ValType>::quiet_NaN();
+ ValType a2 = -std::numeric_limits<ValType>::quiet_NaN();
+ ValType a3 = std::numeric_limits<ValType>::signaling_NaN();
+ ValType a4 = -std::numeric_limits<ValType>::signaling_NaN();
+ ss << a1 << ' ' << a2 << ' ' << a3 << ' ' << a4;
+
+ std::basic_string<CharType> s = S_("nan -nan nan -nan");
+ BOOST_CHECK(ss.str() == s);
+
+ // Alternative C99 representation of NaN.
+ ss << " nan(foo)";
+
+ ValType b1, b2, b3, b4, b5;
+ ss >> b1 >> b2 >> b3 >> b4 >> b5;
+
+ BOOST_CHECK((isnan)(b1));
+ BOOST_CHECK((isnan)(b2));
+ BOOST_CHECK((isnan)(b3));
+ BOOST_CHECK((isnan)(b4));
+ BOOST_CHECK((isnan)(b5));
+
+ BOOST_CHECK(!(signbit)(b1));
+ BOOST_CHECK((signbit)(b2));
+ BOOST_CHECK(!(signbit)(b3));
+ BOOST_CHECK((signbit)(b4));
+ BOOST_CHECK(!(signbit)(b5));
+
+ BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit);
+}
+
+//------------------------------------------------------------------------------
+
+template<class CharType, class ValType> void basic_test_format_impl();
+
+void basic_test_format()
+{
+ basic_test_format_impl<char, float>();
+ basic_test_format_impl<char, double>();
+ basic_test_format_impl<char, long double>();
+ basic_test_format_impl<wchar_t, float>();
+ basic_test_format_impl<wchar_t, double>();
+ basic_test_format_impl<wchar_t, long double>();
+}
+
+template<class CharType, class ValType> void basic_test_format_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>);
+
+ std::basic_stringstream<CharType> ss;
+ ss.imbue(new_locale);
+
+ ValType a = std::numeric_limits<ValType>::infinity();
+
+ ss << std::setw(6) << a; // Expect right justified in field of six, so 3 leading spaces.
+ ss << '|';
+ ss << std::setw(2) << a; // Too narrow for "inf", but should still be "inf".
+ ss << '|';
+ ss << std::left << std::setw(5) << a; // 5 - 3 leaves two trailing spaces.
+ ss << '|';
+ ss << std::showpos << std::internal << std::setw(7) << a; // 3 internal spaces between + and "inf".
+ ss << '|';
+ ss << std::uppercase << std::right << std::setw(6) << a; // still showpos, so "space, space, +INF".
+
+ std::basic_string<CharType> s = S_(" inf|inf|inf |+ inf| +INF");
+ BOOST_CHECK(ss.str() == s);
+}
+
+//------------------------------------------------------------------------------
+
+} // anonymous namespace

Added: trunk/libs/math/test/test_legacy_nonfinite.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/math/test/test_legacy_nonfinite.cpp 2011-04-15 13:52:54 EDT (Fri, 15 Apr 2011)
@@ -0,0 +1,185 @@
+// Copyright (c) 2006 Johan Rade
+// Copyright (c) 2011 Paul A. Bristow comments
+// 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)
+
+/*!
+\file
+\brief Legacy (non-C99) tests of the nonfinite num facets.
+
+\detail legacy_test outputs using nonfinite_num_put facet
+with legacy flag, and reads back in using nonfinite_num_ facet,
+and checks loopback OK.
+
+Also checks that output of infinity, -infinity and NaN are as expected,
+including the 'legacy' "1.#IND", "1.#QNAN", "1.#SNAN" representations
+(was used by MSVC but now all represented on output by "1.#QNAN")
+and qnan snan nanq nans (used by other systems)
+excluding C99 specification "nan -nan nan -nan" and "inf -inf".
+*/
+
+#ifdef _MSC_VER
+# pragma warning(disable : 4702)
+#endif
+
+#include <iomanip>
+#include <locale>
+#include <sstream>
+
+#define BOOST_TEST_MAIN
+#include <boost/test/auto_unit_test.hpp>
+
+//#include "almost_equal.hpp"
+//#include "S_.hpp"
+
+#include <boost/math/special_functions/nonfinite_num_facets.hpp>
+
+namespace {
+
+// The anonymous namespace resolves ambiguities on platforms
+// with fpclassify etc functions declared at global scope.
+
+using namespace boost::math;
+using boost::math::signbit;
+using boost::math::changesign;
+using boost::math::isnan;
+
+//------------------------------------------------------------------------------
+
+void legacy_test_inf();
+void legacy_test_nan();
+
+BOOST_AUTO_TEST_CASE(legacy_test)
+{
+ legacy_test_inf();
+ legacy_test_nan();
+}
+
+//------------------------------------------------------------------------------
+
+template<class CharType, class ValType> void legacy_test_inf_impl();
+
+void legacy_test_inf()
+{
+ legacy_test_inf_impl<char, float>();
+ legacy_test_inf_impl<char, double>();
+ legacy_test_inf_impl<char, long double>();
+ legacy_test_inf_impl<wchar_t, float>();
+ legacy_test_inf_impl<wchar_t, double>();
+ legacy_test_inf_impl<wchar_t, long double>();
+}
+
+template<class CharType, class ValType> void legacy_test_inf_impl()
+{
+ std::locale old_locale;
+ std::locale new_locale(old_locale, new nonfinite_num_get<CharType>(legacy));
+
+ std::basic_stringstream<CharType> ss;
+ ss.imbue(new_locale);
+
+ ValType a1 = std::numeric_limits<ValType>::infinity();
+ ValType a2 = -std::numeric_limits<ValType>::infinity();
+ ss << a1 << ' ' << a2;
+
+ ss << " 1.#INF";
+
+ ValType b1, b2, b3;
+ ss >> b1 >> b2 >> b3;
+
+ BOOST_CHECK(b1 == a1);
+ BOOST_CHECK(b2 == a2);
+ BOOST_CHECK(b3 == std::numeric_limits<ValType>::infinity());
+ BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit);
+}
+
+//------------------------------------------------------------------------------
+
+template<class CharType, class ValType> void legacy_test_nan_impl();
+
+void legacy_test_nan()
+{
+ legacy_test_nan_impl<char, float>();
+ legacy_test_nan_impl<char, double>();
+ legacy_test_nan_impl<char, long double>();
+ legacy_test_nan_impl<wchar_t, float>();
+ legacy_test_nan_impl<wchar_t, double>();
+ legacy_test_nan_impl<wchar_t, long double>();
+}
+
+template<class CharType, class ValType> void legacy_test_nan_impl()
+{
+ std::locale old_locale;
+ std::locale new_locale(old_locale, new nonfinite_num_get<CharType>(legacy));
+
+ std::basic_stringstream<CharType> ss;
+ ss.imbue(new_locale);
+
+ ValType a1 = std::numeric_limits<ValType>::quiet_NaN();
+ ValType a2 = -std::numeric_limits<ValType>::quiet_NaN();
+ ValType a3 = std::numeric_limits<ValType>::signaling_NaN();
+ ValType a4 = -std::numeric_limits<ValType>::signaling_NaN();
+ ss << a1 << ' ' << a2 << ' ' << a3 << ' ' << a4;
+
+ ss << " qnan snan nanq nans 1.#IND 1.#QNAN 1.#SNAN";
+
+ ValType b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11;
+ ss >> b1 >> b2 >> b3 >> b4 >> b5 >> b6 >> b7 >> b8 >> b9 >> b10 >> b11;
+
+ // std::cout << b11 << std::endl; // Confirms that legacy
+ // IND, SNAN and QNAN are considered the same,
+ // and both output the legacy string "1.#QNAN".
+
+ BOOST_CHECK((isnan)(b1));
+ BOOST_CHECK((isnan)(b2));
+ BOOST_CHECK((isnan)(b3));
+ BOOST_CHECK((isnan)(b4));
+ BOOST_CHECK((isnan)(b5));
+ BOOST_CHECK((isnan)(b6));
+ BOOST_CHECK((isnan)(b7));
+ BOOST_CHECK((isnan)(b8));
+ BOOST_CHECK((isnan)(b9));
+ BOOST_CHECK((isnan)(b10));
+ BOOST_CHECK((isnan)(b11)); // Johan V3 1.#SNAN failed on MSVC 10.
+ // Change in nonfinite_num_facet.hpp Paul A. Bristow 11 Apr 11 makes work OK.
+/*
+ // These tests fail on platforms, such as gcc,
+ // that use the same representation of +nan and -nan.
+
+ BOOST_CHECK(!(signbit)(b1));
+ BOOST_CHECK((signbit)(b2));
+ BOOST_CHECK(!(signbit)(b3));
+ BOOST_CHECK((signbit)(b4));
+*/
+ BOOST_CHECK(!(signbit)(b5));
+ BOOST_CHECK(!(signbit)(b6));
+ BOOST_CHECK(!(signbit)(b7));
+ BOOST_CHECK(!(signbit)(b8));
+ BOOST_CHECK(!(signbit)(b9));
+ BOOST_CHECK(!(signbit)(b10));
+ BOOST_CHECK(!(signbit)(b11)); // Johan V3 1.#SNAN failed MSVC 10.
+
+ BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit); // Fails if SNAN test fails.
+}
+
+//------------------------------------------------------------------------------
+
+} // anonymous namespace
+
+/*
+
+Output:
+
+ legacy_test.vcxproj -> J:\Cpp\fp_facet\fp_facet\Debug\legacy_test.exe
+ Running 1 test case...
+ 1.#QNAN
+ 1.#QNAN
+ 1.#QNAN
+ 1.#QNAN
+ 1.#QNAN
+ 1.#QNAN
+
+ *** No errors detected
+
+
+*/

Added: trunk/libs/math/test/test_lexical_cast.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/math/test/test_lexical_cast.cpp 2011-04-15 13:52:54 EDT (Fri, 15 Apr 2011)
@@ -0,0 +1,116 @@
+// Copyright (c) 2006 Johan Rade
+
+// Copyright (c) 2011 Paul A. Bristow incorporated Boost.Math
+
+// 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 : 4127 4511 4512 4701 4702)
+#endif
+
+#define BOOST_TEST_MAIN
+
+#include <limits>
+#include <locale>
+#include <string>
+#include <boost/lexical_cast.hpp>
+#include <boost/test/auto_unit_test.hpp>
+
+#include <boost/math/special_functions/nonfinite_num_facets.hpp>
+#include <boost/math/special_functions/signbit.hpp>
+#include <boost/math/special_functions/fpclassify.hpp>
+#include "almost_equal.ipp"
+#include "S_.ipp"
+
+namespace {
+
+// the anonymous namespace resolves ambiguities on platforms
+// with fpclassify etc functions at global scope
+
+using boost::lexical_cast;
+
+using namespace boost::math;
+using boost::math::signbit;
+using boost::math::changesign;
+using boost::math::isnan;
+
+//------------------------------------------------------------------------------
+
+template<class CharType, class ValType> void lexical_cast_test_impl();
+
+BOOST_AUTO_TEST_CASE(lexical_cast_test)
+{
+ lexical_cast_test_impl<char, float>();
+ lexical_cast_test_impl<char, double>();
+ lexical_cast_test_impl<char, long double>();
+ lexical_cast_test_impl<wchar_t, float>();
+ lexical_cast_test_impl<wchar_t, double>();
+ lexical_cast_test_impl<wchar_t, long double>();
+}
+
+template<class CharType, class ValType> void lexical_cast_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::locale::global(new_locale);
+
+ ValType a1 = static_cast<ValType>(0);
+ ValType a2 = static_cast<ValType>(13);
+ 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);
+ ValType a8 = -std::numeric_limits<ValType>::infinity();
+ ValType a9 = -std::numeric_limits<ValType>::quiet_NaN();
+ ValType a10 = -std::numeric_limits<ValType>::signaling_NaN();
+
+ std::basic_string<CharType> s1 = S_("0");
+ std::basic_string<CharType> s2 = S_("13");
+ std::basic_string<CharType> s3 = S_("inf");
+ std::basic_string<CharType> s4 = S_("nan");
+ std::basic_string<CharType> s5 = S_("nan");
+ std::basic_string<CharType> s6 = S_("-0");
+ std::basic_string<CharType> s7 = S_("-57");
+ std::basic_string<CharType> s8 = S_("-inf");
+ std::basic_string<CharType> s9 = S_("-nan");
+ std::basic_string<CharType> s10 = S_("-nan");
+
+ BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a1) == s1);
+ BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a2) == s2);
+ BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a3) == s3);
+ BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a4) == s4);
+ BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a5) == s5);
+ BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a6) == s6);
+ BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a7) == s7);
+ BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a8) == s8);
+ BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a9) == s9);
+ BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a10) == s10);
+
+ BOOST_CHECK(lexical_cast<ValType>(s1) == a1);
+ BOOST_CHECK(!(signbit)(lexical_cast<ValType>(s1)));
+ BOOST_CHECK(lexical_cast<ValType>(s2) == a2);
+ BOOST_CHECK(lexical_cast<ValType>(s3) == a3);
+ BOOST_CHECK((isnan)(lexical_cast<ValType>(s4)));
+ BOOST_CHECK(!(signbit)(lexical_cast<ValType>(s4)));
+ BOOST_CHECK((isnan)(lexical_cast<ValType>(s5)));
+ BOOST_CHECK(!(signbit)(lexical_cast<ValType>(s5)));
+ BOOST_CHECK(lexical_cast<ValType>(a6) == a6);
+ BOOST_CHECK((signbit)(lexical_cast<ValType>(s6)));
+ BOOST_CHECK(lexical_cast<ValType>(s7) == a7);
+ BOOST_CHECK(lexical_cast<ValType>(s8) == a8);
+ BOOST_CHECK((isnan)(lexical_cast<ValType>(s9)));
+ BOOST_CHECK((signbit)(lexical_cast<ValType>(s9)));
+ BOOST_CHECK((isnan)(lexical_cast<ValType>(s10)));
+ BOOST_CHECK((signbit)(lexical_cast<ValType>(s10)));
+
+ std::locale::global(old_locale);
+}
+
+//------------------------------------------------------------------------------
+
+} // 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