axis_category_test.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2015-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/ignore_unused.hpp>
  7. #include <boost/core/lightweight_test.hpp>
  8. #include <boost/core/lightweight_test_trait.hpp>
  9. #include <boost/histogram/axis/category.hpp>
  10. #include <boost/histogram/axis/ostream.hpp>
  11. #include <boost/histogram/axis/traits.hpp>
  12. #include <limits>
  13. #include <sstream>
  14. #include <string>
  15. #include <type_traits>
  16. #include "std_ostream.hpp"
  17. #include "throw_exception.hpp"
  18. #include "utility_axis.hpp"
  19. #include "utility_str.hpp"
  20. int main() {
  21. using namespace boost::histogram;
  22. BOOST_TEST(std::is_nothrow_move_constructible<axis::category<int>>::value);
  23. BOOST_TEST(std::is_nothrow_move_constructible<axis::category<std::string>>::value);
  24. BOOST_TEST(std::is_nothrow_move_assignable<axis::category<int>>::value);
  25. BOOST_TEST(std::is_nothrow_move_assignable<axis::category<std::string>>::value);
  26. // bad ctor
  27. {
  28. int x[2];
  29. boost::ignore_unused(x);
  30. BOOST_TEST_THROWS(axis::category<int>(x + 1, x), std::invalid_argument);
  31. }
  32. // value should return copy for arithmetic types and const reference otherwise
  33. {
  34. enum class Foo { foo };
  35. BOOST_TEST_TRAIT_SAME(axis::traits::value_type<axis::category<std::string>>,
  36. std::string);
  37. BOOST_TEST_TRAIT_SAME(decltype(std::declval<axis::category<std::string>>().value(0)),
  38. const std::string&);
  39. BOOST_TEST_TRAIT_SAME(axis::traits::value_type<axis::category<const char*>>,
  40. const char*);
  41. BOOST_TEST_TRAIT_SAME(decltype(std::declval<axis::category<const char*>>().value(0)),
  42. const char*);
  43. BOOST_TEST_TRAIT_SAME(axis::traits::value_type<axis::category<Foo>>, Foo);
  44. BOOST_TEST_TRAIT_SAME(decltype(std::declval<axis::category<Foo>>().value(0)), Foo);
  45. BOOST_TEST_TRAIT_SAME(axis::traits::value_type<axis::category<int>>, int);
  46. BOOST_TEST_TRAIT_SAME(decltype(std::declval<axis::category<int>>().value(0)), int);
  47. }
  48. // empty axis::category
  49. {
  50. axis::category<int> a;
  51. axis::category<int> b(std::vector<int>(0));
  52. BOOST_TEST_EQ(a, b);
  53. BOOST_TEST_EQ(a.size(), 0);
  54. BOOST_TEST_EQ(a.index(-1), 0);
  55. BOOST_TEST_EQ(a.index(0), 0);
  56. BOOST_TEST_EQ(a.index(1), 0);
  57. }
  58. // axis::category
  59. {
  60. std::string A("A"), B("B"), C("C"), other;
  61. axis::category<std::string> a({A, B, C}, "foo");
  62. BOOST_TEST_EQ(a.metadata(), "foo");
  63. BOOST_TEST_EQ(static_cast<const axis::category<std::string>&>(a).metadata(), "foo");
  64. a.metadata() = "bar";
  65. BOOST_TEST_EQ(static_cast<const axis::category<std::string>&>(a).metadata(), "bar");
  66. BOOST_TEST_EQ(a.size(), 3);
  67. BOOST_TEST_EQ(a.index(A), 0);
  68. BOOST_TEST_EQ(a.index(B), 1);
  69. BOOST_TEST_EQ(a.index(C), 2);
  70. BOOST_TEST_EQ(a.index(other), 3);
  71. BOOST_TEST_EQ(a.value(0), A);
  72. BOOST_TEST_EQ(a.value(1), B);
  73. BOOST_TEST_EQ(a.value(2), C);
  74. BOOST_TEST_THROWS(a.value(3), std::out_of_range);
  75. BOOST_TEST_EQ(str(a),
  76. "category(\"A\", \"B\", \"C\", metadata=\"bar\", options=overflow)");
  77. }
  78. // category<int, axis::null_type>: copy, move
  79. {
  80. using C = axis::category<int, axis::null_type>;
  81. C a({1, 2, 3});
  82. C a2(a);
  83. BOOST_TEST_EQ(a2, a);
  84. C b;
  85. BOOST_TEST_NE(a, b);
  86. b = a;
  87. BOOST_TEST_EQ(a, b);
  88. b = C{{2, 1, 3}};
  89. BOOST_TEST_NE(a, b);
  90. b = a;
  91. BOOST_TEST_EQ(a, b);
  92. C c = std::move(b);
  93. BOOST_TEST_EQ(c, a);
  94. C d;
  95. BOOST_TEST_NE(c, d);
  96. d = std::move(c);
  97. BOOST_TEST_EQ(d, a);
  98. }
  99. // category<std::string>: copy, move
  100. {
  101. using C = axis::category<std::string>;
  102. C a({"A", "B", "C"}, "foo");
  103. C a2(a);
  104. BOOST_TEST_EQ(a2, a);
  105. C b;
  106. BOOST_TEST_NE(a, b);
  107. b = a;
  108. BOOST_TEST_EQ(a, b);
  109. b = C{{"B", "A", "C"}};
  110. BOOST_TEST_NE(a, b);
  111. b = a;
  112. BOOST_TEST_EQ(a, b);
  113. C c = std::move(b);
  114. BOOST_TEST_EQ(c, a);
  115. C d;
  116. BOOST_TEST_NE(c, d);
  117. d = std::move(c);
  118. BOOST_TEST_EQ(d, a);
  119. }
  120. // axis::category with growth
  121. {
  122. axis::category<int, axis::null_type, axis::option::growth_t> a;
  123. BOOST_TEST_EQ(a.size(), 0);
  124. BOOST_TEST_EQ(a.update(5), std::make_pair(0, -1));
  125. BOOST_TEST_EQ(a.size(), 1);
  126. BOOST_TEST_EQ(a.update(1), std::make_pair(1, -1));
  127. BOOST_TEST_EQ(a.size(), 2);
  128. BOOST_TEST_EQ(a.update(10), std::make_pair(2, -1));
  129. BOOST_TEST_EQ(a.size(), 3);
  130. BOOST_TEST_EQ(a.update(10), std::make_pair(2, 0));
  131. BOOST_TEST_EQ(a.size(), 3);
  132. BOOST_TEST_EQ(str(a), "category(5, 1, 10, options=growth)");
  133. }
  134. // iterators
  135. {
  136. test_axis_iterator(axis::category<>({3, 1, 2}, ""), 0, 3);
  137. test_axis_iterator(axis::category<std::string>({"A", "B"}, ""), 0, 2);
  138. }
  139. return boost::report_errors();
  140. }