12#ifndef MLPACK_TESTS_SERIALIZATION_CATCH_HPP
13#define MLPACK_TESTS_SERIALIZATION_CATCH_HPP
15#include <boost/serialization/serialization.hpp>
16#include <boost/archive/xml_iarchive.hpp>
17#include <boost/archive/xml_oarchive.hpp>
18#include <boost/archive/text_iarchive.hpp>
19#include <boost/archive/text_oarchive.hpp>
20#include <boost/archive/binary_iarchive.hpp>
21#include <boost/archive/binary_oarchive.hpp>
31template<
typename CubeType,
32 typename IArchiveType,
33 typename OArchiveType>
38 std::string fileName =
FilterFileName(
typeid(IArchiveType).name());
47 o << BOOST_SERIALIZATION_NVP(x);
49 catch (boost::archive::archive_exception& e)
55 REQUIRE(success ==
true);
59 arma::Cube<CubeType> orig(x);
68 i >> BOOST_SERIALIZATION_NVP(x);
70 catch (boost::archive::archive_exception& e)
77 remove(fileName.c_str());
79 REQUIRE(success ==
true);
81 REQUIRE(x.n_rows == orig.n_rows);
82 REQUIRE(x.n_cols == orig.n_cols);
83 REQUIRE(x.n_elem_slice == orig.n_elem_slice);
84 REQUIRE(x.n_slices == orig.n_slices);
85 REQUIRE(x.n_elem == orig.n_elem);
87 for (
size_t slice = 0; slice != x.n_slices; ++slice)
89 const auto& origSlice = orig.slice(slice);
90 const auto& xSlice = x.slice(slice);
91 for (
size_t i = 0; i < x.n_cols; ++i)
93 for (
size_t j = 0; j < x.n_rows; ++j)
95 if (
double(origSlice(j, i)) == 0.0)
96 REQUIRE(
double(xSlice(j, i)) == Approx(0.0).margin(1e-8 / 100));
98 REQUIRE(
double(origSlice(j, i)) ==
99 Approx(
double(xSlice(j, i))).epsilon(1e-8 / 100));
106template<
typename CubeType>
110 boost::archive::xml_oarchive>(x);
112 boost::archive::text_oarchive>(x);
114 boost::archive::binary_oarchive>(x);
118template<
typename MatType,
119 typename IArchiveType,
120 typename OArchiveType>
124 std::string fileName =
FilterFileName(
typeid(IArchiveType).name());
133 o << BOOST_SERIALIZATION_NVP(x);
135 catch (boost::archive::archive_exception& e)
141 REQUIRE(success ==
true);
154 i >> BOOST_SERIALIZATION_NVP(x);
156 catch (boost::archive::archive_exception& e)
163 remove(fileName.c_str());
165 REQUIRE(success ==
true);
167 REQUIRE(x.n_rows == orig.n_rows);
168 REQUIRE(x.n_cols == orig.n_cols);
169 REQUIRE(x.n_elem == orig.n_elem);
171 for (
size_t i = 0; i < x.n_cols; ++i)
172 for (
size_t j = 0; j < x.n_rows; ++j)
173 if (
double(orig(j, i)) == 0.0)
174 REQUIRE(
double(x(j, i)) == Approx(0.0).margin(1e-8 / 100));
176 REQUIRE(
double(orig(j, i)) ==
177 Approx(
double(x(j, i))).epsilon(1e-8 / 100));
181template<
typename MatType>
185 boost::archive::xml_oarchive>(x);
187 boost::archive::text_oarchive>(x);
189 boost::archive::binary_oarchive>(x);
194template<
typename T,
typename IArchiveType,
typename OArchiveType>
206 o << BOOST_SERIALIZATION_NVP(t);
208 catch (boost::archive::archive_exception& e)
210 std::cerr << e.what() << std::endl;
216 REQUIRE(success ==
true);
225 i >> BOOST_SERIALIZATION_NVP(newT);
227 catch (boost::archive::archive_exception& e)
229 std::cout << e.what() <<
"\n";
235 remove(fileName.c_str());
237 REQUIRE(success ==
true);
245 boost::archive::xml_oarchive>(t, xmlT);
247 boost::archive::text_oarchive>(t, textT);
249 boost::archive::binary_oarchive>(t, binaryT);
253template<
typename T,
typename IArchiveType,
typename OArchiveType>
264 o << BOOST_SERIALIZATION_NVP(t);
266 catch (boost::archive::archive_exception& e)
268 std::cout << e.what() <<
"\n";
274 REQUIRE(success ==
true);
283 i >> BOOST_SERIALIZATION_NVP(newT);
285 catch (std::exception& e)
287 std::cout << e.what() <<
"\n";
293 remove(fileName.c_str());
295 REQUIRE(success ==
true);
302 boost::archive::text_oarchive>(t, textT);
304 boost::archive::binary_oarchive>(t, binaryT);
306 boost::archive::xml_oarchive>(t, xmlT);
311 const arma::mat& xmlX,
312 const arma::mat& textX,
313 const arma::mat& binaryX);
316 const arma::Mat<size_t>& xmlX,
317 const arma::Mat<size_t>& textX,
318 const arma::Mat<size_t>& binaryX);
321 const arma::cube& xmlX,
322 const arma::cube& textX,
323 const arma::cube& binaryX);
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
Linear algebra utility functions, generally performed on matrices or vectors.
void SerializeObject(T &t, T &newT)
void SerializePointerObjectAll(T *t, T *&xmlT, T *&textT, T *&binaryT)
void TestAllArmadilloSerialization(arma::Cube< CubeType > &x)
void CheckMatrices(const arma::mat &x, const arma::mat &xmlX, const arma::mat &textX, const arma::mat &binaryX)
void SerializePointerObject(T *t, T *&newT)
void TestArmadilloSerialization(arma::Cube< CubeType > &x)
void SerializeObjectAll(T &t, T &xmlT, T &textT, T &binaryT)