Drake
symbolic_test_util.h
Go to the documentation of this file.
1 #pragma once
2 /// @file
3 /// This file provides a set of predicates which can be used with GTEST's
4 /// ASSERT/EXPECT_PRED{n} macros. The motivation is to provide better diagnostic
5 /// information when the assertions fail. Please consider a scenario where a
6 /// user wants to assert that two symbolic expressions, `e1` and `e2`, are
7 /// structurally identical.
8 ///
9 /// @code
10 /// // The following does not work because `operator==(const Expression& e1,
11 /// // const Expression& e2)` does not return a Boolean value. We need to use
12 /// // Expression::EqualTo() method instead.
13 /// ASSERT_EQ(e1, e2);
14 ///
15 /// // The following compiles, but it does not provide enough information when
16 /// // the assertion fails. It merely reports that `e1.EqualTo(e2)` is evaluated
17 /// // to `false`, not to `true`.
18 /// ASSERT_TRUE(e1.EqualTo(e2));
19 ///
20 /// // When the following assertion fails, it reports the value of `e1` and `e2`
21 /// // which should help debugging.
22 /// ASSERT_PRED2(ExprEqual, e1, e2);
23 /// @endcode
24 #include <algorithm>
25 #include <vector>
26 
27 #include <gtest/gtest.h>
28 
29 #include "drake/common/symbolic.h"
30 
31 namespace drake {
32 namespace symbolic {
33 namespace test {
34 
35 inline bool VarEqual(const Variable& v1, const Variable& v2) {
36  return v1.equal_to(v2);
37 }
38 
39 inline bool VarNotEqual(const Variable& v1, const Variable& v2) {
40  return !VarEqual(v1, v2);
41 }
42 
43 inline bool VarLess(const Variable& v1, const Variable& v2) {
44  return v1.less(v2);
45 }
46 
47 inline bool VarNotLess(const Variable& v1, const Variable& v2) {
48  return !VarLess(v1, v2);
49 }
50 
51 inline bool ExprEqual(const Expression& e1, const Expression& e2) {
52  return e1.EqualTo(e2);
53 }
54 
55 inline bool ExprNotEqual(const Expression& e1, const Expression& e2) {
56  return !ExprEqual(e1, e2);
57 }
58 
59 inline bool ExprLess(const Expression& e1, const Expression& e2) {
60  return e1.Less(e2);
61 }
62 
63 inline bool ExprNotLess(const Expression& e1, const Expression& e2) {
64  return !ExprLess(e1, e2);
65 }
66 
67 inline bool PolyEqual(const Polynomial& p1, const Polynomial& p2) {
68  return p1.EqualTo(p2);
69 }
70 
71 inline bool PolyNotEqual(const Polynomial& p1, const Polynomial& p2) {
72  return !PolyEqual(p1, p2);
73 }
74 
75 template <typename F>
76 bool all_of(const std::vector<Formula>& formulas, const F& f) {
77  return std::all_of(formulas.begin(), formulas.end(), f);
78 }
79 
80 template <typename F>
81 bool any_of(const std::vector<Formula>& formulas, const F& f) {
82  return std::any_of(formulas.begin(), formulas.end(), f);
83 }
84 
85 inline bool FormulaEqual(const Formula& f1, const Formula& f2) {
86  return f1.EqualTo(f2);
87 }
88 
89 inline bool FormulaNotEqual(const Formula& f1, const Formula& f2) {
90  return !FormulaEqual(f1, f2);
91 }
92 
93 inline bool FormulaLess(const Formula& f1, const Formula& f2) {
94  return f1.Less(f2);
95 }
96 
97 inline bool FormulaNotLess(const Formula& f1, const Formula& f2) {
98  return !FormulaLess(f1, f2);
99 }
100 
101 /**
102  * Compare if two polynomials p1 and p2 are the same, by checking if all the
103  * coefficients in their difference p1 - p2 is no larger than tol.
104  * @param p1 A polynomial.
105  * @param p2 A polynomial.
106  * @param tol The tolerance on the coefficients of p1 - p2.
107  */
108 ::testing::AssertionResult PolynomialEqual(const symbolic::Polynomial& p1,
109  const symbolic::Polynomial& p2, double tol) {
110  const symbolic::Polynomial diff = p1 - p2;
111  // Check if the absolute value of the coefficient for each monomial is less
112  // than tol.
114  for (const auto& p : map) {
115  if (std::abs(get_constant_value(p.second)) > tol) {
116  return ::testing::AssertionFailure()
117  << "The coefficient for " << p.first << " is " << p.second
118  << ", exceed tolerance " << tol << "\n";
119  }
120  }
121  return ::testing::AssertionSuccess();
122 }
123 } // namespace test
124 } // namespace symbolic
125 } // namespace drake
std::vector< snopt::doublereal > F
Definition: snopt_solver.cc:59
std::unordered_map< Monomial, Expression, hash_value< Monomial >> MapType
Definition: symbolic_polynomial.h:33
bool FormulaNotEqual(const Formula &f1, const Formula &f2)
Definition: symbolic_test_util.h:89
Represents a symbolic variable.
Definition: symbolic_variable.h:24
Definition: automotive_demo.cc:88
bool PolyNotEqual(const Polynomial &p1, const Polynomial &p2)
Definition: symbolic_test_util.h:71
bool all_of(const std::vector< Formula > &formulas, const F &f)
Definition: symbolic_test_util.h:76
Expression abs(const Expression &e)
Definition: symbolic_expression.cc:537
::testing::AssertionResult PolynomialEqual(const symbolic::Polynomial &p1, const symbolic::Polynomial &p2, double tol)
Compare if two polynomials p1 and p2 are the same, by checking if all the coefficients in their diffe...
Definition: symbolic_test_util.h:108
Represents a symbolic form of a first-order logic formula.
Definition: symbolic_formula.h:113
bool Less(const Formula &f) const
Checks lexicographical ordering between this and e.
Definition: symbolic_formula.cc:65
bool less(const Variable &v) const
Compares two variables based on their ID values.
Definition: symbolic_variable.h:70
bool EqualTo(const Expression &e) const
Checks structural equality.
Definition: symbolic_expression.cc:115
bool any_of(const std::vector< Formula > &formulas, const F &f)
Definition: symbolic_test_util.h:81
bool ExprNotLess(const Expression &e1, const Expression &e2)
Definition: symbolic_test_util.h:63
bool VarNotEqual(const Variable &v1, const Variable &v2)
Definition: symbolic_test_util.h:39
bool EqualTo(const Polynomial &p) const
Returns true if this polynomial and p are structurally equal.
Definition: symbolic_polynomial.cc:542
bool Less(const Expression &e) const
Provides lexicographical ordering between expressions.
Definition: symbolic_expression.cc:132
double get_constant_value(const Expression &e)
Returns the constant value of the constant expression e.
Definition: symbolic_expression.cc:781
bool ExprLess(const Expression &e1, const Expression &e2)
Definition: symbolic_test_util.h:59
bool FormulaNotLess(const Formula &f1, const Formula &f2)
Definition: symbolic_test_util.h:97
Represents a symbolic form of an expression.
Definition: symbolic_expression.h:172
bool ExprNotEqual(const Expression &e1, const Expression &e2)
Definition: symbolic_test_util.h:55
bool VarLess(const Variable &v1, const Variable &v2)
Definition: symbolic_test_util.h:43
bool ExprEqual(const Expression &e1, const Expression &e2)
Definition: symbolic_test_util.h:51
bool FormulaLess(const Formula &f1, const Formula &f2)
Definition: symbolic_test_util.h:93
bool VarEqual(const Variable &v1, const Variable &v2)
Definition: symbolic_test_util.h:35
bool EqualTo(const Formula &f) const
Checks structural equality.
Definition: symbolic_formula.cc:47
const MapType & monomial_to_coefficient_map() const
Returns the mapping from a Monomial to its corresponding coefficient of this polynomial.
Definition: symbolic_polynomial.cc:398
Represents symbolic polynomials.
Definition: symbolic_polynomial.h:30
bool FormulaEqual(const Formula &f1, const Formula &f2)
Definition: symbolic_test_util.h:85
Provides public header files of Drake&#39;s symbolic library.
bool equal_to(const Variable &v) const
Checks the equality of two variables based on their ID values.
Definition: symbolic_variable.h:67
bool VarNotLess(const Variable &v1, const Variable &v2)
Definition: symbolic_test_util.h:47
bool PolyEqual(const Polynomial &p1, const Polynomial &p2)
Definition: symbolic_test_util.h:67