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
bool deserializeFromFile(const std::string &filename, T &output)
deserializes from a file
void roundtripBinary(const T &input, T &output)
std::string serialize(const T &input)
serializes to a string
bool equalsDereferencedXML(const T &input=T())
void roundtrip(const T &input, T &output)
bool serializeToFile(const T &input, const std::string &filename)
serializes to a file
bool equalsDereferencedBinary(const T &input=T())
void roundtripFile(const T &input, T &output)
void roundtripXML(const T &input, T &output)
const bool verbose
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 roundtripXMLFile(const T &input, T &output)
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
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
void deserialize(const std::string &serialized, T &output)
deserializes from a string
void roundtripBinaryFile(const T &input, T &output)
bool serializeToXMLFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to an XML file
bool deserializeFromBinaryFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from a binary file
boost::filesystem::path resetFilesystem(boost::filesystem::path folder="actual")


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:44:01