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;
71  r1 = traits<T>::Equals(t,t,tol);
72  r2 = traits<T>::Equals(t,t);
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>;
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
Matrix3f m
#define GTSAM_CONCEPT_ASSERT(concept)
Definition: base/concepts.h:22
bool operator()(const std::shared_ptr< V > &expected, const std::shared_ptr< V > &actual)
Definition: Testable.h:127
Matrix expected
Definition: testMatrix.cpp:971
MatrixType m2(n_dims)
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
static bool Equals(const T &m1, const T &m2, double tol=1e-8)
Definition: Testable.h:160
Requirements on type to pass it to Testable template below.
Definition: Testable.h:135
BOOST_CONCEPT_USAGE(HasTestablePrereqs)
Definition: Testable.h:137
Matrix3d m1
Definition: IOFormat.cpp:2
Definition: pytypes.h:1403
Array< int, Dynamic, 1 > v
BOOST_CONCEPT_USAGE(IsTestable)
Definition: Testable.h:64
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
const G & b
Definition: Group.h:86
equals(double tol=1e-9)
Definition: Testable.h:114
traits
Definition: chartTesting.h:28
equals_star(double tol=1e-9)
Definition: Testable.h:126
static void Print(const T &m, const std::string &str="")
Definition: Testable.h:157
const G double tol
Definition: Group.h:86
bool equal(const T &obj1, const T &obj2, double tol)
Definition: Testable.h:85
double tol_
Definition: Testable.h:113
void Print(const CONTAINER &keys, const string &s, const KeyFormatter &keyFormatter)
Definition: Key.cpp:62
bool operator()(const V &expected, const V &actual)
Definition: Testable.h:115


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:46