Loading...
Searching...
No Matches
Testable.h
Go to the documentation of this file.
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
// \callgraph
#pragma once
#include <boost/concept_check.hpp>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#define GTSAM_PRINT(x)((x).print(#x))
namespace gtsam {
// Forward declaration
template <typename T> struct traits;
template <class T>
class IsTestable {
T t;
bool r1,r2;
public:
BOOST_CONCEPT_USAGE(IsTestable) {
// check print function, with optional string
traits<T>::Print(t, std::string());
traits<T>::Print(t);
// check print, with optional threshold
double tol = 1.0;
r1 = traits<T>::Equals(t,t,tol);
r2 = traits<T>::Equals(t,t);
}
}; // \ Testable
inline void print(float v, const std::string& s = "") {
std::cout << (s.empty() ? s : s + " ") << v << std::endl;
}
inline void print(double v, const std::string& s = "") {
std::cout << (s.empty() ? s : s + " ") << v << std::endl;
}
template<class T>
inline bool equal(const T& obj1, const T& obj2, double tol) {
return traits<T>::Equals(obj1,obj2, tol);
}
template<class T>
inline bool equal(const T& obj1, const T& obj2) {
return traits<T>::Equals(obj1,obj2);
}
template<class V>
bool assert_equal(const V& expected, const V& actual, double tol = 1e-9) {
if (traits<V>::Equals(actual,expected, tol))
return true;
printf("Not equal:\n");
traits<V>::Print(expected,"expected:\n");
traits<V>::Print(actual,"actual:\n");
return false;
}
template<class V>
struct equals : public std::function<bool(const V&, const V&)> {
double tol_;
equals(double tol = 1e-9) : tol_(tol) {}
bool operator()(const V& expected, const V& actual) {
return (traits<V>::Equals(actual, expected, tol_));
}
};
template<class V>
struct equals_star : public std::function<bool(const std::shared_ptr<V>&, const std::shared_ptr<V>&)> {
double tol_;
equals_star(double tol = 1e-9) : tol_(tol) {}
bool operator()(const std::shared_ptr<V>& expected, const std::shared_ptr<V>& actual) {
if (!actual && !expected) return true;
return actual && expected && traits<V>::Equals(*actual,*expected, tol_);
}
};
template<typename T>
struct HasTestablePrereqs {
BOOST_CONCEPT_USAGE(HasTestablePrereqs) {
t->print(str);
b = t->equals(*s,tol);
}
T *t, *s; // Pointer is to allow abstract classes
bool b;
double tol;
std::string str;
};
template<typename T>
struct Testable {
// Check that T has the necessary methods
BOOST_CONCEPT_ASSERT((HasTestablePrereqs<T>));
static void Print(const T& m, const std::string& str = "") {
m.print(str);
}
static bool Equals(const T& m1, const T& m2, double tol = 1e-8) {
return m1.equals(m2, tol);
}
};
} // \namespace gtsam
#define GTSAM_CONCEPT_TESTABLE_INST(T) template class gtsam::IsTestable<T>;
#define GTSAM_CONCEPT_TESTABLE_TYPE(T) using _gtsam_Testable_##T = gtsam::IsTestable<T>;
gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition Matrix.cpp:43
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:156
gtsam::equal
bool equal(const T &obj1, const T &obj2, double tol)
Call equal on the object.
Definition Testable.h:84
gtsam::traits
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition concepts.h:30
gtsam::IsTestable
A testable concept check that should be placed in applicable unit tests and in generic algorithms.
Definition Testable.h:58
gtsam::HasTestablePrereqs
Requirements on type to pass it to Testable template below.
Definition Testable.h:134
gtsam::Testable
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:151