Imported Upstream version 1.71.0
[platform/upstream/boost.git] / libs / gil / test / channel / packed_channel_value.cpp
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
14
15 #define BOOST_TEST_MODULE test_channel_traits
16 #include "unit_test.hpp"
17
18 namespace gil = boost::gil;
19
20 template <typename T>
21 void test_packed_channel_value_members()
22 {
23     static_assert(std::is_same<typename T::value_type, T>::value,
24         "value_type should be the same as packed_channel_value specialization");
25
26     static_assert(std::is_lvalue_reference<typename T::reference>::value,
27         "reference should be lvalue reference type");
28
29     static_assert(std::is_lvalue_reference<typename T::reference>::value,
30         "const_reference should be lvalue reference type");
31
32     static_assert(std::is_pointer<typename T::pointer>::value,
33         "pointer should be pointer type");
34
35     static_assert(std::is_pointer<typename T::const_pointer>::value,
36         "const_pointer should be pointer type");
37
38     static_assert(T::is_mutable, "packed_channel_value should be mutable by default");
39
40     static_assert(std::is_constructible<T, typename T::integer_t>::value,
41         "packed_channel_value should be constructible from underlying integer_t");
42
43     static_assert(std::is_convertible<T, typename T::integer_t>::value,
44         "packed_channel_value should be convertible to underlying integer_t");
45 }
46
47 BOOST_AUTO_TEST_CASE(packed_channel_value_with_num_bits_1)
48 {
49     using bits1 = gil::packed_channel_value<1>;
50
51     test_packed_channel_value_members<bits1>();
52
53     static_assert(std::is_same<bits1::integer_t, std::uint8_t>::value,
54         "smallest integral type to store 1-bit value should be 8-bit unsigned");
55
56     BOOST_TEST(bits1::num_bits() == 1u);
57     BOOST_TEST(bits1::min_value() == 0u);
58     BOOST_TEST(bits1::max_value() == 1u);
59     BOOST_TEST(gil::channel_traits<bits1>::min_value() == 0u);
60     BOOST_TEST(gil::channel_traits<bits1>::max_value() == 1u);
61 }
62
63 BOOST_AUTO_TEST_CASE(packed_channel_value_with_num_bits_8)
64 {
65     using bits8 = gil::packed_channel_value<8>;
66
67     test_packed_channel_value_members<bits8>();
68
69     static_assert(std::is_same<bits8::integer_t, std::uint8_t>::value,
70         "smallest integral type to store 8-bit value should be 8-bit unsigned");
71
72     BOOST_TEST(bits8::num_bits() == 8u);
73     BOOST_TEST(bits8::min_value() == 0u);
74     BOOST_TEST(bits8::max_value() == 255u);
75     BOOST_TEST(gil::channel_traits<bits8>::min_value() == 0u);
76     BOOST_TEST(gil::channel_traits<bits8>::max_value() == 255u);
77 }
78
79 BOOST_AUTO_TEST_CASE(packed_channel_value_with_num_bits15)
80 {
81     using bits15 = gil::packed_channel_value<15>;
82
83     test_packed_channel_value_members<bits15>();
84
85     static_assert(std::is_same<bits15::integer_t, std::uint16_t>::value,
86         "smallest integral type to store 15-bit value should be 8-bit unsigned");
87
88     BOOST_TEST(bits15::num_bits() == 15u);
89     BOOST_TEST(bits15::min_value() == 0u);
90     BOOST_TEST(bits15::max_value() == 32767u);
91     BOOST_TEST(gil::channel_traits<bits15>::min_value() == 0u);
92     BOOST_TEST(gil::channel_traits<bits15>::max_value() == 32767u);
93 }