Imported Upstream version 1.71.0
[platform/upstream/boost.git] / libs / gil / test / channel / algorithm_channel_arithmetic.cpp
1 //
2 // Copyright 2005-2007 Adobe Systems Incorporated
3 // Copyright 2018 Mateusz Loskot <mateusz at loskot dot net>
4 //
5 // Distributed under the Boost Software License, Version 1.0
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
8 //
9 #include <boost/gil/channel_algorithm.hpp>
10 #include <type_traits>
11 #include <utility>
12
13 #define BOOST_TEST_MODULE test_algorithm_channel_arithmetic
14 #include "unit_test.hpp"
15 #include "test_fixture.hpp"
16
17 namespace gil = boost::gil;
18 namespace fixture = boost::gil::test::fixture;
19
20 template <typename ChannelFixtureBase>
21 void test_channel_arithmetic_mutable(boost::mpl::false_)  {}
22
23 template <typename ChannelFixtureBase>
24 void test_channel_arithmetic_mutable(boost::mpl::true_)
25 {
26     using fixture_t = fixture::channel<ChannelFixtureBase>;
27     using channel_value_t = typename fixture_t::channel_value_t;
28     fixture_t f;
29     channel_value_t const v = f.min_v_;
30     channel_value_t const one = 1;
31
32     ++f.min_v_;
33     f.min_v_++;
34     --f.min_v_;
35     f.min_v_--;
36     BOOST_TEST(v == f.min_v_);
37
38     f.min_v_ += one;
39     f.min_v_ -= one;
40     BOOST_TEST(v == f.min_v_);
41
42     f.min_v_ *= one;
43     f.min_v_ /= one;
44     BOOST_TEST(v == f.min_v_);
45
46     f.min_v_ = one; // assignable to scalar
47     BOOST_TEST(f.min_v_ == one);
48     f.min_v_ = v; // and to value type
49     BOOST_TEST(f.min_v_ == v);
50
51     // test swap
52     channel_value_t v1 = f.min_v_;
53     channel_value_t v2 = f.max_v_;
54     std::swap(f.min_v_, f.max_v_);
55     BOOST_TEST(f.min_v_ > f.max_v_);
56     channel_value_t v3 = f.min_v_;
57     channel_value_t v4 = f.max_v_;
58     BOOST_TEST(v1 == v4);
59     BOOST_TEST(v2 == v3);
60 }
61
62 template <typename ChannelFixtureBase>
63 void test_channel_arithmetic()
64 {
65     using fixture_t = fixture::channel<ChannelFixtureBase>;
66     fixture_t f;
67     BOOST_TEST(f.min_v_ * 1 == f.min_v_);
68     BOOST_TEST(f.min_v_ / 1 == f.min_v_);
69     BOOST_TEST((f.min_v_ + 1) + 1 == f.min_v_ + 2);
70     BOOST_TEST((f.max_v_ - 1) - 1 == f.max_v_ - 2);
71
72     using is_mutable_t = boost::mpl::bool_
73         <
74         gil::channel_traits<typename fixture_t::channel_t>::is_mutable
75         >;
76     test_channel_arithmetic_mutable<ChannelFixtureBase>(is_mutable_t());
77 }
78
79 BOOST_AUTO_TEST_CASE_TEMPLATE(channel_value, Channel, fixture::channel_byte_types)
80 {
81     using fixture_t = fixture::channel_value<Channel>;
82     test_channel_arithmetic<fixture_t>();
83 }
84
85 BOOST_AUTO_TEST_CASE_TEMPLATE(channel_reference, Channel, fixture::channel_byte_types)
86 {
87     using fixture_t = fixture::channel_reference<Channel&>;
88     test_channel_arithmetic<fixture_t>();
89 }
90
91 BOOST_AUTO_TEST_CASE_TEMPLATE(
92     channel_reference_const, Channel, fixture::channel_byte_types)
93 {
94     using fixture_t = fixture::channel_reference<Channel const&>;
95     test_channel_arithmetic<fixture_t>();
96 }
97
98 BOOST_AUTO_TEST_CASE_TEMPLATE(
99     packed_channel_reference, BitField, fixture::channel_bitfield_types)
100 {
101     using channels565_t = fixture::packed_channels565<BitField>;
102     test_channel_arithmetic<typename channels565_t::fixture_0_5_t>();
103     test_channel_arithmetic<typename channels565_t::fixture_5_6_t >();
104     test_channel_arithmetic<typename channels565_t::fixture_11_5_t>();
105 }
106
107 BOOST_AUTO_TEST_CASE_TEMPLATE(
108     packed_dynamic_channel_reference, BitField, fixture::channel_bitfield_types)
109 {
110     using channels565_t = fixture::packed_dynamic_channels565<BitField>;
111     test_channel_arithmetic<typename channels565_t::fixture_5_t>();
112     test_channel_arithmetic<typename channels565_t::fixture_6_t>();
113 }