mirror of
https://github.com/boostorg/histogram.git
synced 2026-02-18 02:02:10 +00:00
better serialization tests which avoid having the xml data inline
This commit is contained in:
@@ -48,7 +48,11 @@ alias run-tests :
|
||||
[ run linearize_test.cpp ]
|
||||
[ run meta_test.cpp ]
|
||||
[ run storage_adaptor_serialization_test.cpp
|
||||
/boost/serialization//boost_serialization/<link>static ]
|
||||
/boost/serialization//boost_serialization/<link>static : : :
|
||||
<define>VECTOR_INT_XML=\\\"$(THIS_PATH)/storage_adaptor_serialization_test_vector_int.xml\\\"
|
||||
<define>ARRAY_UNSIGNED_XML=\\\"$(THIS_PATH)/storage_adaptor_serialization_test_array_unsigned.xml\\\"
|
||||
<define>MAP_DOUBLE_XML=\\\"$(THIS_PATH)/storage_adaptor_serialization_test_map_double.xml\\\"
|
||||
]
|
||||
[ run storage_adaptor_test.cpp ]
|
||||
[ run unlimited_storage_serialization_test.cpp
|
||||
/boost/serialization//boost_serialization/<link>static ]
|
||||
|
||||
@@ -4,16 +4,12 @@
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/archive/xml_iarchive.hpp>
|
||||
#include <boost/archive/xml_oarchive.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/histogram.hpp>
|
||||
#include <boost/histogram/serialization.hpp>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include "utility_histogram.hpp"
|
||||
#include "utility_serialization.hpp"
|
||||
|
||||
using namespace boost::histogram;
|
||||
|
||||
@@ -30,35 +26,11 @@ void run_tests(const char* filename) {
|
||||
axis::variable<>({1.0, 2.0, 3.0}, "var"), axis::category<>{3, 1, 2},
|
||||
axis::integer<int, axis::null_type>(0, 2));
|
||||
a(0.5, 0.2, 20, 20, 2.5, 1, 1);
|
||||
|
||||
std::string ref;
|
||||
{
|
||||
std::ifstream file;
|
||||
file.open(filename);
|
||||
assert(file.is_open());
|
||||
while (file.good()) {
|
||||
char buf[1024];
|
||||
file.read(buf, 1024);
|
||||
ref.append(buf, file.gcount());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
std::string ofn(filename);
|
||||
ofn.erase(0, ofn.rfind("/") + 1);
|
||||
ofn.append(".new");
|
||||
std::ofstream of(ofn);
|
||||
boost::archive::xml_oarchive oa(of);
|
||||
oa << boost::serialization::make_nvp("hist", a);
|
||||
}
|
||||
save_xml(filename, a);
|
||||
|
||||
auto b = decltype(a)();
|
||||
BOOST_TEST_NE(a, b);
|
||||
{
|
||||
std::istringstream is(ref);
|
||||
boost::archive::xml_iarchive ia(is);
|
||||
ia >> boost::serialization::make_nvp("hist", b);
|
||||
}
|
||||
load_xml(filename, b);
|
||||
BOOST_TEST_EQ(a, b);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,99 +5,33 @@
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <array>
|
||||
#include <boost/archive/xml_iarchive.hpp>
|
||||
#include <boost/archive/xml_oarchive.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/histogram/serialization.hpp>
|
||||
#include <boost/histogram/storage_adaptor.hpp>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include "utility_serialization.hpp"
|
||||
|
||||
using namespace boost::histogram;
|
||||
|
||||
template <typename T>
|
||||
void test_serialization(const char* ref) {
|
||||
void test_serialization(const char* filename) {
|
||||
auto a = storage_adaptor<T>();
|
||||
a.reset(3);
|
||||
a[1] += 1;
|
||||
a[2] += 2;
|
||||
|
||||
std::ostringstream os;
|
||||
{
|
||||
boost::archive::xml_oarchive oa(os);
|
||||
oa << boost::serialization::make_nvp("storage", a);
|
||||
}
|
||||
save_xml(filename, a);
|
||||
|
||||
auto b = storage_adaptor<T>();
|
||||
BOOST_TEST(!(a == b));
|
||||
{
|
||||
std::istringstream is(ref);
|
||||
boost::archive::xml_iarchive ia(is);
|
||||
ia >> boost::serialization::make_nvp("storage", b);
|
||||
}
|
||||
BOOST_TEST_NOT(a == b);
|
||||
load_xml(filename, b);
|
||||
BOOST_TEST(a == b);
|
||||
}
|
||||
|
||||
int main() {
|
||||
test_serialization<std::vector<int>>(
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
|
||||
"<!DOCTYPE boost_serialization>\n"
|
||||
"<boost_serialization signature=\"serialization::archive\" version=\"17\">\n"
|
||||
"<storage class_id=\"0\" tracking_level=\"0\" version=\"0\">\n"
|
||||
" <impl class_id=\"1\" tracking_level=\"0\" version=\"0\">\n"
|
||||
" <vector>\n"
|
||||
" <count>3</count>\n"
|
||||
" <item_version>0</item_version>\n"
|
||||
" <item>0</item>\n"
|
||||
" <item>1</item>\n"
|
||||
" <item>2</item>\n"
|
||||
" </vector>\n"
|
||||
" </impl>\n"
|
||||
"</storage>\n"
|
||||
"</boost_serialization>\n\n");
|
||||
|
||||
test_serialization<std::array<unsigned, 10>>(
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
|
||||
"<!DOCTYPE boost_serialization>\n"
|
||||
"<boost_serialization signature=\"serialization::archive\" version=\"17\">\n"
|
||||
"<storage class_id=\"0\" tracking_level=\"0\" version=\"0\">\n"
|
||||
" <impl class_id=\"1\" tracking_level=\"0\" version=\"0\">\n"
|
||||
" <size>3</size>\n"
|
||||
" <array>\n"
|
||||
" <item>0</item>\n"
|
||||
" <item>1</item>\n"
|
||||
" <item>2</item>\n"
|
||||
" </array>\n"
|
||||
" </impl>\n"
|
||||
"</storage>\n"
|
||||
"</boost_serialization>\n\n");
|
||||
|
||||
test_serialization<std::map<std::size_t, double>>(
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
|
||||
"<!DOCTYPE boost_serialization>\n"
|
||||
"<boost_serialization signature=\"serialization::archive\" version=\"17\">\n"
|
||||
"<storage class_id=\"0\" tracking_level=\"0\" version=\"0\">\n"
|
||||
" <impl class_id=\"1\" tracking_level=\"0\" version=\"0\">\n"
|
||||
" <size>3</size>\n"
|
||||
" <map class_id=\"2\" tracking_level=\"0\" version=\"0\">\n"
|
||||
" <count>2</count>\n"
|
||||
" <item_version>0</item_version>\n"
|
||||
" <item class_id=\"3\" tracking_level=\"0\" version=\"0\">\n"
|
||||
" <first>1</first>\n"
|
||||
" <second>1.00000000000000000e+00</second>\n"
|
||||
" </item>\n"
|
||||
" <item>\n"
|
||||
" <first>2</first>\n"
|
||||
" <second>2.00000000000000000e+00</second>\n"
|
||||
" </item>\n"
|
||||
" </map>\n"
|
||||
" </impl>\n"
|
||||
"</storage>\n"
|
||||
"</boost_serialization>\n\n");
|
||||
test_serialization<std::vector<int>>(VECTOR_INT_XML);
|
||||
test_serialization<std::array<unsigned, 10>>(ARRAY_UNSIGNED_XML);
|
||||
test_serialization<std::map<std::size_t, double>>(MAP_DOUBLE_XML);
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
15
test/storage_adaptor_serialization_test_array_unsigned.xml
Normal file
15
test/storage_adaptor_serialization_test_array_unsigned.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<!DOCTYPE boost_serialization>
|
||||
<boost_serialization signature="serialization::archive" version="17">
|
||||
<item class_id="0" tracking_level="0" version="0">
|
||||
<impl class_id="1" tracking_level="0" version="0">
|
||||
<size>3</size>
|
||||
<array>
|
||||
<item>0</item>
|
||||
<item>1</item>
|
||||
<item>2</item>
|
||||
</array>
|
||||
</impl>
|
||||
</item>
|
||||
</boost_serialization>
|
||||
|
||||
22
test/storage_adaptor_serialization_test_map_double.xml
Normal file
22
test/storage_adaptor_serialization_test_map_double.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<!DOCTYPE boost_serialization>
|
||||
<boost_serialization signature="serialization::archive" version="17">
|
||||
<item class_id="0" tracking_level="0" version="0">
|
||||
<impl class_id="1" tracking_level="0" version="0">
|
||||
<size>3</size>
|
||||
<map class_id="2" tracking_level="0" version="0">
|
||||
<count>2</count>
|
||||
<item_version>0</item_version>
|
||||
<item class_id="3" tracking_level="0" version="0">
|
||||
<first>1</first>
|
||||
<second>1.00000000000000000e+00</second>
|
||||
</item>
|
||||
<item>
|
||||
<first>2</first>
|
||||
<second>2.00000000000000000e+00</second>
|
||||
</item>
|
||||
</map>
|
||||
</impl>
|
||||
</item>
|
||||
</boost_serialization>
|
||||
|
||||
16
test/storage_adaptor_serialization_test_vector_int.xml
Normal file
16
test/storage_adaptor_serialization_test_vector_int.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<!DOCTYPE boost_serialization>
|
||||
<boost_serialization signature="serialization::archive" version="17">
|
||||
<item class_id="0" tracking_level="0" version="0">
|
||||
<impl class_id="1" tracking_level="0" version="0">
|
||||
<vector>
|
||||
<count>3</count>
|
||||
<item_version>0</item_version>
|
||||
<item>0</item>
|
||||
<item>1</item>
|
||||
<item>2</item>
|
||||
</vector>
|
||||
</impl>
|
||||
</item>
|
||||
</boost_serialization>
|
||||
|
||||
34
test/utility_serialization.hpp
Normal file
34
test/utility_serialization.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
// Copyright 2019 Hans Dembinski
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_HISTOGRAM_TEST_UTILITY_SERIALIZATION_HPP
|
||||
#define BOOST_HISTOGRAM_TEST_UTILITY_SERIALIZATION_HPP
|
||||
|
||||
#include <boost/archive/xml_iarchive.hpp>
|
||||
#include <boost/archive/xml_oarchive.hpp>
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
template <class T>
|
||||
void load_xml(const char* filename, T& t) {
|
||||
std::ifstream ifs(filename);
|
||||
assert(ifs.is_open());
|
||||
boost::archive::xml_iarchive ia(ifs);
|
||||
ia >> boost::serialization::make_nvp("item", t);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void save_xml(const char* filename, const T& t) {
|
||||
std::string mod(filename);
|
||||
mod.erase(0, mod.rfind("/") + 1);
|
||||
mod.append(".new");
|
||||
std::ofstream ofs(mod);
|
||||
boost::archive::xml_oarchive oa(ofs);
|
||||
oa << boost::serialization::make_nvp("item", t);
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user