File: detail_array_wrapper_serialization_test.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (71 lines) | stat: -rw-r--r-- 1,859 bytes parent folder | download | duplicates (7)
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
// 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)

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/histogram/detail/array_wrapper.hpp>
#include <sstream>
#include <vector>
#include "std_ostream.hpp"
#include "throw_exception.hpp"

namespace dtl = boost::histogram::detail;
namespace ba = boost::archive;

template <class T>
struct dummy_array_wrapper {
  T* ptr;
  std::size_t size;
  template <class Archive>
  void serialize(Archive& ar, unsigned /* version */) {
    for (auto&& x : dtl::make_span(ptr, size)) ar& x;
  }
};

template <class OArchive, class IArchive>
void run_tests() {
  std::vector<int> v = {{1, 2, 3}};

  std::stringstream os1;
  {
    OArchive oa(os1);
    auto w = dtl::make_array_wrapper(v.data(), v.size());
    oa << w;
  }

  std::ostringstream os2;
  {
    OArchive oa(os2);
    auto w = dummy_array_wrapper<int>{v.data(), v.size()};
    oa << w;
  }

  BOOST_TEST_EQ(os1.str(), os2.str());

  std::vector<int> v2(3, 0);
  {
    IArchive ia(os1);
    auto w = dtl::make_array_wrapper(v2.data(), v2.size());
    ia >> w;
  }

  BOOST_TEST_EQ(v, v2);
}

int main() {
  BOOST_TEST(dtl::has_array_optimization<ba::binary_oarchive>::value);
  BOOST_TEST(dtl::has_array_optimization<ba::binary_iarchive>::value);
  BOOST_TEST_NOT(dtl::has_array_optimization<ba::text_oarchive>::value);
  BOOST_TEST_NOT(dtl::has_array_optimization<ba::text_iarchive>::value);

  run_tests<ba::binary_oarchive, ba::binary_iarchive>();
  run_tests<ba::text_oarchive, ba::text_iarchive>();

  return boost::report_errors();
}