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


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:42