accumulators_serialization_test.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2019 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/assert.hpp>
  7. #include <boost/core/lightweight_test.hpp>
  8. #include <boost/histogram/accumulators.hpp>
  9. #include <boost/histogram/serialization.hpp>
  10. #include <boost/histogram/weight.hpp>
  11. #include "throw_exception.hpp"
  12. #include "utility_serialization.hpp"
  13. using namespace boost::histogram;
  14. int main(int argc, char** argv) {
  15. BOOST_ASSERT(argc == 2);
  16. // mean v0
  17. {
  18. const auto filename = join(argv[1], "accumulators_serialization_test_mean_v0.xml");
  19. accumulators::mean<> a;
  20. load_xml(filename, a);
  21. BOOST_TEST_EQ(a.count(), 3);
  22. BOOST_TEST_EQ(a.value(), 2);
  23. BOOST_TEST_EQ(a.variance(), 0.5);
  24. }
  25. // mean
  26. {
  27. const auto filename = join(argv[1], "accumulators_serialization_test_mean.xml");
  28. accumulators::mean<> a;
  29. a(1);
  30. a(weight(0.5), 2);
  31. a(3);
  32. print_xml(filename, a);
  33. accumulators::mean<> b;
  34. BOOST_TEST_NOT(a == b);
  35. load_xml(filename, b);
  36. BOOST_TEST(a == b);
  37. }
  38. // sum
  39. {
  40. const auto filename = join(argv[1], "accumulators_serialization_test_sum.xml");
  41. accumulators::sum<> a;
  42. a += 1e100;
  43. a += 1;
  44. print_xml(filename, a);
  45. accumulators::sum<> b;
  46. BOOST_TEST_NOT(a == b);
  47. load_xml(filename, b);
  48. BOOST_TEST(a == b);
  49. }
  50. // weighted_mean
  51. {
  52. const auto filename =
  53. join(argv[1], "accumulators_serialization_test_weighted_mean.xml");
  54. accumulators::weighted_mean<> a;
  55. a(1);
  56. a(weight(0.5), 2);
  57. a(3);
  58. print_xml(filename, a);
  59. accumulators::weighted_mean<> b;
  60. BOOST_TEST_NOT(a == b);
  61. load_xml(filename, b);
  62. BOOST_TEST(a == b);
  63. }
  64. // weighted_sum
  65. {
  66. const auto filename =
  67. join(argv[1], "accumulators_serialization_test_weighted_sum.xml");
  68. accumulators::weighted_sum<> a;
  69. a += 1;
  70. a += 10;
  71. print_xml(filename, a);
  72. accumulators::weighted_sum<> b;
  73. BOOST_TEST_NOT(a == b);
  74. load_xml(filename, b);
  75. BOOST_TEST(a == b);
  76. }
  77. return boost::report_errors();
  78. }