Skip to content

Commit

Permalink
Merge pull request #20 from ethz-asl/fix/use_std_traits_and_documenta…
Browse files Browse the repository at this point in the history
…tion

Use std traits to remove reference and some more documentation
  • Loading branch information
simonlynen committed Aug 19, 2014
2 parents 94ef611 + f5e382a commit bf6308c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 36 deletions.
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
eigen_checks
Eigen check macros
============

A repository for glog CHECK and gtest ASSERT and EXPECT macros for Eigen types.
Expand Down Expand Up @@ -32,6 +32,55 @@ Checks if two matrices are equal to a user-specified precision.
## GTEST
The gtest macros are built to facilitate unit testing with matrix types. This library provides two pieces of functionality: an macro that defines the main function, or *entrypoint*, for a guest invocation, and several macros for testing if matrices are similar.

### GTest
The Gtest `EXPECT()` and `ASSERT()` macros are used to verify assumptions in your tests and print useful information in case these don't match the expected outcome. These Eigen equivalents can be used to check conditions on matrices. See [the gtest documentation](https://code.google.com/p/googletest/wiki/Primer#Assertions) for a full description. The general form of use is:

```c++
EXPECT_TRUE(EIGEN_MATRIX_EQUAL(MatrixA, MatrixB));
EXPECT_FALSE(EIGEN_MATRIX_EQUAL(MatrixA, MatrixB));
ASSERT_TRUE(EIGEN_MATRIX_EQUAL(MatrixA, MatrixB));
ASSERT_FALSE(EIGEN_MATRIX_EQUAL(MatrixA, MatrixB));
```
All tests happen component-wise. They fail if the matrices are different sizes. If the matrices are the same size, the test is applied to each corresponding pair of components.
To get these macros, use:
```c++
#include<eigen-checks/gtest.h>
```

### `EXPECT_TRUE(EIGEN_MATRIX_EQUAL(MatrixA, MatrixB))`

Succeeds if two matrices are binary equal.

### `EXPECT_TRUE(EIGEN_MATRIX_EQUAL_DOUBLE(MatrixA, MatrixB))`

Succeeds if two matrices are equal to floating-point precision

### `EXPECT_TRUE(EIGEN_MATRIX_NEAR(MatrixA, MatrixB, Precision))`

Succeeds if two matrices are equal to a user-specified precision.


### Entrypoint

### Macros
Instead of writing the following code in your unit-test runner file:

```c++
#include <gtest/gtest.h>
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
google::ParseCommandLineFlags(&argc, &argv, true);
::testing::FLAGS_gtest_death_test_style = "threadsafe";
//...
return RUN_ALL_TESTS();
}
```
You can just use:
```c++
#include <eigen-checks/entrypoint.h>
UNITTEST_ENTRYPOINT
```
9 changes: 5 additions & 4 deletions include/eigen-checks/glog.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef EIGEN_CHECKS_GLOG_H_
#define EIGEN_CHECKS_GLOG_H_
#include <glog/logging.h>
#include <limits>
#include <type_traits>

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

// To allow the user to append custom messages we store the result of the
// verification inside a unique local variable to be used in the check and for
Expand All @@ -22,14 +23,14 @@
INTERNAL_EIGEN_CHECKS_MAKE_GLOG_CHECK( \
eigen_checks::internal::MatricesNear( \
MatrixA, #MatrixA, MatrixB, #MatrixB, \
static_cast<typename eigen_checks::internal::RemoveCR< \
static_cast<typename std::remove_reference< \
decltype(MatrixA)>::type::Scalar>(0.0), "0.0"))

#define CHECK_EIGEN_MATRIX_EQUAL_DOUBLE(MatrixA, MatrixB) \
INTERNAL_EIGEN_CHECKS_MAKE_GLOG_CHECK( \
eigen_checks::internal::MatricesNear( \
MatrixA, #MatrixA, MatrixB, #MatrixB, \
static_cast<typename eigen_checks::internal::RemoveCR< \
static_cast<typename std::remove_reference< \
decltype(MatrixA)>::type::Scalar>( \
eigen_checks::internal::kDefaultPrecision), \
"Floating point precision"))
Expand Down
7 changes: 4 additions & 3 deletions include/eigen-checks/gtest.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#ifndef EIGEN_CHECKS_GTEST_H_
#define EIGEN_CHECKS_GTEST_H_
#include <limits>
#include <type_traits>

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

#define EIGEN_MATRIX_EQUAL(MatrixA, MatrixB) \
eigen_checks::internal::MatricesNear(MatrixA, #MatrixA, MatrixB, #MatrixB, \
static_cast<typename eigen_checks::internal::RemoveCR< \
static_cast<typename std::remove_reference< \
decltype(MatrixA)>::type::Scalar>(0.0), "0.0")

#define EIGEN_MATRIX_EQUAL_DOUBLE(MatrixA, MatrixB) \
eigen_checks::internal::MatricesNear(MatrixA, #MatrixA, MatrixB, #MatrixB, \
static_cast<typename eigen_checks::internal::RemoveCR< \
static_cast<typename std::remove_reference< \
decltype(MatrixA)>::type::Scalar>( \
eigen_checks::internal::kDefaultPrecision), "Floating point precision")

Expand Down
27 changes: 0 additions & 27 deletions include/eigen-checks/internal/traits.h

This file was deleted.

0 comments on commit bf6308c

Please sign in to comment.