detail_misc_test.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2015-2019 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/detail/common_type.hpp>
  10. #include <boost/histogram/detail/counting_streambuf.hpp>
  11. #include <boost/histogram/detail/non_member_container_access.hpp>
  12. #include <boost/histogram/fwd.hpp>
  13. #include <boost/histogram/literals.hpp>
  14. #include <boost/histogram/storage_adaptor.hpp>
  15. #include <boost/histogram/unlimited_storage.hpp>
  16. #include <ostream>
  17. #include "std_ostream.hpp"
  18. using namespace boost::histogram;
  19. using namespace boost::histogram::literals;
  20. namespace dtl = boost::histogram::detail;
  21. int main() {
  22. // literals
  23. {
  24. BOOST_TEST_TRAIT_SAME(std::integral_constant<unsigned, 0>, decltype(0_c));
  25. BOOST_TEST_TRAIT_SAME(std::integral_constant<unsigned, 3>, decltype(3_c));
  26. BOOST_TEST_EQ(decltype(10_c)::value, 10);
  27. BOOST_TEST_EQ(decltype(213_c)::value, 213);
  28. }
  29. // common_storage
  30. {
  31. BOOST_TEST_TRAIT_SAME(dtl::common_storage<unlimited_storage<>, unlimited_storage<>>,
  32. unlimited_storage<>);
  33. BOOST_TEST_TRAIT_SAME(
  34. dtl::common_storage<dense_storage<double>, dense_storage<double>>,
  35. dense_storage<double>);
  36. BOOST_TEST_TRAIT_SAME(dtl::common_storage<dense_storage<int>, dense_storage<double>>,
  37. dense_storage<double>);
  38. BOOST_TEST_TRAIT_SAME(dtl::common_storage<dense_storage<double>, dense_storage<int>>,
  39. dense_storage<double>);
  40. BOOST_TEST_TRAIT_SAME(dtl::common_storage<dense_storage<double>, unlimited_storage<>>,
  41. dense_storage<double>);
  42. BOOST_TEST_TRAIT_SAME(dtl::common_storage<dense_storage<int>, unlimited_storage<>>,
  43. unlimited_storage<>);
  44. BOOST_TEST_TRAIT_SAME(dtl::common_storage<dense_storage<double>, weight_storage>,
  45. weight_storage);
  46. }
  47. // size & data
  48. {
  49. char a[4] = {1, 2, 3, 4};
  50. BOOST_TEST_EQ(dtl::size(a), 4u);
  51. BOOST_TEST_EQ(dtl::data(a), a);
  52. auto b = {1, 2};
  53. BOOST_TEST_EQ(dtl::size(b), 2u);
  54. BOOST_TEST_EQ(dtl::data(b), b.begin());
  55. struct C {
  56. unsigned size() const { return 3; }
  57. int* data() { return buf; }
  58. const int* data() const { return buf; }
  59. int buf[1];
  60. } c;
  61. BOOST_TEST_EQ(dtl::size(c), 3u);
  62. BOOST_TEST_EQ(dtl::data(c), c.buf);
  63. BOOST_TEST_EQ(dtl::data(static_cast<const C&>(c)), c.buf);
  64. struct {
  65. int size() const { return 5; }
  66. } d;
  67. BOOST_TEST_EQ(dtl::size(d), 5u);
  68. }
  69. // counting_streambuf
  70. {
  71. dtl::counting_streambuf<char> cbuf;
  72. std::ostream os(&cbuf);
  73. os.put('x');
  74. BOOST_TEST_EQ(cbuf.count, 1);
  75. os << 12;
  76. BOOST_TEST_EQ(cbuf.count, 3);
  77. os << "123";
  78. BOOST_TEST_EQ(cbuf.count, 6);
  79. }
  80. return boost::report_errors();
  81. }