Skip to content

Commit

Permalink
Merge pull request RobotLocomotion#5011 from soonho-tri/pr-refactor-s…
Browse files Browse the repository at this point in the history
…ymbolic-tests

Refactor symbolic tests
  • Loading branch information
soonho-tri authored Feb 1, 2017
2 parents b5fa7eb + c302562 commit befcd69
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 107 deletions.
15 changes: 15 additions & 0 deletions drake/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ drake_cc_library(
],
)

drake_cc_library(
name = "symbolic_test_util",
hdrs = [
"test/symbolic_test_util.h",
],
deps = [
":symbolic",
],
testonly = 1,
)

cc_library(
name = "variable",
srcs = ["variable.cc"],
Expand Down Expand Up @@ -391,6 +402,7 @@ drake_cc_googletest(
":common",
":monomial",
":symbolic",
":symbolic_test_util",
],
)

Expand Down Expand Up @@ -439,6 +451,7 @@ drake_cc_googletest(
deps = [
":common",
":symbolic",
":symbolic_test_util",
],
)

Expand All @@ -463,6 +476,7 @@ drake_cc_googletest(
deps = [
":common",
":symbolic",
":symbolic_test_util",
],
)

Expand All @@ -471,6 +485,7 @@ drake_cc_googletest(
deps = [
":common",
":symbolic",
":symbolic_test_util",
],
)

Expand Down
62 changes: 20 additions & 42 deletions drake/common/test/monomial_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "drake/common/eigen_types.h"
#include "drake/common/hash.h"
#include "drake/common/symbolic_expression.h"
#include "drake/common/test/symbolic_test_util.h"
#include "drake/common/variable.h"

using std::unordered_map;
Expand All @@ -15,30 +16,7 @@ namespace drake {
namespace symbolic {
namespace {

bool ExprEqual(const Expression& e1, const Expression& e2) {
return e1.EqualTo(e2);
}

// Compare two Eigen::Matrix<Expression> m1 and m2.
//
// TODO(soonho): Make CompareMatrices in eigen_matrix_compare.h compatible
// with Eigen::Matrix<symbolic::Expression> and use that instead of this.
bool MatrixEqual(const drake::MatrixX<Expression>& m1,
const drake::MatrixX<Expression>& m2) {
if (m1.rows() != m2.rows() || m1.cols() != m2.cols()) {
return false;
}
for (int i{0}; i < m1.rows(); ++i) {
for (int j{0}; j < m1.cols(); ++j) {
const Expression& e1{m1(i, j)};
const Expression& e2{m2(i, j)};
if (!e1.EqualTo(e2)) {
return false;
}
}
}
return true;
}
using test::ExprEqual;

class MonomialTest : public ::testing::Test {
protected:
Expand Down Expand Up @@ -91,8 +69,8 @@ TEST_F(MonomialTest, MonomialBasis_x_0) {
// MonomialBasis({x}, 0)
expected << Expression{1.0};

EXPECT_PRED2(MatrixEqual, basis1, expected);
EXPECT_PRED2(MatrixEqual, basis2, expected);
EXPECT_EQ(basis1, expected);
EXPECT_EQ(basis2, expected);
}

TEST_F(MonomialTest, MonomialBasis_x_2) {
Expand All @@ -108,8 +86,8 @@ TEST_F(MonomialTest, MonomialBasis_x_2) {
1.0;
// clang-format on

EXPECT_PRED2(MatrixEqual, basis1, expected);
EXPECT_PRED2(MatrixEqual, basis2, expected);
EXPECT_EQ(basis1, expected);
EXPECT_EQ(basis2, expected);
}

TEST_F(MonomialTest, MonomialBasis_x_y_0) {
Expand All @@ -121,8 +99,8 @@ TEST_F(MonomialTest, MonomialBasis_x_y_0) {
// MonomialBasis({x, y}, 0)
expected << Expression{1.0};

EXPECT_PRED2(MatrixEqual, basis1, expected);
EXPECT_PRED2(MatrixEqual, basis2, expected);
EXPECT_EQ(basis1, expected);
EXPECT_EQ(basis2, expected);
}

TEST_F(MonomialTest, MonomialBasis_x_y_1) {
Expand All @@ -138,8 +116,8 @@ TEST_F(MonomialTest, MonomialBasis_x_y_1) {
1.0;
// clang-format on

EXPECT_PRED2(MatrixEqual, basis1, expected);
EXPECT_PRED2(MatrixEqual, basis2, expected);
EXPECT_EQ(basis1, expected);
EXPECT_EQ(basis2, expected);
}

TEST_F(MonomialTest, MonomialBasis_x_y_2) {
Expand All @@ -158,8 +136,8 @@ TEST_F(MonomialTest, MonomialBasis_x_y_2) {
1.0;
// clang-format on

EXPECT_PRED2(MatrixEqual, basis1, expected);
EXPECT_PRED2(MatrixEqual, basis2, expected);
EXPECT_EQ(basis1, expected);
EXPECT_EQ(basis2, expected);
}

TEST_F(MonomialTest, MonomialBasis_x_y_3) {
Expand All @@ -182,8 +160,8 @@ TEST_F(MonomialTest, MonomialBasis_x_y_3) {
1;
// clang-format on

EXPECT_PRED2(MatrixEqual, basis1, expected);
EXPECT_PRED2(MatrixEqual, basis2, expected);
EXPECT_EQ(basis1, expected);
EXPECT_EQ(basis2, expected);
}

TEST_F(MonomialTest, MonomialBasis_x_y_z_2) {
Expand All @@ -208,8 +186,8 @@ TEST_F(MonomialTest, MonomialBasis_x_y_z_2) {
1;
// clang-format on

EXPECT_PRED2(MatrixEqual, basis1, expected);
EXPECT_PRED2(MatrixEqual, basis2, expected);
EXPECT_EQ(basis1, expected);
EXPECT_EQ(basis2, expected);
}

TEST_F(MonomialTest, MonomialBasis_x_y_z_3) {
Expand Down Expand Up @@ -243,8 +221,8 @@ TEST_F(MonomialTest, MonomialBasis_x_y_z_3) {
1;
// clang-format on

EXPECT_PRED2(MatrixEqual, basis1, expected);
EXPECT_PRED2(MatrixEqual, basis2, expected);
EXPECT_EQ(basis1, expected);
EXPECT_EQ(basis2, expected);
}

TEST_F(MonomialTest, MonomialBasis_x_y_z_w_3) {
Expand Down Expand Up @@ -293,8 +271,8 @@ TEST_F(MonomialTest, MonomialBasis_x_y_z_w_3) {
1;
// clang-format on

EXPECT_PRED2(MatrixEqual, basis1, expected);
EXPECT_PRED2(MatrixEqual, basis2, expected);
EXPECT_EQ(basis1, expected);
EXPECT_EQ(basis2, expected);
}

} // namespace
Expand Down
20 changes: 5 additions & 15 deletions drake/common/test/symbolic_expression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "drake/common/environment.h"
#include "drake/common/hash.h"
#include "drake/common/symbolic_formula.h"
#include "drake/common/test/symbolic_test_util.h"
#include "drake/common/variable.h"
#include "drake/common/variables.h"

Expand All @@ -39,21 +40,10 @@ namespace drake {
namespace symbolic {
namespace {

bool ExprEqual(const Expression& e1, const Expression& e2) {
return e1.EqualTo(e2);
}

bool ExprNotEqual(const Expression& e1, const Expression& e2) {
return !ExprEqual(e1, e2);
}

bool ExprLess(const Expression& e1, const Expression& e2) {
return e1.Less(e2);
}

bool ExprNotLess(const Expression& e1, const Expression& e2) {
return !ExprLess(e1, e2);
}
using test::ExprEqual;
using test::ExprLess;
using test::ExprNotEqual;
using test::ExprNotLess;

// Checks if a given 'expressions' is ordered by Expression::Less.
static void CheckOrdering(const vector<Expression>& expressions) {
Expand Down
39 changes: 8 additions & 31 deletions drake/common/test/symbolic_formula_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,27 @@
#include "drake/common/drake_throw.h"
#include "drake/common/environment.h"
#include "drake/common/symbolic_expression.h"
#include "drake/common/test/symbolic_test_util.h"
#include "drake/common/variable.h"
#include "drake/common/variables.h"

namespace drake {
namespace symbolic {
namespace {

using std::all_of;
using std::any_of;
using std::map;
using std::set;
using std::unordered_map;
using std::unordered_set;
using std::vector;

template <typename F>
bool all_of(const vector<Formula>& formulas, const F& f) {
return all_of(formulas.begin(), formulas.end(), f);
}

template <typename F>
bool any_of(const vector<Formula>& formulas, const F& f) {
return any_of(formulas.begin(), formulas.end(), f);
}

static bool ExprEqual(const Expression& e1, const Expression& e2) {
return e1.EqualTo(e2);
}

static bool FormulaEqual(const Formula& f1, const Formula& f2) {
return f1.EqualTo(f2);
}

static bool FormulaNotEqual(const Formula& f1, const Formula& f2) {
return !FormulaEqual(f1, f2);
}

static bool FormulaLess(const Formula& f1, const Formula& f2) {
return f1.Less(f2);
}

static bool FormulaNotLess(const Formula& f1, const Formula& f2) {
return !FormulaLess(f1, f2);
}
using test::all_of;
using test::any_of;
using test::ExprEqual;
using test::FormulaEqual;
using test::FormulaLess;
using test::FormulaNotEqual;
using test::FormulaNotLess;

// Checks if a given 'formulas' is ordered by Formula::Less.
static void CheckOrdering(const vector<Formula>& formulas) {
Expand Down
58 changes: 58 additions & 0 deletions drake/common/test/symbolic_test_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <algorithm>
#include <vector>

#include "gtest/gtest.h"

#include "drake/common/symbolic_expression.h"
#include "drake/common/symbolic_formula.h"
#include "drake/common/variable.h"

namespace drake {
namespace symbolic {
namespace test {

inline bool ExprEqual(const Expression& e1, const Expression& e2) {
return e1.EqualTo(e2);
}

inline bool ExprNotEqual(const Expression& e1, const Expression& e2) {
return !ExprEqual(e1, e2);
}

inline bool ExprLess(const Expression& e1, const Expression& e2) {
return e1.Less(e2);
}

inline bool ExprNotLess(const Expression& e1, const Expression& e2) {
return !ExprLess(e1, e2);
}

template <typename F>
bool all_of(const std::vector<Formula>& formulas, const F& f) {
return std::all_of(formulas.begin(), formulas.end(), f);
}

template <typename F>
bool any_of(const std::vector<Formula>& formulas, const F& f) {
return std::any_of(formulas.begin(), formulas.end(), f);
}

inline bool FormulaEqual(const Formula& f1, const Formula& f2) {
return f1.EqualTo(f2);
}

inline bool FormulaNotEqual(const Formula& f1, const Formula& f2) {
return !FormulaEqual(f1, f2);
}

inline bool FormulaLess(const Formula& f1, const Formula& f2) {
return f1.Less(f2);
}

inline bool FormulaNotLess(const Formula& f1, const Formula& f2) {
return !FormulaLess(f1, f2);
}

} // namespace test
} // namespace symbolic
} // namespace drake
6 changes: 2 additions & 4 deletions drake/common/test/variable_overloading_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
#include "gtest/gtest.h"

#include "drake/common/symbolic_expression.h"
#include "drake/common/test/symbolic_test_util.h"
#include "drake/common/variable.h"

namespace drake {
namespace {

using std::ostringstream;
using symbolic::Expression;

bool ExprEqual(const Expression& e1, const Expression& e2) {
return e1.EqualTo(e2);
}
using symbolic::test::ExprEqual;

// Provides common variables and matrices that are used by the
// following tests.
Expand Down
2 changes: 0 additions & 2 deletions drake/common/test/variable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "gtest/gtest.h"

namespace drake {
namespace symbolic {
namespace {

using std::move;
Expand Down Expand Up @@ -152,5 +151,4 @@ TEST_F(VariableTest, EigenVariableMatrixOutput) {
}

} // namespace
} // namespace symbolic
} // namespace drake
Loading

0 comments on commit befcd69

Please sign in to comment.