Debian Codegraph @

tests/core/math/test_vector4i.h

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