unlimited_storage_serialization_test.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2018 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/archive/text_iarchive.hpp>
  7. #include <boost/archive/text_oarchive.hpp>
  8. #include <boost/assert.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <boost/histogram/serialization.hpp>
  11. #include <boost/histogram/unlimited_storage.hpp>
  12. #include <memory>
  13. #include <sstream>
  14. #include "throw_exception.hpp"
  15. #include "utility_serialization.hpp"
  16. using unlimited_storage_type = boost::histogram::unlimited_storage<>;
  17. using namespace boost::histogram;
  18. template <typename T>
  19. unlimited_storage_type prepare(std::size_t n, const T x) {
  20. std::unique_ptr<T[]> v(new T[n]);
  21. std::fill(v.get(), v.get() + n, static_cast<T>(0));
  22. v.get()[0] = x;
  23. return unlimited_storage_type(n, v.get());
  24. }
  25. template <typename T>
  26. unlimited_storage_type prepare(std::size_t n) {
  27. return unlimited_storage_type(n, static_cast<T*>(nullptr));
  28. }
  29. template <typename T>
  30. void run_test(const std::string& filename) {
  31. const auto a = prepare(1, T(1));
  32. print_xml(filename, a);
  33. unlimited_storage_type b;
  34. BOOST_TEST(!(a == b));
  35. load_xml(filename, b);
  36. BOOST_TEST(a == b);
  37. }
  38. int main(int argc, char** argv) {
  39. BOOST_ASSERT(argc == 2);
  40. run_test<uint8_t>(join(argv[1], "unlimited_storage_serialization_test_u8.xml"));
  41. run_test<uint16_t>(join(argv[1], "unlimited_storage_serialization_test_u16.xml"));
  42. run_test<uint32_t>(join(argv[1], "unlimited_storage_serialization_test_u32.xml"));
  43. run_test<uint64_t>(join(argv[1], "unlimited_storage_serialization_test_u64.xml"));
  44. run_test<unlimited_storage_type::large_int>(
  45. join(argv[1], "unlimited_storage_serialization_test_large_int.xml"));
  46. run_test<double>(join(argv[1], "unlimited_storage_serialization_test_double.xml"));
  47. return boost::report_errors();
  48. }