gtsam 4.1.1
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
33const bool verbose = false;
34
35namespace gtsam {
36namespace serializationTestHelpers {
37
38// templated default object creation so we only need to declare one friend (if applicable)
39template<class T>
40T create() {
41 return T();
42}
43
44// Creates or empties a folder in the build folder and returns the relative path
45boost::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
53template<class T>
54void 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
61template<class T>
62void 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
69template<class T>
70bool 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
78template<class T>
79bool 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
86template<class T>
87bool 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
94template<class T>
95void 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
102template<class T>
103void 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
110template<class T>
111bool 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
119template<class T>
120bool 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
127template<class T>
128bool 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
135template<class T>
136void 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
143template<class T>
144void 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
151template<class T>
152bool 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
160template<class T>
161bool 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
168template<class T>
169bool 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
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
bool deserializeFromXMLFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from an XML file
Definition: serialization.h:173
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:112
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition: Matrix.cpp:42
void deserializeBinary(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in binary
Definition: serialization.h:265
void deserializeXML(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in XML
Definition: serialization.h:191
bool serializeToXMLFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to an XML file
Definition: serialization.h:162
bool serializeToFile(const T &input, const std::string &filename)
serializes to a file
Definition: serialization.h:92
void deserialize(const std::string &serialized, T &output)
deserializes from a string
Definition: serialization.h:118
bool serializeToBinaryFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to a binary file
Definition: serialization.h:236
bool deserializeFromFile(const std::string &filename, T &output)
deserializes from a file
Definition: serialization.h:102
bool deserializeFromBinaryFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from a binary file
Definition: serialization.h:247
Convenience functions for serializing data structures via boost.serialization.