Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / gil / test / core / 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(std::false_type)  {}
22
23 template <typename ChannelFixtureBase>
24 void test_channel_arithmetic_mutable(std::true_type)
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 = std::integral_constant
73         <
74             bool,
75             gil::channel_traits<typename fixture_t::channel_t>::is_mutable
76         >;
77     test_channel_arithmetic_mutable<ChannelFixtureBase>(is_mutable_t{});
78 }
79
80 BOOST_AUTO_TEST_CASE_TEMPLATE(channel_value, Channel, fixture::channel_byte_types)
81 {
82     using fixture_t = fixture::channel_value<Channel>;
83     test_channel_arithmetic<fixture_t>();
84 }
85
86 BOOST_AUTO_TEST_CASE_TEMPLATE(channel_reference, Channel, fixture::channel_byte_types)
87 {
88     using fixture_t = fixture::channel_reference<Channel&>;
89     test_channel_arithmetic<fixture_t>();
90 }
91
92 BOOST_AUTO_TEST_CASE_TEMPLATE(
93     channel_reference_const, Channel, fixture::channel_byte_types)
94 {
95     using fixture_t = fixture::channel_reference<Channel const&>;
96     test_channel_arithmetic<fixture_t>();
97 }
98
99 BOOST_AUTO_TEST_CASE_TEMPLATE(
100     packed_channel_reference, BitField, fixture::channel_bitfield_types)
101 {
102     using channels565_t = fixture::packed_channels565<BitField>;
103     test_channel_arithmetic<typename channels565_t::fixture_0_5_t>();
104     test_channel_arithmetic<typename channels565_t::fixture_5_6_t >();
105     test_channel_arithmetic<typename channels565_t::fixture_11_5_t>();
106 }
107
108 BOOST_AUTO_TEST_CASE_TEMPLATE(
109     packed_dynamic_channel_reference, BitField, fixture::channel_bitfield_types)
110 {
111     using channels565_t = fixture::packed_dynamic_channels565<BitField>;
112     test_channel_arithmetic<typename channels565_t::fixture_5_t>();
113     test_channel_arithmetic<typename channels565_t::fixture_6_t>();
114 }