Debian Codegraph @

tests/core/math/test_vector3i.h

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