From 5d11f64aa227a7395d08170a115a7e961c5a00a6 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 24 Jul 2020 14:09:17 -0400 Subject: [PATCH 1/2] Added missing tests to check_finite --- stan/math/prim/err/check_finite.hpp | 21 +++++++++ test/unit/math/prim/err/check_finite_test.cpp | 43 +++++++++++++------ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/stan/math/prim/err/check_finite.hpp b/stan/math/prim/err/check_finite.hpp index 2e92cd2ca80..d3c95f12115 100644 --- a/stan/math/prim/err/check_finite.hpp +++ b/stan/math/prim/err/check_finite.hpp @@ -15,11 +15,25 @@ namespace stan { namespace math { namespace internal { +/** + * Return true if y is finite + * + * @tparam T_y type of y + * @param y parameter to check + * @return boolean + */ template bool is_finite(const T_y& y) { return is_scal_finite(y); } +/** + * Return true if every element of the matrix y is finite + * + * @tparam T_y type of elements y + * @param y matrix to check + * @return boolean + */ template bool is_finite(const Eigen::Matrix& y) { bool all = true; @@ -29,6 +43,13 @@ bool is_finite(const Eigen::Matrix& y) { return all; } +/** + * Return true if every element of the vector y is finite + * + * @tparam T_y type of elements y + * @param y vector to check + * @return boolean + */ template bool is_finite(const std::vector& y) { bool all = true; diff --git a/test/unit/math/prim/err/check_finite_test.cpp b/test/unit/math/prim/err/check_finite_test.cpp index f94483a87d9..dc7f0610ae4 100644 --- a/test/unit/math/prim/err/check_finite_test.cpp +++ b/test/unit/math/prim/err/check_finite_test.cpp @@ -24,6 +24,31 @@ TEST(ErrorHandlingArr, CheckFinite_Vector) { << "check_finite should throw exception on NaN"; } + +TEST(ErrorHandlingArr, CheckFinite_std_vector_std_vector) { + using stan::math::check_finite; + const char* function = "check_finite"; + std::vector x = {-1, 0, 1}; + std::vector> xx = { x }; + ASSERT_NO_THROW(check_finite(function, "x", xx)) + << "check_finite should be true with finite x"; + + x = {-1, 0, std::numeric_limits::infinity()}; + xx = { x }; + EXPECT_THROW(check_finite(function, "x", xx), std::domain_error) + << "check_finite should throw exception on Inf"; + + x = {-1, 0, -std::numeric_limits::infinity()}; + xx = { x }; + EXPECT_THROW(check_finite(function, "x", xx), std::domain_error) + << "check_finite should throw exception on -Inf"; + + x = {-1, 0, std::numeric_limits::quiet_NaN()}; + xx = { x }; + EXPECT_THROW(check_finite(function, "x", xx), std::domain_error) + << "check_finite should throw exception on NaN"; +} + TEST(ErrorHandlingArr, CheckFinite_nan) { using stan::math::check_finite; const char* function = "check_finite"; @@ -94,26 +119,20 @@ TEST(ErrorHandlingMat, CheckFinite_std_vector_Matrix) { x.resize(3); x << -1, 0, std::numeric_limits::infinity(); - - std::vector> xvi = { x }; - - EXPECT_THROW(check_finite(function, "x", xvi), std::domain_error) + xv = { x }; + EXPECT_THROW(check_finite(function, "x", xv), std::domain_error) << "check_finite should throw exception on Inf"; x.resize(3); x << -1, 0, -std::numeric_limits::infinity(); - - std::vector> nxvi = { x }; - - EXPECT_THROW(check_finite(function, "x", nxvi), std::domain_error) + xv = { x }; + EXPECT_THROW(check_finite(function, "x", xv), std::domain_error) << "check_finite should throw exception on -Inf"; x.resize(3); x << -1, 0, std::numeric_limits::quiet_NaN(); - - std::vector> xvn = { x }; - - EXPECT_THROW(check_finite(function, "x", xvn), std::domain_error) + xv = { x }; + EXPECT_THROW(check_finite(function, "x", xv), std::domain_error) << "check_finite should throw exception on NaN"; } From 54fd022162d59d41f65d1f03a54385b82a52c3c8 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Fri, 24 Jul 2020 18:33:45 +0000 Subject: [PATCH 2/2] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- test/unit/math/prim/err/check_finite_test.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/test/unit/math/prim/err/check_finite_test.cpp b/test/unit/math/prim/err/check_finite_test.cpp index 494dfb76533..b439bf99ebc 100644 --- a/test/unit/math/prim/err/check_finite_test.cpp +++ b/test/unit/math/prim/err/check_finite_test.cpp @@ -24,27 +24,26 @@ TEST(ErrorHandlingArr, CheckFinite_Vector) { << "check_finite should throw exception on NaN"; } - TEST(ErrorHandlingArr, CheckFinite_std_vector_std_vector) { using stan::math::check_finite; const char* function = "check_finite"; std::vector x = {-1, 0, 1}; - std::vector> xx = { x }; + std::vector> xx = {x}; ASSERT_NO_THROW(check_finite(function, "x", xx)) << "check_finite should be true with finite x"; x = {-1, 0, std::numeric_limits::infinity()}; - xx = { x }; + xx = {x}; EXPECT_THROW(check_finite(function, "x", xx), std::domain_error) << "check_finite should throw exception on Inf"; x = {-1, 0, -std::numeric_limits::infinity()}; - xx = { x }; + xx = {x}; EXPECT_THROW(check_finite(function, "x", xx), std::domain_error) << "check_finite should throw exception on -Inf"; x = {-1, 0, std::numeric_limits::quiet_NaN()}; - xx = { x }; + xx = {x}; EXPECT_THROW(check_finite(function, "x", xx), std::domain_error) << "check_finite should throw exception on NaN"; } @@ -119,21 +118,21 @@ TEST(ErrorHandlingMat, CheckFinite_std_vector_Matrix) { x.resize(3); x << -1, 0, std::numeric_limits::infinity(); - xv = { x }; + xv = {x}; EXPECT_THROW(check_finite(function, "x", xv), std::domain_error) << "check_finite should throw exception on Inf"; x.resize(3); x << -1, 0, -std::numeric_limits::infinity(); - xv = { x }; + xv = {x}; EXPECT_THROW(check_finite(function, "x", xv), std::domain_error) << "check_finite should throw exception on -Inf"; x.resize(3); x << -1, 0, std::numeric_limits::quiet_NaN(); - xv = { x }; + xv = {x}; EXPECT_THROW(check_finite(function, "x", xv), std::domain_error) - << "check_finite should throw exception on NaN"; + << "check_finite should throw exception on NaN"; } TEST(ErrorHandlingMat, CheckFinite_Matrix_one_indexed_message) {