Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added logic to do argument promotion for tests (Issue #993)
- Loading branch information
Showing
6 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |