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