Skip to content

Commit

Permalink
Merge pull request #13 from ethz-asl/feature/fixture_and_mods
Browse files Browse the repository at this point in the history
Add test fixture
  • Loading branch information
simonlynen committed Aug 18, 2014
2 parents 3bdf581 + 30984c3 commit 5ded377
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
8 changes: 8 additions & 0 deletions include/eigen-checks/entrypoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
#define TEST_P(a, b) int Test_##a##_##b()
#endif

#ifndef TYPED_TEST_CASE
#define TYPED_TEST_CASE(a, b) int Test_##a##_##b()
#endif

#ifndef TYPED_TEST
#define TYPED_TEST(a, b) int Test_##a##_##b()
#endif

#define UNITTEST_ENTRYPOINT \
int main(int argc, char** argv) { \
google::InitGoogleLogging(argv[0]); \
Expand Down
2 changes: 1 addition & 1 deletion test/test_gtest_equal_double.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <eigen-checks/entrypoint.h>


TEST(EigenChecks, Trivial) {}

UNITTEST_ENTRYPOINT
110 changes: 110 additions & 0 deletions test/test_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#ifndef EIGEN_CHECKS_TEST_HELPER_H_
#define EIGEN_CHECKS_TEST_HELPER_H_
#include <limits>
#include <random>

#include <Eigen/Dense>
#include <gtest/gtest.h>
#include <glog/logging.h>

#include <eigen-checks/internal/gtest-equal.h>

template <typename Scalar>
class EigenChecks : public testing::Test {
protected:
virtual void SetUp() {
int random_seed_ = 42;
std::mt19937 gen(random_seed_);
std::normal_distribution<> dis(10.0, 5.0);
Eigen::Matrix<Scalar, 5, 1> perturbance_v;
perturbance_v << dis(gen), dis(gen), dis(gen), dis(gen), dis(gen);
perturbance_v /= perturbance_v.maxCoeff();

ground_truth_vector_5 << dis(gen), dis(gen), dis(gen), dis(gen), dis(gen);

test_vector_D.resize(4, Eigen::NoChange);
test_vector_D.template block<4, 1>(0, 0) =
ground_truth_vector_5.template block<4, 1>(0, 0);

test_vector_5_random << dis(gen), dis(gen), dis(gen), dis(gen), dis(gen);

test_vector_5_near_e_minus_8 = ground_truth_vector_5;
test_vector_5_near_e_minus_8 +=
perturbance_v * static_cast<Scalar>(1e-8);

test_vector_5_near_e_minus_5 = ground_truth_vector_5;
test_vector_5_near_e_minus_5 +=
perturbance_v * static_cast<Scalar>(1e-5);

test_vector_5_equal = ground_truth_vector_5;

test_vector_5_equal_floating_point = ground_truth_vector_5;
test_vector_5_equal_floating_point +=
perturbance_v * static_cast<Scalar>(
eigen_checks::internal::kDefaultPrecision);

// Test matrix.
Eigen::Matrix<Scalar, 5, 4> perturbance_m;
perturbance_m <<
dis(gen), dis(gen), dis(gen), dis(gen),
dis(gen), dis(gen), dis(gen), dis(gen),
dis(gen), dis(gen), dis(gen), dis(gen),
dis(gen), dis(gen), dis(gen), dis(gen),
dis(gen), dis(gen), dis(gen), dis(gen);
perturbance_m /= perturbance_m.maxCoeff();

ground_truth_matrix_54 <<
dis(gen), dis(gen), dis(gen), dis(gen),
dis(gen), dis(gen), dis(gen), dis(gen),
dis(gen), dis(gen), dis(gen), dis(gen),
dis(gen), dis(gen), dis(gen), dis(gen),
dis(gen), dis(gen), dis(gen), dis(gen);

test_matrix_D4.resize(4, Eigen::NoChange);
test_matrix_D4.template block<4, 4>(0, 0) =
ground_truth_matrix_54.template block<4, 4>(0, 0);

test_matrix_54_random <<
dis(gen), dis(gen), dis(gen), dis(gen),
dis(gen), dis(gen), dis(gen), dis(gen),
dis(gen), dis(gen), dis(gen), dis(gen),
dis(gen), dis(gen), dis(gen), dis(gen),
dis(gen), dis(gen), dis(gen), dis(gen);

test_matrix_54_near_e_minus_8 = ground_truth_matrix_54;
test_matrix_54_near_e_minus_8 +=
perturbance_m * static_cast<Scalar>(1e-10);

test_matrix_54_near_e_minus_5 = ground_truth_matrix_54;
test_matrix_54_near_e_minus_5 +=
perturbance_m * static_cast<Scalar>(1e-5);

test_matrix_54_equal = ground_truth_matrix_54;

test_matrix_54_equal_floating_point = ground_truth_matrix_54;
test_matrix_54_equal_floating_point +=
perturbance_m * static_cast<Scalar>(
eigen_checks::internal::kDefaultPrecision);
}

protected:
Eigen::Matrix<Scalar, 5, 1> ground_truth_vector_5;
Eigen::Matrix<Scalar, Eigen::Dynamic, 1> test_vector_D;
Eigen::Matrix<Scalar, 5, 1> test_vector_5_random;
Eigen::Matrix<Scalar, 5, 1> test_vector_5_near_e_minus_8;
Eigen::Matrix<Scalar, 5, 1> test_vector_5_near_e_minus_5;
Eigen::Matrix<Scalar, 5, 1> test_vector_5_equal;
Eigen::Matrix<Scalar, 5, 1> test_vector_5_equal_floating_point;
Eigen::Matrix<Scalar, 5, 4> ground_truth_matrix_54;
Eigen::Matrix<Scalar, Eigen::Dynamic, 4> test_matrix_D4;
Eigen::Matrix<Scalar, 5, 4> test_matrix_54_random;
Eigen::Matrix<Scalar, 5, 4> test_matrix_54_near_e_minus_8;
Eigen::Matrix<Scalar, 5, 4> test_matrix_54_near_e_minus_5;
Eigen::Matrix<Scalar, 5, 4> test_matrix_54_equal;
Eigen::Matrix<Scalar, 5, 4> test_matrix_54_equal_floating_point;
};

typedef ::testing::Types<double, float> ScalarTypes;
TYPED_TEST_CASE(EigenChecks, ScalarTypes);

#endif // EIGEN_CHECKS_TEST_HELPER_H_

0 comments on commit 5ded377

Please sign in to comment.