Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / histogram / test / detail_misc_test.cpp
1 // Copyright 2015-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
7 #include <boost/core/lightweight_test.hpp>
8 #include <boost/core/lightweight_test_trait.hpp>
9 #include <boost/histogram/accumulators/weighted_sum.hpp>
10 #include <boost/histogram/detail/common_type.hpp>
11 #include <boost/histogram/detail/counting_streambuf.hpp>
12 #include <boost/histogram/detail/non_member_container_access.hpp>
13 #include <boost/histogram/fwd.hpp>
14 #include <boost/histogram/literals.hpp>
15 #include <boost/histogram/storage_adaptor.hpp>
16 #include <boost/histogram/unlimited_storage.hpp>
17 #include <ostream>
18 #include "std_ostream.hpp"
19
20 using namespace boost::histogram;
21 using namespace boost::histogram::literals;
22 namespace dtl = boost::histogram::detail;
23
24 int main() {
25   // literals
26   {
27     BOOST_TEST_TRAIT_SAME(std::integral_constant<unsigned, 0>, decltype(0_c));
28     BOOST_TEST_TRAIT_SAME(std::integral_constant<unsigned, 3>, decltype(3_c));
29     BOOST_TEST_EQ(decltype(10_c)::value, 10);
30     BOOST_TEST_EQ(decltype(213_c)::value, 213);
31   }
32
33   // common_storage
34   {
35     BOOST_TEST_TRAIT_SAME(dtl::common_storage<unlimited_storage<>, unlimited_storage<>>,
36                           unlimited_storage<>);
37     BOOST_TEST_TRAIT_SAME(
38         dtl::common_storage<dense_storage<double>, dense_storage<double>>,
39         dense_storage<double>);
40     BOOST_TEST_TRAIT_SAME(dtl::common_storage<dense_storage<int>, dense_storage<double>>,
41                           dense_storage<double>);
42     BOOST_TEST_TRAIT_SAME(dtl::common_storage<dense_storage<double>, dense_storage<int>>,
43                           dense_storage<double>);
44     BOOST_TEST_TRAIT_SAME(dtl::common_storage<dense_storage<double>, unlimited_storage<>>,
45                           dense_storage<double>);
46     BOOST_TEST_TRAIT_SAME(dtl::common_storage<dense_storage<int>, unlimited_storage<>>,
47                           unlimited_storage<>);
48     BOOST_TEST_TRAIT_SAME(dtl::common_storage<dense_storage<double>, weight_storage>,
49                           weight_storage);
50   }
51
52   // size & data
53   {
54     char a[4] = {1, 2, 3, 4};
55     BOOST_TEST_EQ(dtl::size(a), 4u);
56     BOOST_TEST_EQ(dtl::data(a), a);
57     auto b = {1, 2};
58     BOOST_TEST_EQ(dtl::size(b), 2u);
59     BOOST_TEST_EQ(dtl::data(b), b.begin());
60     struct C {
61       unsigned size() const { return 3; }
62       int* data() { return buf; }
63       const int* data() const { return buf; }
64       int buf[1];
65     } c;
66     BOOST_TEST_EQ(dtl::size(c), 3u);
67     BOOST_TEST_EQ(dtl::data(c), c.buf);
68     BOOST_TEST_EQ(dtl::data(static_cast<const C&>(c)), c.buf);
69     struct {
70       int size() const { return 5; }
71     } d;
72     BOOST_TEST_EQ(dtl::size(d), 5u);
73   }
74
75   // counting_streambuf
76   {
77     dtl::counting_streambuf<char> cbuf;
78     std::ostream os(&cbuf);
79     os.put('x');
80     BOOST_TEST_EQ(cbuf.count, 1);
81     os << 12;
82     BOOST_TEST_EQ(cbuf.count, 3);
83     os << "123";
84     BOOST_TEST_EQ(cbuf.count, 6);
85   }
86
87   return boost::report_errors();
88 }