14 #ifndef __MLPACK_TESTS_SERIALIZATION_HPP
15 #define __MLPACK_TESTS_SERIALIZATION_HPP
17 #include <boost/serialization/serialization.hpp>
18 #include <boost/archive/xml_iarchive.hpp>
19 #include <boost/archive/xml_oarchive.hpp>
20 #include <boost/archive/text_iarchive.hpp>
21 #include <boost/archive/text_oarchive.hpp>
22 #include <boost/archive/binary_iarchive.hpp>
23 #include <boost/archive/binary_oarchive.hpp>
26 #include <boost/test/unit_test.hpp>
32 template<
typename MatType,
33 typename IArchiveType,
34 typename OArchiveType>
44 o << BOOST_SERIALIZATION_NVP(x);
46 catch (boost::archive::archive_exception& e)
51 BOOST_REQUIRE_EQUAL(success,
true);
62 i >> BOOST_SERIALIZATION_NVP(x);
64 catch (boost::archive::archive_exception& e)
69 BOOST_REQUIRE_EQUAL(success,
true);
71 BOOST_REQUIRE_EQUAL(x.n_rows, orig.n_rows);
72 BOOST_REQUIRE_EQUAL(x.n_cols, orig.n_cols);
73 BOOST_REQUIRE_EQUAL(x.n_elem, orig.n_elem);
75 for (
size_t i = 0; i < x.n_cols; ++i)
76 for (
size_t j = 0; j < x.n_rows; ++j)
77 if (
double(orig(j, i)) == 0.0)
78 BOOST_REQUIRE_SMALL(
double(x(j, i)), 1e-8);
80 BOOST_REQUIRE_CLOSE(
double(orig(j, i)),
double(x(j, i)), 1e-8);
86 template<
typename MatType>
90 boost::archive::xml_oarchive>(x);
92 boost::archive::text_oarchive>(x);
94 boost::archive::binary_oarchive>(x);
99 template<
typename T,
typename IArchiveType,
typename OArchiveType>
110 catch (boost::archive::archive_exception& e)
116 BOOST_REQUIRE_EQUAL(success,
true);
125 catch (boost::archive::archive_exception& e)
131 BOOST_REQUIRE_EQUAL(success,
true);
139 boost::archive::text_oarchive>(t, textT);
141 boost::archive::binary_oarchive>(t, binaryT);
143 boost::archive::xml_oarchive>(t, xmlT);
147 template<
typename T,
typename IArchiveType,
typename OArchiveType>
158 catch (boost::archive::archive_exception& e)
164 BOOST_REQUIRE_EQUAL(success,
true);
173 catch (std::exception& e)
179 BOOST_REQUIRE_EQUAL(success,
true);
186 boost::archive::text_oarchive>(t, textT);
188 boost::archive::binary_oarchive>(t, binaryT);
190 boost::archive::xml_oarchive>(t, xmlT);
195 const arma::mat& xmlX,
196 const arma::mat& textX,
197 const arma::mat& binaryX);
200 const arma::Mat<size_t>& xmlX,
201 const arma::Mat<size_t>& textX,
202 const arma::Mat<size_t>& binaryX);
void SerializePointerObject(T *t, T *&newT)
Linear algebra utility functions, generally performed on matrices or vectors.
FirstShim< T > CreateNVP(T &t, const std::string &name, typename boost::enable_if< HasSerialize< T >>::type *=0)
Call this function to produce a name-value pair; this is similar to BOOST_SERIALIZATION_NVP(), but should be used for types that have a Serialize() function (or contain a type that has a Serialize() function) instead of a serialize() function.
void CheckMatrices(const arma::mat &x, const arma::mat &xmlX, const arma::mat &textX, const arma::mat &binaryX)
void TestAllArmadilloSerialization(MatType &x)
void SerializePointerObjectAll(T *t, T *&xmlT, T *&textT, T *&binaryT)
void SerializeObjectAll(T &t, T &xmlT, T &textT, T &binaryT)
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
void SerializeObject(T &t, T &newT)
void TestArmadilloSerialization(MatType &x)