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