Skip to content

Commit

Permalink
Added logic to do argument promotion for tests (Issue #993)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbales2 committed Aug 20, 2018
1 parent 2057ad1 commit 8d2d46b
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions stan/math/prim/arr.hpp
Expand Up @@ -26,6 +26,7 @@
#include <stan/math/prim/arr/fun/fill.hpp>
#include <stan/math/prim/arr/fun/inverse_softmax.hpp>
#include <stan/math/prim/arr/fun/log_sum_exp.hpp>
#include <stan/math/prim/arr/fun/promote_double_to_T.hpp>
#include <stan/math/prim/arr/fun/promote_elements.hpp>
#include <stan/math/prim/arr/fun/promote_scalar.hpp>
#include <stan/math/prim/arr/fun/promote_scalar_type.hpp>
Expand Down
26 changes: 26 additions & 0 deletions stan/math/prim/arr/fun/promote_double_to_T.hpp
@@ -0,0 +1,26 @@
#ifndef STAN_MATH_PRIM_ARR_FUN_PROMOTE_DOUBLE_TO_T_HPP
#define STAN_MATH_PRIM_ARR_FUN_PROMOTE_DOUBLE_TO_T_HPP

#include <vector>

namespace stan {
namespace math {

template <typename T, typename R>
inline const std::vector<R>& promote_double_to_T(const std::vector<R>& x) {
return x;
}

template <typename T>
inline std::vector<T> promote_double_to_T(const std::vector<double>& x) {
std::vector<T> out;
out.reserve(x.size());
for (int i = 0; i < x.size(); ++i) {
out.push_back(x[i]);
}
return out;
}

} // namespace math
} // namespace stan
#endif
1 change: 1 addition & 0 deletions stan/math/prim/scal.hpp
Expand Up @@ -159,6 +159,7 @@
#include <stan/math/prim/scal/fun/primitive_value.hpp>
#include <stan/math/prim/scal/fun/prob_constrain.hpp>
#include <stan/math/prim/scal/fun/prob_free.hpp>
#include <stan/math/prim/scal/fun/promote_double_to_T.hpp>
#include <stan/math/prim/scal/fun/promote_elements.hpp>
#include <stan/math/prim/scal/fun/promote_scalar.hpp>
#include <stan/math/prim/scal/fun/promote_scalar_type.hpp>
Expand Down
21 changes: 21 additions & 0 deletions stan/math/prim/scal/fun/promote_double_to_T.hpp
@@ -0,0 +1,21 @@
#ifndef STAN_MATH_PRIM_SCAL_FUN_PROMOTE_DOUBLE_TO_T_HPP
#define STAN_MATH_PRIM_SCAL_FUN_PROMOTE_DOUBLE_TO_T_HPP

#include <vector>

namespace stan {
namespace math {

template <typename T, typename R>
inline const R& promote_double_to_T(const R& x) {
return x;
}

template <typename T>
inline T promote_double_to_T(double x) {
return x;
}

} // namespace math
} // namespace stan
#endif
24 changes: 24 additions & 0 deletions test/unit/math/prim/arr/fun/promote_double_to_T_test.cpp
@@ -0,0 +1,24 @@
#include <stan/math/prim/arr.hpp>
#include <gtest/gtest.h>
#include <type_traits>
#include <vector>

TEST(MathFunctions, promote_double_to_T_std_vector_int) {
std::vector<int> x(3);
EXPECT_TRUE((std::is_same<const std::vector<int>&,
decltype(stan::math::promote_double_to_T<double>(
x))>::value));
EXPECT_FALSE((std::is_same<std::vector<double>,
decltype(stan::math::promote_double_to_T<double>(
x))>::value));
}

TEST(MathFunctions, promote_double_to_T_std_vector_double) {
std::vector<double> x(3);
EXPECT_TRUE((std::is_same<std::vector<float>,
decltype(stan::math::promote_double_to_T<float>(
x))>::value));
EXPECT_FALSE((std::is_same<const std::vector<double>&,
decltype(stan::math::promote_double_to_T<float>(
x))>::value));
}
23 changes: 23 additions & 0 deletions test/unit/math/prim/scal/fun/promote_double_to_T_test.cpp
@@ -0,0 +1,23 @@
#include <stan/math/prim/scal.hpp>
#include <gtest/gtest.h>
#include <type_traits>

TEST(MathFunctions, promote_double_to_T_int) {
int x = 3;
EXPECT_TRUE((
std::is_same<const int&, decltype(stan::math::promote_double_to_T<double>(
x))>::value));
EXPECT_FALSE(
(std::is_same<double, decltype(stan::math::promote_double_to_T<double>(
x))>::value));
}

TEST(MathFunctions, promote_double_to_T_double) {
double x = 3.0;
EXPECT_TRUE(
(std::is_same<float, decltype(stan::math::promote_double_to_T<float>(
x))>::value));
EXPECT_FALSE((std::is_same<const double&,
decltype(stan::math::promote_double_to_T<float>(
x))>::value));
}

0 comments on commit 8d2d46b

Please sign in to comment.