File: point.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 (235 lines) | stat: -rw-r--r-- 5,965 bytes parent folder | download | duplicates (10)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// Copyright 2018-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/point.hpp>

#include <boost/core/lightweight_test.hpp>

#include <type_traits>

#include "test_utility_output_stream.hpp"

namespace gil = boost::gil;

void test_default_constructor()
{
    gil::point<int> p;
    BOOST_TEST_EQ(p.x, 0);
    BOOST_TEST_EQ(p.y, 0);
}

void test_user_constructor()
{
    gil::point<int> p{1, 2};
    BOOST_TEST_EQ(p.x, 1);
    BOOST_TEST_EQ(p.y, 2);
}

void test_copy_constructor()
{
    gil::point<int> p1{1, 2};
    gil::point<int> p2{p1};
    BOOST_TEST_EQ(p1.x, p2.x);
    BOOST_TEST_EQ(p1.y, p2.y);
}

void test_copy_assignment_operator()
{
    gil::point<int> p1{1, 2};
    gil::point<int> p2;
    p2 = p1;
    BOOST_TEST_EQ(p1.x, p2.x);
    BOOST_TEST_EQ(p1.y, p2.y);
}

void test_index_operator()
{
    gil::point<int> p{1, 2};
    BOOST_TEST_EQ(p[0], 1);
    BOOST_TEST_EQ(p[1], 2);
}

void test_addition_operator()
{
    gil::point<int> p1{1, 1};
    gil::point<int> const p2{2, 4};
    auto p3 = p1 + p2;
    BOOST_TEST_EQ(p3.x, 3);
    BOOST_TEST_EQ(p3.y, 5);
}

void test_addition_assignment_operator()
{
    gil::point<int> p1{1, 1};
    gil::point<int> const p2{2, 4};
    p1 += p2;
    BOOST_TEST_EQ(p1.x, 3);
    BOOST_TEST_EQ(p1.y, 5);
}

void test_subtraction_assignment_operator()
{
    gil::point<int> p1{2, 4};
    gil::point<int> const p2{1, 1};
    p1 -= p2;
    BOOST_TEST_EQ(p1.x, 1);
    BOOST_TEST_EQ(p1.y, 3);
}

void test_subtraction_operator()
{
    gil::point<int> p1{2, 4};
    gil::point<int> const p2{1, 1};
    p1 = p1 - p2;
    BOOST_TEST_EQ(p1.x, 1);
    BOOST_TEST_EQ(p1.y, 3);
}

void test_unary_minus_operator()
{
    gil::point<int> p1{2, 4};
    auto p2 = -p1;
    BOOST_TEST_EQ(p2.x, -2);
    BOOST_TEST_EQ(p2.y, -4);
    p2 = -p2;
    BOOST_TEST_EQ(p2.x, p1.x);
    BOOST_TEST_EQ(p2.y, p1.y);
}

void test_division_assignment_operator()
{
    {
        gil::point<int> p1{2, 4};
        p1 /= 2;
        static_assert(std::is_same<decltype((p1 / short{}).x), int>::value, "!int");
        static_assert(std::is_same<decltype((p1 / int{}).x), int>::value, "!int");
        static_assert(std::is_same<decltype((p1 / float{}).x), float>::value, "!float");
        static_assert(std::is_same<decltype((p1 / double{}).x), double>::value, "!double");
        BOOST_TEST_EQ(p1.x, 1);
        BOOST_TEST_EQ(p1.y, 2);
    }
    // point / d
    {
        gil::point<int> p1{2, 4};
        auto p2 = p1 / float{2};
        static_assert(std::is_same<decltype((p2 / int{}).x), float>::value, "!float");
        static_assert(std::is_same<decltype((p2 / float{}).x), float>::value, "!float");
        static_assert(std::is_same<decltype((p2 / double{}).x), double>::value, "!double");
        BOOST_TEST_GE(p2.x, 1.0); // means, but >= avoids compiler warning
        BOOST_TEST_GE(p2.y, 2.0);
    }
}

void test_multiplication_operator()
{
    gil::point<int> p1{2, 4};
    p1 *= 2;
    BOOST_TEST_EQ(p1.x, 4);
    BOOST_TEST_EQ(p1.y, 8);

    // point * m
    {
        auto p2 = p1 * int{2};
        static_assert(std::is_same<decltype(p2.x), int>::value, "!int");
        static_assert(std::is_same<decltype(p2.y), int>::value, "!int");
        BOOST_TEST_EQ(p2.x, 8);
        BOOST_TEST_EQ(p2.y, 16);
    }
    // m * point
    {
        auto p2 = double{2} *p1;
        static_assert(std::is_same<decltype(p2.x), double>::value, "!double");
        static_assert(std::is_same<decltype(p2.y), double>::value, "!double");
        BOOST_TEST_GE(p2.x, 8); // means, but >= avoids compiler warning
        BOOST_TEST_GE(p2.y, 16);
    }
}

void test_multiplication_assignment_operator()
{
    gil::point<int> p1{2, 4};
    // point * m
    {
        auto p2 = p1 * int{2};
        static_assert(std::is_same<decltype(p2.x), int>::value, "!int");
        static_assert(std::is_same<decltype(p2.y), int>::value, "!int");
        BOOST_TEST_EQ(p2.x, 4);
        BOOST_TEST_EQ(p2.y, 8);
    }
    // m * point
    {
        auto p2 = double{2} * p1;
        static_assert(std::is_same<decltype(p2.x), double>::value, "!double");
        static_assert(std::is_same<decltype(p2.y), double>::value, "!double");
        BOOST_TEST_GE(p2.x, 4); // means, but >= avoids compiler warning
        BOOST_TEST_GE(p2.y, 8);
    }
}

void test_bitwise_left_shift_operator()
{
    gil::point<unsigned int> p{2, 4};
    p = p << 1;
    BOOST_TEST_EQ(p.x, 4u);
    BOOST_TEST_EQ(p.y, 8u);
}

void test_bitwise_right_shift_operator()
{
    gil::point<unsigned int> p{2, 4};
    p = p >> 1;
    BOOST_TEST_EQ(p.x, 2u / 2);
    BOOST_TEST_EQ(p.y, 4u / 2);
}

void test_equal_to_operator()
{
    gil::point<int> p1{2, 4};
    gil::point<int> p2{2, 4};
    BOOST_TEST_EQ(p1, p2);
}

void test_not_equal_to_operator()
{
    gil::point<int> p1{1, 1};
    gil::point<int> p2{2, 4};
    BOOST_TEST_NE(p1, p2);
}

void test_axis_value()
{
    gil::point<int> p1{1, 2};
    gil::point<int> const p2{1, 2};
    BOOST_TEST_EQ(gil::axis_value<0>(p1), p1.x);
    BOOST_TEST_EQ(gil::axis_value<1>(p1), p1.y);
    BOOST_TEST_EQ(gil::axis_value<0>(p2), p2.x);
    BOOST_TEST_EQ(gil::axis_value<1>(p2), p2.y);
}

int main()
{
    test_default_constructor();
    test_user_constructor();
    test_copy_constructor();
    test_copy_assignment_operator();
    test_index_operator();
    test_addition_operator();
    test_addition_assignment_operator();
    test_subtraction_assignment_operator();
    test_subtraction_operator();
    test_unary_minus_operator();
    test_division_assignment_operator();
    test_multiplication_operator();
    test_multiplication_assignment_operator();
    test_bitwise_left_shift_operator();
    test_bitwise_right_shift_operator();
    test_equal_to_operator();
    test_not_equal_to_operator();
    test_axis_value();

    return boost::report_errors();
}