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


gtsam
Author(s):
autogenerated on Sat Dec 28 2024 04:03:59