]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright 2015-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 | ||
7 | #include <boost/core/lightweight_test.hpp> | |
8 | #include <boost/histogram/accumulators/count.hpp> | |
9 | #include <boost/histogram/accumulators/ostream.hpp> | |
10 | #include <sstream> | |
11 | #include <thread> | |
12 | #include "throw_exception.hpp" | |
13 | #include "utility_str.hpp" | |
14 | ||
15 | using namespace boost::histogram; | |
16 | using namespace std::literals; | |
17 | ||
18 | constexpr int N = 10000; | |
19 | ||
20 | template <class F> | |
21 | void parallel(F f) { | |
22 | auto g = [&]() { | |
23 | for (int i = 0; i < N; ++i) f(); | |
24 | }; | |
25 | ||
26 | std::thread a(g), b(g), c(g), d(g); | |
27 | a.join(); | |
28 | b.join(); | |
29 | c.join(); | |
30 | d.join(); | |
31 | } | |
32 | ||
33 | template <class T> | |
34 | void test_on() { | |
35 | using ts_t = accumulators::count<T, true>; | |
36 | ||
37 | // default ctor | |
38 | { | |
39 | ts_t i; | |
40 | BOOST_TEST_EQ(i, static_cast<T>(0)); | |
41 | } | |
42 | ||
43 | // ctor from value | |
44 | { | |
45 | ts_t i{1001}; | |
46 | BOOST_TEST_EQ(i, static_cast<T>(1001)); | |
47 | BOOST_TEST_EQ(str(i), "1001"s); | |
48 | } | |
49 | ||
50 | // add null | |
51 | BOOST_TEST_EQ(ts_t{} += ts_t{}, ts_t{}); | |
52 | ||
53 | // add non-null | |
54 | BOOST_TEST_EQ((ts_t{} += ts_t{2}), (ts_t{2})); | |
55 | ||
56 | // operator++ | |
57 | { | |
58 | ts_t t; | |
59 | parallel([&]() { ++t; }); | |
60 | BOOST_TEST_EQ(t, static_cast<T>(4 * N)); | |
61 | } | |
62 | ||
63 | // operator+= with value | |
64 | { | |
65 | ts_t t; | |
66 | parallel([&]() { t += 2; }); | |
67 | BOOST_TEST_EQ(t, static_cast<T>(8 * N)); | |
68 | } | |
69 | ||
70 | // operator+= with another thread-safe | |
71 | { | |
72 | ts_t t, u; | |
73 | u = 2; | |
74 | parallel([&]() { t += u; }); | |
75 | BOOST_TEST_EQ(t, static_cast<T>(8 * N)); | |
76 | } | |
77 | } | |
78 | ||
79 | int main() { | |
80 | test_on<int>(); | |
81 | test_on<float>(); | |
82 | ||
83 | // copy and assignment from other thread-safe | |
84 | { | |
85 | accumulators::count<char, true> r{1}; | |
86 | accumulators::count<int, true> a{r}, b; | |
87 | b = r; | |
88 | BOOST_TEST_EQ(a, 1); | |
89 | BOOST_TEST_EQ(b, 1); | |
90 | } | |
91 | ||
92 | return boost::report_errors(); | |
93 | } |