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 <gtsam/base/concepts.h>
37 
38 #include <functional>
39 #include <iostream>
40 #include <memory>
41 #include <string>
42 
43 #define GTSAM_PRINT(x)((x).print(#x))
44 
45 namespace gtsam {
46 
47  // Forward declaration
48  template <typename T> struct traits;
49 
58  template <class T>
59  class IsTestable {
60  T t;
61  bool r1,r2;
62  public:
63 
65  // check print function, with optional string
66  traits<T>::Print(t, std::string());
68 
69  // check print, with optional threshold
70  double tol = 1.0;
73  }
74  }; // \ Testable
75 
76  inline void print(float v, const std::string& s = "") {
77  std::cout << (s.empty() ? s : s + " ") << v << std::endl;
78  }
79  inline void print(double v, const std::string& s = "") {
80  std::cout << (s.empty() ? s : s + " ") << v << std::endl;
81  }
82 
84  template<class T>
85  inline bool equal(const T& obj1, const T& obj2, double tol) {
86  return traits<T>::Equals(obj1,obj2, tol);
87  }
88 
90  template<class T>
91  inline bool equal(const T& obj1, const T& obj2) {
92  return traits<T>::Equals(obj1,obj2);
93  }
94 
98  template<class V>
99  bool assert_equal(const V& expected, const V& actual, double tol = 1e-9) {
100  if (traits<V>::Equals(actual,expected, tol))
101  return true;
102  printf("Not equal:\n");
103  traits<V>::Print(expected,"expected:\n");
104  traits<V>::Print(actual,"actual:\n");
105  return false;
106  }
107 
111  template<class V>
112  struct equals : public std::function<bool(const V&, const V&)> {
113  double tol_;
114  equals(double tol = 1e-9) : tol_(tol) {}
115  bool operator()(const V& expected, const V& actual) {
116  return (traits<V>::Equals(actual, expected, tol_));
117  }
118  };
119 
123  template<class V>
124  struct equals_star : public std::function<bool(const std::shared_ptr<V>&, const std::shared_ptr<V>&)> {
125  double tol_;
126  equals_star(double tol = 1e-9) : tol_(tol) {}
127  bool operator()(const std::shared_ptr<V>& expected, const std::shared_ptr<V>& actual) {
128  if (!actual && !expected) return true;
129  return actual && expected && traits<V>::Equals(*actual,*expected, tol_);
130  }
131  };
132 
134  template<typename T>
136 
138  t->print(str);
139  b = t->equals(*s,tol);
140  }
141 
142  T *t, *s; // Pointer is to allow abstract classes
143  bool b;
144  double tol;
145  std::string str;
146  };
147 
151  template<typename T>
152  struct Testable {
153 
154  // Check that T has the necessary methods
156 
157  static void Print(const T& m, const std::string& str = "") {
158  m.print(str);
159  }
160  static bool Equals(const T& m1, const T& m2, double tol = 1e-8) {
161  return m1.equals(m2, tol);
162  }
163  };
164 
165 } // \namespace gtsam
166 
176 #define GTSAM_CONCEPT_TESTABLE_INST(T) template class gtsam::IsTestable<T>;
177 #define GTSAM_CONCEPT_TESTABLE_TYPE(T) using _gtsam_Testable_##T = gtsam::IsTestable<T>;
gtsam::equals_star::operator()
bool operator()(const std::shared_ptr< V > &expected, const std::shared_ptr< V > &actual)
Definition: Testable.h:127
gtsam::IsTestable::r1
bool r1
Definition: Testable.h:61
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
gtsam::HasTestablePrereqs::BOOST_CONCEPT_USAGE
BOOST_CONCEPT_USAGE(HasTestablePrereqs)
Definition: Testable.h:137
m1
Matrix3d m1
Definition: IOFormat.cpp:2
concepts.h
m2
MatrixType m2(n_dims)
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
gtsam::HasTestablePrereqs::s
T * s
Definition: Testable.h:142
gtsam::HasTestablePrereqs::tol
double tol
Definition: Testable.h:144
gtsam::HasTestablePrereqs::str
std::string str
Definition: Testable.h:145
gtsam::equals_star
Definition: Testable.h:124
gtsam::IsTestable::t
T t
Definition: Testable.h:60
cholesky::expected
Matrix expected
Definition: testMatrix.cpp:971
gtsam::IsTestable::BOOST_CONCEPT_USAGE
BOOST_CONCEPT_USAGE(IsTestable)
Definition: Testable.h:64
gtsam::IsTestable::r2
bool r2
Definition: Testable.h:61
gtsam::Testable::Equals
static bool Equals(const T &m1, const T &m2, double tol=1e-8)
Definition: Testable.h:160
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
Eigen::Triplet< double >
gtsam::equals::operator()
bool operator()(const V &expected, const V &actual)
Definition: Testable.h:115
gtsam::equals
Definition: Testable.h:112
str
Definition: pytypes.h:1524
gtsam::Testable::GTSAM_CONCEPT_ASSERT
GTSAM_CONCEPT_ASSERT(HasTestablePrereqs< T >)
gtsam
traits
Definition: chartTesting.h:28
gtsam::Testable
Definition: Testable.h:152
gtsam::traits
Definition: Group.h:36
gtsam::Testable::Print
static void Print(const T &m, const std::string &str="")
Definition: Testable.h:157
gtsam::Print
void Print(const CONTAINER &keys, const string &s, const KeyFormatter &keyFormatter)
Definition: Key.cpp:65
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
gtsam::equals_star::tol_
double tol_
Definition: Testable.h:125
gtsam::tol
const G double tol
Definition: Group.h:79
V
MatrixXcd V
Definition: EigenSolver_EigenSolver_MatrixType.cpp:15
gtsam::equals_star::equals_star
equals_star(double tol=1e-9)
Definition: Testable.h:126
gtsam::HasTestablePrereqs::t
T * t
Definition: Testable.h:142
gtsam::IsTestable
Definition: Testable.h:59
gtsam::HasTestablePrereqs
Requirements on type to pass it to Testable template below.
Definition: Testable.h:135
gtsam::equals::equals
equals(double tol=1e-9)
Definition: Testable.h:114
gtsam::equal
bool equal(const T &obj1, const T &obj2, double tol)
Definition: Testable.h:85
gtsam::equals::tol_
double tol_
Definition: Testable.h:113
gtsam::HasTestablePrereqs::b
bool b
Definition: Testable.h:143


gtsam
Author(s):
autogenerated on Thu Jun 13 2024 03:08:55