1 // Copyright 2019 Przemyslaw Bartosik
2 // Copyright 2019 Hans Dembinski
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #include <boost/core/lightweight_test.hpp>
9 #include <boost/histogram/accumulators/mean.hpp>
10 #include <boost/histogram/accumulators/ostream.hpp>
11 #include <boost/histogram/axis/category.hpp>
12 #include <boost/histogram/axis/integer.hpp>
13 #include <boost/histogram/axis/option.hpp>
14 #include <boost/histogram/axis/regular.hpp>
15 #include <boost/histogram/make_histogram.hpp>
16 #include <boost/histogram/ostream.hpp>
20 #include "throw_exception.hpp"
21 #include "utility_histogram.hpp"
23 using namespace boost::histogram;
25 template <class Histogram>
26 auto str(const Histogram& h, const unsigned width = 0) {
27 std::ostringstream os;
28 // BEGIN and END make nicer error messages
29 os << "BEGIN\n" << std::setw(width) << h << "END";
35 using R = axis::regular<>;
37 axis::regular<double, boost::use_default, axis::null_type, axis::option::none_t>;
38 using R3 = axis::regular<double, axis::transform::log>;
39 using C = axis::category<std::string>;
40 using I = axis::integer<>;
44 auto h = make(Tag(), R(3, -0.5, 1.0));
51 "histogram(regular(3, -0.5, 1, options=underflow | overflow))\n"
52 " +------------------------------------------------------------+\n"
53 "[-inf, -0.5) 0 | |\n"
54 "[-0.5, 0) 1 |====== |\n"
55 "[ 0, 0.5) 10 |=========================================================== |\n"
56 "[ 0.5, 1) 5 |============================== |\n"
58 " +------------------------------------------------------------+\n"
61 BOOST_TEST_CSTR_EQ(expected, str(h).c_str());
66 auto h = make(Tag(), R2(3, -0.5, 1.0));
71 const auto expected = "BEGIN\n"
72 "histogram(regular(3, -0.5, 1, options=none))\n"
73 " +-----------------------+\n"
75 "[ 0, 0.5) 10 |====================== |\n"
76 "[ 0.5, 1) 2 |==== |\n"
77 " +-----------------------+\n"
80 BOOST_TEST_CSTR_EQ(expected, str(h, 40).c_str());
83 BOOST_TEST_CSTR_EQ("BEGIN\n"
84 "histogram(regular(3, -0.5, 1, options=none))END",
90 auto h = make(Tag(), R2(3, -0.5, 1.0));
97 "histogram(regular(3, -0.5, 1, options=none))\n"
98 " +-------------------------------------------------------------+\n"
99 "[-0.5, 0) 1 | ========= |\n"
100 "[ 0, 0.5) -5 |=========================================== |\n"
101 "[ 0.5, 1) 2 | ================= |\n"
102 " +-------------------------------------------------------------+\n"
105 BOOST_TEST_CSTR_EQ(expected, str(h).c_str());
110 auto h = make(Tag(), R3(6, 1e-3, 1e3, "foo"));
112 const auto expected =
114 "histogram(regular_log(6, 0.001, 1000, metadata=\"foo\", options=underflow | "
116 " +-----------------------------------------------------------+\n"
117 "[ 0, 0.001) 0 | |\n"
118 "[0.001, 0.01) 0 | |\n"
119 "[ 0.01, 0.1) 0 | |\n"
123 "[ 100, 1000) 0 | |\n"
124 "[ 1000, inf) 0 | |\n"
125 " +-----------------------------------------------------------+\n"
128 BOOST_TEST_CSTR_EQ(expected, str(h).c_str());
133 auto h = make(Tag(), I(0, 1));
137 const auto expected =
139 "histogram(integer(0, 1, options=underflow | overflow))\n"
140 " +---------------------------------------------------------------------+\n"
142 " 0 -10 |============================================= |\n"
143 " 1 5 | ======================= |\n"
144 " +---------------------------------------------------------------------+\n"
147 BOOST_TEST_CSTR_EQ(expected, str(h).c_str());
152 auto h = make(Tag(), C({"a", "bb", "ccc", "dddd"}));
155 h.at(2) = 1.2345789e-3;
156 h.at(3) = 1.2345789e-12;
157 h.at(4) = std::numeric_limits<double>::quiet_NaN();
159 const auto expected =
161 "histogram(category(\"a\", \"bb\", \"ccc\", \"dddd\", options=overflow))\n"
162 " +------------------------------------------------------------+\n"
163 " a 1.23 |=========================================================== |\n"
164 " bb 1 |================================================ |\n"
165 " ccc 0.001235 | |\n"
166 " dddd 1.235e-12 | |\n"
168 " +------------------------------------------------------------+\n"
171 BOOST_TEST_CSTR_EQ(expected, str(h).c_str());
174 // histogram with axis that has no value method
176 struct minimal_axis {
177 int index(int x) const { return x % 2; }
178 int size() const { return 2; }
181 auto h = make(Tag(), minimal_axis{});
185 const auto expected =
187 "histogram(<unstreamable>)\n"
188 " +------------------------------------------------------------------------+\n"
189 "0 3 |===================================================== |\n"
190 "1 4 |======================================================================= |\n"
191 " +------------------------------------------------------------------------+\n"
194 BOOST_TEST_CSTR_EQ(expected, str(h).c_str());
199 auto h = make(Tag(), R(1, -1, 1), R(2, -4, 7));
202 h.at(1, 0) = 1.23456789;
203 h.at(-1, 2) = std::numeric_limits<double>::quiet_NaN();
205 const auto expected =
208 " regular(1, -1, 1, options=underflow | overflow)\n"
209 " regular(2, -4, 7, options=underflow | overflow)\n"
210 " (-1 -1): 123 ( 0 -1): 0 ( 1 -1): 0 (-1 0): 1000 \n"
211 " ( 0 0): 0 ( 1 0): 1.235 (-1 1): 0 ( 0 1): 0 \n"
212 " ( 1 1): 0 (-1 2): nan ( 0 2): 0 ( 1 2): 0 \n"
215 BOOST_TEST_CSTR_EQ(expected, str(h).c_str());
218 // fallback for profile
220 auto h = make_s(Tag(), profile_storage(), R(1, -1, 1));
221 h.at(0) = accumulators::mean<>(10, 100, 1000);
223 const auto expected = "BEGIN\n"
225 " regular(1, -1, 1, options=underflow | overflow)\n"
226 " (-1): mean(0, 0, -0) ( 0): mean(10, 100, 1000)\n"
227 " ( 1): mean(0, 0, -0) \n"
230 BOOST_TEST_CSTR_EQ(expected, str(h).c_str());
235 run_tests<static_tag>();
236 run_tests<dynamic_tag>();
239 // cannot make empty static histogram
240 auto h = histogram<std::vector<axis::regular<>>>();
242 const auto expected = "BEGIN\n"
245 BOOST_TEST_CSTR_EQ(expected, str(h).c_str());
248 return boost::report_errors();