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