12 #ifndef MLPACK_TESTS_SERIALIZATION_HPP 13 #define MLPACK_TESTS_SERIALIZATION_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> 24 #include <boost/test/unit_test.hpp> 30 template<
typename CubeType,
31 typename IArchiveType,
32 typename OArchiveType>
42 o << BOOST_SERIALIZATION_NVP(x);
44 catch (boost::archive::archive_exception& e)
49 BOOST_REQUIRE_EQUAL(success,
true);
53 arma::Cube<CubeType> orig(x);
60 i >> BOOST_SERIALIZATION_NVP(x);
62 catch (boost::archive::archive_exception& e)
67 BOOST_REQUIRE_EQUAL(success,
true);
69 BOOST_REQUIRE_EQUAL(x.n_rows, orig.n_rows);
70 BOOST_REQUIRE_EQUAL(x.n_cols, orig.n_cols);
71 BOOST_REQUIRE_EQUAL(x.n_elem_slice, orig.n_elem_slice);
72 BOOST_REQUIRE_EQUAL(x.n_slices, orig.n_slices);
73 BOOST_REQUIRE_EQUAL(x.n_elem, orig.n_elem);
75 for(
size_t slice = 0; slice != x.n_slices; ++slice){
76 auto const &orig_slice = orig.slice(slice);
77 auto const &x_slice = x.slice(slice);
78 for (
size_t i = 0; i < x.n_cols; ++i){
79 for (
size_t j = 0; j < x.n_rows; ++j){
80 if (
double(orig_slice(j, i)) == 0.0)
81 BOOST_REQUIRE_SMALL(
double(x_slice(j, i)), 1e-8);
83 BOOST_REQUIRE_CLOSE(
double(orig_slice(j, i)),
double(x_slice(j, i)), 1e-8);
92 template<
typename CubeType>
96 boost::archive::xml_oarchive>(x);
98 boost::archive::text_oarchive>(x);
100 boost::archive::binary_oarchive>(x);
104 template<
typename MatType,
105 typename IArchiveType,
106 typename OArchiveType>
116 o << BOOST_SERIALIZATION_NVP(x);
118 catch (boost::archive::archive_exception& e)
123 BOOST_REQUIRE_EQUAL(success,
true);
134 i >> BOOST_SERIALIZATION_NVP(x);
136 catch (boost::archive::archive_exception& e)
141 BOOST_REQUIRE_EQUAL(success,
true);
143 BOOST_REQUIRE_EQUAL(x.n_rows, orig.n_rows);
144 BOOST_REQUIRE_EQUAL(x.n_cols, orig.n_cols);
145 BOOST_REQUIRE_EQUAL(x.n_elem, orig.n_elem);
147 for (
size_t i = 0; i < x.n_cols; ++i)
148 for (
size_t j = 0; j < x.n_rows; ++j)
149 if (
double(orig(j, i)) == 0.0)
150 BOOST_REQUIRE_SMALL(
double(x(j, i)), 1e-8);
152 BOOST_REQUIRE_CLOSE(
double(orig(j, i)),
double(x(j, i)), 1e-8);
158 template<
typename MatType>
162 boost::archive::xml_oarchive>(x);
164 boost::archive::text_oarchive>(x);
166 boost::archive::binary_oarchive>(x);
171 template<
typename T,
typename IArchiveType,
typename OArchiveType>
182 catch (boost::archive::archive_exception& e)
188 BOOST_REQUIRE_EQUAL(success,
true);
197 catch (boost::archive::archive_exception& e)
203 BOOST_REQUIRE_EQUAL(success,
true);
211 boost::archive::text_oarchive>(t, textT);
213 boost::archive::binary_oarchive>(t, binaryT);
215 boost::archive::xml_oarchive>(t, xmlT);
219 template<
typename T,
typename IArchiveType,
typename OArchiveType>
230 catch (boost::archive::archive_exception& e)
236 BOOST_REQUIRE_EQUAL(success,
true);
245 catch (std::exception& e)
251 BOOST_REQUIRE_EQUAL(success,
true);
258 boost::archive::text_oarchive>(t, textT);
260 boost::archive::binary_oarchive>(t, binaryT);
262 boost::archive::xml_oarchive>(t, xmlT);
267 const arma::mat& xmlX,
268 const arma::mat& textX,
269 const arma::mat& binaryX);
272 const arma::Mat<size_t>& xmlX,
273 const arma::Mat<size_t>& textX,
274 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 std::enable_if_t< HasSerialize< T >::value > *=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 TestArmadilloSerialization(arma::Cube< CubeType > &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 TestAllArmadilloSerialization(arma::Cube< CubeType > &x)