diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e49ffa6..08985055 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -136,6 +136,10 @@ if (TEST_SERIALIZATION) compiled_test(test/storage_adaptor_serialization_test.cpp) target_link_libraries(storage_adaptor_serialization_test PRIVATE Boost::serialization) + target_compile_definitions(storage_adaptor_serialization_test PRIVATE + -DVECTOR_INT_XML="${PROJECT_SOURCE_DIR}/test/storage_adaptor_serialization_test_vector_int.xml" + -DARRAY_UNSIGNED_XML="${PROJECT_SOURCE_DIR}/test/storage_adaptor_serialization_test_array_unsigned.xml" + -DMAP_DOUBLE_XML="${PROJECT_SOURCE_DIR}/test/storage_adaptor_serialization_test_map_double.xml") compiled_test(test/histogram_serialization_test.cpp Boost::serialization) target_compile_definitions(histogram_serialization_test PRIVATE diff --git a/test/Jamfile b/test/Jamfile index dc5f1857..611d93a6 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -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/static ] + /boost/serialization//boost_serialization/static : : : + VECTOR_INT_XML=\\\"$(THIS_PATH)/storage_adaptor_serialization_test_vector_int.xml\\\" + ARRAY_UNSIGNED_XML=\\\"$(THIS_PATH)/storage_adaptor_serialization_test_array_unsigned.xml\\\" + 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/static ] diff --git a/test/histogram_serialization_test.cpp b/test/histogram_serialization_test.cpp index d3eb9e6f..d26d57a8 100644 --- a/test/histogram_serialization_test.cpp +++ b/test/histogram_serialization_test.cpp @@ -4,16 +4,12 @@ // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) -#include -#include #include #include #include #include -#include -#include -#include #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(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); } diff --git a/test/storage_adaptor_serialization_test.cpp b/test/storage_adaptor_serialization_test.cpp index 9801b5a0..7fb2e6cf 100644 --- a/test/storage_adaptor_serialization_test.cpp +++ b/test/storage_adaptor_serialization_test.cpp @@ -5,99 +5,33 @@ // or copy at http://www.boost.org/LICENSE_1_0.txt) #include -#include -#include #include #include #include #include -#include -#include -#include -#include #include +#include "utility_serialization.hpp" using namespace boost::histogram; template -void test_serialization(const char* ref) { +void test_serialization(const char* filename) { auto a = storage_adaptor(); 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(); - 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>( - "\n" - "\n" - "\n" - "\n" - " \n" - " \n" - " 3\n" - " 0\n" - " 0\n" - " 1\n" - " 2\n" - " \n" - " \n" - "\n" - "\n\n"); - - test_serialization>( - "\n" - "\n" - "\n" - "\n" - " \n" - " 3\n" - " \n" - " 0\n" - " 1\n" - " 2\n" - " \n" - " \n" - "\n" - "\n\n"); - - test_serialization>( - "\n" - "\n" - "\n" - "\n" - " \n" - " 3\n" - " \n" - " 2\n" - " 0\n" - " \n" - " 1\n" - " 1.00000000000000000e+00\n" - " \n" - " \n" - " 2\n" - " 2.00000000000000000e+00\n" - " \n" - " \n" - " \n" - "\n" - "\n\n"); + test_serialization>(VECTOR_INT_XML); + test_serialization>(ARRAY_UNSIGNED_XML); + test_serialization>(MAP_DOUBLE_XML); return boost::report_errors(); } diff --git a/test/storage_adaptor_serialization_test_array_unsigned.xml b/test/storage_adaptor_serialization_test_array_unsigned.xml new file mode 100644 index 00000000..15c01e0f --- /dev/null +++ b/test/storage_adaptor_serialization_test_array_unsigned.xml @@ -0,0 +1,15 @@ + + + + + + 3 + + 0 + 1 + 2 + + + + + diff --git a/test/storage_adaptor_serialization_test_map_double.xml b/test/storage_adaptor_serialization_test_map_double.xml new file mode 100644 index 00000000..67118ae3 --- /dev/null +++ b/test/storage_adaptor_serialization_test_map_double.xml @@ -0,0 +1,22 @@ + + + + + + 3 + + 2 + 0 + + 1 + 1.00000000000000000e+00 + + + 2 + 2.00000000000000000e+00 + + + + + + diff --git a/test/storage_adaptor_serialization_test_vector_int.xml b/test/storage_adaptor_serialization_test_vector_int.xml new file mode 100644 index 00000000..5229d07e --- /dev/null +++ b/test/storage_adaptor_serialization_test_vector_int.xml @@ -0,0 +1,16 @@ + + + + + + + 3 + 0 + 0 + 1 + 2 + + + + + diff --git a/test/utility_serialization.hpp b/test/utility_serialization.hpp new file mode 100644 index 00000000..74b66e9a --- /dev/null +++ b/test/utility_serialization.hpp @@ -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 +#include +#include +#include +#include + +template +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 +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