File: detail_array_wrapper_serialization_test.cpp

package info (click to toggle)
boost1.90 1.90.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 593,168 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,162; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (72 lines) | stat: -rw-r--r-- 1,894 bytes parent folder | download
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
72
// 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/core/make_span.hpp>
#include <boost/histogram/detail/array_wrapper.hpp>
#include <sstream>
#include <vector>
#include "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 : boost::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();
}