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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
// Copyright 2015-2018 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/is_same.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/histogram/axis.hpp>
#include <boost/histogram/axis/ostream.hpp>
#include <boost/histogram/detail/detect.hpp>
#include <boost/histogram/histogram.hpp>
#include <boost/histogram/ostream.hpp>
#include <boost/throw_exception.hpp>
#include <string>
#include <vector>
#include "dummy_storage.hpp"
#include "std_ostream.hpp"
#include "throw_exception.hpp"
#include "utility_histogram.hpp"
using namespace boost::histogram;
template <typename Tag>
void run_tests() {
// arithmetic operators
{
auto a = make(Tag(), axis::integer<>(0, 2));
auto b = a;
a.at(-1) = 2;
a.at(0) = 1;
b.at(-1) = 1;
b.at(1) = 1;
b.at(2) = 3;
auto c = a + b;
BOOST_TEST_EQ(c.at(-1), 3);
BOOST_TEST_EQ(c.at(0), 1);
BOOST_TEST_EQ(c.at(1), 1);
BOOST_TEST_EQ(c.at(2), 3);
c += b;
BOOST_TEST_EQ(c.at(-1), 4);
BOOST_TEST_EQ(c.at(0), 1);
BOOST_TEST_EQ(c.at(1), 2);
BOOST_TEST_EQ(c.at(2), 6);
auto d = a + b + c;
BOOST_TEST_TRAIT_SAME(decltype(d), decltype(a));
BOOST_TEST_EQ(d.at(-1), 7);
BOOST_TEST_EQ(d.at(0), 2);
BOOST_TEST_EQ(d.at(1), 3);
BOOST_TEST_EQ(d.at(2), 9);
auto d2 = d - a - b - c;
BOOST_TEST_TRAIT_SAME(decltype(d2), decltype(a));
BOOST_TEST_EQ(d2.at(-1), 0);
BOOST_TEST_EQ(d2.at(0), 0);
BOOST_TEST_EQ(d2.at(1), 0);
BOOST_TEST_EQ(d2.at(2), 0);
d2 -= a;
BOOST_TEST_EQ(d2.at(-1), -2);
BOOST_TEST_EQ(d2.at(0), -1);
BOOST_TEST_EQ(d2.at(1), 0);
BOOST_TEST_EQ(d2.at(2), 0);
auto d3 = d;
d3 *= d;
BOOST_TEST_EQ(d3.at(-1), 49);
BOOST_TEST_EQ(d3.at(0), 4);
BOOST_TEST_EQ(d3.at(1), 9);
BOOST_TEST_EQ(d3.at(2), 81);
auto d4 = d3 * (1 * d); // converted return type
BOOST_TEST_TRAIT_FALSE((boost::core::is_same<decltype(d4), decltype(d3)>));
BOOST_TEST_EQ(d4.at(0), 8);
BOOST_TEST_EQ(d4.at(1), 27);
d4 /= d;
BOOST_TEST_EQ(d4.at(0), 4);
BOOST_TEST_EQ(d4.at(1), 9);
auto d5 = d4 / d;
BOOST_TEST_EQ(d5.at(0), 2);
BOOST_TEST_EQ(d5.at(1), 3);
auto e = 3 * a; // converted return type
auto f = b * 2; // converted return type
BOOST_TEST_TRAIT_FALSE((boost::core::is_same<decltype(e), decltype(a)>));
BOOST_TEST_TRAIT_FALSE((boost::core::is_same<decltype(f), decltype(a)>));
BOOST_TEST_EQ(e.at(0), 3);
BOOST_TEST_EQ(e.at(1), 0);
BOOST_TEST_EQ(f.at(0), 0);
BOOST_TEST_EQ(f.at(1), 2);
auto r = 1.0 * a;
r += b;
r += e;
BOOST_TEST_EQ(r.at(0), 4);
BOOST_TEST_EQ(r.at(1), 1);
BOOST_TEST_EQ(r, a + b + 3 * a);
auto s = r / 4;
r /= 4;
BOOST_TEST_EQ(r.at(0), 1);
BOOST_TEST_EQ(r.at(1), 0.25);
BOOST_TEST_EQ(r, s);
}
// arithmetic operators with mixed storage: unlimited vs. vector<unsigned>
{
auto ia = axis::integer<int, axis::null_type, axis::option::none_t>(0, 2);
auto a = make(Tag(), ia);
a(0, weight(2));
a(1, weight(2));
auto b = a;
auto c = make_s(Tag(), std::vector<int>(), ia);
c(0, weight(2));
c(1, weight(2));
auto a2 = a;
a2 += c;
BOOST_TEST_EQ(a2, (a + b));
auto a3 = a;
a3 *= c;
BOOST_TEST_EQ(a3, (a * b));
auto a4 = a;
a4 -= c;
BOOST_TEST_EQ(a4, (a - b));
auto a5 = a;
a5 /= c;
BOOST_TEST_EQ(a5, (a / b));
}
// arithmetic operators with mixed storage: vector<unsigned char> vs. vector<unsigned>
{
auto ia = axis::integer<int, axis::null_type, axis::option::none_t>(0, 2);
auto a = make_s(Tag(), std::vector<unsigned long>{}, ia);
auto c = make_s(Tag(), std::vector<unsigned>(), ia);
a(0, weight(2u));
a(1, weight(2u));
auto b = a;
c(0, weight(2u));
c(1, weight(2u));
auto a2 = a;
a2 += c;
BOOST_TEST_EQ(a2, (a + b));
auto a3 = a;
a3 *= c;
BOOST_TEST_EQ(a3, (a * b));
auto a4 = a;
a4 -= c;
BOOST_TEST_EQ(a4, (a - b));
auto a5 = a;
a5 /= c;
BOOST_TEST_EQ(a5, (a / b));
}
// add operators with weighted storage
{
auto ia = axis::integer<int, axis::null_type, axis::option::none_t>(0, 2);
auto a = make_s(Tag(), std::vector<accumulators::weighted_sum<>>(), ia);
auto b = make_s(Tag(), std::vector<accumulators::weighted_sum<>>(), ia);
a(0);
BOOST_TEST_EQ(a.at(0).variance(), 1);
b(weight(3), 1);
BOOST_TEST_EQ(b.at(1).variance(), 9);
auto c = a;
c += b;
BOOST_TEST_EQ(c.at(0).value(), 1);
BOOST_TEST_EQ(c.at(0).variance(), 1);
BOOST_TEST_EQ(c.at(1).value(), 3);
BOOST_TEST_EQ(c.at(1).variance(), 9);
auto d = a;
d += b;
BOOST_TEST_EQ(d.at(0).value(), 1);
BOOST_TEST_EQ(d.at(0).variance(), 1);
BOOST_TEST_EQ(d.at(1).value(), 3);
BOOST_TEST_EQ(d.at(1).variance(), 9);
// add unweighted histogram
auto e = make_s(Tag(), std::vector<int>(), ia);
std::fill(e.begin(), e.end(), 2);
d += e;
BOOST_TEST_EQ(d.at(0).value(), 3);
BOOST_TEST_EQ(d.at(0).variance(), 3);
BOOST_TEST_EQ(d.at(1).value(), 5);
BOOST_TEST_EQ(d.at(1).variance(), 11);
}
// merging add
{
using C = axis::category<int, use_default, axis::option::growth_t>;
using I = axis::integer<int, axis::null_type, axis::option::growth_t>;
{
auto empty = std::initializer_list<int>{};
auto a = make(Tag(), C(empty, "foo"));
auto b = make(Tag(), C(empty, "foo"));
a(2);
a(1);
b(2);
b(3);
b(4);
a += b;
BOOST_TEST_EQ(a.axis(), C({2, 1, 3, 4}, "foo"));
BOOST_TEST_EQ(a[0], 2);
BOOST_TEST_EQ(a[1], 1);
BOOST_TEST_EQ(a[2], 1);
BOOST_TEST_EQ(a[3], 1);
}
{
auto a = make(Tag(), C{1, 2}, I{4, 5});
auto b = make(Tag(), C{2, 3}, I{5, 6});
std::fill(a.begin(), a.end(), 1);
std::fill(b.begin(), b.end(), 1);
a += b;
BOOST_TEST_EQ(a.axis(0), (C{1, 2, 3}));
BOOST_TEST_EQ(a.axis(1), (I{4, 6}));
BOOST_TEST_EQ(a.at(0, 0), 1);
BOOST_TEST_EQ(a.at(1, 0), 1);
BOOST_TEST_EQ(a.at(2, 0), 0); // v=(3, 4) did not exist in a or b
BOOST_TEST_EQ(a.at(0, 1), 0); // v=(1, 5) did not exist in a or b
BOOST_TEST_EQ(a.at(1, 1), 1);
BOOST_TEST_EQ(a.at(2, 1), 1);
}
{
using CI = axis::category<int, use_default, axis::option::growth_t>;
using CS = axis::category<std::string, use_default, axis::option::growth_t>;
auto h1 = make(Tag{}, CI{}, CS{});
auto h2 = make(Tag{}, CI{}, CS{});
auto h3 = make(Tag{}, CI{}, CS{});
h1(1, "b");
h1(2, "a");
h1(1, "a");
h1(2, "b");
h2(2, "b");
h2(3, "b");
h2(4, "c");
h2(5, "c");
h3(1, "b");
h3(2, "a");
h3(1, "a");
h3(2, "b");
h3(2, "b");
h3(3, "b");
h3(4, "c");
h3(5, "c");
BOOST_TEST_EQ(h3, h1 + h2);
}
{
// C2 is not growing and has overflow
using C2 = axis::category<int>;
auto a = make(Tag(), C{1, 2}, C2{4, 5});
auto b = make(Tag(), C{1, 2}, C2{5, 6});
// axis C2 is incompatible
BOOST_TEST_THROWS(a += b, std::invalid_argument);
std::fill(a.begin(), a.end(), 1);
b = a;
b(3, 4);
a += b;
BOOST_TEST_EQ(a.at(0, 0), 2);
BOOST_TEST_EQ(a.at(1, 0), 2);
BOOST_TEST_EQ(a.at(2, 0), 1);
BOOST_TEST_EQ(a.at(0, 1), 2);
BOOST_TEST_EQ(a.at(1, 1), 2);
BOOST_TEST_EQ(a.at(2, 1), 0);
BOOST_TEST_EQ(a.at(0, 2), 2);
BOOST_TEST_EQ(a.at(1, 2), 2);
BOOST_TEST_EQ(a.at(2, 2), 0);
// incompatible labels
b.axis(0).metadata() = "foo";
BOOST_TEST_THROWS(a += b, std::invalid_argument);
// incompatible axis types
auto c = make(Tag(), C{1, 2}, I{4, 6});
BOOST_TEST_THROWS(a += c, std::invalid_argument);
}
}
// bad operations
{
auto a = make(Tag(), axis::regular<>(2, 0, 4));
auto b = make(Tag(), axis::regular<>(2, 0, 2));
BOOST_TEST_THROWS(a += b, std::invalid_argument);
BOOST_TEST_THROWS(a -= b, std::invalid_argument);
BOOST_TEST_THROWS(a *= b, std::invalid_argument);
BOOST_TEST_THROWS(a /= b, std::invalid_argument);
auto c = make(Tag(), axis::regular<>(2, 0, 2), axis::regular<>(2, 0, 4));
BOOST_TEST_THROWS(a += c, std::invalid_argument);
}
// scaling
{
auto b = make_s(Tag{}, dummy_storage<double, true>{}, axis::integer<>(0, 1));
b(0);
BOOST_TEST_EQ(b[0], 1);
b *= 2; // intentionally does not do anything
BOOST_TEST_EQ(b[0], 1);
auto c = make_s(Tag{}, dummy_storage<double, false>{}, axis::integer<>(0, 1));
c(0);
BOOST_TEST_EQ(c[0], 1);
c *= 2; // this calls *= on each element
BOOST_TEST_EQ(c[0], 2);
using h1_t = decltype(
make_s(Tag{}, dummy_storage<unscaleable, false>{}, axis::integer<>(0, 1)));
BOOST_TEST_NOT((detail::has_operator_rmul<h1_t, double>::value));
using h2_t = decltype(
make_s(Tag{}, dummy_storage<unscaleable, true>{}, axis::integer<>(0, 1)));
BOOST_TEST_NOT((detail::has_operator_rmul<h2_t, double>::value));
}
}
int main() {
run_tests<static_tag>();
run_tests<dynamic_tag>();
return boost::report_errors();
}
|