Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/math/test_vector4i.cpp
23450 views
1
/**************************************************************************/
2
/* test_vector4i.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_vector4i)
34
35
#include "core/math/vector4i.h"
36
37
namespace TestVector4i {
38
39
TEST_CASE("[Vector4i] Constructor methods") {
40
constexpr Vector4i vector_empty = Vector4i();
41
constexpr Vector4i vector_zero = Vector4i(0, 0, 0, 0);
42
static_assert(
43
vector_empty == vector_zero,
44
"Vector4i Constructor with no inputs should return a zero Vector4i.");
45
}
46
47
TEST_CASE("[Vector4i] Axis methods") {
48
Vector4i vector = Vector4i(1, 2, 3, 4);
49
CHECK_MESSAGE(
50
vector.max_axis_index() == Vector4i::Axis::AXIS_W,
51
"Vector4i max_axis_index should work as expected.");
52
CHECK_MESSAGE(
53
vector.min_axis_index() == Vector4i::Axis::AXIS_X,
54
"Vector4i min_axis_index should work as expected.");
55
CHECK_MESSAGE(
56
vector[vector.max_axis_index()] == 4,
57
"Vector4i array operator should work as expected.");
58
CHECK_MESSAGE(
59
vector[vector.min_axis_index()] == 1,
60
"Vector4i array operator should work as expected.");
61
62
vector[Vector4i::Axis::AXIS_Y] = 5;
63
CHECK_MESSAGE(
64
vector[Vector4i::Axis::AXIS_Y] == 5,
65
"Vector4i array operator setter should work as expected.");
66
}
67
68
TEST_CASE("[Vector4i] Clamp method") {
69
constexpr Vector4i vector = Vector4i(10, 10, 10, 10);
70
CHECK_MESSAGE(
71
Vector4i(-5, 5, 15, INT_MAX).clamp(Vector4i(), vector) == Vector4i(0, 5, 10, 10),
72
"Vector4i clamp should work as expected.");
73
CHECK_MESSAGE(
74
vector.clamp(Vector4i(0, 10, 15, -10), Vector4i(5, 10, 20, -5)) == Vector4i(5, 10, 15, -5),
75
"Vector4i clamp should work as expected.");
76
}
77
78
TEST_CASE("[Vector4i] Length methods") {
79
constexpr Vector4i vector1 = Vector4i(10, 10, 10, 10);
80
constexpr Vector4i vector2 = Vector4i(20, 30, 40, 50);
81
CHECK_MESSAGE(
82
vector1.length_squared() == 400,
83
"Vector4i length_squared should work as expected and return exact result.");
84
CHECK_MESSAGE(
85
vector1.length() == doctest::Approx(20),
86
"Vector4i length should work as expected.");
87
CHECK_MESSAGE(
88
vector2.length_squared() == 5400,
89
"Vector4i length_squared should work as expected and return exact result.");
90
CHECK_MESSAGE(
91
vector2.length() == doctest::Approx(73.4846922835),
92
"Vector4i length should work as expected.");
93
CHECK_MESSAGE(
94
vector1.distance_squared_to(vector2) == 3000,
95
"Vector4i distance_squared_to should work as expected.");
96
CHECK_MESSAGE(
97
vector1.distance_to(vector2) == doctest::Approx(54.772255750517),
98
"Vector4i distance_to should work as expected.");
99
}
100
101
TEST_CASE("[Vector4i] Operators") {
102
constexpr Vector4i vector1 = Vector4i(4, 5, 9, 2);
103
constexpr Vector4i vector2 = Vector4i(1, 2, 3, 4);
104
105
static_assert(
106
-vector1 == Vector4i(-4, -5, -9, -2),
107
"Vector4i change of sign should work as expected.");
108
static_assert(
109
(vector1 + vector2) == Vector4i(5, 7, 12, 6),
110
"Vector4i addition with integers should give exact results.");
111
static_assert(
112
(vector1 - vector2) == Vector4i(3, 3, 6, -2),
113
"Vector4i subtraction with integers should give exact results.");
114
static_assert(
115
(vector1 * vector2) == Vector4i(4, 10, 27, 8),
116
"Vector4i multiplication with integers should give exact results.");
117
static_assert(
118
(vector1 / vector2) == Vector4i(4, 2, 3, 0),
119
"Vector4i division with integers should give exact results.");
120
121
static_assert(
122
(vector1 * 2) == Vector4i(8, 10, 18, 4),
123
"Vector4i multiplication with integers should give exact results.");
124
static_assert(
125
(vector1 / 2) == Vector4i(2, 2, 4, 1),
126
"Vector4i division with integers should give exact results.");
127
128
CHECK_MESSAGE(
129
((Vector4)vector1) == Vector4(4, 5, 9, 2),
130
"Vector4i cast to Vector4 should work as expected.");
131
CHECK_MESSAGE(
132
((Vector4)vector2) == Vector4(1, 2, 3, 4),
133
"Vector4i cast to Vector4 should work as expected.");
134
CHECK_MESSAGE(
135
Vector4i(Vector4(1.1, 2.9, 3.9, 100.5)) == Vector4i(1, 2, 3, 100),
136
"Vector4i constructed from Vector4 should work as expected.");
137
}
138
139
TEST_CASE("[Vector3i] Other methods") {
140
constexpr Vector4i vector = Vector4i(1, 3, -7, 13);
141
142
CHECK_MESSAGE(
143
vector.min(Vector4i(3, 2, 5, 8)) == Vector4i(1, 2, -7, 8),
144
"Vector4i min should return expected value.");
145
146
CHECK_MESSAGE(
147
vector.max(Vector4i(5, 2, 4, 8)) == Vector4i(5, 3, 4, 13),
148
"Vector4i max should return expected value.");
149
150
CHECK_MESSAGE(
151
vector.snapped(Vector4i(4, 2, 5, 8)) == Vector4i(0, 4, -5, 16),
152
"Vector4i snapped should work as expected.");
153
}
154
155
TEST_CASE("[Vector4i] Abs and sign methods") {
156
constexpr Vector4i vector1 = Vector4i(1, 3, 5, 7);
157
constexpr Vector4i vector2 = Vector4i(1, -3, -5, 7);
158
CHECK_MESSAGE(
159
vector1.abs() == vector1,
160
"Vector4i abs should work as expected.");
161
CHECK_MESSAGE(
162
vector2.abs() == vector1,
163
"Vector4i abs should work as expected.");
164
165
CHECK_MESSAGE(
166
vector1.sign() == Vector4i(1, 1, 1, 1),
167
"Vector4i sign should work as expected.");
168
CHECK_MESSAGE(
169
vector2.sign() == Vector4i(1, -1, -1, 1),
170
"Vector4i sign should work as expected.");
171
}
172
173
} // namespace TestVector4i
174
175