12 #ifndef MLPACK_TESTS_SERIALIZATION_CATCH_HPP 13 #define MLPACK_TESTS_SERIALIZATION_CATCH_HPP 23 template<
typename CubeType,
24 typename IArchiveType,
25 typename OArchiveType>
26 void TestArmadilloSerialization(arma::Cube<CubeType>& x)
30 std::string fileName = FilterFileName(
typeid(IArchiveType).name());
31 std::ofstream ofs(fileName, std::ios::binary);
41 arma::Cube<CubeType> orig(x);
42 std::ifstream ifs(fileName, std::ios::binary);
50 remove(fileName.c_str());
52 REQUIRE(x.n_rows == orig.n_rows);
53 REQUIRE(x.n_cols == orig.n_cols);
54 REQUIRE(x.n_elem_slice == orig.n_elem_slice);
55 REQUIRE(x.n_slices == orig.n_slices);
56 REQUIRE(x.n_elem == orig.n_elem);
58 for (
size_t slice = 0; slice != x.n_slices; ++slice)
60 const auto& origSlice = orig.slice(slice);
61 const auto& xSlice = x.slice(slice);
62 for (
size_t i = 0; i < x.n_cols; ++i)
64 for (
size_t j = 0; j < x.n_rows; ++j)
66 if (
double(origSlice(j, i)) == 0.0)
67 REQUIRE(
double(xSlice(j, i)) ==
Approx(0.0).margin(1e-8 / 100));
69 REQUIRE(
double(origSlice(j, i)) ==
70 Approx(
double(xSlice(j, i))).epsilon(1e-8 / 100));
77 template<
typename CubeType>
78 void TestAllArmadilloSerialization(arma::Cube<CubeType>& x)
80 TestArmadilloSerialization<CubeType, cereal::XMLInputArchive,
81 cereal::XMLOutputArchive>(x);
82 TestArmadilloSerialization<CubeType, cereal::JSONInputArchive,
83 cereal::JSONOutputArchive>(x);
84 TestArmadilloSerialization<CubeType, cereal::BinaryInputArchive,
85 cereal::BinaryOutputArchive>(x);
89 template<
typename MatType,
90 typename IArchiveType,
91 typename OArchiveType>
92 void TestArmadilloSerialization(MatType& x)
95 std::string fileName = FilterFileName(
typeid(IArchiveType).name());
96 std::ofstream ofs(fileName, std::ios::binary);
107 std::ifstream ifs(fileName, std::ios::binary);
115 remove(fileName.c_str());
117 REQUIRE(x.n_rows == orig.n_rows);
118 REQUIRE(x.n_cols == orig.n_cols);
119 REQUIRE(x.n_elem == orig.n_elem);
121 for (
size_t i = 0; i < x.n_cols; ++i)
122 for (
size_t j = 0; j < x.n_rows; ++j)
123 if (
double(orig(j, i)) == 0.0)
124 REQUIRE(
double(x(j, i)) ==
Approx(0.0).margin(1e-8 / 100));
126 REQUIRE(
double(orig(j, i)) ==
127 Approx(
double(x(j, i))).epsilon(1e-8 / 100));
131 template<
typename MatType>
132 void TestAllArmadilloSerialization(MatType& x)
134 TestArmadilloSerialization<MatType, cereal::XMLInputArchive,
135 cereal::XMLOutputArchive>(x);
136 TestArmadilloSerialization<MatType, cereal::JSONInputArchive,
137 cereal::JSONOutputArchive>(x);
138 TestArmadilloSerialization<MatType, cereal::BinaryInputArchive,
139 cereal::BinaryOutputArchive>(x);
144 template<
typename T,
typename IArchiveType,
typename OArchiveType>
145 void SerializeObject(T& t, T& newT)
147 std::string fileName = FilterFileName(
typeid(T).name());
148 std::ofstream ofs(fileName, std::ios::binary);
158 std::ifstream ifs(fileName, std::ios::binary);
167 remove(fileName.c_str());
172 void SerializeObjectAll(T& t, T& xmlT, T& jsonT, T& binaryT)
174 SerializeObject<T, cereal::XMLInputArchive,
175 cereal::XMLOutputArchive>(t, xmlT);
176 SerializeObject<T, cereal::JSONInputArchive,
177 cereal::JSONOutputArchive>(t, jsonT);
178 SerializeObject<T, cereal::BinaryInputArchive,
179 cereal::BinaryOutputArchive>(t, binaryT);
183 template<
typename T,
typename IArchiveType,
typename OArchiveType>
184 void SerializePointerObject(T* t, T*& newT)
186 std::string fileName = FilterFileName(
typeid(T).name());
187 std::ofstream ofs(fileName, std::ios::binary);
195 std::ifstream ifs(fileName, std::ios::binary);
202 remove(fileName.c_str());
206 void SerializePointerObjectAll(T* t, T*& xmlT, T*& jsonT, T*& binaryT)
208 SerializePointerObject<T, cereal::JSONInputArchive,
209 cereal::JSONOutputArchive>(t, jsonT);
210 SerializePointerObject<T, cereal::BinaryInputArchive,
211 cereal::BinaryOutputArchive>(t, binaryT);
212 SerializePointerObject<T, cereal::XMLInputArchive,
213 cereal::XMLOutputArchive>(t, xmlT);
218 const arma::mat& xmlX,
219 const arma::mat& jsonX,
220 const arma::mat& binaryX);
223 const arma::Mat<size_t>& xmlX,
224 const arma::Mat<size_t>& jsonX,
225 const arma::Mat<size_t>& binaryX);
228 const arma::cube& xmlX,
229 const arma::cube& jsonX,
230 const arma::cube& binaryX);
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
void CheckMatrices(std::vector< std::vector< double >> &vec1, std::vector< std::vector< double >> &vec2, const double tolerance=1e-3)
Check that 2 matrices of type vector<vector<double>> are close to equal, using the given tolerance...
Definition: range_search_utils.hpp:41
#define CEREAL_POINTER(T)
Cereal does not support the serialization of raw pointer.
Definition: pointer_wrapper.hpp:96
Definition: catch.hpp:3076