deduction_guides_test.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 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. #include <boost/core/lightweight_test.hpp>
  7. #include <boost/core/lightweight_test_trait.hpp>
  8. #include <boost/histogram/accumulators/weighted_sum.hpp>
  9. #include <boost/histogram/axis.hpp>
  10. #include <boost/histogram/axis/ostream.hpp>
  11. #include <boost/histogram/histogram.hpp>
  12. #include <boost/histogram/ostream.hpp>
  13. #include <boost/histogram/storage_adaptor.hpp>
  14. #include <boost/histogram/unlimited_storage.hpp>
  15. #include <tuple>
  16. #include <type_traits>
  17. #include <vector>
  18. #include "std_ostream.hpp"
  19. #include "throw_exception.hpp"
  20. using namespace boost::histogram;
  21. namespace tr = axis::transform;
  22. // tests requires a C++17 compatible compiler
  23. #define TEST BOOST_TEST_TRAIT_SAME
  24. int main() {
  25. using axis::null_type;
  26. {
  27. using axis::regular;
  28. BOOST_TEST_TRAIT_SAME(decltype(regular(1, 0.0, 1.0)),
  29. regular<double, tr::id, null_type>);
  30. BOOST_TEST_TRAIT_SAME(decltype(regular(1, 0, 1)), regular<double, tr::id, null_type>);
  31. BOOST_TEST_TRAIT_SAME(decltype(regular(1, 0.0f, 1.0f)),
  32. regular<float, tr::id, null_type>);
  33. BOOST_TEST_TRAIT_SAME(decltype(regular(1, 0, 1, 0)), regular<double, tr::id, int>);
  34. BOOST_TEST_TRAIT_SAME(decltype(regular(1, 0.0f, 1.0f, "x")),
  35. regular<float, tr::id, std::string>);
  36. BOOST_TEST_TRAIT_SAME(decltype(regular(tr::sqrt(), 1, 0, 1)),
  37. regular<double, tr::sqrt, null_type>);
  38. BOOST_TEST_TRAIT_SAME(decltype(regular(tr::sqrt(), 1, 0.0f, 1.0f, "x")),
  39. regular<float, tr::sqrt, std::string>);
  40. BOOST_TEST_TRAIT_SAME(decltype(regular(tr::sqrt(), 1, 0, 1, 0)),
  41. regular<double, tr::sqrt, int>);
  42. }
  43. {
  44. using axis::integer;
  45. BOOST_TEST_TRAIT_SAME(decltype(integer(1, 2)), integer<int, null_type>);
  46. BOOST_TEST_TRAIT_SAME(decltype(integer(1l, 2l)), integer<int, null_type>);
  47. BOOST_TEST_TRAIT_SAME(decltype(integer(1.0, 2.0)), integer<double, null_type>);
  48. BOOST_TEST_TRAIT_SAME(decltype(integer(1.0f, 2.0f)), integer<float, null_type>);
  49. BOOST_TEST_TRAIT_SAME(decltype(integer(1, 2, "foo")), integer<int, std::string>);
  50. BOOST_TEST_TRAIT_SAME(decltype(integer(1, 2, 0)), integer<int, int>);
  51. }
  52. {
  53. using axis::variable;
  54. BOOST_TEST_TRAIT_SAME(decltype(variable{-1.0f, 1.0f}), variable<float, null_type>);
  55. BOOST_TEST_TRAIT_SAME(decltype(variable{-1, 0, 1, 2}), variable<double, null_type>);
  56. BOOST_TEST_TRAIT_SAME(decltype(variable{-1.0, 1.0}), variable<double, null_type>);
  57. BOOST_TEST_TRAIT_SAME(decltype(variable({-1, 0, 1}, "foo")),
  58. variable<double, std::string>);
  59. BOOST_TEST_TRAIT_SAME(decltype(variable({-1, 1}, 0)), variable<double, int>);
  60. BOOST_TEST_TRAIT_SAME(decltype(variable(std::vector<int>{{-1, 1}})),
  61. variable<double, null_type>);
  62. BOOST_TEST_TRAIT_SAME(decltype(variable(std::vector<float>{{-1.0f, 1.0f}})),
  63. variable<float, null_type>);
  64. BOOST_TEST_TRAIT_SAME(decltype(variable(std::vector<int>{{-1, 1}}, "foo")),
  65. variable<double, std::string>);
  66. BOOST_TEST_TRAIT_SAME(decltype(variable(std::vector<int>{{-1, 1}}, 0)),
  67. variable<double, int>);
  68. }
  69. {
  70. using axis::category;
  71. BOOST_TEST_TRAIT_SAME(decltype(category{1, 2}), category<int, null_type>);
  72. BOOST_TEST_TRAIT_SAME(decltype(category{"x", "y"}), category<std::string, null_type>);
  73. BOOST_TEST_TRAIT_SAME(decltype(category({1, 2}, "foo")), category<int, std::string>);
  74. BOOST_TEST_TRAIT_SAME(decltype(category({1, 2}, 0)), category<int, int>);
  75. }
  76. {
  77. auto h = histogram(axis::regular(3, -1, 1), axis::integer(0, 4));
  78. BOOST_TEST_TRAIT_SAME(decltype(h),
  79. histogram<std::tuple<axis::regular<double, tr::id, null_type>,
  80. axis::integer<int, null_type>>>);
  81. BOOST_TEST_EQ(h.axis(0), axis::regular(3, -1, 1));
  82. BOOST_TEST_EQ(h.axis(1), axis::integer(0, 4));
  83. }
  84. {
  85. auto h = histogram(std::tuple(axis::regular(3, -1, 1), axis::integer(0, 4)),
  86. weight_storage());
  87. BOOST_TEST_TRAIT_SAME(decltype(h),
  88. histogram<std::tuple<axis::regular<double, tr::id, null_type>,
  89. axis::integer<int, null_type>>,
  90. weight_storage>);
  91. BOOST_TEST_EQ(h.axis(0), axis::regular(3, -1, 1));
  92. BOOST_TEST_EQ(h.axis(1), axis::integer(0, 4));
  93. }
  94. {
  95. auto a0 = axis::regular(5, 0, 5);
  96. auto a1 = axis::regular(3, -1, 1);
  97. auto axes = {a0, a1};
  98. auto h = histogram(axes);
  99. BOOST_TEST_TRAIT_SAME(
  100. decltype(h), histogram<std::vector<axis::regular<double, tr::id, null_type>>>);
  101. BOOST_TEST_EQ(h.rank(), 2);
  102. BOOST_TEST_EQ(h.axis(0), a0);
  103. BOOST_TEST_EQ(h.axis(1), a1);
  104. }
  105. {
  106. auto a0 = axis::regular(5, 0, 5);
  107. auto a1 = axis::regular(3, -1, 1);
  108. auto axes = {a0, a1};
  109. auto h = histogram(axes, weight_storage());
  110. BOOST_TEST_TRAIT_SAME(
  111. decltype(h),
  112. histogram<std::vector<axis::regular<double, tr::id, null_type>>, weight_storage>);
  113. BOOST_TEST_EQ(h.rank(), 2);
  114. BOOST_TEST_EQ(h.axis(0), a0);
  115. BOOST_TEST_EQ(h.axis(1), a1);
  116. }
  117. {
  118. auto a0 = axis::regular(5, 0, 5);
  119. auto a1 = axis::regular(3, -1, 1);
  120. auto axes = std::vector<decltype(a0)>{{a0, a1}};
  121. auto h = histogram(axes);
  122. BOOST_TEST_TRAIT_SAME(
  123. decltype(h), histogram<std::vector<axis::regular<double, tr::id, null_type>>>);
  124. BOOST_TEST_EQ(h.rank(), 2);
  125. BOOST_TEST_EQ(h.axis(0), a0);
  126. BOOST_TEST_EQ(h.axis(1), a1);
  127. }
  128. return boost::report_errors();
  129. }