1 //
2 // Copyright 2019-2020 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
11 #include <boost/core/lightweight_test.hpp>
12
13 #include <cstdint>
14 #include <limits>
15 #include <type_traits>
16
17 namespace gil = boost::gil;
18
19 template <typename T>
test_packed_channel_value_members()20 void test_packed_channel_value_members()
21 {
22 static_assert(std::is_same<typename T::value_type, T>::value,
23 "value_type should be the same as packed_channel_value specialization");
24
25 static_assert(std::is_lvalue_reference<typename T::reference>::value,
26 "reference should be lvalue reference type");
27
28 static_assert(std::is_lvalue_reference<typename T::reference>::value,
29 "const_reference should be lvalue reference type");
30
31 static_assert(std::is_pointer<typename T::pointer>::value,
32 "pointer should be pointer type");
33
34 static_assert(std::is_pointer<typename T::const_pointer>::value,
35 "const_pointer should be pointer type");
36
37 static_assert(T::is_mutable, "packed_channel_value should be mutable by default");
38
39 static_assert(std::is_constructible<T, typename T::integer_t>::value,
40 "packed_channel_value should be constructible from underlying integer_t");
41
42 static_assert(std::is_convertible<T, typename T::integer_t>::value,
43 "packed_channel_value should be convertible to underlying integer_t");
44 }
45
test_packed_channel_value_with_num_bits_1()46 void test_packed_channel_value_with_num_bits_1()
47 {
48 using bits1 = gil::packed_channel_value<1>;
49
50 test_packed_channel_value_members<bits1>();
51
52 static_assert(std::is_same<bits1::integer_t, std::uint8_t>::value,
53 "smallest integral type to store 1-bit value should be 8-bit unsigned");
54
55 BOOST_TEST_EQ(bits1::num_bits(), 1u);
56 BOOST_TEST_EQ(bits1::min_value(), 0u);
57 BOOST_TEST_EQ(bits1::max_value(), 1u);
58 BOOST_TEST_EQ(gil::channel_traits<bits1>::min_value(), 0u);
59 BOOST_TEST_EQ(gil::channel_traits<bits1>::max_value(), 1u);
60 }
61
test_packed_channel_value_with_num_bits_8()62 void test_packed_channel_value_with_num_bits_8()
63 {
64 using bits8 = gil::packed_channel_value<8>;
65
66 test_packed_channel_value_members<bits8>();
67
68 static_assert(std::is_same<bits8::integer_t, std::uint8_t>::value,
69 "smallest integral type to store 8-bit value should be 8-bit unsigned");
70
71 BOOST_TEST_EQ(bits8::num_bits(), 8u);
72 BOOST_TEST_EQ(bits8::min_value(), 0u);
73 BOOST_TEST_EQ(bits8::max_value(), 255u);
74 BOOST_TEST_EQ(gil::channel_traits<bits8>::min_value(), 0u);
75 BOOST_TEST_EQ(gil::channel_traits<bits8>::max_value(), 255u);
76 }
77
test_packed_channel_value_with_num_bits15()78 void test_packed_channel_value_with_num_bits15()
79 {
80 using bits15 = gil::packed_channel_value<15>;
81
82 test_packed_channel_value_members<bits15>();
83
84 static_assert(std::is_same<bits15::integer_t, std::uint16_t>::value,
85 "smallest integral type to store 15-bit value should be 8-bit unsigned");
86
87 BOOST_TEST_EQ(bits15::num_bits(), 15u);
88 BOOST_TEST_EQ(bits15::min_value(), 0u);
89 BOOST_TEST_EQ(bits15::max_value(), 32767u);
90 BOOST_TEST_EQ(gil::channel_traits<bits15>::min_value(), 0u);
91 BOOST_TEST_EQ(gil::channel_traits<bits15>::max_value(), 32767u);
92 }
93
94 using fixture = gil::packed_channel_value<8>;
95
test_packed_channel_value_default_constructor()96 void test_packed_channel_value_default_constructor()
97 {
98 fixture f;
99 std::uint8_t v = f;
100 BOOST_TEST_EQ(v, std::uint8_t{0});
101 }
102
test_packed_channel_value_user_defined_constructors()103 void test_packed_channel_value_user_defined_constructors()
104 {
105 fixture f{1};
106 std::uint8_t v = f;
107 BOOST_TEST_EQ(v, std::uint8_t{1});
108 }
109
test_packed_channel_value_copy_constructors()110 void test_packed_channel_value_copy_constructors()
111 {
112 fixture f1{128};
113 fixture f2{f1};
114
115 BOOST_TEST_EQ(std::uint8_t{f1}, std::uint8_t{128});
116 BOOST_TEST_EQ(std::uint8_t{f1}, std::uint8_t{f2});
117 }
118
test_packed_channel_value_assignment()119 void test_packed_channel_value_assignment()
120 {
121 fixture f;
122 f = 64;
123 BOOST_TEST_EQ(f, std::uint8_t{64});
124 }
125
main()126 int main()
127 {
128 test_packed_channel_value_with_num_bits_1();
129 test_packed_channel_value_with_num_bits_8();
130 test_packed_channel_value_with_num_bits15();
131 test_packed_channel_value_default_constructor();
132 test_packed_channel_value_user_defined_constructors();
133 test_packed_channel_value_copy_constructors();
134 test_packed_channel_value_assignment();
135
136 return ::boost::report_errors();
137 }
138