| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    Testable | 
					
						
							|  |  |  |  * @brief   Abstract base class for values that can be used in unit tests | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 22:11:48 +08:00
										 |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-11-12 01:15:17 +08:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2009-10-31 22:11:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-11 12:57:14 +08:00
										 |  |  | #define GTSAM_PRINT(x)((x).print(#x))
 | 
					
						
							| 
									
										
										
										
											2009-11-23 05:18:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * The Testable class should be templated with the derived class, e.g. | 
					
						
							|  |  |  |    * class Rot3 : public Testable<Rot3>. This allows us to define the | 
					
						
							|  |  |  |    * input type of equals as a Rot3 as well. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 	template <class Derived> | 
					
						
							|  |  |  | 	class Testable { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2009-12-17 05:41:33 +08:00
										 |  |  | 	    virtual ~Testable() {} | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * print | 
					
						
							|  |  |  | 		 * @param s optional string naming the object | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		virtual void print(const std::string& name) const = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * equality up to tolerance | 
					
						
							| 
									
										
										
										
											2009-11-13 00:47:12 +08:00
										 |  |  | 		 * tricky to implement, see NonlinearFactor1 for an example | 
					
						
							| 
									
										
										
										
											2009-10-23 01:23:24 +08:00
										 |  |  | 		 * equals is not supposed to print out *anything*, just return true|false | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 		 */ | 
					
						
							|  |  |  | 		virtual bool equals(const Derived& expected, double tol) const = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	}; // Testable class
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * This template works for any type with equals | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	template<class V> | 
					
						
							| 
									
										
										
										
											2009-10-26 06:26:56 +08:00
										 |  |  | 	bool assert_equal(const V& expected, const V& actual, double tol = 1e-9) { | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 		if (actual.equals(expected, tol)) | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		printf("Not equal:\n"); | 
					
						
							|  |  |  | 		expected.print("expected"); | 
					
						
							| 
									
										
										
										
											2009-10-26 06:26:56 +08:00
										 |  |  | 		actual.print("actual"); | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 22:11:48 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Template to create a binary predicate | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	template<class V> | 
					
						
							| 
									
										
										
										
											2009-11-03 14:29:56 +08:00
										 |  |  | 	struct equals : public std::binary_function<const V&, const V&, bool> { | 
					
						
							|  |  |  | 		double tol_; | 
					
						
							|  |  |  | 		equals(double tol = 1e-9) : tol_(tol) {} | 
					
						
							|  |  |  | 		bool operator()(const V& expected, const V& actual) { | 
					
						
							|  |  |  | 			return (actual.equals(expected, tol_)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2009-10-31 22:11:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Binary predicate on shared pointers | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	template<class V> | 
					
						
							| 
									
										
										
										
											2009-11-03 14:29:56 +08:00
										 |  |  | 	struct equals_star : public std::binary_function<const boost::shared_ptr<V>&, const boost::shared_ptr<V>&, bool> { | 
					
						
							|  |  |  | 		double tol_; | 
					
						
							|  |  |  | 		equals_star(double tol = 1e-9) : tol_(tol) {} | 
					
						
							|  |  |  | 		bool operator()(const boost::shared_ptr<V>& expected, const boost::shared_ptr<V>& actual) { | 
					
						
							|  |  |  | 			return (actual->equals(*expected, tol_)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2009-10-31 22:11:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | } // gtsam
 |