gtsam  4.0.0
gtsam
serializationTestHelpers.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 
20 #pragma once
21 
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 
26 #include <gtsam/base/serialization.h>
27 
28 #include <boost/serialization/serialization.hpp>
29 
30 
31 // whether to print the serialized text to stdout
32 const bool verbose = false;
33 
34 namespace gtsam {
35 namespace serializationTestHelpers {
36 
37 // templated default object creation so we only need to declare one friend (if applicable)
38 template<class T>
39 T create() {
40  return T();
41 }
42 
43 // Templated round-trip serialization
44 template<class T>
45 void roundtrip(const T& input, T& output) {
46  // Serialize
47  std::string serialized = serialize(input);
48  if (verbose) std::cout << serialized << std::endl << std::endl;
49 
50  deserialize(serialized, output);
51 }
52 
53 // This version requires equality operator
54 template<class T>
55 bool equality(const T& input = T()) {
56  T output = create<T>();
57  roundtrip<T>(input,output);
58  return input==output;
59 }
60 
61 // This version requires Testable
62 template<class T>
63 bool equalsObj(const T& input = T()) {
64  T output = create<T>();
65  roundtrip<T>(input,output);
66  return assert_equal(input, output);
67 }
68 
69 // De-referenced version for pointers, requires equals method
70 template<class T>
71 bool equalsDereferenced(const T& input) {
72  T output = create<T>();
73  roundtrip<T>(input,output);
74  return input->equals(*output);
75 }
76 
77 // Templated round-trip serialization using XML
78 template<class T>
79 void roundtripXML(const T& input, T& output) {
80  // Serialize
81  std::string serialized = serializeXML<T>(input);
82  if (verbose) std::cout << serialized << std::endl << std::endl;
83 
84  // De-serialize
85  deserializeXML(serialized, output);
86 }
87 
88 // This version requires equality operator
89 template<class T>
90 bool equalityXML(const T& input = T()) {
91  T output = create<T>();
92  roundtripXML<T>(input,output);
93  return input==output;
94 }
95 
96 // This version requires Testable
97 template<class T>
98 bool equalsXML(const T& input = T()) {
99  T output = create<T>();
100  roundtripXML<T>(input,output);
101  return assert_equal(input, output);
102 }
103 
104 // This version is for pointers, requires equals method
105 template<class T>
106 bool equalsDereferencedXML(const T& input = T()) {
107  T output = create<T>();
108  roundtripXML<T>(input,output);
109  return input->equals(*output);
110 }
111 
112 // Templated round-trip serialization using XML
113 template<class T>
114 void roundtripBinary(const T& input, T& output) {
115  // Serialize
116  std::string serialized = serializeBinary<T>(input);
117  if (verbose) std::cout << serialized << std::endl << std::endl;
118 
119  // De-serialize
120  deserializeBinary(serialized, output);
121 }
122 
123 // This version requires equality operator
124 template<class T>
125 bool equalityBinary(const T& input = T()) {
126  T output = create<T>();
127  roundtripBinary<T>(input,output);
128  return input==output;
129 }
130 
131 // This version requires Testable
132 template<class T>
133 bool equalsBinary(const T& input = T()) {
134  T output = create<T>();
135  roundtripBinary<T>(input,output);
136  return assert_equal(input, output);
137 }
138 
139 // This version is for pointers, requires equals method
140 template<class T>
141 bool equalsDereferencedBinary(const T& input = T()) {
142  T output = create<T>();
143  roundtripBinary<T>(input,output);
144  return input->equals(*output);
145 }
146 
147 } // \namespace serializationTestHelpers
148 } // \namespace gtsam
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition: Matrix.cpp:42
Global functions in a separate testing namespace.
Definition: chartTesting.h:28