Loading...
Searching...
No Matches
serializationTestHelpers.h
Go to the documentation of this file.
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
#pragma once
#include <iostream>
#include <sstream>
#include <string>
#include <gtsam/base/serialization.h>
#include <boost/serialization/serialization.hpp>
#include <boost/filesystem.hpp>
// whether to print the serialized text to stdout
const bool verbose = false;
namespace gtsam {
namespace serializationTestHelpers {
// templated default object creation so we only need to declare one friend (if applicable)
template<class T>
T create() {
return T();
}
// Creates or empties a folder in the build folder and returns the relative path
inline boost::filesystem::path resetFilesystem(
boost::filesystem::path folder = "actual") {
boost::filesystem::remove_all(folder);
boost::filesystem::create_directory(folder);
return folder;
}
// Templated round-trip serialization
template<class T>
void roundtrip(const T& input, T& output) {
std::string serialized = serialize(input);
if (verbose) std::cout << serialized << std::endl << std::endl;
deserialize(serialized, output);
}
// Templated round-trip serialization using a file
template<class T>
void roundtripFile(const T& input, T& output) {
boost::filesystem::path path = resetFilesystem()/"graph.dat";
serializeToFile(input, path.string());
deserializeFromFile(path.string(), output);
}
// This version requires equality operator and uses string and file round-trips
template<class T>
bool equality(const T& input = T()) {
T output = create<T>(), outputf = create<T>();
roundtrip<T>(input,output);
roundtripFile<T>(input,outputf);
return (input==output) && (input==outputf);
}
// This version requires Testable
template<class T>
bool equalsObj(const T& input = T()) {
T output = create<T>();
roundtrip<T>(input,output);
return assert_equal(input, output);
}
// De-referenced version for pointers, requires equals method
template<class T>
bool equalsDereferenced(const T& input) {
T output = create<T>();
roundtrip<T>(input,output);
return input->equals(*output);
}
// Templated round-trip serialization using XML
template<class T>
void roundtripXML(const T& input, T& output) {
std::string serialized = serializeXML<T>(input);
if (verbose) std::cout << serialized << std::endl << std::endl;
deserializeXML(serialized, output);
}
// Templated round-trip serialization using XML File
template<class T>
void roundtripXMLFile(const T& input, T& output) {
boost::filesystem::path path = resetFilesystem()/"graph.xml";
serializeToXMLFile(input, path.string());
deserializeFromXMLFile(path.string(), output);
}
// This version requires equality operator
template<class T>
bool equalityXML(const T& input = T()) {
T output = create<T>(), outputf = create<T>();
roundtripXML<T>(input,output);
roundtripXMLFile<T>(input,outputf);
return (input==output) && (input==outputf);
}
// This version requires Testable
template<class T>
bool equalsXML(const T& input = T()) {
T output = create<T>();
roundtripXML<T>(input,output);
return assert_equal(input, output);
}
// This version is for pointers, requires equals method
template<class T>
bool equalsDereferencedXML(const T& input = T()) {
T output = create<T>();
roundtripXML<T>(input,output);
return input->equals(*output);
}
// Templated round-trip serialization using XML
template<class T>
void roundtripBinary(const T& input, T& output) {
std::string serialized = serializeBinary<T>(input);
if (verbose) std::cout << serialized << std::endl << std::endl;
deserializeBinary(serialized, output);
}
// Templated round-trip serialization using Binary file
template<class T>
void roundtripBinaryFile(const T& input, T& output) {
boost::filesystem::path path = resetFilesystem()/"graph.bin";
serializeToBinaryFile(input, path.string());
deserializeFromBinaryFile(path.string(), output);
}
// This version requires equality operator
template<class T>
bool equalityBinary(const T& input = T()) {
T output = create<T>(), outputf = create<T>();
roundtripBinary<T>(input,output);
roundtripBinaryFile<T>(input,outputf);
return (input==output) && (input==outputf);
}
// This version requires Testable
template<class T>
bool equalsBinary(const T& input = T()) {
T output = create<T>();
roundtripBinary<T>(input,output);
return assert_equal(input, output);
}
// This version is for pointers, requires equals method
template<class T>
bool equalsDereferencedBinary(const T& input = T()) {
T output = create<T>();
roundtripBinary<T>(input,output);
return input->equals(*output);
}
} // \namespace serializationTestHelpers
} // \namespace gtsam
serialization.h
Convenience functions for serializing data structures via boost.serialization.
gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
gtsam::deserializeFromXMLFile
bool deserializeFromXMLFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from an XML file
Definition serialization.h:174
gtsam::serialize
std::string serialize(const T &input)
serializes to a string
Definition serialization.h:113
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition Matrix.cpp:43
gtsam::deserializeBinary
void deserializeBinary(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in binary
Definition serialization.h:266
gtsam::deserializeXML
void deserializeXML(const std::string &serialized, T &output, const std::string &name="data")
deserializes from a string in XML
Definition serialization.h:192
gtsam::serializeToXMLFile
bool serializeToXMLFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to an XML file
Definition serialization.h:163
gtsam::serializeXML
std::string serializeXML(const T &input, const std::string &name="data")
serializes to a string in XML
Definition serialization.h:185
gtsam::serializeToFile
bool serializeToFile(const T &input, const std::string &filename)
serializes to a file
Definition serialization.h:93
gtsam::deserialize
void deserialize(const std::string &serialized, T &output)
deserializes from a string
Definition serialization.h:119
gtsam::serializeBinary
std::string serializeBinary(const T &input, const std::string &name="data")
serializes to a string in binary
Definition serialization.h:259
gtsam::serializeToBinaryFile
bool serializeToBinaryFile(const T &input, const std::string &filename, const std::string &name="data")
serializes to a binary file
Definition serialization.h:237
gtsam::deserializeFromFile
bool deserializeFromFile(const std::string &filename, T &output)
deserializes from a file
Definition serialization.h:103
gtsam::deserializeFromBinaryFile
bool deserializeFromBinaryFile(const std::string &filename, T &output, const std::string &name="data")
deserializes from a binary file
Definition serialization.h:248