gtsam  4.1.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 
27 
28 #include <boost/serialization/serialization.hpp>
29 #include <boost/filesystem.hpp>
30 
31 
32 // whether to print the serialized text to stdout
33 const bool verbose = false;
34 
35 namespace gtsam {
36 namespace serializationTestHelpers {
37 
38 // templated default object creation so we only need to declare one friend (if applicable)
39 template<class T>
40 T create() {
41  return T();
42 }
43 
44 // Creates or empties a folder in the build folder and returns the relative path
45 boost::filesystem::path resetFilesystem(
46  boost::filesystem::path folder = "actual") {
47  boost::filesystem::remove_all(folder);
48  boost::filesystem::create_directory(folder);
49  return folder;
50 }
51 
52 // Templated round-trip serialization
53 template<class T>
54 void roundtrip(const T& input, T& output) {
55  std::string serialized = serialize(input);
56  if (verbose) std::cout << serialized << std::endl << std::endl;
57  deserialize(serialized, output);
58 }
59 
60 // Templated round-trip serialization using a file
61 template<class T>
62 void roundtripFile(const T& input, T& output) {
63  boost::filesystem::path path = resetFilesystem()/"graph.dat";
64  serializeToFile(input, path.string());
65  deserializeFromFile(path.string(), output);
66 }
67 
68 // This version requires equality operator and uses string and file round-trips
69 template<class T>
70 bool equality(const T& input = T()) {
71  T output = create<T>(), outputf = create<T>();
72  roundtrip<T>(input,output);
73  roundtripFile<T>(input,outputf);
74  return (input==output) && (input==outputf);
75 }
76 
77 // This version requires Testable
78 template<class T>
79 bool equalsObj(const T& input = T()) {
80  T output = create<T>();
81  roundtrip<T>(input,output);
82  return assert_equal(input, output);
83 }
84 
85 // De-referenced version for pointers, requires equals method
86 template<class T>
87 bool equalsDereferenced(const T& input) {
88  T output = create<T>();
89  roundtrip<T>(input,output);
90  return input->equals(*output);
91 }
92 
93 // Templated round-trip serialization using XML
94 template<class T>
95 void roundtripXML(const T& input, T& output) {
96  std::string serialized = serializeXML<T>(input);
97  if (verbose) std::cout << serialized << std::endl << std::endl;
98  deserializeXML(serialized, output);
99 }
100 
101 // Templated round-trip serialization using XML File
102 template<class T>
103 void roundtripXMLFile(const T& input, T& output) {
104  boost::filesystem::path path = resetFilesystem()/"graph.xml";
105  serializeToXMLFile(input, path.string());
106  deserializeFromXMLFile(path.string(), output);
107 }
108 
109 // This version requires equality operator
110 template<class T>
111 bool equalityXML(const T& input = T()) {
112  T output = create<T>(), outputf = create<T>();
113  roundtripXML<T>(input,output);
114  roundtripXMLFile<T>(input,outputf);
115  return (input==output) && (input==outputf);
116 }
117 
118 // This version requires Testable
119 template<class T>
120 bool equalsXML(const T& input = T()) {
121  T output = create<T>();
122  roundtripXML<T>(input,output);
123  return assert_equal(input, output);
124 }
125 
126 // This version is for pointers, requires equals method
127 template<class T>
128 bool equalsDereferencedXML(const T& input = T()) {
129  T output = create<T>();
130  roundtripXML<T>(input,output);
131  return input->equals(*output);
132 }
133 
134 // Templated round-trip serialization using XML
135 template<class T>
136 void roundtripBinary(const T& input, T& output) {
137  std::string serialized = serializeBinary<T>(input);
138  if (verbose) std::cout << serialized << std::endl << std::endl;
139  deserializeBinary(serialized, output);
140 }
141 
142 // Templated round-trip serialization using Binary file
143 template<class T>
144 void roundtripBinaryFile(const T& input, T& output) {
145  boost::filesystem::path path = resetFilesystem()/"graph.bin";
146  serializeToBinaryFile(input, path.string());
147  deserializeFromBinaryFile(path.string(), output);
148 }
149 
150 // This version requires equality operator
151 template<class T>
152 bool equalityBinary(const T& input = T()) {
153  T output = create<T>(), outputf = create<T>();
154  roundtripBinary<T>(input,output);
155  roundtripBinaryFile<T>(input,outputf);
156  return (input==output) && (input==outputf);
157 }
158 
159 // This version requires Testable
160 template<class T>
161 bool equalsBinary(const T& input = T()) {
162  T output = create<T>();
163  roundtripBinary<T>(input,output);
164  return assert_equal(input, output);
165 }
166 
167 // This version is for pointers, requires equals method
168 template<class T>
169 bool equalsDereferencedBinary(const T& input = T()) {
170  T output = create<T>();
171  roundtripBinary<T>(input,output);
172  return input->equals(*output);
173 }
174 
175 } // \namespace serializationTestHelpers
176 } // \namespace gtsam
gtsam::deserializeXML
void deserializeXML(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in XML
Definition: serialization.h:179
gtsam::serializeToBinaryFile
bool serializeToBinaryFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to a binary file
Definition: serialization.h:224
gtsam::serialize
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:100
gtsam::deserializeFromFile
bool deserializeFromFile(const std::string &filename, T &output)
deserializes from a file
Definition: serialization.h:90
gtsam::deserializeFromBinaryFile
bool deserializeFromBinaryFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from a binary file
Definition: serialization.h:235
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::serializeToXMLFile
bool serializeToXMLFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to an XML file
Definition: serialization.h:150
gtsam::deserializeBinary
void deserializeBinary(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in binary
Definition: serialization.h:253
gtsam::deserializeFromXMLFile
bool deserializeFromXMLFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from an XML file
Definition: serialization.h:161
gtsam::deserialize
void deserialize(const std::string &serialized, T &output)
deserializes from a string
Definition: serialization.h:106
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:42
gtsam::serializeToFile
bool serializeToFile(const T &input, const std::string &filename)
serializes to a file
Definition: serialization.h:80
serialization.h
Convenience functions for serializing data structures via boost.serialization.