From 76b2c5165e5f003af6b28186f2e6e61c6512e7e8 Mon Sep 17 00:00:00 2001 From: stereomatchingkiss Date: Wed, 10 Feb 2016 17:25:50 +0800 Subject: [PATCH] add test case for cube seriliazation --- src/mlpack/tests/serialization.hpp | 74 +++++++++++++++++++++++++ src/mlpack/tests/serialization_test.cpp | 7 +++ 2 files changed, 81 insertions(+) diff --git a/src/mlpack/tests/serialization.hpp b/src/mlpack/tests/serialization.hpp index 530217b0e3..d423a3c509 100644 --- a/src/mlpack/tests/serialization.hpp +++ b/src/mlpack/tests/serialization.hpp @@ -21,6 +21,80 @@ namespace mlpack { +// Test function for loading and saving Armadillo objects. +template +void TestArmadilloSerialization(arma::Cube& x) +{ + // First save it. + std::ofstream ofs("test", std::ios::binary); + OArchiveType o(ofs); + + bool success = true; + try + { + o << BOOST_SERIALIZATION_NVP(x); + } + catch (boost::archive::archive_exception& e) + { + success = false; + } + + BOOST_REQUIRE_EQUAL(success, true); + ofs.close(); + + // Now load it. + arma::Cube orig(x); + success = true; + std::ifstream ifs("test", std::ios::binary); + IArchiveType i(ifs); + + try + { + i >> BOOST_SERIALIZATION_NVP(x); + } + catch (boost::archive::archive_exception& e) + { + success = false; + } + + BOOST_REQUIRE_EQUAL(success, true); + + BOOST_REQUIRE_EQUAL(x.n_rows, orig.n_rows); + BOOST_REQUIRE_EQUAL(x.n_cols, orig.n_cols); + BOOST_REQUIRE_EQUAL(x.n_elem_slice, orig.n_elem_slice); + BOOST_REQUIRE_EQUAL(x.n_slices, orig.n_slices); + BOOST_REQUIRE_EQUAL(x.n_elem, orig.n_elem); + + for(size_t slice = 0; slice != x.n_slices; ++slice){ + auto const &orig_slice = orig.slice(slice); + auto const &x_slice = x.slice(slice); + for (size_t i = 0; i < x.n_cols; ++i){ + for (size_t j = 0; j < x.n_rows; ++j){ + if (double(orig_slice(j, i)) == 0.0) + BOOST_REQUIRE_SMALL(double(x_slice(j, i)), 1e-8); + else + BOOST_REQUIRE_CLOSE(double(orig_slice(j, i)), double(x_slice(j, i)), 1e-8); + } + } + } + + remove("test"); +} + +// Test all serialization strategies. +template +void TestAllArmadilloSerialization(arma::Cube& x) +{ + TestArmadilloSerialization(x); + TestArmadilloSerialization(x); + TestArmadilloSerialization(x); +} + // Test function for loading and saving Armadillo objects. template