Skip to content

Commit

Permalink
Merge pull request #1895 from stan-dev/feature/1886-test-util-cleanup…
Browse files Browse the repository at this point in the history
…-pt1

Feature/1886 /test utility cleanup - part 1
  • Loading branch information
rok-cesnovar authored Jun 22, 2020
2 parents 3415717 + 87cf933 commit 2a7a857
Show file tree
Hide file tree
Showing 90 changed files with 556 additions and 609 deletions.
4 changes: 2 additions & 2 deletions stan/math/prim/eigen_plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ struct val_Op{
//Returns value from a vari*
template<typename T = Scalar>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
std::enable_if_t<std::is_pointer<T>::value, reverse_return_t<T>>
std::enable_if_t<std::is_pointer<T>::value, const double&>
operator()(T &v) const { return v->val_; }

//Returns value from a var
template<typename T = Scalar>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
std::enable_if_t<(!std::is_pointer<T>::value && !is_fvar<T>::value
&& !std::is_arithmetic<T>::value), reverse_return_t<T>>
&& !std::is_arithmetic<T>::value), const double&>
operator()(T &v) const { return v.vi_->val_; }

//Returns value from an fvar
Expand Down
52 changes: 27 additions & 25 deletions test/unit/math/fwd/eigen_plugins_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stan/math/fwd.hpp>
#include <test/unit/math/prim/fun/expect_matrix_eq.hpp>
#include <test/unit/util.hpp>
#include <gtest/gtest.h>

TEST(AgradFwdMatrixAddons, fvar_double_matrix) {
Expand All @@ -18,11 +18,11 @@ TEST(AgradFwdMatrixAddons, fvar_double_matrix) {
}
}

expect_matrix_eq(vals, mat_in.val());
expect_matrix_eq(vals.array().exp(), mat_in.val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(vals, mat_in.val());
EXPECT_MATRIX_FLOAT_EQ(vals.array().exp(), mat_in.val().array().exp());

expect_matrix_eq(derivs, mat_in.d());
expect_matrix_eq(derivs.array().exp(), mat_in.d().array().exp());
EXPECT_MATRIX_FLOAT_EQ(derivs, mat_in.d());
EXPECT_MATRIX_FLOAT_EQ(derivs.array().exp(), mat_in.d().array().exp());

EXPECT_EQ(mat_in.val().rows(), vals.rows());
EXPECT_EQ(mat_in.val().cols(), vals.cols());
Expand All @@ -47,11 +47,11 @@ TEST(AgradFwdMatrixAddons, fvarfvar_double_matrix) {
}
}

expect_matrix_eq(vals, mat_in.val().val());
expect_matrix_eq(vals.array().exp(), mat_in.val().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(vals, mat_in.val().val());
EXPECT_MATRIX_FLOAT_EQ(vals.array().exp(), mat_in.val().val().array().exp());

expect_matrix_eq(derivs, mat_in.d().val());
expect_matrix_eq(derivs.array().exp(), mat_in.d().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(derivs, mat_in.d().val());
EXPECT_MATRIX_FLOAT_EQ(derivs.array().exp(), mat_in.d().val().array().exp());

EXPECT_EQ(mat_in.val().rows(), vals.rows());
EXPECT_EQ(mat_in.val().cols(), vals.cols());
Expand All @@ -74,11 +74,11 @@ TEST(AgradFwdMatrixAddons, fvar_double_vector) {
vec_in(i).d_ = derivs(i);
}

expect_matrix_eq(vals, vec_in.val());
expect_matrix_eq(vals.array().exp(), vec_in.val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(vals, vec_in.val());
EXPECT_MATRIX_FLOAT_EQ(vals.array().exp(), vec_in.val().array().exp());

expect_matrix_eq(derivs, vec_in.d());
expect_matrix_eq(derivs.array().exp(), vec_in.d().array().exp());
EXPECT_MATRIX_FLOAT_EQ(derivs, vec_in.d());
EXPECT_MATRIX_FLOAT_EQ(derivs.array().exp(), vec_in.d().array().exp());

EXPECT_EQ(vec_in.val().rows(), vals.rows());
EXPECT_EQ(vec_in.val().cols(), vals.cols());
Expand All @@ -101,11 +101,11 @@ TEST(AgradFwdMatrixAddons, fvarfvar_double_vector) {
vec_in(i).d_.val_ = derivs(i);
}

expect_matrix_eq(vals, vec_in.val().val());
expect_matrix_eq(vals.array().exp(), vec_in.val().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(vals, vec_in.val().val());
EXPECT_MATRIX_FLOAT_EQ(vals.array().exp(), vec_in.val().val().array().exp());

expect_matrix_eq(derivs, vec_in.d().val());
expect_matrix_eq(derivs.array().exp(), vec_in.d().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(derivs, vec_in.d().val());
EXPECT_MATRIX_FLOAT_EQ(derivs.array().exp(), vec_in.d().val().array().exp());

EXPECT_EQ(vec_in.val().rows(), vals.rows());
EXPECT_EQ(vec_in.val().cols(), vals.cols());
Expand All @@ -128,11 +128,11 @@ TEST(AgradFwdMatrixAddons, fvar_double_rowvector) {
row_vec_in(i).d_ = derivs(i);
}

expect_matrix_eq(vals, row_vec_in.val());
expect_matrix_eq(vals.array().exp(), row_vec_in.val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(vals, row_vec_in.val());
EXPECT_MATRIX_FLOAT_EQ(vals.array().exp(), row_vec_in.val().array().exp());

expect_matrix_eq(derivs, row_vec_in.d());
expect_matrix_eq(derivs.array().exp(), row_vec_in.d().array().exp());
EXPECT_MATRIX_FLOAT_EQ(derivs, row_vec_in.d());
EXPECT_MATRIX_FLOAT_EQ(derivs.array().exp(), row_vec_in.d().array().exp());

EXPECT_EQ(row_vec_in.val().rows(), vals.rows());
EXPECT_EQ(row_vec_in.val().cols(), vals.cols());
Expand All @@ -155,11 +155,13 @@ TEST(AgradFwdMatrixAddons, fvarfvar_double_rowvector) {
row_vec_in(i).d_.val_ = derivs(i);
}

expect_matrix_eq(vals, row_vec_in.val().val());
expect_matrix_eq(vals.array().exp(), row_vec_in.val().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(vals, row_vec_in.val().val());
EXPECT_MATRIX_FLOAT_EQ(vals.array().exp(),
row_vec_in.val().val().array().exp());

expect_matrix_eq(derivs, row_vec_in.d().val());
expect_matrix_eq(derivs.array().exp(), row_vec_in.d().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(derivs, row_vec_in.d().val());
EXPECT_MATRIX_FLOAT_EQ(derivs.array().exp(),
row_vec_in.d().val().array().exp());

EXPECT_EQ(row_vec_in.val().rows(), vals.rows());
EXPECT_EQ(row_vec_in.val().cols(), vals.cols());
Expand Down
40 changes: 21 additions & 19 deletions test/unit/math/fwd/fun/read_fvar_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stan/math/fwd.hpp>
#include <test/unit/math/prim/fun/expect_matrix_eq.hpp>
#include <test/unit/util.hpp>
#include <gtest/gtest.h>

TEST(AgradFwdMatrix, read_fvar_mat_fd) {
Expand All @@ -13,8 +13,8 @@ TEST(AgradFwdMatrix, read_fvar_mat_fd) {
matrix_fvar.d() = MatrixXd::Random(100, 100);
read_fvar(matrix_fvar, matrix_val, matrix_deriv);

expect_matrix_eq(matrix_fvar.val(), matrix_val);
expect_matrix_eq(matrix_fvar.d(), matrix_deriv);
EXPECT_MATRIX_FLOAT_EQ(matrix_fvar.val(), matrix_val);
EXPECT_MATRIX_FLOAT_EQ(matrix_fvar.d(), matrix_deriv);
}

TEST(AgradFwdMatrix, read_fvar_vec_fd) {
Expand All @@ -28,8 +28,8 @@ TEST(AgradFwdMatrix, read_fvar_vec_fd) {
vector_fvar.d() = VectorXd::Random(100);
read_fvar(vector_fvar, vector_val, vector_deriv);

expect_matrix_eq(vector_fvar.val(), vector_val);
expect_matrix_eq(vector_fvar.d(), vector_deriv);
EXPECT_MATRIX_FLOAT_EQ(vector_fvar.val(), vector_val);
EXPECT_MATRIX_FLOAT_EQ(vector_fvar.d(), vector_deriv);
}

TEST(AgradFwdMatrix, read_fvar_rowvec_fd) {
Expand All @@ -43,8 +43,8 @@ TEST(AgradFwdMatrix, read_fvar_rowvec_fd) {
row_vector_fvar.d() = RowVectorXd::Random(100);
read_fvar(row_vector_fvar, row_vector_val, row_vector_deriv);

expect_matrix_eq(row_vector_fvar.val(), row_vector_val);
expect_matrix_eq(row_vector_fvar.d(), row_vector_deriv);
EXPECT_MATRIX_FLOAT_EQ(row_vector_fvar.val(), row_vector_val);
EXPECT_MATRIX_FLOAT_EQ(row_vector_fvar.d(), row_vector_deriv);
}

TEST(AgradFwdMatrix, read_fvar_expr_fd) {
Expand All @@ -58,8 +58,8 @@ TEST(AgradFwdMatrix, read_fvar_expr_fd) {
matrix_fvar.d() = MatrixXd::Random(100, 100);
read_fvar(matrix_fvar.diagonal(), matrix_diag_val, matrix_diag_deriv);

expect_matrix_eq(matrix_fvar.diagonal().val(), matrix_diag_val);
expect_matrix_eq(matrix_fvar.diagonal().d(), matrix_diag_deriv);
EXPECT_MATRIX_FLOAT_EQ(matrix_fvar.diagonal().val(), matrix_diag_val);
EXPECT_MATRIX_FLOAT_EQ(matrix_fvar.diagonal().d(), matrix_diag_deriv);
}

TEST(AgradFwdMatrix, read_fvar_mat_ffd) {
Expand All @@ -74,8 +74,8 @@ TEST(AgradFwdMatrix, read_fvar_mat_ffd) {
matrix_fvar_fvar.d().val() = MatrixXd::Random(100, 100);
read_fvar(matrix_fvar_fvar, matrix_val, matrix_deriv);

expect_matrix_eq(matrix_fvar_fvar.val().val(), matrix_val.val());
expect_matrix_eq(matrix_fvar_fvar.d().val(), matrix_deriv.val());
EXPECT_MATRIX_FLOAT_EQ(matrix_fvar_fvar.val().val(), matrix_val.val());
EXPECT_MATRIX_FLOAT_EQ(matrix_fvar_fvar.d().val(), matrix_deriv.val());
}

TEST(AgradFwdMatrix, read_fvar_vec_ffd) {
Expand All @@ -90,8 +90,8 @@ TEST(AgradFwdMatrix, read_fvar_vec_ffd) {
vector_fvar_fvar.d().val() = VectorXd::Random(100);
read_fvar(vector_fvar_fvar, vector_val, vector_deriv);

expect_matrix_eq(vector_fvar_fvar.val().val(), vector_val.val());
expect_matrix_eq(vector_fvar_fvar.d().val(), vector_deriv.val());
EXPECT_MATRIX_FLOAT_EQ(vector_fvar_fvar.val().val(), vector_val.val());
EXPECT_MATRIX_FLOAT_EQ(vector_fvar_fvar.d().val(), vector_deriv.val());
}

TEST(AgradFwdMatrix, read_fvar_rowvec_ffd) {
Expand All @@ -106,8 +106,10 @@ TEST(AgradFwdMatrix, read_fvar_rowvec_ffd) {
row_vector_fvar_fvar.d().val() = RowVectorXd::Random(100);
read_fvar(row_vector_fvar_fvar, row_vector_val, row_vector_deriv);

expect_matrix_eq(row_vector_fvar_fvar.val().val(), row_vector_val.val());
expect_matrix_eq(row_vector_fvar_fvar.d().val(), row_vector_deriv.val());
EXPECT_MATRIX_FLOAT_EQ(row_vector_fvar_fvar.val().val(),
row_vector_val.val());
EXPECT_MATRIX_FLOAT_EQ(row_vector_fvar_fvar.d().val(),
row_vector_deriv.val());
}

TEST(AgradFwdMatrix, read_fvar_expr_ffd) {
Expand All @@ -122,8 +124,8 @@ TEST(AgradFwdMatrix, read_fvar_expr_ffd) {
matrix_fvar_fvar.d().val() = MatrixXd::Random(100, 100);
read_fvar(matrix_fvar_fvar.diagonal(), matrix_diag_val, matrix_diag_deriv);

expect_matrix_eq(matrix_fvar_fvar.diagonal().val().val(),
matrix_diag_val.val());
expect_matrix_eq(matrix_fvar_fvar.diagonal().d().val(),
matrix_diag_deriv.val());
EXPECT_MATRIX_FLOAT_EQ(matrix_fvar_fvar.diagonal().val().val(),
matrix_diag_val.val());
EXPECT_MATRIX_FLOAT_EQ(matrix_fvar_fvar.diagonal().d().val(),
matrix_diag_deriv.val());
}
60 changes: 33 additions & 27 deletions test/unit/math/mix/eigen_plugins_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stan/math/mix.hpp>
#include <test/unit/math/prim/fun/expect_matrix_eq.hpp>
#include <test/unit/util.hpp>
#include <gtest/gtest.h>

TEST(AgradMixMatrixAddons, matrix_fv) {
Expand All @@ -17,11 +17,11 @@ TEST(AgradMixMatrixAddons, matrix_fv) {
}
}

expect_matrix_eq(vals, mat_in.val().val());
expect_matrix_eq(vals.array().exp(), mat_in.val().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(vals, mat_in.val().val());
EXPECT_MATRIX_FLOAT_EQ(vals.array().exp(), mat_in.val().val().array().exp());

expect_matrix_eq(derivs, mat_in.d().val());
expect_matrix_eq(derivs.array().exp(), mat_in.d().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(derivs, mat_in.d().val());
EXPECT_MATRIX_FLOAT_EQ(derivs.array().exp(), mat_in.d().val().array().exp());

EXPECT_EQ(mat_in.val().val().rows(), vals.rows());
EXPECT_EQ(mat_in.val().val().cols(), vals.cols());
Expand All @@ -45,11 +45,13 @@ TEST(AgradMixMatrixAddons, matrix_ffv) {
}
}

expect_matrix_eq(vals, mat_in.val().val().val());
expect_matrix_eq(vals.array().exp(), mat_in.val().val().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(vals, mat_in.val().val().val());
EXPECT_MATRIX_FLOAT_EQ(vals.array().exp(),
mat_in.val().val().val().array().exp());

expect_matrix_eq(derivs, mat_in.d().val().val());
expect_matrix_eq(derivs.array().exp(), mat_in.d().val().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(derivs, mat_in.d().val().val());
EXPECT_MATRIX_FLOAT_EQ(derivs.array().exp(),
mat_in.d().val().val().array().exp());

EXPECT_EQ(mat_in.val().val().val().rows(), vals.rows());
EXPECT_EQ(mat_in.val().val().val().cols(), vals.cols());
Expand All @@ -71,11 +73,11 @@ TEST(AgradMixMatrixAddons, vector_fv) {
vec_in(i).d_ = derivs(i);
}

expect_matrix_eq(vals, vec_in.val().val());
expect_matrix_eq(vals.array().exp(), vec_in.val().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(vals, vec_in.val().val());
EXPECT_MATRIX_FLOAT_EQ(vals.array().exp(), vec_in.val().val().array().exp());

expect_matrix_eq(derivs, vec_in.d().val());
expect_matrix_eq(derivs.array().exp(), vec_in.d().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(derivs, vec_in.d().val());
EXPECT_MATRIX_FLOAT_EQ(derivs.array().exp(), vec_in.d().val().array().exp());

EXPECT_EQ(vec_in.val().val().rows(), vals.rows());
EXPECT_EQ(vec_in.val().val().cols(), vals.cols());
Expand All @@ -97,11 +99,13 @@ TEST(AgradMixMatrixAddons, vector_ffv) {
vec_in(i).d_ = derivs(i);
}

expect_matrix_eq(vals, vec_in.val().val().val());
expect_matrix_eq(vals.array().exp(), vec_in.val().val().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(vals, vec_in.val().val().val());
EXPECT_MATRIX_FLOAT_EQ(vals.array().exp(),
vec_in.val().val().val().array().exp());

expect_matrix_eq(derivs, vec_in.d().val().val());
expect_matrix_eq(derivs.array().exp(), vec_in.d().val().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(derivs, vec_in.d().val().val());
EXPECT_MATRIX_FLOAT_EQ(derivs.array().exp(),
vec_in.d().val().val().array().exp());

EXPECT_EQ(vec_in.val().val().val().rows(), vals.rows());
EXPECT_EQ(vec_in.val().val().val().cols(), vals.cols());
Expand All @@ -123,11 +127,13 @@ TEST(AgradMixMatrixAddons, row_vector_fv) {
row_vec_in(i).d_ = derivs(i);
}

expect_matrix_eq(vals, row_vec_in.val().val());
expect_matrix_eq(vals.array().exp(), row_vec_in.val().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(vals, row_vec_in.val().val());
EXPECT_MATRIX_FLOAT_EQ(vals.array().exp(),
row_vec_in.val().val().array().exp());

expect_matrix_eq(derivs, row_vec_in.d().val());
expect_matrix_eq(derivs.array().exp(), row_vec_in.d().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(derivs, row_vec_in.d().val());
EXPECT_MATRIX_FLOAT_EQ(derivs.array().exp(),
row_vec_in.d().val().array().exp());

EXPECT_EQ(row_vec_in.val().val().rows(), vals.rows());
EXPECT_EQ(row_vec_in.val().val().cols(), vals.cols());
Expand All @@ -149,13 +155,13 @@ TEST(AgradMixMatrixAddons, row_vector_ffv) {
row_vec_in(i).d_ = derivs(i);
}

expect_matrix_eq(vals, row_vec_in.val().val().val());
expect_matrix_eq(vals.array().exp(),
row_vec_in.val().val().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(vals, row_vec_in.val().val().val());
EXPECT_MATRIX_FLOAT_EQ(vals.array().exp(),
row_vec_in.val().val().val().array().exp());

expect_matrix_eq(derivs, row_vec_in.d().val().val());
expect_matrix_eq(derivs.array().exp(),
row_vec_in.d().val().val().array().exp());
EXPECT_MATRIX_FLOAT_EQ(derivs, row_vec_in.d().val().val());
EXPECT_MATRIX_FLOAT_EQ(derivs.array().exp(),
row_vec_in.d().val().val().array().exp());

EXPECT_EQ(row_vec_in.val().val().val().rows(), vals.rows());
EXPECT_EQ(row_vec_in.val().val().val().cols(), vals.cols());
Expand Down
5 changes: 1 addition & 4 deletions test/unit/math/opencl/async_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

#include <stan/math/prim.hpp>
#include <stan/math/opencl/opencl.hpp>
#include <test/unit/util.hpp>
#include <gtest/gtest.h>

#define EXPECT_MATRIX_NEAR(A, B, DELTA) \
for (int i = 0; i < A.size(); i++) \
EXPECT_NEAR(A(i), B(i), DELTA);

// Test we can handle a semi-complicated but normal queue
TEST(async_opencl, thrash_opencl) {
auto size = 100;
Expand Down
5 changes: 1 addition & 4 deletions test/unit/math/opencl/cholesky_decompose_test.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#ifdef STAN_OPENCL
#include <stan/math/prim.hpp>
#include <stan/math/opencl/opencl.hpp>
#include <test/unit/util.hpp>
#include <gtest/gtest.h>
#include <algorithm>
#include <vector>

#define EXPECT_MATRIX_NEAR(A, B, DELTA) \
for (int i = 0; i < A.size(); i++) \
EXPECT_NEAR(A(i), B(i), DELTA);

TEST(MathMatrixOpenCL, cholesky_decompose_cpu_vs_cl_small) {
stan::math::matrix_d m0(3, 3);
m0 << 25, 15, -5, 15, 18, 0, -5, 0, 11;
Expand Down
5 changes: 1 addition & 4 deletions test/unit/math/opencl/kernel_cl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

#include <stan/math/prim.hpp>
#include <stan/math/opencl/opencl.hpp>
#include <test/unit/util.hpp>
#include <gtest/gtest.h>
#include <algorithm>

#define EXPECT_MATRIX_NEAR(A, B, DELTA) \
for (int i = 0; i < A.size(); i++) \
EXPECT_NEAR(A(i), B(i), DELTA);

TEST(MathGpu, make_kernel) {
stan::math::matrix_d m(3, 3);

Expand Down
5 changes: 1 addition & 4 deletions test/unit/math/opencl/kernel_generator/append_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
#include <stan/math/opencl/matrix_cl.hpp>
#include <stan/math/opencl/copy.hpp>
#include <stan/math/prim/fun.hpp>
#include <test/unit/util.hpp>
#include <Eigen/Dense>
#include <gtest/gtest.h>

#define EXPECT_MATRIX_NEAR(A, B, DELTA) \
for (int i = 0; i < A.size(); i++) \
EXPECT_NEAR(A(i), B(i), DELTA);

TEST(KernelGenerator, append_row_errors) {
using Eigen::MatrixXd;
using stan::math::append_row;
Expand Down
Loading

0 comments on commit 2a7a857

Please sign in to comment.