File: Vector3.test.cpp

package info (click to toggle)
libsfml 3.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 13,704 kB
  • sloc: cpp: 52,754; ansic: 24,944; objc: 668; sh: 172; xml: 25; makefile: 18
file content (236 lines) | stat: -rw-r--r-- 6,809 bytes parent folder | download
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
236
#include <SFML/System/Vector3.hpp>

#include <catch2/catch_template_test_macros.hpp>

#include <SystemUtil.hpp>
#include <type_traits>

TEMPLATE_TEST_CASE("[System] sf::Vector3", "", int, float)
{
    SECTION("Type traits")
    {
        STATIC_CHECK(std::is_copy_constructible_v<sf::Vector3<TestType>>);
        STATIC_CHECK(std::is_copy_assignable_v<sf::Vector3<TestType>>);
        STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::Vector3<TestType>>);
        STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Vector3<TestType>>);
    }

    SECTION("Construction")
    {
        SECTION("Default constructor")
        {
            constexpr sf::Vector3<TestType> vector;
            STATIC_CHECK(vector.x == 0);
            STATIC_CHECK(vector.y == 0);
            STATIC_CHECK(vector.z == 0);
        }

        SECTION("(x, y, z) coordinate constructor")
        {
            constexpr sf::Vector3<TestType> vector(1, 2, 3);
            STATIC_CHECK(vector.x == 1);
            STATIC_CHECK(vector.y == 2);
            STATIC_CHECK(vector.z == 3);
        }

        SECTION("Conversion operator")
        {
            STATIC_CHECK(!std::is_convertible_v<sf::Vector3f, sf::Vector3i>);

            constexpr sf::Vector3f sourceVector(1.0f, 2.0f, 3.0f);
            constexpr sf::Vector3i vector(sourceVector);

            STATIC_CHECK(vector.x == static_cast<int>(sourceVector.x));
            STATIC_CHECK(vector.y == static_cast<int>(sourceVector.y));
            STATIC_CHECK(vector.z == static_cast<int>(sourceVector.z));
        }
    }

    SECTION("Unary operations")
    {
        SECTION("-vector")
        {
            constexpr sf::Vector3<TestType> vector(1, 2, 3);
            constexpr sf::Vector3<TestType> negatedVector = -vector;

            STATIC_CHECK(negatedVector.x == -1);
            STATIC_CHECK(negatedVector.y == -2);
            STATIC_CHECK(negatedVector.z == -3);
        }
    }

    SECTION("Arithmetic operations between two vectors")
    {
        sf::Vector3<TestType>           firstVector(2, 5, 6);
        constexpr sf::Vector3<TestType> secondVector(8, 3, 7);

        SECTION("vector += vector")
        {
            firstVector += secondVector;

            CHECK(firstVector.x == 10);
            CHECK(firstVector.y == 8);
            CHECK(firstVector.z == 13);
        }

        SECTION("vector -= vector")
        {
            firstVector -= secondVector;

            CHECK(firstVector.x == -6);
            CHECK(firstVector.y == 2);
            CHECK(firstVector.z == -1);
        }

        SECTION("vector + vector")
        {
            const sf::Vector3<TestType> result = firstVector + secondVector;

            CHECK(result.x == 10);
            CHECK(result.y == 8);
            CHECK(result.z == 13);
        }

        SECTION("vector - vector")
        {
            const sf::Vector3<TestType> result = firstVector - secondVector;

            CHECK(result.x == -6);
            CHECK(result.y == 2);
            CHECK(result.z == -1);
        }
    }

    SECTION("Arithmetic operations between vector and scalar value")
    {
        sf::Vector3<TestType> vector(26, 12, 6);
        constexpr TestType    scalar = 2;

        SECTION("vector * scalar")
        {
            const sf::Vector3<TestType> result = vector * scalar;

            CHECK(result.x == 52);
            CHECK(result.y == 24);
            CHECK(result.z == 12);
        }

        SECTION("scalar * vector")
        {
            const sf::Vector3<TestType> result = scalar * vector;

            CHECK(result.x == 52);
            CHECK(result.y == 24);
            CHECK(result.z == 12);
        }

        SECTION("vector *= scalar")
        {
            vector *= scalar;

            CHECK(vector.x == 52);
            CHECK(vector.y == 24);
            CHECK(vector.z == 12);
        }

        SECTION("vector / scalar")
        {
            const sf::Vector3<TestType> result = vector / scalar;

            CHECK(result.x == 13);
            CHECK(result.y == 6);
            CHECK(result.z == 3);
        }

        SECTION("vector /= scalar")
        {
            vector /= scalar;

            CHECK(vector.x == 13);
            CHECK(vector.y == 6);
            CHECK(vector.z == 3);
        }
    }

    SECTION("Comparison operations (two equal and one different vector)")
    {
        constexpr sf::Vector3<TestType> firstEqualVector(1, 5, 6);
        constexpr sf::Vector3<TestType> secondEqualVector(1, 5, 6);
        constexpr sf::Vector3<TestType> differentVector(6, 9, 7);

        SECTION("vector == vector")
        {
            STATIC_CHECK(firstEqualVector == secondEqualVector);
            STATIC_CHECK_FALSE(firstEqualVector == differentVector);
        }

        SECTION("vector != vector")
        {
            STATIC_CHECK(firstEqualVector != differentVector);
            STATIC_CHECK_FALSE(firstEqualVector != secondEqualVector);
        }
    }

    SECTION("Structured bindings")
    {
        sf::Vector3<TestType> vector(1, 2, 3); // NOLINT(misc-const-correctness)

        SECTION("destructure by value")
        {
            auto [x, y, z] = vector;

            CHECK(x == 1);
            CHECK(y == 2);
            CHECK(z == 3);

            STATIC_CHECK(std::is_same_v<decltype(x), decltype(vector.x)>);

            x = 3;

            CHECK(x == 3);
            CHECK(vector.x == 1);
        }

        SECTION("destructure by ref")
        {
            auto& [x, y, z] = vector;

            CHECK(x == 1);
            CHECK(y == 2);
            CHECK(z == 3);

            STATIC_CHECK(std::is_same_v<decltype(x), decltype(vector.x)>);

            x = 3;

            CHECK(x == 3);
            CHECK(vector.x == 3);
        }
    }

    SECTION("Length and normalization")
    {
        constexpr sf::Vector3f v(2.4f, 3.0f, 5.2f);

        CHECK(v.length() == Approx(6.46529f));
        CHECK(v.lengthSquared() == Approx(41.79997f));
        CHECK(v.normalized() == Approx(sf::Vector3f(0.37121f, 0.46401f, 0.80429f)));
    }

    SECTION("Products and quotients")
    {
        constexpr sf::Vector3f v(2.4f, 3.0f, 5.2f);
        constexpr sf::Vector3f w(-0.7f, -2.2f, -4.8f);

        CHECK(v.dot(w) == Approx(-33.24f));
        CHECK(w.dot(v) == Approx(-33.24f));

        CHECK(v.cross(w) == Approx(sf::Vector3f(-2.96f, 7.88f, -3.18f)));
        CHECK(w.cross(v) == Approx(sf::Vector3f(2.96f, -7.88f, 3.18f)));

        CHECK(v.componentWiseMul(w) == Approx(sf::Vector3f(-1.68f, -6.6f, -24.96f)));
        CHECK(w.componentWiseMul(v) == Approx(sf::Vector3f(-1.68f, -6.6f, -24.96f)));
        CHECK(v.componentWiseDiv(w) == Approx(sf::Vector3f(-3.428571f, -1.363636f, -1.0833333f)));
        CHECK(w.componentWiseDiv(v) == Approx(sf::Vector3f(-0.291666f, -0.733333f, -0.9230769f)));
    }
}