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