| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |  * @file   testVectorConfig.cpp | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @brief  Unit tests for Factor Graph Configuration | 
					
						
							|  |  |  |  * @author Carlos Nieto | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*STL/C++*/ | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <sstream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //#include TEST_AC_DEFINE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_BOOST_SERIALIZATION
 | 
					
						
							|  |  |  | #include <boost/archive/text_oarchive.hpp>
 | 
					
						
							|  |  |  | #include <boost/archive/text_iarchive.hpp>
 | 
					
						
							|  |  |  | #endif //HAVE_BOOST_SERIALIZATION
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | #define GTSAM_MAGIC_KEY
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | #include "Matrix.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | #include "VectorConfig.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include "smallExample.cpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-10 00:47:37 +08:00
										 |  |  | TEST( VectorConfig, equals1 ) | 
					
						
							|  |  |  |  { | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |    VectorConfig expected; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    Vector v = Vector_(3, 5.0, 6.0, 7.0); | 
					
						
							|  |  |  |    expected.insert("a",v); | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |    VectorConfig actual; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |    actual.insert("a",v); | 
					
						
							| 
									
										
										
										
											2009-12-12 06:43:34 +08:00
										 |  |  |    CHECK(assert_equal(expected,actual)); | 
					
						
							| 
									
										
										
										
											2009-11-10 00:47:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( VectorConfig, equals2 ) | 
					
						
							|  |  |  |  {	  | 
					
						
							|  |  |  |    VectorConfig cfg1, cfg2; | 
					
						
							|  |  |  |    Vector v1 = Vector_(3, 5.0, 6.0, 7.0); | 
					
						
							|  |  |  |    Vector v2 = Vector_(3, 5.0, 6.0, 8.0); | 
					
						
							|  |  |  |    cfg1.insert("x", v1); | 
					
						
							|  |  |  |    cfg2.insert("x", v2); | 
					
						
							|  |  |  |    CHECK(!cfg1.equals(cfg2)); | 
					
						
							|  |  |  |    CHECK(!cfg2.equals(cfg1)); | 
					
						
							|  |  |  |  } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-14 10:50:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <limits>
 | 
					
						
							|  |  |  | double inf = std::numeric_limits<double>::infinity(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 00:47:37 +08:00
										 |  |  | TEST( VectorConfig, equals_nan ) | 
					
						
							|  |  |  |  { | 
					
						
							|  |  |  |    VectorConfig cfg1, cfg2; | 
					
						
							|  |  |  |    Vector v1 = Vector_(3, 5.0, 6.0, 7.0); | 
					
						
							| 
									
										
										
										
											2010-01-14 10:50:06 +08:00
										 |  |  |    Vector v2 = Vector_(3, inf, inf, inf); | 
					
						
							| 
									
										
										
										
											2009-11-10 00:47:37 +08:00
										 |  |  |    cfg1.insert("x", v1); | 
					
						
							|  |  |  |    cfg2.insert("x", v2); | 
					
						
							|  |  |  |    CHECK(!cfg1.equals(cfg2)); | 
					
						
							|  |  |  |    CHECK(!cfg2.equals(cfg1)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | TEST( VectorConfig, contains) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   VectorConfig fg; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   Vector v = Vector_(3, 5.0, 6.0, 7.0); | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |   fg.insert("a", v); | 
					
						
							|  |  |  |   CHECK(fg.contains("a")); | 
					
						
							|  |  |  |   CHECK(!fg.contains("g")); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 06:43:34 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | TEST( VectorConfig, expmap) | 
					
						
							| 
									
										
										
										
											2009-12-12 06:43:34 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	VectorConfig c = createConfig(); | 
					
						
							|  |  |  | 	Vector v = Vector_(6, 0.0,-1.0, 0.0, 0.0, 1.5, 0.0); // l1, x1, x2
 | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  |   CHECK(assert_equal(expmap(c,c),expmap(c,v))); | 
					
						
							| 
									
										
										
										
											2009-12-12 06:43:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | TEST( VectorConfig, plus) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  |   VectorConfig c; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   Vector vx = Vector_(3, 5.0, 6.0, 7.0), vy = Vector_(2, 8.0, 9.0); | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  |   c += VectorConfig("x",vx); | 
					
						
							|  |  |  |   c += VectorConfig("y",vy); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   VectorConfig delta; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   Vector dx = Vector_(3, 1.0, 1.0, 1.0), dy = Vector_(2, -1.0, -1.0); | 
					
						
							|  |  |  |   delta.insert("x", dx).insert("y",dy); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   VectorConfig expected; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   Vector wx = Vector_(3, 6.0, 7.0, 8.0), wy = Vector_(2, 7.0, 8.0); | 
					
						
							|  |  |  |   expected.insert("x", wx).insert("y",wy); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // functional
 | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  |   VectorConfig actual = expmap(c,delta); | 
					
						
							| 
									
										
										
										
											2009-12-12 06:43:34 +08:00
										 |  |  |   CHECK(assert_equal(expected,actual)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-20 13:10:55 +08:00
										 |  |  | TEST( VectorConfig, scale) { | 
					
						
							|  |  |  | 	VectorConfig cfg; | 
					
						
							|  |  |  | 	cfg.insert("x", Vector_(2, 1.0, 2.0)); | 
					
						
							|  |  |  | 	cfg.insert("y", Vector_(2,-1.0,-2.0)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	VectorConfig actual = cfg.scale(2.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	VectorConfig expected; | 
					
						
							|  |  |  | 	expected.insert("x", Vector_(2, 2.0, 4.0)); | 
					
						
							|  |  |  | 	expected.insert("y", Vector_(2,-2.0,-4.0)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(assert_equal(actual, expected)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-24 23:12:59 +08:00
										 |  |  | TEST( VectorConfig, update_with_large_delta) { | 
					
						
							|  |  |  | 	// this test ensures that if the update for delta is larger than
 | 
					
						
							|  |  |  | 	// the size of the config, it only updates existing variables
 | 
					
						
							|  |  |  | 	VectorConfig init, delta; | 
					
						
							|  |  |  | 	init.insert("x", Vector_(2, 1.0, 2.0)); | 
					
						
							|  |  |  | 	init.insert("y", Vector_(2, 3.0, 4.0)); | 
					
						
							|  |  |  | 	delta.insert("x", Vector_(2, 0.1, 0.1)); | 
					
						
							|  |  |  | 	delta.insert("y", Vector_(2, 0.1, 0.1)); | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	delta.insert("p", Vector_(2, 0.1, 0.1)); | 
					
						
							| 
									
										
										
										
											2009-11-24 23:12:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | 	VectorConfig actual = expmap(init,delta); | 
					
						
							| 
									
										
										
										
											2009-11-24 23:12:59 +08:00
										 |  |  | 	VectorConfig expected; | 
					
						
							|  |  |  | 	expected.insert("x", Vector_(2, 1.1, 2.1)); | 
					
						
							|  |  |  | 	expected.insert("y", Vector_(2, 3.1, 4.1)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(assert_equal(actual, expected)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | TEST( VectorConfig, dot) { | 
					
						
							|  |  |  | 	VectorConfig c = createConfig(); | 
					
						
							|  |  |  | 	DOUBLES_EQUAL(3.25,dot(c,c),1e-9); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( VectorConfig, dim) { | 
					
						
							|  |  |  | 	VectorConfig c = createConfig(); | 
					
						
							|  |  |  | 	LONGS_EQUAL(6,c.dim()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( VectorConfig, operators) { | 
					
						
							|  |  |  | 	VectorConfig c; c.insert("x", Vector_(2, 1.1, 2.2)); | 
					
						
							|  |  |  | 	VectorConfig expected1; expected1.insert("x", Vector_(2, 2.2, 4.4)); | 
					
						
							|  |  |  | 	CHECK(assert_equal(expected1,c*2)); | 
					
						
							|  |  |  | 	CHECK(assert_equal(expected1,c+c)); | 
					
						
							|  |  |  | 	VectorConfig expected2; expected2.insert("x", Vector_(2, 0.0, 0.0)); | 
					
						
							|  |  |  | 	CHECK(assert_equal(expected2,c-c)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-30 22:53:40 +08:00
										 |  |  | TEST( VectorConfig, getReference) { | 
					
						
							|  |  |  | 	VectorConfig c; c.insert("x", Vector_(2, 1.1, 2.2)); | 
					
						
							|  |  |  | 	Vector& cx = c.getReference("x"); | 
					
						
							|  |  |  | 	cx = cx*2.0; | 
					
						
							|  |  |  | 	VectorConfig expected; expected.insert("x", Vector_(2, 2.2, 4.4)); | 
					
						
							|  |  |  | 	CHECK(assert_equal(expected,c)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #ifdef HAVE_BOOST_SERIALIZATION
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | TEST( VectorConfig, serialize) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     //DEBUG:
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |     cout << "VectorConfig: Running Serialization Test" << endl; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |     //create an VectorConfig
 | 
					
						
							|  |  |  |     VectorConfig fg = createConfig(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      | 
					
						
							|  |  |  |     //serialize the config
 | 
					
						
							|  |  |  |     std::ostringstream in_archive_stream; | 
					
						
							|  |  |  |     boost::archive::text_oarchive in_archive(in_archive_stream); | 
					
						
							|  |  |  |     in_archive << fg; | 
					
						
							|  |  |  |     std::string serialized_fgc = in_archive_stream.str(); | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     //deserialize the config
 | 
					
						
							|  |  |  |     std::istringstream out_archive_stream(serialized_fgc); | 
					
						
							|  |  |  |     boost::archive::text_iarchive out_archive(out_archive_stream); | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |     VectorConfig output; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     out_archive >> output; | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     //check for equality
 | 
					
						
							|  |  |  |     CHECK(fg.equals(output)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif //HAVE_BOOST_SERIALIZATION
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr); } | 
					
						
							|  |  |  | /* ************************************************************************* */ |