gtsam
Loading...
Searching...
No Matches
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
19
20#pragma once
21
22#include <gtsam/config.h>
23
24#if GTSAM_ENABLE_BOOST_SERIALIZATION
25
26#include <filesystem>
27#include <iostream>
28#include <sstream>
29#include <string>
30
33
34#include <boost/serialization/serialization.hpp>
35
36
37// whether to print the serialized text to stdout
38const bool verbose = false;
39
40namespace gtsam {
41namespace serializationTestHelpers {
42
43// templated default object creation so we only need to declare one friend (if applicable)
44template<class T>
45T create() {
46 return T();
47}
48
49// Creates or empties a folder in the build folder and returns the relative path
50inline std::filesystem::path resetFilesystem(
51 std::filesystem::path folder = "actual") {
52 std::filesystem::remove_all(folder);
53 std::filesystem::create_directory(folder);
54 return folder;
55}
56
57// Templated round-trip serialization
58template<class T>
59void 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
66template<class T>
67void roundtripFile(const T& input, T& output) {
68 std::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
74template<class T>
75bool 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
83template<class T>
84bool 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
91template<class T>
92bool 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
99template<class T>
100void 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
107template<class T>
108void roundtripXMLFile(const T& input, T& output) {
109 std::filesystem::path path = resetFilesystem()/"graph.xml";
110 serializeToXMLFile(input, path.string());
111 deserializeFromXMLFile(path.string(), output);
112}
113
114// This version requires equality operator
115template<class T>
116bool 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
124template<class T>
125bool 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
132template<class T>
133bool 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
140template<class T>
141void 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
148template<class T>
149void roundtripBinaryFile(const T& input, T& output) {
150 std::filesystem::path path = resetFilesystem()/"graph.bin";
151 serializeToBinaryFile(input, path.string());
152 deserializeFromBinaryFile(path.string(), output);
153}
154
155// This version requires equality operator
156template<class T>
157bool 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
165template<class T>
166bool 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
173template<class T>
174bool 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
Provides additional testing facilities for common data structures.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition Matrix.cpp:39
Convenience functions for serializing data structures via boost.serialization.