Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/math/test_vector3i.cpp
23450 views
1
/**************************************************************************/
2
/* test_vector3i.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_vector3i)
34
35
#include "core/math/vector3i.h"
36
37
namespace TestVector3i {
38
39
TEST_CASE("[Vector3i] Constructor methods") {
40
constexpr Vector3i vector_empty = Vector3i();
41
constexpr Vector3i vector_zero = Vector3i(0, 0, 0);
42
static_assert(
43
vector_empty == vector_zero,
44
"Vector3i Constructor with no inputs should return a zero Vector3i.");
45
}
46
47
TEST_CASE("[Vector3i] Axis methods") {
48
Vector3i vector = Vector3i(1, 2, 3);
49
CHECK_MESSAGE(
50
vector.max_axis_index() == Vector3i::Axis::AXIS_Z,
51
"Vector3i max_axis_index should work as expected.");
52
CHECK_MESSAGE(
53
vector.min_axis_index() == Vector3i::Axis::AXIS_X,
54
"Vector3i min_axis_index should work as expected.");
55
CHECK_MESSAGE(
56
vector[vector.max_axis_index()] == 3,
57
"Vector3i array operator should work as expected.");
58
CHECK_MESSAGE(
59
vector[vector.min_axis_index()] == 1,
60
"Vector3i array operator should work as expected.");
61
62
vector[Vector3i::Axis::AXIS_Y] = 5;
63
CHECK_MESSAGE(
64
vector[Vector3i::Axis::AXIS_Y] == 5,
65
"Vector3i array operator setter should work as expected.");
66
}
67
68
TEST_CASE("[Vector3i] Clamp method") {
69
constexpr Vector3i vector = Vector3i(10, 10, 10);
70
CHECK_MESSAGE(
71
Vector3i(-5, 5, 15).clamp(Vector3i(), vector) == Vector3i(0, 5, 10),
72
"Vector3i clamp should work as expected.");
73
CHECK_MESSAGE(
74
vector.clamp(Vector3i(0, 10, 15), Vector3i(5, 10, 20)) == Vector3i(5, 10, 15),
75
"Vector3i clamp should work as expected.");
76
}
77
78
TEST_CASE("[Vector3i] Length methods") {
79
constexpr Vector3i vector1 = Vector3i(10, 10, 10);
80
constexpr Vector3i vector2 = Vector3i(20, 30, 40);
81
CHECK_MESSAGE(
82
vector1.length_squared() == 300,
83
"Vector3i length_squared should work as expected and return exact result.");
84
CHECK_MESSAGE(
85
vector1.length() == doctest::Approx(10 * Math::SQRT3),
86
"Vector3i length should work as expected.");
87
CHECK_MESSAGE(
88
vector2.length_squared() == 2900,
89
"Vector3i length_squared should work as expected and return exact result.");
90
CHECK_MESSAGE(
91
vector2.length() == doctest::Approx(53.8516480713450403125),
92
"Vector3i length should work as expected.");
93
CHECK_MESSAGE(
94
vector1.distance_squared_to(vector2) == 1400,
95
"Vector3i distance_squared_to should work as expected and return exact result.");
96
CHECK_MESSAGE(
97
vector1.distance_to(vector2) == doctest::Approx(37.41657386773941385584),
98
"Vector3i distance_to should work as expected.");
99
}
100
101
TEST_CASE("[Vector3i] Operators") {
102
constexpr Vector3i vector1 = Vector3i(4, 5, 9);
103
constexpr Vector3i vector2 = Vector3i(1, 2, 3);
104
105
static_assert(
106
(vector1 + vector2) == Vector3i(5, 7, 12),
107
"Vector3i addition with integers should give exact results.");
108
static_assert(
109
(vector1 - vector2) == Vector3i(3, 3, 6),
110
"Vector3i subtraction with integers should give exact results.");
111
static_assert(
112
(vector1 * vector2) == Vector3i(4, 10, 27),
113
"Vector3i multiplication with integers should give exact results.");
114
static_assert(
115
(vector1 / vector2) == Vector3i(4, 2, 3),
116
"Vector3i division with integers should give exact results.");
117
118
static_assert(
119
(vector1 * 2) == Vector3i(8, 10, 18),
120
"Vector3i multiplication with integers should give exact results.");
121
static_assert(
122
(vector1 / 2) == Vector3i(2, 2, 4),
123
"Vector3i division with integers should give exact results.");
124
125
CHECK_MESSAGE(
126
((Vector3)vector1) == Vector3(4, 5, 9),
127
"Vector3i cast to Vector3 should work as expected.");
128
CHECK_MESSAGE(
129
((Vector3)vector2) == Vector3(1, 2, 3),
130
"Vector3i cast to Vector3 should work as expected.");
131
CHECK_MESSAGE(
132
Vector3i(Vector3(1.1, 2.9, 3.9)) == Vector3i(1, 2, 3),
133
"Vector3i constructed from Vector3 should work as expected.");
134
}
135
136
TEST_CASE("[Vector3i] Other methods") {
137
constexpr Vector3i vector = Vector3i(1, 3, -7);
138
139
CHECK_MESSAGE(
140
vector.min(Vector3i(3, 2, 5)) == Vector3i(1, 2, -7),
141
"Vector3i min should return expected value.");
142
CHECK_MESSAGE(
143
vector.max(Vector3i(5, 2, 4)) == Vector3i(5, 3, 4),
144
"Vector3i max should return expected value.");
145
146
CHECK_MESSAGE(
147
vector.snapped(Vector3i(4, 2, 5)) == Vector3i(0, 4, -5),
148
"Vector3i snapped should work as expected.");
149
}
150
151
TEST_CASE("[Vector3i] Abs and sign methods") {
152
constexpr Vector3i vector1 = Vector3i(1, 3, 5);
153
constexpr Vector3i vector2 = Vector3i(1, -3, -5);
154
CHECK_MESSAGE(
155
vector1.abs() == vector1,
156
"Vector3i abs should work as expected.");
157
CHECK_MESSAGE(
158
vector2.abs() == vector1,
159
"Vector3i abs should work as expected.");
160
161
CHECK_MESSAGE(
162
vector1.sign() == Vector3i(1, 1, 1),
163
"Vector3i sign should work as expected.");
164
CHECK_MESSAGE(
165
vector2.sign() == Vector3i(1, -1, -1),
166
"Vector3i sign should work as expected.");
167
}
168
169
} // namespace TestVector3i
170
171