/usr/include/mlpack/tests/serialization.hpp is in libmlpack-dev 2.0.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | /**
* @file serialization.hpp
* @author Ryan Curtin
*
* Miscellaneous utility functions for serialization tests.
*
* This file is part of mlpack 2.0.1.
*
* mlpack is free software; you may redstribute it and/or modify it under the
* terms of the 3-clause BSD license. You should have received a copy of the
* 3-clause BSD license along with mlpack. If not, see
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
*/
#ifndef __MLPACK_TESTS_SERIALIZATION_HPP
#define __MLPACK_TESTS_SERIALIZATION_HPP
#include <boost/serialization/serialization.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <mlpack/core.hpp>
#include <boost/test/unit_test.hpp>
#include "old_boost_test_definitions.hpp"
namespace mlpack {
// Test function for loading and saving Armadillo objects.
template<typename MatType,
typename IArchiveType,
typename OArchiveType>
void TestArmadilloSerialization(MatType& 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.
MatType 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, orig.n_elem);
for (size_t i = 0; i < x.n_cols; ++i)
for (size_t j = 0; j < x.n_rows; ++j)
if (double(orig(j, i)) == 0.0)
BOOST_REQUIRE_SMALL(double(x(j, i)), 1e-8);
else
BOOST_REQUIRE_CLOSE(double(orig(j, i)), double(x(j, i)), 1e-8);
remove("test");
}
// Test all serialization strategies.
template<typename MatType>
void TestAllArmadilloSerialization(MatType& x)
{
TestArmadilloSerialization<MatType, boost::archive::xml_iarchive,
boost::archive::xml_oarchive>(x);
TestArmadilloSerialization<MatType, boost::archive::text_iarchive,
boost::archive::text_oarchive>(x);
TestArmadilloSerialization<MatType, boost::archive::binary_iarchive,
boost::archive::binary_oarchive>(x);
}
// Save and load an mlpack object.
// The re-loaded copy is placed in 'newT'.
template<typename T, typename IArchiveType, typename OArchiveType>
void SerializeObject(T& t, T& newT)
{
std::ofstream ofs("test", std::ios::binary);
OArchiveType o(ofs);
bool success = true;
try
{
o << data::CreateNVP(t, "t");
}
catch (boost::archive::archive_exception& e)
{
success = false;
}
ofs.close();
BOOST_REQUIRE_EQUAL(success, true);
std::ifstream ifs("test", std::ios::binary);
IArchiveType i(ifs);
try
{
i >> data::CreateNVP(newT, "t");
}
catch (boost::archive::archive_exception& e)
{
success = false;
}
ifs.close();
BOOST_REQUIRE_EQUAL(success, true);
}
// Test mlpack serialization with all three archive types.
template<typename T>
void SerializeObjectAll(T& t, T& xmlT, T& textT, T& binaryT)
{
SerializeObject<T, boost::archive::text_iarchive,
boost::archive::text_oarchive>(t, textT);
SerializeObject<T, boost::archive::binary_iarchive,
boost::archive::binary_oarchive>(t, binaryT);
SerializeObject<T, boost::archive::xml_iarchive,
boost::archive::xml_oarchive>(t, xmlT);
}
// Save and load a non-default-constructible mlpack object.
template<typename T, typename IArchiveType, typename OArchiveType>
void SerializePointerObject(T* t, T*& newT)
{
std::ofstream ofs("test", std::ios::binary);
OArchiveType o(ofs);
bool success = true;
try
{
o << data::CreateNVP(*t, "t");
}
catch (boost::archive::archive_exception& e)
{
success = false;
}
ofs.close();
BOOST_REQUIRE_EQUAL(success, true);
std::ifstream ifs("test", std::ios::binary);
IArchiveType i(ifs);
try
{
newT = new T(i);
}
catch (std::exception& e)
{
success = false;
}
ifs.close();
BOOST_REQUIRE_EQUAL(success, true);
}
template<typename T>
void SerializePointerObjectAll(T* t, T*& xmlT, T*& textT, T*& binaryT)
{
SerializePointerObject<T, boost::archive::text_iarchive,
boost::archive::text_oarchive>(t, textT);
SerializePointerObject<T, boost::archive::binary_iarchive,
boost::archive::binary_oarchive>(t, binaryT);
SerializePointerObject<T, boost::archive::xml_iarchive,
boost::archive::xml_oarchive>(t, xmlT);
}
// Utility function to check the equality of two Armadillo matrices.
void CheckMatrices(const arma::mat& x,
const arma::mat& xmlX,
const arma::mat& textX,
const arma::mat& binaryX);
void CheckMatrices(const arma::Mat<size_t>& x,
const arma::Mat<size_t>& xmlX,
const arma::Mat<size_t>& textX,
const arma::Mat<size_t>& binaryX);
} // namespace mlpack
#endif
|