gtsam
Loading...
Searching...
No Matches
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
31
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
45namespace 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
64 GTSAM_CONCEPT_USAGE(IsTestable) {
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
137 GTSAM_CONCEPT_USAGE(HasTestablePrereqs) {
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
155 GTSAM_CONCEPT_ASSERT(HasTestablePrereqs<T>);
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>;
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:39
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
bool equal(const T &obj1, const T &obj2, double tol)
Call equal on the object.
Definition Testable.h:85
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A testable concept check that should be placed in applicable unit tests and in generic algorithms.
Definition Testable.h:59
Requirements on type to pass it to Testable template below.
Definition Testable.h:135
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152