gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Testable.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
32 // \callgraph
33 
34 #pragma once
35 
36 #include <boost/shared_ptr.hpp>
37 #include <stdio.h>
38 #include <string>
39 
40 #define GTSAM_PRINT(x)((x).print(#x))
41 
42 namespace gtsam {
43 
52  template <class T>
54  static bool checkTestableConcept(const T& d) {
55  // check print function, with optional string
56  d.print(std::string());
57  d.print();
58 
59  // check print, with optional threshold
60  double tol = 1.0;
61  bool r1 = d.equals(d, tol);
62  bool r2 = d.equals(d);
63  return r1 && r2;
64  }
65  };
66 
68  template<class T>
69  inline void print(const T& object, const std::string& s = "") {
70  object.print(s);
71  }
72 
74  template<class T>
75  inline bool equal(const T& obj1, const T& obj2, double tol) {
76  return obj1.equals(obj2, tol);
77  }
78 
80  template<class T>
81  inline bool equal(const T& obj1, const T& obj2) {
82  return obj1.equals(obj2);
83  }
84 
88  template<class V>
89  bool assert_equal(const V& expected, const V& actual, double tol = 1e-9) {
90  if (actual.equals(expected, tol))
91  return true;
92  printf("Not equal:\n");
93  expected.print("expected:\n");
94  actual.print("actual:\n");
95  return false;
96  }
97 
101  template<class V>
102  struct equals : public std::binary_function<const V&, const V&, bool> {
103  double tol_;
104  equals(double tol = 1e-9) : tol_(tol) {}
105  bool operator()(const V& expected, const V& actual) {
106  return (actual.equals(expected, tol_));
107  }
108  };
109 
113  template<class V>
114  struct equals_star : public std::binary_function<const boost::shared_ptr<V>&, const boost::shared_ptr<V>&, bool> {
115  double tol_;
116  equals_star(double tol = 1e-9) : tol_(tol) {}
117  bool operator()(const boost::shared_ptr<V>& expected, const boost::shared_ptr<V>& actual) {
118  if (!actual || !expected) return false;
119  return (actual->equals(*expected, tol_));
120  }
121  };
122 
123 } // \namespace gtsam
124 
133 #define GTSAM_CONCEPT_TESTABLE_INST(T) template class gtsam::TestableConcept<T>;
134 #define GTSAM_CONCEPT_TESTABLE_TYPE(T) typedef gtsam::TestableConcept<T> _gtsam_TestableConcept_##T;
Binary predicate on shared pointers.
Definition: Testable.h:114
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
Template to create a binary predicate.
Definition: Testable.h:102
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition: Matrix.cpp:60
Definition: Testable.h:53
bool equal(const T &obj1, const T &obj2, double tol)
Call equal on the object.
Definition: Testable.h:75