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>
37 std::string fileName =
FilterFileName(
typeid(IArchiveType).name());
46 o << BOOST_SERIALIZATION_NVP(x);
48 catch (boost::archive::archive_exception& e)
54 BOOST_REQUIRE_EQUAL(success,
true);
58 arma::Cube<CubeType> orig(x);
67 i >> BOOST_SERIALIZATION_NVP(x);
69 catch (boost::archive::archive_exception& e)
76 remove(fileName.c_str());
78 BOOST_REQUIRE_EQUAL(success,
true);
80 BOOST_REQUIRE_EQUAL(x.n_rows, orig.n_rows);
81 BOOST_REQUIRE_EQUAL(x.n_cols, orig.n_cols);
82 BOOST_REQUIRE_EQUAL(x.n_elem_slice, orig.n_elem_slice);
83 BOOST_REQUIRE_EQUAL(x.n_slices, orig.n_slices);
84 BOOST_REQUIRE_EQUAL(x.n_elem, orig.n_elem);
86 for (
size_t slice = 0; slice != x.n_slices; ++slice)
88 const auto& origSlice = orig.slice(slice);
89 const auto& xSlice = x.slice(slice);
90 for (
size_t i = 0; i < x.n_cols; ++i)
92 for (
size_t j = 0; j < x.n_rows; ++j)
94 if (
double(origSlice(j, i)) == 0.0)
95 BOOST_REQUIRE_SMALL(
double(xSlice(j, i)), 1e-8);
97 BOOST_REQUIRE_CLOSE(
double(origSlice(j, i)),
double(xSlice(j, i)),
105 template<
typename CubeType>
109 boost::archive::xml_oarchive>(x);
111 boost::archive::text_oarchive>(x);
113 boost::archive::binary_oarchive>(x);
117 template<
typename MatType,
118 typename IArchiveType,
119 typename OArchiveType>
123 std::string fileName =
FilterFileName(
typeid(IArchiveType).name());
132 o << BOOST_SERIALIZATION_NVP(x);
134 catch (boost::archive::archive_exception& e)
140 BOOST_REQUIRE_EQUAL(success,
true);
153 i >> BOOST_SERIALIZATION_NVP(x);
155 catch (boost::archive::archive_exception& e)
162 remove(fileName.c_str());
164 BOOST_REQUIRE_EQUAL(success,
true);
166 BOOST_REQUIRE_EQUAL(x.n_rows, orig.n_rows);
167 BOOST_REQUIRE_EQUAL(x.n_cols, orig.n_cols);
168 BOOST_REQUIRE_EQUAL(x.n_elem, orig.n_elem);
170 for (
size_t i = 0; i < x.n_cols; ++i)
171 for (
size_t j = 0; j < x.n_rows; ++j)
172 if (
double(orig(j, i)) == 0.0)
173 BOOST_REQUIRE_SMALL(
double(x(j, i)), 1e-8);
175 BOOST_REQUIRE_CLOSE(
double(orig(j, i)),
double(x(j, i)), 1e-8);
179 template<
typename MatType>
183 boost::archive::xml_oarchive>(x);
185 boost::archive::text_oarchive>(x);
187 boost::archive::binary_oarchive>(x);
192 template<
typename T,
typename IArchiveType,
typename OArchiveType>
204 o << BOOST_SERIALIZATION_NVP(t);
206 catch (boost::archive::archive_exception& e)
208 std::cerr << e.what() << std::endl;
214 BOOST_REQUIRE_EQUAL(success,
true);
223 i >> BOOST_SERIALIZATION_NVP(newT);
225 catch (boost::archive::archive_exception& e)
227 std::cout << e.what() <<
"\n";
233 remove(fileName.c_str());
235 BOOST_REQUIRE_EQUAL(success,
true);
243 boost::archive::xml_oarchive>(t, xmlT);
245 boost::archive::text_oarchive>(t, textT);
247 boost::archive::binary_oarchive>(t, binaryT);
251 template<
typename T,
typename IArchiveType,
typename OArchiveType>
262 o << BOOST_SERIALIZATION_NVP(t);
264 catch (boost::archive::archive_exception& e)
266 std::cout << e.what() <<
"\n";
272 BOOST_REQUIRE_EQUAL(success,
true);
281 i >> BOOST_SERIALIZATION_NVP(newT);
283 catch (std::exception& e)
285 std::cout << e.what() <<
"\n";
291 remove(fileName.c_str());
293 BOOST_REQUIRE_EQUAL(success,
true);
300 boost::archive::text_oarchive>(t, textT);
302 boost::archive::binary_oarchive>(t, binaryT);
304 boost::archive::xml_oarchive>(t, xmlT);
309 const arma::mat& xmlX,
310 const arma::mat& textX,
311 const arma::mat& binaryX);
314 const arma::Mat<size_t>& xmlX,
315 const arma::Mat<size_t>& textX,
316 const arma::Mat<size_t>& binaryX);
319 const arma::cube& xmlX,
320 const arma::cube& textX,
321 const arma::cube& binaryX);
void SerializePointerObject(T *t, T *&newT)
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)