diff --git a/stan/math/prim/err.hpp b/stan/math/prim/err.hpp index c6d0199da6d..3c74503179c 100644 --- a/stan/math/prim/err.hpp +++ b/stan/math/prim/err.hpp @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -27,7 +26,6 @@ #include #include #include -#include #include #include #include diff --git a/stan/math/prim/err/check_finite.hpp b/stan/math/prim/err/check_finite.hpp index ddb6cab80fc..d3c95f12115 100644 --- a/stan/math/prim/err/check_finite.hpp +++ b/stan/math/prim/err/check_finite.hpp @@ -1,27 +1,143 @@ #ifndef STAN_MATH_PRIM_ERR_CHECK_FINITE_HPP #define STAN_MATH_PRIM_ERR_CHECK_FINITE_HPP -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include 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; + for (size_t n = 0; n < y.size(); ++n) { + all &= is_finite(y(n)); + } + 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; + for (size_t n = 0; n < stan::math::size(y); ++n) { + all &= is_finite(y[n]); + } + return all; +} +} // namespace internal /** * Check if y is finite. * This function is vectorized and will check each element of * y. - * @tparam T_y type of y - * @param function function name (for error messages) - * @param name variable name (for error messages) - * @param y variable to check + * @tparam T_y Type of y + * @param function Function name (for error messages) + * @param name Variable name (for error messages) + * @param y Variable to check * @throw domain_error if y is infinity, -infinity, or NaN */ -template +template * = nullptr> inline void check_finite(const char* function, const char* name, const T_y& y) { - if (check_finite_screen(y)) { - auto is_good = [](const auto& y) { return std::isfinite(y); }; - elementwise_check(is_good, function, name, y, ", but must be finite!"); + if (!internal::is_finite(y)) { + throw_domain_error(function, name, y, "is ", ", but must be finite!"); + } +} + +/** + * Return true if all values in the std::vector are finite. + * + * @tparam T_y type of elements in the std::vector + * + * @param function name of function (for error messages) + * @param name variable name (for error messages) + * @param y std::vector to test + * @return true if all values are finite + **/ +template * = nullptr> +inline void check_finite(const char* function, const char* name, + const std::vector& y) { + for (size_t n = 0; n < stan::math::size(y); n++) { + if (!internal::is_finite(stan::get(y, n))) { + throw_domain_error_vec(function, name, y, n, "is ", + ", but must be finite!"); + } + } +} + +/** + * Return true is the specified matrix is finite. + * + * @tparam Derived Eigen derived type + * + * @param function name of function (for error messages) + * @param name variable name (for error messages) + * @param y matrix to test + * @return true if the matrix is finite + **/ +template * = nullptr> +inline void check_finite(const char* function, const char* name, + const EigMat& y) { + if (!value_of(y).allFinite()) { + for (int n = 0; n < y.size(); ++n) { + if (!std::isfinite(value_of_rec(y(n)))) { + throw_domain_error_vec(function, name, y, n, "is ", + ", but must be finite!"); + } + } + } +} + +/** + * Return true if all values in the std::vector are finite. + * + * @tparam T_y type of elements in the std::vector + * + * @param function name of function (for error messages) + * @param name variable name (for error messages) + * @param y std::vector to test + * @return true if all values are finite + **/ +template * = nullptr> +inline void check_finite(const char* function, const char* name, + const std::vector& y) { + for (size_t n = 0; n < stan::math::size(y); n++) { + if (!internal::is_finite(stan::get(y, n))) { + throw_domain_error(function, name, "", "", "is not finite!"); + } } } diff --git a/stan/math/prim/err/check_finite_screen.hpp b/stan/math/prim/err/check_finite_screen.hpp deleted file mode 100644 index 09b2f256a15..00000000000 --- a/stan/math/prim/err/check_finite_screen.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef STAN_MATH_PRIM_ERR_CHECK_FINITE_SCREEN_HPP -#define STAN_MATH_PRIM_ERR_CHECK_FINITE_SCREEN_HPP - -#include -#include - -namespace stan { -namespace math { - -/** - * Run a quick check on an eigen container to see if the elements - * are finite. - * - * If returns false, the values in the container are finite. - * - * If it returns true, a more thorough check is necessary. - * - * @tparam T_y type of y - * @param y variable to check - * @return true if full check is necessary - */ -template * = nullptr> -inline bool check_finite_screen(const T_y& y) { - return !value_of_rec(y).allFinite(); -} - -/** - * For input types where no quick finite check is available, return true - * (implying the elements in the container are not necessarily finite) - * - * @tparam T_y type of y - * @param y variable to check - * @return true if full check is necessary - */ -template * = nullptr> -inline bool check_finite_screen(const T_y& y) { - return true; -} - -} // namespace math -} // namespace stan - -#endif diff --git a/stan/math/prim/err/check_nonnegative.hpp b/stan/math/prim/err/check_nonnegative.hpp index 38f5de60d9c..891667b3624 100644 --- a/stan/math/prim/err/check_nonnegative.hpp +++ b/stan/math/prim/err/check_nonnegative.hpp @@ -1,11 +1,42 @@ #ifndef STAN_MATH_PRIM_ERR_CHECK_NONNEGATIVE_HPP #define STAN_MATH_PRIM_ERR_CHECK_NONNEGATIVE_HPP -#include +#include +#include +#include +#include +#include +#include namespace stan { namespace math { +namespace internal { +template +struct nonnegative { + static void check(const char* function, const char* name, const T_y& y) { + // have to use not is_unsigned. is_signed will be false for + // floating point types that have no unsigned versions. + if (!std::is_unsigned::value && !(y >= 0)) { + throw_domain_error(function, name, y, "is ", ", but must be >= 0!"); + } + } +}; + +template +struct nonnegative { + static void check(const char* function, const char* name, const T_y& y) { + for (size_t n = 0; n < stan::math::size(y); n++) { + if (!std::is_unsigned::type>::value + && !(stan::get(y, n) >= 0)) { + throw_domain_error_vec(function, name, y, n, "is ", + ", but must be >= 0!"); + } + } + } +}; +} // namespace internal + /** * Check if y is non-negative. * This function is vectorized and will check each element of y. @@ -19,10 +50,9 @@ namespace math { template inline void check_nonnegative(const char* function, const char* name, const T_y& y) { - auto is_good = [](const auto& y) { return y >= 0; }; - elementwise_check(is_good, function, name, y, ", but must be >= 0!"); + internal::nonnegative::value>::check(function, name, + y); } - } // namespace math } // namespace stan #endif diff --git a/stan/math/prim/err/check_not_nan.hpp b/stan/math/prim/err/check_not_nan.hpp index 83b0f0df4f1..f44cadf5c14 100644 --- a/stan/math/prim/err/check_not_nan.hpp +++ b/stan/math/prim/err/check_not_nan.hpp @@ -1,12 +1,40 @@ #ifndef STAN_MATH_PRIM_ERR_CHECK_NOT_NAN_HPP #define STAN_MATH_PRIM_ERR_CHECK_NOT_NAN_HPP -#include -#include +#include +#include +#include +#include +#include +#include +#include namespace stan { namespace math { +namespace internal { +template +struct not_nan { + static void check(const char* function, const char* name, const T_y& y) { + if (is_nan(value_of_rec(y))) { + throw_domain_error(function, name, y, "is ", ", but must not be nan!"); + } + } +}; + +template +struct not_nan { + static void check(const char* function, const char* name, const T_y& y) { + for (size_t n = 0; n < stan::math::size(y); n++) { + if (is_nan(value_of_rec(stan::get(y, n)))) { + throw_domain_error_vec(function, name, y, n, "is ", + ", but must not be nan!"); + } + } + } +}; +} // namespace internal + /** * Check if y is not NaN. * This function is vectorized and will check each element of @@ -21,10 +49,7 @@ namespace math { template inline void check_not_nan(const char* function, const char* name, const T_y& y) { - if (check_not_nan_screen(y)) { - auto is_good = [](const auto& y) { return !std::isnan(y); }; - elementwise_check(is_good, function, name, y, ", but must not be nan!"); - } + internal::not_nan::value>::check(function, name, y); } } // namespace math diff --git a/stan/math/prim/err/check_not_nan_screen.hpp b/stan/math/prim/err/check_not_nan_screen.hpp deleted file mode 100644 index c5f60f0ecdb..00000000000 --- a/stan/math/prim/err/check_not_nan_screen.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef STAN_MATH_PRIM_ERR_CHECK_NOT_NAN_SCREEN_HPP -#define STAN_MATH_PRIM_ERR_CHECK_NOT_NAN_SCREEN_HPP - -#include -#include - -namespace stan { -namespace math { - -/** - * Run a quick check on an eigen container to see if it contains - * any NaNs. - * - * If returns false, the values in the container are not NaNs. - * - * If it returns true, a more thorough check is necessary. - * - * @tparam T_y type of y - * @param y variable to check - * @return true if full check is necessary - */ -template * = nullptr> -inline bool check_not_nan_screen(const T_y& y) { - return value_of_rec(y).hasNaN(); -} - -/** - * For input types where no quick NaN check is available, return true - * (implying the elements in the container could be NaN) - * - * @tparam T_y type of y - * @param y variable to check - * @return true if full check is necessary - */ -template * = nullptr> -inline bool check_not_nan_screen(const T_y& y) { - return true; -} - -} // namespace math -} // namespace stan - -#endif diff --git a/stan/math/prim/err/check_positive.hpp b/stan/math/prim/err/check_positive.hpp index ec02a6d6593..6af8f48908a 100644 --- a/stan/math/prim/err/check_positive.hpp +++ b/stan/math/prim/err/check_positive.hpp @@ -1,14 +1,46 @@ #ifndef STAN_MATH_PRIM_ERR_CHECK_POSITIVE_HPP #define STAN_MATH_PRIM_ERR_CHECK_POSITIVE_HPP -#include +#include +#include +#include #include +#include +#include +#include #include -#include namespace stan { namespace math { +namespace { + +template +struct positive { + static void check(const char* function, const char* name, const T_y& y) { + // have to use not is_unsigned. is_signed will be false + // floating point types that have no unsigned versions. + if (!std::is_unsigned::value && !(y > 0)) { + throw_domain_error(function, name, y, "is ", ", but must be > 0!"); + } + } +}; + +template +struct positive { + static void check(const char* function, const char* name, const T_y& y) { + for (size_t n = 0; n < stan::math::size(y); n++) { + if (!std::is_unsigned::type>::value + && !(stan::get(y, n) > 0)) { + throw_domain_error_vec(function, name, y, n, "is ", + ", but must be > 0!"); + } + } + } +}; + +} // namespace + /** * Check if y is positive. * This function is vectorized and will check each element of @@ -23,8 +55,7 @@ namespace math { template inline void check_positive(const char* function, const char* name, const T_y& y) { - auto is_good = [](const auto& y) { return y > 0; }; - elementwise_check(is_good, function, name, y, ", but must be > 0!"); + positive::value>::check(function, name, y); } /** diff --git a/stan/math/prim/err/check_positive_finite.hpp b/stan/math/prim/err/check_positive_finite.hpp index 61cc2095b42..5ccd75384a2 100644 --- a/stan/math/prim/err/check_positive_finite.hpp +++ b/stan/math/prim/err/check_positive_finite.hpp @@ -1,7 +1,9 @@ #ifndef STAN_MATH_PRIM_ERR_CHECK_POSITIVE_FINITE_HPP #define STAN_MATH_PRIM_ERR_CHECK_POSITIVE_FINITE_HPP -#include +#include +#include +#include namespace stan { namespace math { @@ -15,16 +17,15 @@ namespace math { * @param name Variable name (for error messages) * @param y Variable to check * @throw domain_error if any element of y is not positive or - * if any element of y is NaN or infinity. + * if any element of y is NaN. */ - template inline void check_positive_finite(const char* function, const char* name, const T_y& y) { - auto is_good = [](const auto& y) { return y > 0 && std::isfinite(y); }; - elementwise_check(is_good, function, name, y, - ", but must be positive and finite!"); + check_positive(function, name, y); + check_finite(function, name, y); } + } // namespace math } // namespace stan #endif diff --git a/stan/math/prim/err/is_nonnegative.hpp b/stan/math/prim/err/is_nonnegative.hpp deleted file mode 100644 index 447cef621dd..00000000000 --- a/stan/math/prim/err/is_nonnegative.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef STAN_MATH_PRIM_ERR_IS_NONNEGATIVE_HPP -#define STAN_MATH_PRIM_ERR_IS_NONNEGATIVE_HPP - -#include - -namespace stan { -namespace math { - -/** - * Return true if y is nonnegative. - * This function is vectorized and will check each element of - * y. - * @tparam T_y Type of y - * @param y Variable to check - * @return true if every element of y is >=0. - */ -template -inline bool is_nonnegative(const T_y& y) { - auto is_good = [](const auto& y) { return y >= 0; }; - return elementwise_is(is_good, y); -} -} // namespace math -} // namespace stan -#endif diff --git a/stan/math/prim/err/is_not_nan.hpp b/stan/math/prim/err/is_not_nan.hpp index 84f9c8efe34..4e94a02cd13 100644 --- a/stan/math/prim/err/is_not_nan.hpp +++ b/stan/math/prim/err/is_not_nan.hpp @@ -1,8 +1,11 @@ #ifndef STAN_MATH_PRIM_ERR_IS_NOT_NAN_HPP #define STAN_MATH_PRIM_ERR_IS_NOT_NAN_HPP -#include -#include +#include +#include +#include +#include +#include namespace stan { namespace math { @@ -12,17 +15,17 @@ namespace math { * This function is vectorized and will check each element of * y. If no element is NaN, this * function will return true. - * @tparam T_y type of y - * @param y variable to check + * @tparam T_y Type of y + * @param y Variable to check * @return true if no element of y is NaN */ template inline bool is_not_nan(const T_y& y) { - if (check_not_nan_screen(y)) { - auto is_good = [](const auto& y) { return !std::isnan(y); }; - return elementwise_is(is_good, y); + for (size_t n = 0; n < stan::math::size(y); ++n) { + if (is_nan(value_of_rec(stan::get(y, n)))) { + return false; + } } - return true; } diff --git a/stan/math/prim/err/is_positive.hpp b/stan/math/prim/err/is_positive.hpp index 67f99417026..b33b3cb758e 100644 --- a/stan/math/prim/err/is_positive.hpp +++ b/stan/math/prim/err/is_positive.hpp @@ -1,7 +1,9 @@ #ifndef STAN_MATH_PRIM_ERR_IS_POSITIVE_HPP #define STAN_MATH_PRIM_ERR_IS_POSITIVE_HPP -#include +#include +#include +#include namespace stan { namespace math { @@ -12,12 +14,16 @@ namespace math { * y. * @tparam T_y Type of y * @param y Variable to check - * @return true if y contains only positive elements + * @return true if vector contains only positive elements */ template inline bool is_positive(const T_y& y) { - auto is_good = [](const auto& y) { return y > 0; }; - return elementwise_is(is_good, y); + for (size_t n = 0; n < stan::math::size(y); ++n) { + if (!(stan::get(y, n) > 0)) { + return false; + } + } + return true; } /** diff --git a/stan/math/prim/err/is_positive_finite.hpp b/stan/math/prim/err/is_positive_finite.hpp deleted file mode 100644 index 440b375a253..00000000000 --- a/stan/math/prim/err/is_positive_finite.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef STAN_MATH_PRIM_ERR_IS_POSITIVE_FINITE_HPP -#define STAN_MATH_PRIM_ERR_IS_POSITIVE_FINITE_HPP - -#include - -namespace stan { -namespace math { - -/** - * Return true if y is positive and finite. - * This function is vectorized and will check each element of - * y. - * @tparam T_y Type of y - * @param y Variable to check - * @return true if every element of y is >0 and if no element of y - * is NaN or infinity. - */ -template -inline bool is_positive_finite(const T_y& y) { - auto is_good = [](const auto& y) { return y > 0 && std::isfinite(y); }; - return elementwise_is(is_good, y); -} -} // namespace math -} // namespace stan -#endif diff --git a/stan/math/prim/err/is_scal_finite.hpp b/stan/math/prim/err/is_scal_finite.hpp index 0d25196788b..bdebf549ab1 100644 --- a/stan/math/prim/err/is_scal_finite.hpp +++ b/stan/math/prim/err/is_scal_finite.hpp @@ -1,8 +1,11 @@ #ifndef STAN_MATH_PRIM_ERR_IS_SCAL_FINITE_HPP #define STAN_MATH_PRIM_ERR_IS_SCAL_FINITE_HPP -#include -#include +#include +#include +#include +#include +#include namespace stan { namespace math { @@ -13,15 +16,15 @@ namespace math { * y. * @tparam T_y Type of y * @param y Variable to check - * @return true if no element in y is infinity, -infinity, or NaN + * @throw true if y is not infinity, -infinity, or NaN */ template inline bool is_scal_finite(const T_y& y) { - if (check_finite_screen(y)) { - auto is_good = [](const auto& y) { return std::isfinite(y); }; - return elementwise_is(is_good, y); + for (size_t n = 0; n < stan::math::size(y); ++n) { + if (!std::isfinite(value_of_rec(stan::get(y, n)))) { + return false; + } } - return true; } diff --git a/stan/math/prim/fun/csr_u_to_z.hpp b/stan/math/prim/fun/csr_u_to_z.hpp index a43fa4b4067..7a4411c2bef 100644 --- a/stan/math/prim/fun/csr_u_to_z.hpp +++ b/stan/math/prim/fun/csr_u_to_z.hpp @@ -19,12 +19,12 @@ namespace math { * @param[in] u U vector. * @param[in] i Index into resulting z vector. * @return z[i] where z is conversion from u. - * @throw std::domain_error if u does not contain at least 2 elements. + * @throw std::domain_error if u is zero length. * @throw std::out_of_range if i is out of range. */ inline int csr_u_to_z(const std::vector& u, int i) { - check_greater("csr_u_to_z", "u.size()", u.size(), 1); - check_range("csr_u_to_z", "i", u.size() - 1, i + 1, "index out of range"); + check_positive("csr_u_to_z", "u.size()", u.size()); + check_range("csr_u_to_z", "i", u.size(), i + 1, "index out of range"); return u[i + 1] - u[i]; } diff --git a/test/unit/math/prim/err/check_finite_screen_test.cpp b/test/unit/math/prim/err/check_finite_screen_test.cpp deleted file mode 100644 index a21201791b1..00000000000 --- a/test/unit/math/prim/err/check_finite_screen_test.cpp +++ /dev/null @@ -1,158 +0,0 @@ -#include -#include -#include -#include -#include - -TEST(ErrorHandlingArr, CheckFiniteScreen_Vector) { - using stan::math::check_finite_screen; - std::vector x = {-1, 0, 1}; - EXPECT_TRUE(check_finite_screen(x)); - - x = {-1, 0, std::numeric_limits::infinity()}; - EXPECT_TRUE(check_finite_screen(x)); - - x = {-1, 0, -std::numeric_limits::infinity()}; - EXPECT_TRUE(check_finite_screen(x)); - - x = {-1, 0, std::numeric_limits::quiet_NaN()}; - EXPECT_TRUE(check_finite_screen(x)); -} - -TEST(ErrorHandlingArr, CheckFiniteScreen_std_vector_std_vector) { - using stan::math::check_finite_screen; - std::vector x = {-1, 0, 1}; - std::vector> xx = {x}; - EXPECT_TRUE(check_finite_screen(xx)); - - x = {-1, 0, std::numeric_limits::infinity()}; - xx = {x}; - EXPECT_TRUE(check_finite_screen(xx)); - - x = {-1, 0, -std::numeric_limits::infinity()}; - xx = {x}; - EXPECT_TRUE(check_finite_screen(xx)); - - x = {-1, 0, std::numeric_limits::quiet_NaN()}; - xx = {x}; - EXPECT_TRUE(check_finite_screen(xx)); -} - -TEST(ErrorHandlingArr, CheckFiniteScreen_nan) { - using stan::math::check_finite_screen; - double nan = std::numeric_limits::quiet_NaN(); - - std::vector x = {nan, 0, 1}; - EXPECT_TRUE(check_finite_screen(x)); - - x = {1, nan, 1}; - EXPECT_TRUE(check_finite_screen(x)); - - x = {1, 0, nan}; - EXPECT_TRUE(check_finite_screen(x)); -} - -TEST(ErrorHandlingMat, CheckFiniteScreen_Matrix) { - using stan::math::check_finite_screen; - Eigen::Matrix x; - - x.resize(3); - x << -1, 0, 1; - EXPECT_FALSE(check_finite_screen(x)); - - EXPECT_FALSE(check_finite_screen(x.array())); - - EXPECT_FALSE(check_finite_screen(x.transpose())); - - x.resize(3); - x << -1, 0, std::numeric_limits::infinity(); - EXPECT_TRUE(check_finite_screen(x)); - - x.resize(3); - x << -1, 0, -std::numeric_limits::infinity(); - EXPECT_TRUE(check_finite_screen(x)); - - x.resize(3); - x << -1, 0, std::numeric_limits::quiet_NaN(); - EXPECT_TRUE(check_finite_screen(x)); -} - -TEST(ErrorHandlingMat, CheckFiniteScreen_std_vector_Matrix) { - using stan::math::check_finite_screen; - Eigen::Matrix x; - - x.resize(3); - x << -1, 0, 1; - - std::vector> xv = {x}; - std::vector> xva = {x.array()}; - std::vector> xvt = {x.transpose()}; - - EXPECT_TRUE(check_finite_screen(xv)); - - EXPECT_TRUE(check_finite_screen(xva)); - - EXPECT_TRUE(check_finite_screen(xvt)); - - x.resize(3); - x << -1, 0, std::numeric_limits::infinity(); - xv = {x}; - EXPECT_TRUE(check_finite_screen(xv)); - - x.resize(3); - x << -1, 0, -std::numeric_limits::infinity(); - xv = {x}; - EXPECT_TRUE(check_finite_screen(xv)); - - x.resize(3); - x << -1, 0, std::numeric_limits::quiet_NaN(); - xv = {x}; - EXPECT_TRUE(check_finite_screen(xv)); -} - -TEST(ErrorHandlingMat, CheckFiniteScreen_nan) { - using stan::math::check_finite_screen; - double nan = std::numeric_limits::quiet_NaN(); - - Eigen::Matrix x_mat(3); - x_mat << nan, 0, 1; - EXPECT_TRUE(check_finite_screen(x_mat)); - - x_mat << 1, nan, 1; - EXPECT_TRUE(check_finite_screen(x_mat)); - - x_mat << 1, 0, nan; - EXPECT_TRUE(check_finite_screen(x_mat)); -} - -TEST(ErrorHandlingScalar, CheckFiniteScreen) { - using stan::math::check_finite_screen; - double x = 0; - - EXPECT_TRUE(check_finite_screen(x)); - - x = std::numeric_limits::infinity(); - EXPECT_TRUE(check_finite_screen(x)); - - x = -std::numeric_limits::infinity(); - EXPECT_TRUE(check_finite_screen(x)); - - x = std::numeric_limits::quiet_NaN(); - EXPECT_TRUE(check_finite_screen(x)); -} - -TEST(ErrorHandlingScalar, CheckFiniteScreen_nan) { - using stan::math::check_finite_screen; - double nan = std::numeric_limits::quiet_NaN(); - - EXPECT_TRUE(check_finite_screen(nan)); -} - -TEST(ErrorHandlingScalar, CheckFiniteScreenVectorization) { - using stan::math::check_finite_screen; - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 0); - EXPECT_TRUE(check_finite_screen(std::vector{m, m, m})); - Eigen::MatrixXd m2 = m; - m2(1, 1) = stan::math::INFTY; - EXPECT_TRUE(check_finite_screen(std::vector{m, m2, m})); -} diff --git a/test/unit/math/prim/err/check_finite_test.cpp b/test/unit/math/prim/err/check_finite_test.cpp index ca0bc6b60ba..b439bf99ebc 100644 --- a/test/unit/math/prim/err/check_finite_test.cpp +++ b/test/unit/math/prim/err/check_finite_test.cpp @@ -197,16 +197,3 @@ TEST(ErrorHandlingScalar, CheckFinite_nan) { EXPECT_THROW(check_finite(function, "x", nan), std::domain_error); } - -TEST(ErrorHandlingScalar, CheckFiniteVectorization) { - using stan::math::check_finite; - const char* function = "check_finite"; - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 0); - EXPECT_NO_THROW( - check_finite(function, "m", std::vector{m, m, m})); - Eigen::MatrixXd m2 = m; - m2(1, 1) = stan::math::INFTY; - EXPECT_THROW( - check_finite(function, "m", std::vector{m, m2, m}), - std::domain_error); -} diff --git a/test/unit/math/prim/err/check_nonnegative_test.cpp b/test/unit/math/prim/err/check_nonnegative_test.cpp index 980de4060da..165ffefaa4e 100644 --- a/test/unit/math/prim/err/check_nonnegative_test.cpp +++ b/test/unit/math/prim/err/check_nonnegative_test.cpp @@ -6,8 +6,8 @@ TEST(ErrorHandlingArr, CheckNonnegativeVectorized) { using stan::math::check_nonnegative; - const char* function = "check_nonnegative"; int N = 5; + const char* function = "check_nonnegative"; std::vector x(N); x.assign(N, 0); @@ -34,8 +34,8 @@ TEST(ErrorHandlingArr, CheckNonnegativeVectorized) { TEST(ErrorHandlingArr, CheckNonnegativeVectorized_one_indexed_message) { using stan::math::check_nonnegative; - const char* function = "check_nonnegative"; int N = 5; + const char* function = "check_nonnegative"; std::vector x(N); std::string message; @@ -99,26 +99,3 @@ TEST(ErrorHandlingScalar, CheckNonnegative_nan) { EXPECT_THROW(check_nonnegative(function, "x", nan), std::domain_error); } - -TEST(ErrorHandlingScalar, CheckNonnegative_0) { - using stan::math::check_nonnegative; - const char* function = "check_nonnegative"; - EXPECT_NO_THROW(check_nonnegative(function, "x", 0U)); - EXPECT_NO_THROW(check_nonnegative(function, "x", (size_t)0)); - EXPECT_NO_THROW(check_nonnegative(function, "x", 0.0)); - EXPECT_NO_THROW(check_nonnegative(function, "x", -0.0)); - EXPECT_NO_THROW(check_nonnegative(function, "x", 0)); -} - -TEST(ErrorHandlingScalar, CheckNonNegativeVectorization) { - using stan::math::check_nonnegative; - const char* function = "check_nonnegative"; - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 0); - EXPECT_NO_THROW( - check_nonnegative(function, "m", std::vector{m, m, m})); - Eigen::MatrixXd m2 = m; - m2(1, 1) = -1; - EXPECT_THROW( - check_nonnegative(function, "m", std::vector{m, m2, m}), - std::domain_error); -} diff --git a/test/unit/math/prim/err/check_not_nan_screen_test.cpp b/test/unit/math/prim/err/check_not_nan_screen_test.cpp deleted file mode 100644 index f30aaee6c00..00000000000 --- a/test/unit/math/prim/err/check_not_nan_screen_test.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include -#include -#include -#include - -TEST(ErrorHandlingArr, CheckNotNanScreenVectorized) { - using stan::math::check_not_nan_screen; - int N = 5; - std::vector x(N); - - x.assign(N, 0); - EXPECT_TRUE(check_not_nan_screen(x)) - << "check_not_nan_screen(vector) should be true with finite x: " << x[0]; - - x.assign(N, std::numeric_limits::infinity()); - EXPECT_TRUE(check_not_nan_screen(x)) - << "check_not_nan_screen(vector) should be true with x = Inf: " << x[0]; - - x.assign(N, -std::numeric_limits::infinity()); - EXPECT_TRUE(check_not_nan_screen(x)) - << "check_not_nan_screen(vector) should be true with x = -Inf: " << x[0]; - - x.assign(N, std::numeric_limits::quiet_NaN()); - EXPECT_TRUE(check_not_nan_screen(x)) - << "check_not_nan_screen(vector) should throw exception on NaN: " << x[0]; -} - -TEST(ErrorHandlingMatrix, checkNotNanScreenEigenRow) { - using stan::math::check_not_nan_screen; - stan::math::vector_d y; - y.resize(3); - y << 1, 2, 3; - - EXPECT_FALSE(stan::math::check_not_nan_screen(y)); - EXPECT_FALSE(stan::math::check_not_nan_screen(y)); - - y(1) = std::numeric_limits::quiet_NaN(); - EXPECT_TRUE(stan::math::check_not_nan_screen(y)); - EXPECT_TRUE(stan::math::check_not_nan_screen(y)); -} - -TEST(ErrorHandlingScalar, CheckNotNanScreen) { - using stan::math::check_not_nan_screen; - double x = 0; - - EXPECT_TRUE(check_not_nan_screen(x)); - - x = std::numeric_limits::infinity(); - EXPECT_TRUE(check_not_nan_screen(x)); - - x = -std::numeric_limits::infinity(); - EXPECT_TRUE(check_not_nan_screen(x)); - - x = std::numeric_limits::quiet_NaN(); - EXPECT_TRUE(check_not_nan_screen(x)); -} - -TEST(ErrorHandlingScalar, CheckNotNaNScreenVectorization) { - using stan::math::check_not_nan_screen; - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 0); - EXPECT_TRUE(check_not_nan_screen(std::vector{m, m, m})); - Eigen::MatrixXd m2 = m; - m2(1, 1) = stan::math::NOT_A_NUMBER; - EXPECT_TRUE(check_not_nan_screen(std::vector{m, m2, m})); -} diff --git a/test/unit/math/prim/err/check_not_nan_test.cpp b/test/unit/math/prim/err/check_not_nan_test.cpp index f620f54e965..ee8bc1bced2 100644 --- a/test/unit/math/prim/err/check_not_nan_test.cpp +++ b/test/unit/math/prim/err/check_not_nan_test.cpp @@ -85,16 +85,3 @@ TEST(ErrorHandlingScalar, CheckNotNan) { EXPECT_THROW(check_not_nan(function, "x", x), std::domain_error) << "check_not_nan should throw exception on NaN: " << x; } - -TEST(ErrorHandlingScalar, CheckNotNaNVectorization) { - using stan::math::check_not_nan; - const char* function = "check_not_nan"; - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 0); - EXPECT_NO_THROW( - check_not_nan(function, "m", std::vector{m, m, m})); - Eigen::MatrixXd m2 = m; - m2(1, 1) = stan::math::NOT_A_NUMBER; - EXPECT_THROW( - check_not_nan(function, "m", std::vector{m, m2, m}), - std::domain_error); -} diff --git a/test/unit/math/prim/err/check_pos_definite_test.cpp b/test/unit/math/prim/err/check_pos_definite_test.cpp index 8ef8e49c2fa..7b3bdc4c646 100644 --- a/test/unit/math/prim/err/check_pos_definite_test.cpp +++ b/test/unit/math/prim/err/check_pos_definite_test.cpp @@ -147,7 +147,7 @@ TEST_F(ErrorHandlingMatrixPosDef, checkPosDefinite_nan) { y << nan; std::stringstream expected_msg; - expected_msg << "function: y[row=1, col=1] is nan, but must not be nan!"; + expected_msg << "function: y[1] is nan, but must not be nan!"; EXPECT_THROW_MSG(check_pos_definite(function, "y", y), std::domain_error, expected_msg.str()); @@ -167,7 +167,20 @@ TEST_F(ErrorHandlingMatrixPosDef, checkPosDefinite_nan) { y << 2, -1, 0, -1, 2, -1, 0, -1, 2; y(i, j) = nan; if (i >= j) { - EXPECT_THROW(check_pos_definite(function, "y", y), std::domain_error); + // expected_msg.str(""); + // if (i == j) + // expected_msg << "function: y[" + // << j*y.cols() + i + 1 + // << "] is " << nan + // << ", but must not be nan!"; + // else + // expected_msg << "function: y is not symmetric. " + // << "y[" << j+1 << ", " << i+1 << "] = " << y(j, i) + // << ", but y[" << i+1 << ", " << j+1 << "] = " + // << y(i, j); + EXPECT_THROW(check_pos_definite(function, "y", y), + // , expected_msg.str()); + std::domain_error); } } diff --git a/test/unit/math/prim/err/check_pos_semidefinite_test.cpp b/test/unit/math/prim/err/check_pos_semidefinite_test.cpp index 5ef12b3f055..f3dada20449 100644 --- a/test/unit/math/prim/err/check_pos_semidefinite_test.cpp +++ b/test/unit/math/prim/err/check_pos_semidefinite_test.cpp @@ -77,7 +77,7 @@ TEST_F(ErrorHandlingMatrixPosSemiDef, checkPosSemidefinite_nan) { y << 2, -1, 0, -1, nan, -1, 0, -1, 2; EXPECT_THROW_MSG(check_pos_semidefinite(function, "y", y), std::domain_error, - "function: y[row=2, col=2] is nan, but must not be nan!"); + "function: y[5] is nan, but must not be nan!"); y << 2, -1, 0, -1, 2, nan, 0, nan, 2; EXPECT_THROW_MSG( diff --git a/test/unit/math/prim/err/check_positive_finite_test.cpp b/test/unit/math/prim/err/check_positive_finite_test.cpp index 5f369764034..3962341785d 100644 --- a/test/unit/math/prim/err/check_positive_finite_test.cpp +++ b/test/unit/math/prim/err/check_positive_finite_test.cpp @@ -15,11 +15,11 @@ TEST(ErrorHandlingArr, CheckPositiveFinite_Vector) { EXPECT_THROW(check_positive_finite(function, "x", x), std::domain_error) << "check_positive_finite should throw exception on Inf"; - x = {-1, 2, 1}; + x = {-1, 2, std::numeric_limits::infinity()}; EXPECT_THROW(check_positive_finite(function, "x", x), std::domain_error) << "check_positive_finite should throw exception on negative x"; - x = {0, 2, 1}; + x = {0, 2, std::numeric_limits::infinity()}; EXPECT_THROW(check_positive_finite(function, "x", x), std::domain_error) << "check_positive_finite should throw exception on x=0"; @@ -62,12 +62,12 @@ TEST(ErrorHandlingMat, CheckPositiveFinite_Matrix) { << "check_positive_finite should throw exception on Inf"; x.resize(3); - x << 0, 1, 2; + x << 0, 1, std::numeric_limits::infinity(); EXPECT_THROW(check_positive_finite(function, "x", x), std::domain_error) << "check_positive_finite should throw exception on x=0"; x.resize(3); - x << -1, 1, 2; + x << -1, 1, std::numeric_limits::infinity(); EXPECT_THROW(check_positive_finite(function, "x", x), std::domain_error) << "check_positive_finite should throw exception on x=-1"; @@ -188,20 +188,3 @@ TEST(ErrorHandlingScalar, CheckPositiveFinite_nan) { EXPECT_THROW(check_positive_finite(function, "x", nan), std::domain_error); } - -TEST(ErrorHandlingScalar, CheckPositiveFiniteVectorization) { - using stan::math::check_positive_finite; - const char* function = "check_positive_finite"; - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 1); - EXPECT_NO_THROW(check_positive_finite(function, "m", - std::vector{m, m, m})); - Eigen::MatrixXd m2 = m; - m2(1, 1) = -1; - EXPECT_THROW(check_positive_finite(function, "m", - std::vector{m, m2, m}), - std::domain_error); - m2(1, 1) = stan::math::NOT_A_NUMBER; - EXPECT_THROW(check_positive_finite(function, "m", - std::vector{m, m2, m}), - std::domain_error); -} diff --git a/test/unit/math/prim/err/check_positive_test.cpp b/test/unit/math/prim/err/check_positive_test.cpp index 427d927584a..987f5b62a77 100644 --- a/test/unit/math/prim/err/check_positive_test.cpp +++ b/test/unit/math/prim/err/check_positive_test.cpp @@ -5,89 +5,71 @@ TEST(ErrorHandlingArr, CheckPositive) { using stan::math::check_positive; + const char* function = "check_positive"; + std::vector x = {1, 2, 3}; for (size_t i = 0; i < x.size(); i++) { - EXPECT_NO_THROW(check_positive("check_positive_test", "x", x)); + EXPECT_NO_THROW(check_positive(function, "x", x)); } } TEST(ErrorHandlingArr, CheckPositive_nan) { using stan::math::check_positive; + const char* function = "check_positive"; + double nan = std::numeric_limits::quiet_NaN(); std::vector x = {1, 2, 3}; for (size_t i = 0; i < x.size(); i++) { x[i] = nan; - EXPECT_THROW(check_positive("check_positive_test", "x", x), - std::domain_error); + EXPECT_THROW(check_positive(function, "x", x), std::domain_error); x[i] = i; } } TEST(ErrorHandlingMat, CheckPositive) { using stan::math::check_positive; + const char* function = "check_positive"; + Eigen::Matrix x_mat(3); x_mat << 1, 2, 3; for (int i = 0; i < x_mat.size(); i++) { - EXPECT_NO_THROW(check_positive("check_positive_test", "x", x_mat)); + EXPECT_NO_THROW(check_positive(function, "x", x_mat)); } x_mat(0) = 0; - EXPECT_THROW(check_positive("check_positive_test", "x", x_mat), - std::domain_error); + EXPECT_THROW(check_positive(function, "x", x_mat), std::domain_error); } TEST(ErrorHandlingMat, CheckPositive_nan) { using stan::math::check_positive; + const char* function = "check_positive"; + double nan = std::numeric_limits::quiet_NaN(); Eigen::Matrix x_mat(3); x_mat << 1, 2, 3; for (int i = 0; i < x_mat.size(); i++) { x_mat(i) = nan; - EXPECT_THROW(check_positive("check_positive_test", "x", x_mat), - std::domain_error); + EXPECT_THROW(check_positive(function, "x", x_mat), std::domain_error); x_mat(i) = i; } } TEST(ErrorHandlingScalar, CheckPositive) { using stan::math::check_positive; - EXPECT_NO_THROW(check_positive("check_positive_test", "x", 3.0)); + const char* function = "check_positive"; + + EXPECT_NO_THROW(check_positive(function, "x", 3.0)); } TEST(ErrorHandlingScalar, CheckPositive_nan) { using stan::math::check_positive; - double nan = std::numeric_limits::quiet_NaN(); + const char* function = "check_positive"; - EXPECT_THROW(check_positive("check_positive_test", "x", nan), - std::domain_error); -} - -TEST(ErrorHandlingScalar, CheckPositive_0) { - using stan::math::check_positive; - EXPECT_THROW(check_positive("check_positive_test", "x", 0u), - std::domain_error); - EXPECT_THROW(check_positive("check_positive_test", "x", (size_t)0), - std::domain_error); - EXPECT_THROW(check_positive("check_positive_test", "x", 0.0), - std::domain_error); - EXPECT_THROW(check_positive("check_positive_test", "x", -0.0), - std::domain_error); - EXPECT_THROW(check_positive("check_positive_test", "x", 0), - std::domain_error); -} + double nan = std::numeric_limits::quiet_NaN(); -TEST(ErrorHandlingScalar, CheckPositiveVectorization) { - using stan::math::check_positive; - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 1); - EXPECT_NO_THROW(check_positive("check_positive_test", "m", - std::vector{m, m, m})); - Eigen::MatrixXd m2 = m; - m2(1, 1) = -1; - EXPECT_THROW(check_positive("check_positive_test", "m", - std::vector{m, m2, m}), - std::domain_error); + EXPECT_THROW(check_positive(function, "x", nan), std::domain_error); } diff --git a/test/unit/math/prim/err/is_nonnegative_test.cpp b/test/unit/math/prim/err/is_nonnegative_test.cpp deleted file mode 100644 index c22664533b5..00000000000 --- a/test/unit/math/prim/err/is_nonnegative_test.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include -#include -#include - -using stan::math::is_nonnegative; - -TEST(ErrorHandlingScalar, isNonnegative) { - EXPECT_FALSE(is_nonnegative(stan::math::NEGATIVE_INFTY)); - EXPECT_FALSE(is_nonnegative(-3.0)); - EXPECT_FALSE(is_nonnegative(-3)); - EXPECT_TRUE(is_nonnegative(-0.0)); - EXPECT_TRUE(is_nonnegative(0.0)); - EXPECT_TRUE(is_nonnegative(0u)); - EXPECT_TRUE(is_nonnegative((size_t)0)); - EXPECT_TRUE(is_nonnegative(0)); - EXPECT_TRUE(is_nonnegative(3.0)); - EXPECT_TRUE(is_nonnegative(3)); - EXPECT_TRUE(is_nonnegative(stan::math::INFTY)); - EXPECT_FALSE(is_nonnegative(stan::math::NOT_A_NUMBER)); -} - -TEST(ErrorHandlingScalar, isNonnegativeVectorization) { - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 0); - EXPECT_TRUE(is_nonnegative(std::vector{m, m, m})); - Eigen::MatrixXd m2 = m; - m2(1, 1) = -0.5; - EXPECT_FALSE(is_nonnegative(std::vector{m, m2, m})); -} diff --git a/test/unit/math/prim/err/is_not_nan_test.cpp b/test/unit/math/prim/err/is_not_nan_test.cpp index 360dfea2f96..1f9c2b046ea 100644 --- a/test/unit/math/prim/err/is_not_nan_test.cpp +++ b/test/unit/math/prim/err/is_not_nan_test.cpp @@ -1,42 +1,19 @@ +#include #include -#include -#include -#include +#include TEST(ErrorHandlingScalar, isNotNan) { using stan::math::is_not_nan; + double x = 0; - EXPECT_TRUE(is_not_nan(stan::math::NEGATIVE_INFTY)); - EXPECT_TRUE(is_not_nan(-3.0)); - EXPECT_TRUE(is_not_nan(-3)); - EXPECT_TRUE(is_not_nan(-0.0)); - EXPECT_TRUE(is_not_nan(0.0)); - EXPECT_TRUE(is_not_nan(0u)); - EXPECT_TRUE(is_not_nan((size_t)0)); - EXPECT_TRUE(is_not_nan(0)); - EXPECT_TRUE(is_not_nan(3.0)); - EXPECT_TRUE(is_not_nan(3)); - EXPECT_TRUE(is_not_nan(stan::math::INFTY)); - EXPECT_TRUE(is_not_nan(-stan::math::INFTY)); - EXPECT_FALSE(is_not_nan(stan::math::NOT_A_NUMBER)); -} + EXPECT_TRUE(is_not_nan(x)); -TEST(ErrorHandlingScalar, isNotNanEigen) { - using stan::math::is_not_nan; + x = std::numeric_limits::infinity(); + EXPECT_TRUE(is_not_nan(x)); - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 1); - EXPECT_TRUE(is_not_nan(m)); - Eigen::MatrixXd m2 = m; - m2(1, 1) = stan::math::NOT_A_NUMBER; - EXPECT_FALSE(is_not_nan(m2)); -} - -TEST(ErrorHandlingScalar, isNotNanVectorization) { - using stan::math::is_not_nan; + x = -std::numeric_limits::infinity(); + EXPECT_TRUE(is_not_nan(x)); - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 1); - EXPECT_TRUE(is_not_nan(std::vector{m, m, m})); - Eigen::MatrixXd m2 = m; - m2(1, 1) = stan::math::NOT_A_NUMBER; - EXPECT_FALSE(is_not_nan(std::vector{m, m2, m})); + x = std::numeric_limits::quiet_NaN(); + EXPECT_FALSE(is_not_nan(x)); } diff --git a/test/unit/math/prim/err/is_positive_finite_test.cpp b/test/unit/math/prim/err/is_positive_finite_test.cpp deleted file mode 100644 index 6f73b34b96e..00000000000 --- a/test/unit/math/prim/err/is_positive_finite_test.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -#include - -using stan::math::is_positive_finite; - -TEST(ErrorHandlingScalar, isPositiveFinite) { - EXPECT_FALSE(is_positive_finite(stan::math::NEGATIVE_INFTY)); - EXPECT_FALSE(is_positive_finite(-3.0)); - EXPECT_FALSE(is_positive_finite(-3)); - EXPECT_FALSE(is_positive_finite(-0.0)); - EXPECT_FALSE(is_positive_finite(0.0)); - EXPECT_FALSE(is_positive_finite(0u)); - EXPECT_FALSE(is_positive_finite((size_t)0)); - EXPECT_FALSE(is_positive_finite(0)); - EXPECT_TRUE(is_positive_finite(3.0)); - EXPECT_TRUE(is_positive_finite(3)); - EXPECT_FALSE(is_positive_finite(stan::math::INFTY)); - EXPECT_FALSE(is_positive_finite(stan::math::NOT_A_NUMBER)); -} - -TEST(ErrorHandlingScalar, isPositiveFiniteVectorization) { - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 1); - EXPECT_TRUE(is_positive_finite(std::vector{m, m, m})); - EXPECT_TRUE(is_positive_finite(m)); - Eigen::MatrixXd m2 = m; - m2(1, 1) = -0.5; - EXPECT_FALSE(is_positive_finite(std::vector{m, m2, m})); - EXPECT_FALSE(is_positive_finite(m2)); - m2(1, 1) = stan::math::NOT_A_NUMBER; - EXPECT_FALSE(is_positive_finite(std::vector{m, m2, m})); - EXPECT_FALSE(is_positive_finite(m2)); -} diff --git a/test/unit/math/prim/err/is_positive_test.cpp b/test/unit/math/prim/err/is_positive_test.cpp index f84e3433bee..f5a023253d8 100644 --- a/test/unit/math/prim/err/is_positive_test.cpp +++ b/test/unit/math/prim/err/is_positive_test.cpp @@ -1,29 +1,14 @@ +#include #include -#include -#include -#include +#include TEST(ErrorHandlingScalar, isPositive) { using stan::math::is_positive; - EXPECT_FALSE(is_positive(stan::math::NEGATIVE_INFTY)); - EXPECT_FALSE(is_positive(-3.0)); - EXPECT_FALSE(is_positive(-3)); - EXPECT_FALSE(is_positive(-0.0)); - EXPECT_FALSE(is_positive(0.0)); - EXPECT_FALSE(is_positive(0u)); - EXPECT_FALSE(is_positive((size_t)0)); - EXPECT_FALSE(is_positive(0)); EXPECT_TRUE(is_positive(3.0)); - EXPECT_TRUE(is_positive(3)); - EXPECT_TRUE(is_positive(stan::math::INFTY)); - EXPECT_FALSE(is_positive(stan::math::NOT_A_NUMBER)); } -TEST(ErrorHandlingScalar, isPositiveVectorization) { +TEST(ErrorHandlingScalar, isPositive_nan) { using stan::math::is_positive; - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 1); - EXPECT_TRUE(is_positive(std::vector{m, m, m})); - Eigen::MatrixXd m2 = m; - m2(1, 1) = -0.5; - EXPECT_FALSE(is_positive(std::vector{m, m2, m})); + double nan = std::numeric_limits::quiet_NaN(); + EXPECT_FALSE(is_positive(nan)); } diff --git a/test/unit/math/prim/err/is_scal_finite_test.cpp b/test/unit/math/prim/err/is_scal_finite_test.cpp index b38e2abd515..715d40fab76 100644 --- a/test/unit/math/prim/err/is_scal_finite_test.cpp +++ b/test/unit/math/prim/err/is_scal_finite_test.cpp @@ -1,39 +1,24 @@ +#include #include -#include -#include -#include +#include TEST(ErrorHandlingScalar, isScalFinite) { using stan::math::is_scal_finite; - EXPECT_FALSE(is_scal_finite(stan::math::NEGATIVE_INFTY)); - EXPECT_TRUE(is_scal_finite(-3.0)); - EXPECT_TRUE(is_scal_finite(-3)); - EXPECT_TRUE(is_scal_finite(-0.0)); - EXPECT_TRUE(is_scal_finite(0.0)); - EXPECT_TRUE(is_scal_finite(0u)); - EXPECT_TRUE(is_scal_finite((size_t)0)); - EXPECT_TRUE(is_scal_finite(0)); - EXPECT_TRUE(is_scal_finite(3.0)); - EXPECT_TRUE(is_scal_finite(3)); - EXPECT_FALSE(is_scal_finite(stan::math::INFTY)); - EXPECT_FALSE(is_scal_finite(-stan::math::INFTY)); - EXPECT_FALSE(is_scal_finite(stan::math::NOT_A_NUMBER)); -} + double x = 0; + EXPECT_TRUE(is_scal_finite(x)); -TEST(ErrorHandlingScalar, isScalFiniteEigen) { - using stan::math::is_scal_finite; - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 1); - EXPECT_TRUE(is_scal_finite(m)); - Eigen::MatrixXd m2 = m; - m2(1, 1) = stan::math::NOT_A_NUMBER; - EXPECT_FALSE(is_scal_finite(m2)); + x = std::numeric_limits::infinity(); + EXPECT_FALSE(is_scal_finite(x)); + + x = -std::numeric_limits::infinity(); + EXPECT_FALSE(is_scal_finite(x)); + + x = std::numeric_limits::quiet_NaN(); + EXPECT_FALSE(is_scal_finite(x)); } -TEST(ErrorHandlingScalar, isScalFiniteVectorization) { +TEST(ErrorHandlingScalar, isScalFinite_nan) { using stan::math::is_scal_finite; - Eigen::MatrixXd m = Eigen::MatrixXd::Constant(3, 2, 1); - EXPECT_TRUE(is_scal_finite(std::vector{m, m, m})); - Eigen::MatrixXd m2 = m; - m2(1, 1) = stan::math::NOT_A_NUMBER; - EXPECT_FALSE(is_scal_finite(std::vector{m, m2, m})); + double nan = std::numeric_limits::quiet_NaN(); + EXPECT_FALSE(is_scal_finite(nan)); } diff --git a/test/unit/math/prim/fun/csr_u_to_z_test.cpp b/test/unit/math/prim/fun/csr_u_to_z_test.cpp index 2acfa81cdb3..5879f8918ad 100644 --- a/test/unit/math/prim/fun/csr_u_to_z_test.cpp +++ b/test/unit/math/prim/fun/csr_u_to_z_test.cpp @@ -7,15 +7,7 @@ TEST(MathUtoZ, sizeZeroInput) { using stan::math::csr_u_to_z; std::vector u(0); for (int n = -1; n < 2; ++n) - EXPECT_THROW(csr_u_to_z(u, n), std::domain_error); - u.push_back(1); - for (int n = -1; n < 2; ++n) - EXPECT_THROW(csr_u_to_z(u, n), std::domain_error); - u.push_back(1); - EXPECT_THROW(csr_u_to_z(u, -1), std::out_of_range); - EXPECT_NO_THROW(csr_u_to_z(u, 0)); - EXPECT_THROW(csr_u_to_z(u, 1), std::out_of_range); - EXPECT_THROW(csr_u_to_z(u, 2), std::out_of_range); + EXPECT_THROW(csr_u_to_z(u, n), std::out_of_range); } TEST(MathUtoZ, nonemptyInput) { using stan::math::csr_u_to_z; diff --git a/test/unit/math/prim/prob/neg_binomial_2_log_test.cpp b/test/unit/math/prim/prob/neg_binomial_2_log_test.cpp index f6193ae4078..7742732d86f 100644 --- a/test/unit/math/prim/prob/neg_binomial_2_log_test.cpp +++ b/test/unit/math/prim/prob/neg_binomial_2_log_test.cpp @@ -43,7 +43,7 @@ TEST(ProbDistributionsNegBinomial2Log, error_check) { error_msg = "neg_binomial_2_log_rng: Exponential " "of the log-location parameter divided by the precision " - "parameter is inf, but must be positive and finite!"; + "parameter is inf, but must be finite!"; try { stan::math::neg_binomial_2_log_rng(log(1e300), 1e-300, rng); FAIL() << "neg_binomial_2_log_rng should have thrown" << std::endl; diff --git a/test/unit/math/prim/prob/neg_binomial_2_test.cpp b/test/unit/math/prim/prob/neg_binomial_2_test.cpp index 17a5da1429f..47853d4e0b8 100644 --- a/test/unit/math/prim/prob/neg_binomial_2_test.cpp +++ b/test/unit/math/prim/prob/neg_binomial_2_test.cpp @@ -63,7 +63,7 @@ TEST(ProbDistributionsNegBinomial2, error_check) { error_msg = "neg_binomial_2_rng: Location parameter " "divided by the precision parameter is " - "inf, but must be positive and finite!"; + "inf, but must be finite!"; try { stan::math::neg_binomial_2_rng(1e300, 1e-300, rng); FAIL() << "neg_binomial_2_rng should have thrown" << std::endl; diff --git a/test/unit/math/rev/err/check_pos_semidefinite_test.cpp b/test/unit/math/rev/err/check_pos_semidefinite_test.cpp index 1fa789ee6a4..b384b023483 100644 --- a/test/unit/math/rev/err/check_pos_semidefinite_test.cpp +++ b/test/unit/math/rev/err/check_pos_semidefinite_test.cpp @@ -14,10 +14,9 @@ TEST(AgradRevErrorHandlingMatrix, checkPosSemiDefiniteMatrix_nan) { y.resize(1, 1); y << nan; - EXPECT_THROW_MSG( - check_pos_semidefinite("checkPosDefiniteMatrix", "y", y), - std::domain_error, - "checkPosDefiniteMatrix: y[row=1, col=1] is nan, but must not be nan!"); + EXPECT_THROW_MSG(check_pos_semidefinite("checkPosDefiniteMatrix", "y", y), + std::domain_error, + "checkPosDefiniteMatrix: y[1] is nan, but must not be nan!"); y.resize(3, 3); y << 2, -1, 0, -1, 2, -1, 0, -1, 2;