File: packed_channel_value.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (137 lines) | stat: -rw-r--r-- 4,300 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// Copyright 2019-2020 Mateusz Loskot <mateusz at loskot dot net>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#include <boost/gil/channel.hpp>
#include <boost/gil/typedefs.hpp>

#include <boost/core/lightweight_test.hpp>

#include <cstdint>
#include <limits>
#include <type_traits>

namespace gil = boost::gil;

template <typename T>
void test_packed_channel_value_members()
{
    static_assert(std::is_same<typename T::value_type, T>::value,
        "value_type should be the same as packed_channel_value specialization");

    static_assert(std::is_lvalue_reference<typename T::reference>::value,
        "reference should be lvalue reference type");

    static_assert(std::is_lvalue_reference<typename T::reference>::value,
        "const_reference should be lvalue reference type");

    static_assert(std::is_pointer<typename T::pointer>::value,
        "pointer should be pointer type");

    static_assert(std::is_pointer<typename T::const_pointer>::value,
        "const_pointer should be pointer type");

    static_assert(T::is_mutable, "packed_channel_value should be mutable by default");

    static_assert(std::is_constructible<T, typename T::integer_t>::value,
        "packed_channel_value should be constructible from underlying integer_t");

    static_assert(std::is_convertible<T, typename T::integer_t>::value,
        "packed_channel_value should be convertible to underlying integer_t");
}

void test_packed_channel_value_with_num_bits_1()
{
    using bits1 = gil::packed_channel_value<1>;

    test_packed_channel_value_members<bits1>();

    static_assert(std::is_same<bits1::integer_t, std::uint8_t>::value,
        "smallest integral type to store 1-bit value should be 8-bit unsigned");

    BOOST_TEST_EQ(bits1::num_bits(), 1u);
    BOOST_TEST_EQ(bits1::min_value(), 0u);
    BOOST_TEST_EQ(bits1::max_value(), 1u);
    BOOST_TEST_EQ(gil::channel_traits<bits1>::min_value(), 0u);
    BOOST_TEST_EQ(gil::channel_traits<bits1>::max_value(), 1u);
}

void test_packed_channel_value_with_num_bits_8()
{
    using bits8 = gil::packed_channel_value<8>;

    test_packed_channel_value_members<bits8>();

    static_assert(std::is_same<bits8::integer_t, std::uint8_t>::value,
        "smallest integral type to store 8-bit value should be 8-bit unsigned");

    BOOST_TEST_EQ(bits8::num_bits(), 8u);
    BOOST_TEST_EQ(bits8::min_value(), 0u);
    BOOST_TEST_EQ(bits8::max_value(), 255u);
    BOOST_TEST_EQ(gil::channel_traits<bits8>::min_value(), 0u);
    BOOST_TEST_EQ(gil::channel_traits<bits8>::max_value(), 255u);
}

void test_packed_channel_value_with_num_bits15()
{
    using bits15 = gil::packed_channel_value<15>;

    test_packed_channel_value_members<bits15>();

    static_assert(std::is_same<bits15::integer_t, std::uint16_t>::value,
        "smallest integral type to store 15-bit value should be 8-bit unsigned");

    BOOST_TEST_EQ(bits15::num_bits(), 15u);
    BOOST_TEST_EQ(bits15::min_value(), 0u);
    BOOST_TEST_EQ(bits15::max_value(), 32767u);
    BOOST_TEST_EQ(gil::channel_traits<bits15>::min_value(), 0u);
    BOOST_TEST_EQ(gil::channel_traits<bits15>::max_value(), 32767u);
}

using fixture = gil::packed_channel_value<8>;

void test_packed_channel_value_default_constructor()
{
    fixture f;
    std::uint8_t v = f;
    BOOST_TEST_EQ(v, std::uint8_t{0});
}

void test_packed_channel_value_user_defined_constructors()
{
    fixture f{1};
    std::uint8_t v = f;
    BOOST_TEST_EQ(v, std::uint8_t{1});
}

void test_packed_channel_value_copy_constructors()
{
    fixture f1{128};
    fixture f2{f1};

    BOOST_TEST_EQ(std::uint8_t{f1}, std::uint8_t{128});
    BOOST_TEST_EQ(std::uint8_t{f1}, std::uint8_t{f2});
}

void test_packed_channel_value_assignment()
{
    fixture f;
    f = 64;
    BOOST_TEST_EQ(f, std::uint8_t{64});
}

int main()
{
    test_packed_channel_value_with_num_bits_1();
    test_packed_channel_value_with_num_bits_8();
    test_packed_channel_value_with_num_bits15();
    test_packed_channel_value_default_constructor();
    test_packed_channel_value_user_defined_constructors();
    test_packed_channel_value_copy_constructors();
    test_packed_channel_value_assignment();

    return ::boost::report_errors();
}