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/concept_check.hpp>
37 #include <boost/shared_ptr.hpp>
38 #include <iostream>
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 
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  std::cout << (s.empty() ? s : s + " ") << v << std::endl;
76  }
77  inline void print(double v, const std::string& s = "") {
78  std::cout << (s.empty() ? s : s + " ") << v << std::endl;
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 
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
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;
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
Matrix3f m
Matrix expected
Definition: testMatrix.cpp:974
MatrixType m2(n_dims)
ArrayXcf v
Definition: Cwise_arg.cpp:1
BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept< ListOfOneContainer< int > >))
bool operator()(const boost::shared_ptr< V > &expected, const boost::shared_ptr< V > &actual)
Definition: Testable.h:125
static bool Equals(const T &m1, const T &m2, double tol=1e-8)
Definition: Testable.h:158
Requirements on type to pass it to Testable template below.
Definition: Testable.h:133
BOOST_CONCEPT_USAGE(HasTestablePrereqs)
Definition: Testable.h:135
Matrix3d m1
Definition: IOFormat.cpp:2
Definition: pytypes.h:928
BOOST_CONCEPT_USAGE(IsTestable)
Definition: Testable.h:62
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
const G & b
Definition: Group.h:83
equals(double tol=1e-9)
Definition: Testable.h:112
traits
Definition: chartTesting.h:28
equals_star(double tol=1e-9)
Definition: Testable.h:124
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
static void Print(const T &m, const std::string &str="")
Definition: Testable.h:155
const G double tol
Definition: Group.h:83
bool equal(const T &obj1, const T &obj2, double tol)
Definition: Testable.h:83
double tol_
Definition: Testable.h:111
void Print(const CONTAINER &keys, const string &s, const KeyFormatter &keyFormatter)
Definition: Key.cpp:59
bool operator()(const V &expected, const V &actual)
Definition: Testable.h:113


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:04