packed_channel_value.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Copyright 2019 Mateusz Loskot <mateusz at loskot dot net>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #include <boost/gil/channel.hpp>
  9. #include <boost/gil/typedefs.hpp>
  10. #include <cstdint>
  11. #include <limits>
  12. #include <type_traits>
  13. #define BOOST_TEST_MODULE test_channel_traits
  14. #include "unit_test.hpp"
  15. namespace gil = boost::gil;
  16. template <typename T>
  17. void test_packed_channel_value_members()
  18. {
  19. static_assert(std::is_same<typename T::value_type, T>::value,
  20. "value_type should be the same as packed_channel_value specialization");
  21. static_assert(std::is_lvalue_reference<typename T::reference>::value,
  22. "reference should be lvalue reference type");
  23. static_assert(std::is_lvalue_reference<typename T::reference>::value,
  24. "const_reference should be lvalue reference type");
  25. static_assert(std::is_pointer<typename T::pointer>::value,
  26. "pointer should be pointer type");
  27. static_assert(std::is_pointer<typename T::const_pointer>::value,
  28. "const_pointer should be pointer type");
  29. static_assert(T::is_mutable, "packed_channel_value should be mutable by default");
  30. static_assert(std::is_constructible<T, typename T::integer_t>::value,
  31. "packed_channel_value should be constructible from underlying integer_t");
  32. static_assert(std::is_convertible<T, typename T::integer_t>::value,
  33. "packed_channel_value should be convertible to underlying integer_t");
  34. }
  35. BOOST_AUTO_TEST_CASE(packed_channel_value_with_num_bits_1)
  36. {
  37. using bits1 = gil::packed_channel_value<1>;
  38. test_packed_channel_value_members<bits1>();
  39. static_assert(std::is_same<bits1::integer_t, std::uint8_t>::value,
  40. "smallest integral type to store 1-bit value should be 8-bit unsigned");
  41. BOOST_TEST(bits1::num_bits() == 1u);
  42. BOOST_TEST(bits1::min_value() == 0u);
  43. BOOST_TEST(bits1::max_value() == 1u);
  44. BOOST_TEST(gil::channel_traits<bits1>::min_value() == 0u);
  45. BOOST_TEST(gil::channel_traits<bits1>::max_value() == 1u);
  46. }
  47. BOOST_AUTO_TEST_CASE(packed_channel_value_with_num_bits_8)
  48. {
  49. using bits8 = gil::packed_channel_value<8>;
  50. test_packed_channel_value_members<bits8>();
  51. static_assert(std::is_same<bits8::integer_t, std::uint8_t>::value,
  52. "smallest integral type to store 8-bit value should be 8-bit unsigned");
  53. BOOST_TEST(bits8::num_bits() == 8u);
  54. BOOST_TEST(bits8::min_value() == 0u);
  55. BOOST_TEST(bits8::max_value() == 255u);
  56. BOOST_TEST(gil::channel_traits<bits8>::min_value() == 0u);
  57. BOOST_TEST(gil::channel_traits<bits8>::max_value() == 255u);
  58. }
  59. BOOST_AUTO_TEST_CASE(packed_channel_value_with_num_bits15)
  60. {
  61. using bits15 = gil::packed_channel_value<15>;
  62. test_packed_channel_value_members<bits15>();
  63. static_assert(std::is_same<bits15::integer_t, std::uint16_t>::value,
  64. "smallest integral type to store 15-bit value should be 8-bit unsigned");
  65. BOOST_TEST(bits15::num_bits() == 15u);
  66. BOOST_TEST(bits15::min_value() == 0u);
  67. BOOST_TEST(bits15::max_value() == 32767u);
  68. BOOST_TEST(gil::channel_traits<bits15>::min_value() == 0u);
  69. BOOST_TEST(gil::channel_traits<bits15>::max_value() == 32767u);
  70. }
  71. using fixture = gil::packed_channel_value<8>;
  72. BOOST_AUTO_TEST_CASE(packed_channel_value_default_constructor)
  73. {
  74. fixture f;
  75. std::uint8_t v = f;
  76. BOOST_TEST(v == std::uint8_t{0});
  77. }
  78. BOOST_AUTO_TEST_CASE(packed_channel_value_user_defined_constructors)
  79. {
  80. fixture f{1};
  81. std::uint8_t v = f;
  82. BOOST_TEST(v == std::uint8_t{1});
  83. }
  84. BOOST_AUTO_TEST_CASE(packed_channel_value_copy_constructors)
  85. {
  86. fixture f1{128};
  87. fixture f2{f1};
  88. BOOST_TEST(std::uint8_t{f1} == std::uint8_t{128});
  89. BOOST_TEST(std::uint8_t{f1} == std::uint8_t{f2});
  90. }
  91. BOOST_AUTO_TEST_CASE(packed_channel_value_assignment)
  92. {
  93. fixture f;
  94. f = 64;
  95. BOOST_TEST(f == std::uint8_t{64});
  96. }