gtsam  4.1.0
gtsam
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 <boost/concept_check.hpp>
38 #include <stdio.h>
39 #include <string>
40 
41 #define GTSAM_PRINT(x)((x).print(#x))
42 
43 namespace gtsam {
44 
45  // Forward declaration
46  template <typename T> struct traits;
47 
56  template <class T>
57  class IsTestable {
58  T t;
59  bool r1,r2;
60  public:
61 
62  BOOST_CONCEPT_USAGE(IsTestable) {
63  // check print function, with optional string
64  traits<T>::Print(t, std::string());
66 
67  // check print, with optional threshold
68  double tol = 1.0;
69  r1 = traits<T>::Equals(t,t,tol);
70  r2 = traits<T>::Equals(t,t);
71  }
72  }; // \ Testable
73 
74  inline void print(float v, const std::string& s = "") {
75  printf("%s%f\n",s.c_str(),v);
76  }
77  inline void print(double v, const std::string& s = "") {
78  printf("%s%lf\n",s.c_str(),v);
79  }
80 
82  template<class T>
83  inline bool equal(const T& obj1, const T& obj2, double tol) {
84  return traits<T>::Equals(obj1,obj2, tol);
85  }
86 
88  template<class T>
89  inline bool equal(const T& obj1, const T& obj2) {
90  return traits<T>::Equals(obj1,obj2);
91  }
92 
96  template<class V>
97  bool assert_equal(const V& expected, const V& actual, double tol = 1e-9) {
98  if (traits<V>::Equals(actual,expected, tol))
99  return true;
100  printf("Not equal:\n");
101  traits<V>::Print(expected,"expected:\n");
102  traits<V>::Print(actual,"actual:\n");
103  return false;
104  }
105 
109  template<class V>
110  struct equals : public std::function<bool(const V&, const V&)> {
111  double tol_;
112  equals(double tol = 1e-9) : tol_(tol) {}
113  bool operator()(const V& expected, const V& actual) {
114  return (traits<V>::Equals(actual, expected, tol_));
115  }
116  };
117 
121  template<class V>
122  struct equals_star : public std::function<bool(const boost::shared_ptr<V>&, const boost::shared_ptr<V>&)> {
123  double tol_;
124  equals_star(double tol = 1e-9) : tol_(tol) {}
125  bool operator()(const boost::shared_ptr<V>& expected, const boost::shared_ptr<V>& actual) {
126  if (!actual && !expected) return true;
127  return actual && expected && traits<V>::Equals(*actual,*expected, tol_);
128  }
129  };
130 
132  template<typename T>
134 
135  BOOST_CONCEPT_USAGE(HasTestablePrereqs) {
136  t->print(str);
137  b = t->equals(*s,tol);
138  }
139 
140  T *t, *s; // Pointer is to allow abstract classes
141  bool b;
142  double tol;
143  std::string str;
144  };
145 
149  template<typename T>
150  struct Testable {
151 
152  // Check that T has the necessary methods
153  BOOST_CONCEPT_ASSERT((HasTestablePrereqs<T>));
154 
155  static void Print(const T& m, const std::string& str = "") {
156  m.print(str);
157  }
158  static bool Equals(const T& m1, const T& m2, double tol = 1e-8) {
159  return m1.equals(m2, tol);
160  }
161  };
162 
163 } // \namespace gtsam
164 
174 #define GTSAM_CONCEPT_TESTABLE_INST(T) template class gtsam::IsTestable<T>;
175 #define GTSAM_CONCEPT_TESTABLE_TYPE(T) typedef gtsam::IsTestable<T> _gtsam_Testable_##T;
gtsam::equals
Template to create a binary predicate.
Definition: Testable.h:110
gtsam::traits
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
gtsam::IsTestable
Definition: Testable.h:57
gtsam::equals_star
Binary predicate on shared pointers.
Definition: Testable.h:122
gtsam::equal
bool equal(const T &obj1, const T &obj2, double tol)
Call equal on the object.
Definition: Testable.h:83
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
gtsam::Testable
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
gtsam::HasTestablePrereqs
Requirements on type to pass it to Testable template below.
Definition: Testable.h:133
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition: Matrix.cpp:42