Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r78771 - in trunk: boost/math/distributions libs/math/test
From: john_at_[hidden]
Date: 2012-05-30 12:52:20


Author: johnmaddock
Date: 2012-05-30 12:52:18 EDT (Wed, 30 May 2012)
New Revision: 78771
URL: http://svn.boost.org/trac/boost/changeset/78771

Log:
Update to use new out-of-bounds error checking.
Refs #6934.
Text files modified:
   trunk/boost/math/distributions/rayleigh.hpp | 12 ++++++++----
   trunk/boost/math/distributions/skew_normal.hpp | 4 +++-
   trunk/boost/math/distributions/students_t.hpp | 6 ++++++
   trunk/libs/math/test/test_pareto.cpp | 2 ++
   trunk/libs/math/test/test_poisson.cpp | 3 ++-
   trunk/libs/math/test/test_rayleigh.cpp | 9 +++++++++
   trunk/libs/math/test/test_skew_normal.cpp | 2 ++
   trunk/libs/math/test/test_students_t.cpp | 2 ++
   trunk/libs/math/test/test_triangular.cpp | 2 ++
   trunk/libs/math/test/test_uniform.cpp | 2 ++
   trunk/libs/math/test/test_weibull.cpp | 37 ++-----------------------------------
   11 files changed, 40 insertions(+), 41 deletions(-)

Modified: trunk/boost/math/distributions/rayleigh.hpp
==============================================================================
--- trunk/boost/math/distributions/rayleigh.hpp (original)
+++ trunk/boost/math/distributions/rayleigh.hpp 2012-05-30 12:52:18 EDT (Wed, 30 May 2012)
@@ -28,11 +28,11 @@
   template <class RealType, class Policy>
   inline bool verify_sigma(const char* function, RealType sigma, RealType* presult, const Policy& pol)
   {
- if(sigma <= 0)
+ if((sigma <= 0) || (!(boost::math::isfinite)(sigma)))
      {
         *presult = policies::raise_domain_error<RealType>(
            function,
- "The scale parameter \"sigma\" must be > 0, but was: %1%.", sigma, pol);
+ "The scale parameter \"sigma\" must be > 0 and finite, but was: %1%.", sigma, pol);
         return false;
      }
      return true;
@@ -41,7 +41,7 @@
   template <class RealType, class Policy>
   inline bool verify_rayleigh_x(const char* function, RealType x, RealType* presult, const Policy& pol)
   {
- if(x < 0)
+ if((x < 0) || (boost::math::isnan)(x))
      {
         *presult = policies::raise_domain_error<RealType>(
            function,
@@ -81,7 +81,7 @@
 inline const std::pair<RealType, RealType> range(const rayleigh_distribution<RealType, Policy>& /*dist*/)
 { // Range of permissible values for random variable x.
    using boost::math::tools::max_value;
- return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
+ return std::pair<RealType, RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>());
 }
 
 template <class RealType, class Policy>
@@ -108,6 +108,10 @@
    {
       return result;
    }
+ if((boost::math::isinf)(x))
+ {
+ return 0;
+ }
    RealType sigmasqr = sigma * sigma;
    result = x * (exp(-(x * x) / ( 2 * sigmasqr))) / sigmasqr;
    return result;

Modified: trunk/boost/math/distributions/skew_normal.hpp
==============================================================================
--- trunk/boost/math/distributions/skew_normal.hpp (original)
+++ trunk/boost/math/distributions/skew_normal.hpp 2012-05-30 12:52:18 EDT (Wed, 30 May 2012)
@@ -100,7 +100,9 @@
   inline const std::pair<RealType, RealType> range(const skew_normal_distribution<RealType, Policy>& /*dist*/)
   { // Range of permissible values for random variable x.
     using boost::math::tools::max_value;
- return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
+ return std::pair<RealType, RealType>(
+ std::numeric_limits<RealType>::has_infinity ? -std::numeric_limits<RealType>::infinity() : -max_value<RealType>(),
+ std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>()); // - to + max value.
   }
 
   template <class RealType, class Policy>

Modified: trunk/boost/math/distributions/students_t.hpp
==============================================================================
--- trunk/boost/math/distributions/students_t.hpp (original)
+++ trunk/boost/math/distributions/students_t.hpp 2012-05-30 12:52:18 EDT (Wed, 30 May 2012)
@@ -87,6 +87,9 @@
    if(false == detail::check_df(
       "boost::math::pdf(const students_t_distribution<%1%>&, %1%)", degrees_of_freedom, &error_result, Policy()))
       return error_result;
+ if(false == detail::check_x(
+ "boost::math::pdf(const students_t_distribution<%1%>&, %1%)", t, &error_result, Policy()))
+ return error_result;
    // Might conceivably permit df = +infinity and use normal distribution.
    RealType result;
    RealType basem1 = t * t / degrees_of_freedom;
@@ -111,6 +114,9 @@
    if(false == detail::check_df(
       "boost::math::cdf(const students_t_distribution<%1%>&, %1%)", degrees_of_freedom, &error_result, Policy()))
       return error_result;
+ if(false == detail::check_x(
+ "boost::math::pdf(const students_t_distribution<%1%>&, %1%)", t, &error_result, Policy()))
+ return error_result;
 
    if (t == 0)
    {

Modified: trunk/libs/math/test/test_pareto.cpp
==============================================================================
--- trunk/libs/math/test/test_pareto.cpp (original)
+++ trunk/libs/math/test/test_pareto.cpp 2012-05-30 12:52:18 EDT (Wed, 30 May 2012)
@@ -30,6 +30,7 @@
 #include <boost/math/distributions/pareto.hpp>
     using boost::math::pareto_distribution;
 #include <boost/math/tools/test.hpp>
+#include "test_out_of_range.hpp"
 
 #include <iostream>
    using std::cout;
@@ -233,6 +234,7 @@
     // Check kurtosis excess = kurtosis - 3;
 
     // Error condition checks:
+ check_out_of_range<pareto_distribution<RealType> >(1, 1);
     BOOST_CHECK_THROW(pdf(pareto_distribution<RealType>(0, 1), 0), std::domain_error);
     BOOST_CHECK_THROW(pdf(pareto_distribution<RealType>(1, 0), 0), std::domain_error);
     BOOST_CHECK_THROW(pdf(pareto_distribution<RealType>(-1, 1), 0), std::domain_error);

Modified: trunk/libs/math/test/test_poisson.cpp
==============================================================================
--- trunk/libs/math/test/test_poisson.cpp (original)
+++ trunk/libs/math/test/test_poisson.cpp 2012-05-30 12:52:18 EDT (Wed, 30 May 2012)
@@ -34,6 +34,7 @@
 #include <boost/math/special_functions/gamma.hpp> // for (incomplete) gamma.
 // using boost::math::qamma_Q;
 #include "table_type.hpp"
+#include "test_out_of_range.hpp"
 
 #include <iostream>
    using std::cout;
@@ -485,7 +486,7 @@
      x = quantile(complement(p6, poisson_quantile_data[i][1]));
      BOOST_CHECK_EQUAL(x, floor(poisson_quantile_data[i][3] + 0.5f));
   }
-
+ check_out_of_range<poisson_distribution<RealType> >(1);
 } // template <class RealType>void test_spots(RealType)
 
 //

Modified: trunk/libs/math/test/test_rayleigh.cpp
==============================================================================
--- trunk/libs/math/test/test_rayleigh.cpp (original)
+++ trunk/libs/math/test/test_rayleigh.cpp 2012-05-30 12:52:18 EDT (Wed, 30 May 2012)
@@ -18,6 +18,7 @@
 
 #include <boost/test/test_exec_monitor.hpp> // Boost.Test
 #include <boost/test/floating_point_comparison.hpp>
+#include "test_out_of_range.hpp"
 
 #include <iostream>
    using std::cout;
@@ -58,6 +59,13 @@
             x,
             tolerance); // %
    }
+ if(std::numeric_limits<RealType>::has_infinity)
+ {
+ RealType inf = std::numeric_limits<RealType>::infinity();
+ BOOST_CHECK_EQUAL(pdf(rayleigh_distribution<RealType>(s), inf), 0);
+ BOOST_CHECK_EQUAL(cdf(rayleigh_distribution<RealType>(s), inf), 1);
+ BOOST_CHECK_EQUAL(cdf(complement(rayleigh_distribution<RealType>(s), inf)), 0);
+ }
 } // void test_spot
 
 template <class RealType>
@@ -79,6 +87,7 @@
    // Things that are errors:
    rayleigh_distribution<RealType> dist(0.5);
 
+ check_out_of_range<rayleigh_distribution<RealType> >(1);
    BOOST_CHECK_THROW(
        quantile(dist,
        RealType(1.)), // quantile unity should overflow.

Modified: trunk/libs/math/test/test_skew_normal.cpp
==============================================================================
--- trunk/libs/math/test/test_skew_normal.cpp (original)
+++ trunk/libs/math/test/test_skew_normal.cpp 2012-05-30 12:52:18 EDT (Wed, 30 May 2012)
@@ -30,6 +30,7 @@
 using std::setprecision;
 #include <limits>
 using std::numeric_limits;
+#include "test_out_of_range.hpp"
 
 template <class RealType>
 void check_skew_normal(RealType mean, RealType scale, RealType shape, RealType x, RealType p, RealType q, RealType tol)
@@ -440,6 +441,7 @@
       BOOST_CHECK_THROW(cdf(skew_normal_distribution<RealType>(0, -1, 0), 0), std::domain_error);
       BOOST_CHECK_THROW(quantile(skew_normal_distribution<RealType>(0, 1, 0), -1), std::domain_error);
       BOOST_CHECK_THROW(quantile(skew_normal_distribution<RealType>(0, 1, 0), 2), std::domain_error);
+ check_out_of_range<skew_normal_distribution<RealType> >(1, 1, 1);
     }
 
 

Modified: trunk/libs/math/test/test_students_t.cpp
==============================================================================
--- trunk/libs/math/test/test_students_t.cpp (original)
+++ trunk/libs/math/test/test_students_t.cpp 2012-05-30 12:52:18 EDT (Wed, 30 May 2012)
@@ -21,6 +21,7 @@
 #include <boost/math/distributions/students_t.hpp>
     using boost::math::students_t_distribution;
 #include <boost/math/tools/test.hpp> // for real_concept
+#include "test_out_of_range.hpp"
 
 #include <iostream>
    using std::cout;
@@ -469,6 +470,7 @@
     BOOST_CHECK_THROW(quantile(dist, 2), std::domain_error);
     BOOST_CHECK_THROW(pdf(students_t_distribution<RealType>(0), 0), std::domain_error);
     BOOST_CHECK_THROW(pdf(students_t_distribution<RealType>(-1), 0), std::domain_error);
+ check_out_of_range<students_t_distribution<RealType> >(1);
 } // template <class RealType>void test_spots(RealType)
 
 int test_main(int, char* [])

Modified: trunk/libs/math/test/test_triangular.cpp
==============================================================================
--- trunk/libs/math/test/test_triangular.cpp (original)
+++ trunk/libs/math/test/test_triangular.cpp 2012-05-30 12:52:18 EDT (Wed, 30 May 2012)
@@ -23,6 +23,7 @@
 using boost::math::triangular_distribution;
 #include <boost/math/tools/test.hpp>
 #include <boost/math/special_functions/fpclassify.hpp>
+#include "test_out_of_range.hpp"
 
 #include <iostream>
 using std::cout;
@@ -524,6 +525,7 @@
   }
   BOOST_CHECK_THROW(triangular_distribution<RealType>(1, 0), std::domain_error); // lower > upper!
 
+ check_out_of_range<triangular_distribution<RealType> >(-1, 0, 1);
 } // template <class RealType>void test_spots(RealType)
 
 int test_main(int, char* [])

Modified: trunk/libs/math/test/test_uniform.cpp
==============================================================================
--- trunk/libs/math/test/test_uniform.cpp (original)
+++ trunk/libs/math/test/test_uniform.cpp 2012-05-30 12:52:18 EDT (Wed, 30 May 2012)
@@ -22,6 +22,7 @@
 #include <boost/math/distributions/uniform.hpp>
     using boost::math::uniform_distribution;
 #include <boost/math/tools/test.hpp>
+#include "test_out_of_range.hpp"
 
 #include <iostream>
    using std::cout;
@@ -367,6 +368,7 @@
    BOOST_CHECK_THROW(uniform_distribution<RealType>(1, 0), std::domain_error); // lower > upper!
    BOOST_CHECK_THROW(uniform_distribution<RealType>(1, 1), std::domain_error); // lower == upper!
 
+ check_out_of_range<uniform_distribution<RealType> >(1, 5);
 } // template <class RealType>void test_spots(RealType)
 
 int test_main(int, char* [])

Modified: trunk/libs/math/test/test_weibull.cpp
==============================================================================
--- trunk/libs/math/test/test_weibull.cpp (original)
+++ trunk/libs/math/test/test_weibull.cpp 2012-05-30 12:52:18 EDT (Wed, 30 May 2012)
@@ -20,6 +20,7 @@
 #include <boost/math/distributions/weibull.hpp>
     using boost::math::weibull_distribution;
 #include <boost/math/tools/test.hpp>
+#include "test_out_of_range.hpp"
 
 #include <iostream>
    using std::cout;
@@ -342,41 +343,7 @@
    BOOST_CHECK_EQUAL(pdf(weibull_distribution<RealType>(1, 3), 0), exp(-pow(RealType(0) / RealType(3), RealType(1))) * pow(RealType(0), RealType(0)) * RealType(1) / RealType(3));
    BOOST_CHECK_THROW(pdf(weibull_distribution<RealType>(0.5, 3), 0), std::overflow_error);
 
- // No longer allow any parameter to be NaN or inf, so all these tests should throw.
- if (std::numeric_limits<RealType>::has_quiet_NaN)
- {
- // Attempt to construct from non-finite should throw.
- RealType nan = std::numeric_limits<RealType>::quiet_NaN();
- BOOST_CHECK_THROW(weibull_distribution<RealType> w(nan), std::domain_error);
- BOOST_CHECK_THROW(weibull_distribution<RealType> w(1, nan), std::domain_error);
-
- // Non-finite parameters should throw.
- weibull_distribution<RealType> w(RealType(1));
- BOOST_CHECK_THROW(pdf(w, +nan), std::domain_error); // x = NaN
- BOOST_CHECK_THROW(cdf(w, +nan), std::domain_error); // x = NaN
- BOOST_CHECK_THROW(cdf(complement(w, +nan)), std::domain_error); // x = + nan
- BOOST_CHECK_THROW(quantile(w, +nan), std::domain_error); // p = + nan
- BOOST_CHECK_THROW(quantile(complement(w, +nan)), std::domain_error); // p = + nan
- } // has_quiet_NaN
-
- if (std::numeric_limits<RealType>::has_infinity)
- {
- RealType inf = std::numeric_limits<RealType>::infinity();
-
- BOOST_CHECK_THROW(weibull_distribution<RealType> w(inf), std::domain_error);
- BOOST_CHECK_THROW(weibull_distribution<RealType> w(1, inf), std::domain_error);
-
- weibull_distribution<RealType> w(RealType(1));
- BOOST_CHECK_THROW(weibull_distribution<RealType> w(inf), std::domain_error);
- BOOST_CHECK_THROW(weibull_distribution<RealType> w(1, inf), std::domain_error);
- BOOST_CHECK_THROW(pdf(w, +inf), std::domain_error); // x = inf
- BOOST_CHECK_THROW(cdf(w, +inf), std::domain_error); // x = inf
- BOOST_CHECK_THROW(cdf(complement(w, +inf)), std::domain_error); // x = + inf
- BOOST_CHECK_THROW(quantile(w, +inf), std::domain_error); // p = + inf
- BOOST_CHECK_THROW(quantile(complement(w, +inf)), std::domain_error); // p = + inf
- } // has_infinity
-
-
+ check_out_of_range<weibull_distribution<RealType> >(1, 1);
 } // template <class RealType>void test_spots(RealType)
 
 int test_main(int, char* [])


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