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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// Copyright 2019 Przemyslaw Bartosik
// 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/core/lightweight_test.hpp>
#include <boost/histogram/accumulators/mean.hpp>
#include <boost/histogram/accumulators/ostream.hpp>
#include <boost/histogram/axis/category.hpp>
#include <boost/histogram/axis/integer.hpp>
#include <boost/histogram/axis/option.hpp>
#include <boost/histogram/axis/regular.hpp>
#include <boost/histogram/make_histogram.hpp>
#include <boost/histogram/ostream.hpp>
#include <limits>
#include <sstream>
#include <string>
#include "throw_exception.hpp"
#include "utility_histogram.hpp"
using namespace boost::histogram;
template <class Histogram>
auto str(const Histogram& h, const unsigned width = 0) {
std::ostringstream os;
// BEGIN and END make nicer error messages
os << "BEGIN\n" << std::setw(width) << h << "END";
return os.str();
}
template <class Tag>
void run_tests() {
using R = axis::regular<>;
using R2 =
axis::regular<double, boost::use_default, axis::null_type, axis::option::none_t>;
using R3 = axis::regular<double, axis::transform::log>;
using C = axis::category<std::string>;
using I = axis::integer<>;
// regular
{
auto h = make(Tag(), R(3, -0.5, 1.0));
h.at(0) = 1;
h.at(1) = 10;
h.at(2) = 5;
const auto expected =
"BEGIN\n"
"histogram(regular(3, -0.5, 1, options=underflow | overflow))\n"
" +------------------------------------------------------------+\n"
"[-inf, -0.5) 0 | |\n"
"[-0.5, 0) 1 |====== |\n"
"[ 0, 0.5) 10 |=========================================================== |\n"
"[ 0.5, 1) 5 |============================== |\n"
"[ 1, inf) 0 | |\n"
" +------------------------------------------------------------+\n"
"END";
BOOST_TEST_CSTR_EQ(str(h).c_str(), expected);
}
// regular, narrow
{
auto h = make(Tag(), R2(3, -0.5, 1.0));
h.at(0) = 1;
h.at(1) = 10;
h.at(2) = 2;
const auto expected = "BEGIN\n"
"histogram(regular(3, -0.5, 1, options=none))\n"
" +-----------------------+\n"
"[-0.5, 0) 1 |== |\n"
"[ 0, 0.5) 10 |====================== |\n"
"[ 0.5, 1) 2 |==== |\n"
" +-----------------------+\n"
"END";
BOOST_TEST_CSTR_EQ(str(h, 40).c_str(), expected);
// too narrow
BOOST_TEST_CSTR_EQ(str(h, 10).c_str(),
"BEGIN\n"
"histogram(regular(3, -0.5, 1, options=none))END");
}
// regular2
{
auto h = make(Tag(), R2(3, -0.5, 1.0));
h.at(0) = 1;
h.at(1) = -5;
h.at(2) = 2;
const auto expected =
"BEGIN\n"
"histogram(regular(3, -0.5, 1, options=none))\n"
" +-------------------------------------------------------------+\n"
"[-0.5, 0) 1 | ========= |\n"
"[ 0, 0.5) -5 |=========================================== |\n"
"[ 0.5, 1) 2 | ================= |\n"
" +-------------------------------------------------------------+\n"
"END";
BOOST_TEST_CSTR_EQ(str(h).c_str(), expected);
}
// regular with log
{
auto h = make(Tag(), R3(6, 1e-3, 1e3, "foo"));
const auto expected =
"BEGIN\n"
"histogram(regular(transform::log{}, 6, 0.001, 1000, metadata=\"foo\", "
"options=underflow | overflow))\n"
" +-----------------------------------------------------------+\n"
"[ 0, 0.001) 0 | |\n"
"[0.001, 0.01) 0 | |\n"
"[ 0.01, 0.1) 0 | |\n"
"[ 0.1, 1) 0 | |\n"
"[ 1, 10) 0 | |\n"
"[ 10, 100) 0 | |\n"
"[ 100, 1000) 0 | |\n"
"[ 1000, inf) 0 | |\n"
" +-----------------------------------------------------------+\n"
"END";
BOOST_TEST_CSTR_EQ(str(h).c_str(), expected);
}
// integer
{
auto h = make(Tag(), I(0, 1));
h.at(0) = -10;
h.at(1) = 5;
const auto expected =
"BEGIN\n"
"histogram(integer(0, 1, options=underflow | overflow))\n"
" +---------------------------------------------------------------------+\n"
"-1 0 | |\n"
" 0 -10 |============================================= |\n"
" 1 5 | ======================= |\n"
" +---------------------------------------------------------------------+\n"
"END";
BOOST_TEST_CSTR_EQ(str(h).c_str(), expected);
}
// catorgy<string>
{
auto h = make(Tag(), C({"a", "bb", "ccc", "dddd"}));
h.at(0) = 1.23;
h.at(1) = 1;
h.at(2) = 1.2345789e-3;
h.at(3) = 1.2345789e-12;
h.at(4) = std::numeric_limits<double>::quiet_NaN();
const auto expected =
"BEGIN\n"
"histogram(category(\"a\", \"bb\", \"ccc\", \"dddd\", options=overflow))\n"
" +------------------------------------------------------------+\n"
" a 1.23 |=========================================================== |\n"
" bb 1 |================================================ |\n"
" ccc 0.001235 | |\n"
" dddd 1.235e-12 | |\n"
"other nan | |\n"
" +------------------------------------------------------------+\n"
"END";
BOOST_TEST_CSTR_EQ(str(h).c_str(), expected);
}
// histogram with axis that has no value method
{
struct minimal_axis {
int index(int x) const { return x % 2; }
int size() const { return 2; }
};
auto h = make(Tag(), minimal_axis{});
h.at(0) = 3;
h.at(1) = 4;
const auto expected =
"BEGIN\n"
"histogram(" +
detail::type_name<minimal_axis>() +
")\n"
" +------------------------------------------------------------------------+\n"
"0 3 |===================================================== |\n"
"1 4 |======================================================================= |\n"
" +------------------------------------------------------------------------+\n"
"END";
BOOST_TEST_CSTR_EQ(str(h).c_str(), expected.c_str());
}
// fallback for 2D
{
auto h = make(Tag(), R(1, -1, 1), R(2, -4, 7));
h.at(-1, 0) = 1000;
h.at(-1, -1) = 123;
h.at(1, 0) = 1.23456789;
h.at(-1, 2) = std::numeric_limits<double>::quiet_NaN();
const auto expected =
"BEGIN\n"
"histogram(\n"
" regular(1, -1, 1, options=underflow | overflow)\n"
" regular(2, -4, 7, options=underflow | overflow)\n"
" (-1 -1): 123 ( 0 -1): 0 ( 1 -1): 0 (-1 0): 1000 \n"
" ( 0 0): 0 ( 1 0): 1.235 (-1 1): 0 ( 0 1): 0 \n"
" ( 1 1): 0 (-1 2): nan ( 0 2): 0 ( 1 2): 0 \n"
")END";
BOOST_TEST_CSTR_EQ(str(h).c_str(), expected);
}
// fallback for profile
{
auto h = make_s(Tag(), profile_storage(), R(1, -1, 1));
h.at(0) = accumulators::mean<>(10, 100, 1000);
const auto expected = "BEGIN\n"
"histogram(\n"
" regular(1, -1, 1, options=underflow | overflow)\n"
" (-1): mean(0, 0, -0) ( 0): mean(10, 100, 1000)\n"
" ( 1): mean(0, 0, -0) \n"
")END";
BOOST_TEST_CSTR_EQ(str(h).c_str(), expected);
}
}
int main() {
run_tests<static_tag>();
run_tests<dynamic_tag>();
{
// cannot make empty static histogram
auto h = histogram<std::vector<axis::regular<>>>();
const auto expected = "BEGIN\n"
"histogram()END";
BOOST_TEST_CSTR_EQ(str(h).c_str(), expected);
}
return boost::report_errors();
}
|