mirror of
https://github.com/boostorg/histogram.git
synced 2025-05-11 05:07:58 +00:00
200 lines
5.4 KiB
C++
200 lines
5.4 KiB
C++
// Copyright 2015-2017 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/core/lightweight_test_trait.hpp>
|
|
#include <boost/histogram/detail/axis_visitor.hpp>
|
|
#include <boost/histogram/detail/cat.hpp>
|
|
#include <boost/histogram/detail/meta.hpp>
|
|
#include <boost/histogram/detail/utility.hpp>
|
|
#include <boost/variant.hpp>
|
|
#include <boost/mp11.hpp>
|
|
#include <tuple>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
#include <utility>
|
|
|
|
using namespace boost::histogram::detail;
|
|
|
|
struct for_each_test_visitor {
|
|
int i = 0;
|
|
bool result = true;
|
|
template <typename T> void operator()(T& t) {
|
|
// expect: int char float
|
|
switch (i++) {
|
|
case 0:
|
|
result &= std::is_same<T, int>::value;
|
|
break;
|
|
case 1:
|
|
result &= std::is_same<T, char>::value;
|
|
break;
|
|
case 2:
|
|
result &= std::is_same<T, float>::value;
|
|
};
|
|
t += 1;
|
|
}
|
|
};
|
|
|
|
|
|
int main() {
|
|
// escape0
|
|
{
|
|
std::ostringstream os;
|
|
escape(os, std::string("abc"));
|
|
BOOST_TEST_EQ(os.str(), std::string("'abc'"));
|
|
}
|
|
|
|
// escape1
|
|
{
|
|
std::ostringstream os;
|
|
escape(os, std::string("abc\n"));
|
|
BOOST_TEST_EQ(os.str(), std::string("'abc\n'"));
|
|
}
|
|
|
|
// escape2
|
|
{
|
|
std::ostringstream os;
|
|
escape(os, std::string("'abc'"));
|
|
BOOST_TEST_EQ(os.str(), std::string("'\\\'abc\\\''"));
|
|
}
|
|
|
|
// // assign_axis unreachable branch
|
|
// {
|
|
// using V1 = boost::variant<float>;
|
|
// using V2 = boost::variant<int>;
|
|
// V1 v1(1.0);
|
|
// V2 v2(2);
|
|
// boost::apply_visitor(assign_axis<V1>(v1), v2);
|
|
// BOOST_TEST_EQ(v1, V1(1.0));
|
|
// BOOST_TEST_EQ(v2, V2(2));
|
|
// }
|
|
|
|
// index_mapper 1
|
|
{
|
|
std::vector<unsigned> n{{2, 2}};
|
|
std::vector<bool> b{{true, false}};
|
|
index_mapper m(std::move(n), std::move(b));
|
|
BOOST_TEST_EQ(m.first, 0);
|
|
BOOST_TEST_EQ(m.second, 0);
|
|
BOOST_TEST_EQ(m.next(), true);
|
|
BOOST_TEST_EQ(m.first, 1);
|
|
BOOST_TEST_EQ(m.second, 1);
|
|
BOOST_TEST_EQ(m.next(), true);
|
|
BOOST_TEST_EQ(m.first, 2);
|
|
BOOST_TEST_EQ(m.second, 0);
|
|
BOOST_TEST_EQ(m.next(), true);
|
|
BOOST_TEST_EQ(m.first, 3);
|
|
BOOST_TEST_EQ(m.second, 1);
|
|
BOOST_TEST_EQ(m.next(), false);
|
|
}
|
|
|
|
// index_mapper 2
|
|
{
|
|
std::vector<unsigned> n{{2, 2}};
|
|
std::vector<bool> b{{false, true}};
|
|
index_mapper m(std::move(n), std::move(b));
|
|
BOOST_TEST_EQ(m.first, 0);
|
|
BOOST_TEST_EQ(m.second, 0);
|
|
BOOST_TEST_EQ(m.next(), true);
|
|
BOOST_TEST_EQ(m.first, 1);
|
|
BOOST_TEST_EQ(m.second, 0);
|
|
BOOST_TEST_EQ(m.next(), true);
|
|
BOOST_TEST_EQ(m.first, 2);
|
|
BOOST_TEST_EQ(m.second, 1);
|
|
BOOST_TEST_EQ(m.next(), true);
|
|
BOOST_TEST_EQ(m.first, 3);
|
|
BOOST_TEST_EQ(m.second, 1);
|
|
BOOST_TEST_EQ(m.next(), false);
|
|
}
|
|
|
|
// cat
|
|
{ BOOST_TEST_EQ(cat("foo", 1, "bar"), std::string("foo1bar")); }
|
|
|
|
// unique_sorted
|
|
{
|
|
using input = ::boost::mp11::mp_list_c<int, 3, 2, 1, 2, 3, 1, 3>;
|
|
using result = unique_sorted_t<input>;
|
|
using expected = ::boost::mp11::mp_list_c<int, 1, 2, 3>;
|
|
|
|
BOOST_TEST_TRAIT_TRUE((std::is_same<result, expected>));
|
|
}
|
|
|
|
// union
|
|
{
|
|
using main_list = ::boost::mp11::mp_list<int, unsigned, char>;
|
|
using aux_list = ::boost::mp11::mp_list<unsigned, void *>;
|
|
using result = union_t<main_list, aux_list>;
|
|
using expected = ::boost::mp11::mp_list<int, unsigned, char, void *>;
|
|
|
|
BOOST_TEST_TRAIT_TRUE((std::is_same<result, expected>));
|
|
}
|
|
|
|
// has_variance_support
|
|
{
|
|
struct no_methods {};
|
|
|
|
struct value_method {
|
|
const double &value() const;
|
|
};
|
|
|
|
struct variance_method {
|
|
const double &variance() const;
|
|
};
|
|
|
|
struct value_and_variance_methods {
|
|
const double &value() const;
|
|
const double &variance() const;
|
|
};
|
|
|
|
BOOST_TEST_EQ(has_variance_support_t<no_methods>(), false);
|
|
BOOST_TEST_EQ(has_variance_support_t<value_method>(), false);
|
|
BOOST_TEST_EQ(has_variance_support_t<variance_method>(),
|
|
false);
|
|
BOOST_TEST_EQ(has_variance_support_t<value_and_variance_methods>(),
|
|
true);
|
|
}
|
|
|
|
// classify_container
|
|
{
|
|
using result1 = classify_container_t<int>;
|
|
BOOST_TEST_TRAIT_TRUE(( std::is_same<result1, no_container_tag> ));
|
|
|
|
using result1a = classify_container_t<int&>;
|
|
BOOST_TEST_TRAIT_TRUE(( std::is_same<result1a, no_container_tag> ));
|
|
|
|
using result2 = classify_container_t<std::vector<int>>;
|
|
BOOST_TEST_TRAIT_TRUE(( std::is_same<result2, dynamic_container_tag> ));
|
|
|
|
using result2a = classify_container_t<std::vector<int>&>;
|
|
BOOST_TEST_TRAIT_TRUE(( std::is_same<result2a, dynamic_container_tag> ));
|
|
|
|
using result3 = classify_container_t<std::pair<int, int>>;
|
|
BOOST_TEST_TRAIT_TRUE(( std::is_same<result3, static_container_tag> ));
|
|
|
|
using result3a = classify_container_t<std::pair<int, int>&>;
|
|
BOOST_TEST_TRAIT_TRUE(( std::is_same<result3a, static_container_tag> ));
|
|
|
|
using result4 = classify_container_t<decltype("abc")>;
|
|
BOOST_TEST_TRAIT_TRUE(( std::is_same<result4, dynamic_container_tag> ));
|
|
}
|
|
|
|
// for_each
|
|
{
|
|
for_each_test_visitor v;
|
|
std::tuple<int, char, float> t(0, 0, 0);
|
|
::boost::histogram::detail::for_each(t, v);
|
|
BOOST_TEST_EQ(v.i, 3);
|
|
BOOST_TEST_EQ(v.result, true);
|
|
BOOST_TEST_EQ(std::get<0>(t), 1);
|
|
BOOST_TEST_EQ(std::get<1>(t), 1);
|
|
BOOST_TEST_EQ(std::get<2>(t), 1);
|
|
}
|
|
|
|
return boost::report_errors();
|
|
}
|