|
| 1 | +#include <stan/math/prim.hpp> |
| 2 | +#include <test/unit/util.hpp> |
| 3 | +#include <gtest/gtest.h> |
| 4 | +#include <cmath> |
| 5 | +#include <limits> |
| 6 | + |
| 7 | +TEST(MathFunctions, to_int_values) { |
| 8 | + using stan::math::to_int; |
| 9 | + |
| 10 | + EXPECT_EQ(2, to_int(2.0)); |
| 11 | + EXPECT_EQ(2, to_int(2.1)); |
| 12 | + EXPECT_EQ(2, to_int(2.9)); |
| 13 | + EXPECT_EQ(2, to_int(2.999999999)); |
| 14 | + |
| 15 | + EXPECT_EQ(-36574, to_int(-36574.0)); |
| 16 | + EXPECT_EQ(-36574, to_int(-36574.1)); |
| 17 | + EXPECT_EQ(-36574, to_int(-36574.9)); |
| 18 | + EXPECT_EQ(-36574, to_int(-36574.999999999)); |
| 19 | +} |
| 20 | + |
| 21 | +TEST(MathFunctions, to_int_errors) { |
| 22 | + using stan::math::INFTY; |
| 23 | + using stan::math::NEGATIVE_INFTY; |
| 24 | + using stan::math::NOT_A_NUMBER; |
| 25 | + using stan::math::to_int; |
| 26 | + |
| 27 | + EXPECT_THROW(to_int(std::numeric_limits<int>::max() + 1.0), |
| 28 | + std::domain_error); |
| 29 | + EXPECT_THROW(to_int(std::numeric_limits<int>::min() - 1.0), |
| 30 | + std::domain_error); |
| 31 | + |
| 32 | + EXPECT_THROW(to_int(NOT_A_NUMBER), std::domain_error); |
| 33 | + EXPECT_THROW(to_int(INFTY), std::domain_error); |
| 34 | + EXPECT_THROW(to_int(NEGATIVE_INFTY), std::domain_error); |
| 35 | +} |
| 36 | + |
| 37 | +TEST(MathFunctions, to_int_vec) { |
| 38 | + using stan::math::to_int; |
| 39 | + |
| 40 | + std::vector<double> inputs_1{2.1, -34.64, 10.89, 1000000}; |
| 41 | + std::vector<double> inputs_2{-409831.987, 403.1, 10.61, -0.00001}; |
| 42 | + std::vector<std::vector<double>> inputs{inputs_1, inputs_2}; |
| 43 | + |
| 44 | + std::vector<int> target_result_1{2, -34, 10, 1000000}; |
| 45 | + std::vector<int> target_result_2{-409831, 403, 10, 0}; |
| 46 | + std::vector<std::vector<int>> target_result{target_result_1, target_result_2}; |
| 47 | + |
| 48 | + EXPECT_STD_VECTOR_EQ(to_int(inputs), target_result); |
| 49 | + |
| 50 | + inputs[0][2] = std::numeric_limits<int>::min() - 1.0; |
| 51 | + EXPECT_THROW(to_int(inputs), std::domain_error); |
| 52 | + |
| 53 | + std::vector<double> inputs_empty; |
| 54 | + std::vector<double> inputs_size_one{1.5}; |
| 55 | + |
| 56 | + EXPECT_NO_THROW(to_int(inputs_empty)); |
| 57 | + EXPECT_STD_VECTOR_EQ(to_int(inputs_size_one), std::vector<int>{1}); |
| 58 | +} |
0 commit comments