| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/math/rect2.h" |
| 34 |
#include "core/math/rect2i.h" |
| 35 |
|
| 36 |
#include "thirdparty/doctest/doctest.h" |
| 37 |
|
| 38 |
namespace TestRect2 { |
| 39 |
TEST_CASE("[Rect2] Constructor methods") { |
| 40 |
constexpr Rect2 rect = Rect2(0, 100, 1280, 720); |
| 41 |
constexpr Rect2 rect_vector = Rect2(Vector2(0, 100), Vector2(1280, 720)); |
| 42 |
constexpr Rect2 rect_copy_rect = Rect2(rect); |
| 43 |
const Rect2 rect_copy_recti = Rect2(Rect2i(0, 100, 1280, 720)); |
| 44 |
|
| 45 |
static_assert( |
| 46 |
rect == rect_vector, |
| 47 |
"Rect2s created with the same dimensions but by different methods should be equal."); |
| 48 |
static_assert( |
| 49 |
rect == rect_copy_rect, |
| 50 |
"Rect2s created with the same dimensions but by different methods should be equal."); |
| 51 |
CHECK_MESSAGE( |
| 52 |
rect == rect_copy_recti, |
| 53 |
"Rect2s created with the same dimensions but by different methods should be equal."); |
| 54 |
} |
| 55 |
|
| 56 |
TEST_CASE("[Rect2] String conversion") { |
| 57 |
|
| 58 |
CHECK_MESSAGE( |
| 59 |
String(Rect2(0, 100, 1280, 720)) == "[P: (0.0, 100.0), S: (1280.0, 720.0)]", |
| 60 |
"The string representation should match the expected value."); |
| 61 |
} |
| 62 |
|
| 63 |
TEST_CASE("[Rect2] Basic getters") { |
| 64 |
constexpr Rect2 rect = Rect2(0, 100, 1280, 720); |
| 65 |
CHECK_MESSAGE( |
| 66 |
rect.get_position().is_equal_approx(Vector2(0, 100)), |
| 67 |
"get_position() should return the expected value."); |
| 68 |
CHECK_MESSAGE( |
| 69 |
rect.get_size().is_equal_approx(Vector2(1280, 720)), |
| 70 |
"get_size() should return the expected value."); |
| 71 |
CHECK_MESSAGE( |
| 72 |
rect.get_end().is_equal_approx(Vector2(1280, 820)), |
| 73 |
"get_end() should return the expected value."); |
| 74 |
CHECK_MESSAGE( |
| 75 |
rect.get_center().is_equal_approx(Vector2(640, 460)), |
| 76 |
"get_center() should return the expected value."); |
| 77 |
CHECK_MESSAGE( |
| 78 |
Rect2(0, 100, 1281, 721).get_center().is_equal_approx(Vector2(640.5, 460.5)), |
| 79 |
"get_center() should return the expected value."); |
| 80 |
} |
| 81 |
|
| 82 |
TEST_CASE("[Rect2] Basic setters") { |
| 83 |
Rect2 rect = Rect2(0, 100, 1280, 720); |
| 84 |
rect.set_end(Vector2(4000, 4000)); |
| 85 |
CHECK_MESSAGE( |
| 86 |
rect.is_equal_approx(Rect2(0, 100, 4000, 3900)), |
| 87 |
"set_end() should result in the expected Rect2."); |
| 88 |
|
| 89 |
rect = Rect2(0, 100, 1280, 720); |
| 90 |
rect.set_position(Vector2(4000, 4000)); |
| 91 |
CHECK_MESSAGE( |
| 92 |
rect.is_equal_approx(Rect2(4000, 4000, 1280, 720)), |
| 93 |
"set_position() should result in the expected Rect2."); |
| 94 |
|
| 95 |
rect = Rect2(0, 100, 1280, 720); |
| 96 |
rect.set_size(Vector2(4000, 4000)); |
| 97 |
CHECK_MESSAGE( |
| 98 |
rect.is_equal_approx(Rect2(0, 100, 4000, 4000)), |
| 99 |
"set_size() should result in the expected Rect2."); |
| 100 |
} |
| 101 |
|
| 102 |
TEST_CASE("[Rect2] Area getters") { |
| 103 |
CHECK_MESSAGE( |
| 104 |
Rect2(0, 100, 1280, 720).get_area() == doctest::Approx(921'600), |
| 105 |
"get_area() should return the expected value."); |
| 106 |
CHECK_MESSAGE( |
| 107 |
Rect2(0, 100, -1280, -720).get_area() == doctest::Approx(921'600), |
| 108 |
"get_area() should return the expected value."); |
| 109 |
CHECK_MESSAGE( |
| 110 |
Rect2(0, 100, 1280, -720).get_area() == doctest::Approx(-921'600), |
| 111 |
"get_area() should return the expected value."); |
| 112 |
CHECK_MESSAGE( |
| 113 |
Rect2(0, 100, -1280, 720).get_area() == doctest::Approx(-921'600), |
| 114 |
"get_area() should return the expected value."); |
| 115 |
CHECK_MESSAGE( |
| 116 |
Math::is_zero_approx(Rect2(0, 100, 0, 720).get_area()), |
| 117 |
"get_area() should return the expected value."); |
| 118 |
|
| 119 |
CHECK_MESSAGE( |
| 120 |
Rect2(0, 100, 1280, 720).has_area(), |
| 121 |
"has_area() should return the expected value on Rect2 with an area."); |
| 122 |
CHECK_MESSAGE( |
| 123 |
!Rect2(0, 100, 0, 500).has_area(), |
| 124 |
"has_area() should return the expected value on Rect2 with no area."); |
| 125 |
CHECK_MESSAGE( |
| 126 |
!Rect2(0, 100, 500, 0).has_area(), |
| 127 |
"has_area() should return the expected value on Rect2 with no area."); |
| 128 |
CHECK_MESSAGE( |
| 129 |
!Rect2(0, 100, 0, 0).has_area(), |
| 130 |
"has_area() should return the expected value on Rect2 with no area."); |
| 131 |
} |
| 132 |
|
| 133 |
TEST_CASE("[Rect2] Absolute coordinates") { |
| 134 |
CHECK_MESSAGE( |
| 135 |
Rect2(0, 100, 1280, 720).abs().is_equal_approx(Rect2(0, 100, 1280, 720)), |
| 136 |
"abs() should return the expected Rect2."); |
| 137 |
CHECK_MESSAGE( |
| 138 |
Rect2(0, -100, 1280, 720).abs().is_equal_approx(Rect2(0, -100, 1280, 720)), |
| 139 |
"abs() should return the expected Rect2."); |
| 140 |
CHECK_MESSAGE( |
| 141 |
Rect2(0, -100, -1280, -720).abs().is_equal_approx(Rect2(-1280, -820, 1280, 720)), |
| 142 |
"abs() should return the expected Rect2."); |
| 143 |
CHECK_MESSAGE( |
| 144 |
Rect2(0, 100, -1280, 720).abs().is_equal_approx(Rect2(-1280, 100, 1280, 720)), |
| 145 |
"abs() should return the expected Rect2."); |
| 146 |
} |
| 147 |
|
| 148 |
TEST_CASE("[Rect2] Intersection") { |
| 149 |
CHECK_MESSAGE( |
| 150 |
Rect2(0, 100, 1280, 720).intersection(Rect2(0, 300, 100, 100)).is_equal_approx(Rect2(0, 300, 100, 100)), |
| 151 |
"intersection() with fully enclosed Rect2 should return the expected result."); |
| 152 |
|
| 153 |
CHECK_MESSAGE( |
| 154 |
Rect2(0, 100, 1280, 720).intersection(Rect2(1200, 700, 100, 100)).is_equal_approx(Rect2(1200, 700, 80, 100)), |
| 155 |
"intersection() with partially enclosed Rect2 should return the expected result."); |
| 156 |
CHECK_MESSAGE( |
| 157 |
Rect2(0, 100, 1280, 720).intersection(Rect2(-4000, -4000, 100, 100)).is_equal_approx(Rect2()), |
| 158 |
"intersection() with non-enclosed Rect2 should return the expected result."); |
| 159 |
} |
| 160 |
|
| 161 |
TEST_CASE("[Rect2] Enclosing") { |
| 162 |
CHECK_MESSAGE( |
| 163 |
Rect2(0, 100, 1280, 720).encloses(Rect2(0, 300, 100, 100)), |
| 164 |
"encloses() with fully contained Rect2 should return the expected result."); |
| 165 |
CHECK_MESSAGE( |
| 166 |
!Rect2(0, 100, 1280, 720).encloses(Rect2(1200, 700, 100, 100)), |
| 167 |
"encloses() with partially contained Rect2 should return the expected result."); |
| 168 |
CHECK_MESSAGE( |
| 169 |
!Rect2(0, 100, 1280, 720).encloses(Rect2(-4000, -4000, 100, 100)), |
| 170 |
"encloses() with non-contained Rect2 should return the expected result."); |
| 171 |
} |
| 172 |
|
| 173 |
TEST_CASE("[Rect2] Expanding") { |
| 174 |
CHECK_MESSAGE( |
| 175 |
Rect2(0, 100, 1280, 720).expand(Vector2(500, 600)).is_equal_approx(Rect2(0, 100, 1280, 720)), |
| 176 |
"expand() with contained Vector2 should return the expected result."); |
| 177 |
CHECK_MESSAGE( |
| 178 |
Rect2(0, 100, 1280, 720).expand(Vector2(0, 0)).is_equal_approx(Rect2(0, 0, 1280, 820)), |
| 179 |
"expand() with non-contained Vector2 should return the expected result."); |
| 180 |
} |
| 181 |
|
| 182 |
TEST_CASE("[Rect2] Get support") { |
| 183 |
constexpr Rect2 rect = Rect2(Vector2(-1.5, 2), Vector2(4, 5)); |
| 184 |
CHECK_MESSAGE( |
| 185 |
rect.get_support(Vector2(1, 0)) == Vector2(2.5, 2), |
| 186 |
"get_support() should return the expected value."); |
| 187 |
CHECK_MESSAGE( |
| 188 |
rect.get_support(Vector2(0.5, 1)) == Vector2(2.5, 7), |
| 189 |
"get_support() should return the expected value."); |
| 190 |
CHECK_MESSAGE( |
| 191 |
rect.get_support(Vector2(0.5, 1)) == Vector2(2.5, 7), |
| 192 |
"get_support() should return the expected value."); |
| 193 |
CHECK_MESSAGE( |
| 194 |
rect.get_support(Vector2(0, -1)) == Vector2(-1.5, 2), |
| 195 |
"get_support() should return the expected value."); |
| 196 |
CHECK_MESSAGE( |
| 197 |
rect.get_support(Vector2(0, -0.1)) == Vector2(-1.5, 2), |
| 198 |
"get_support() should return the expected value."); |
| 199 |
CHECK_MESSAGE( |
| 200 |
rect.get_support(Vector2()) == Vector2(-1.5, 2), |
| 201 |
"get_support() should return the Rect2 position when given a zero vector."); |
| 202 |
} |
| 203 |
|
| 204 |
TEST_CASE("[Rect2] Growing") { |
| 205 |
CHECK_MESSAGE( |
| 206 |
Rect2(0, 100, 1280, 720).grow(100).is_equal_approx(Rect2(-100, 0, 1480, 920)), |
| 207 |
"grow() with positive value should return the expected Rect2."); |
| 208 |
CHECK_MESSAGE( |
| 209 |
Rect2(0, 100, 1280, 720).grow(-100).is_equal_approx(Rect2(100, 200, 1080, 520)), |
| 210 |
"grow() with negative value should return the expected Rect2."); |
| 211 |
CHECK_MESSAGE( |
| 212 |
Rect2(0, 100, 1280, 720).grow(-4000).is_equal_approx(Rect2(4000, 4100, -6720, -7280)), |
| 213 |
"grow() with large negative value should return the expected Rect2."); |
| 214 |
|
| 215 |
CHECK_MESSAGE( |
| 216 |
Rect2(0, 100, 1280, 720).grow_individual(100, 200, 300, 400).is_equal_approx(Rect2(-100, -100, 1680, 1320)), |
| 217 |
"grow_individual() with positive values should return the expected Rect2."); |
| 218 |
CHECK_MESSAGE( |
| 219 |
Rect2(0, 100, 1280, 720).grow_individual(-100, 200, 300, -400).is_equal_approx(Rect2(100, -100, 1480, 520)), |
| 220 |
"grow_individual() with positive and negative values should return the expected Rect2."); |
| 221 |
|
| 222 |
CHECK_MESSAGE( |
| 223 |
Rect2(0, 100, 1280, 720).grow_side(SIDE_TOP, 500).is_equal_approx(Rect2(0, -400, 1280, 1220)), |
| 224 |
"grow_side() with positive value should return the expected Rect2."); |
| 225 |
CHECK_MESSAGE( |
| 226 |
Rect2(0, 100, 1280, 720).grow_side(SIDE_TOP, -500).is_equal_approx(Rect2(0, 600, 1280, 220)), |
| 227 |
"grow_side() with negative value should return the expected Rect2."); |
| 228 |
} |
| 229 |
|
| 230 |
TEST_CASE("[Rect2] Has point") { |
| 231 |
Rect2 rect = Rect2(0, 100, 1280, 720); |
| 232 |
CHECK_MESSAGE( |
| 233 |
rect.has_point(Vector2(500, 600)), |
| 234 |
"has_point() with contained Vector2 should return the expected result."); |
| 235 |
CHECK_MESSAGE( |
| 236 |
!rect.has_point(Vector2(0, 0)), |
| 237 |
"has_point() with non-contained Vector2 should return the expected result."); |
| 238 |
|
| 239 |
CHECK_MESSAGE( |
| 240 |
rect.has_point(rect.position), |
| 241 |
"has_point() with positive size should include `position`."); |
| 242 |
CHECK_MESSAGE( |
| 243 |
rect.has_point(rect.position + Vector2(1, 1)), |
| 244 |
"has_point() with positive size should include `position + (1, 1)`."); |
| 245 |
CHECK_MESSAGE( |
| 246 |
!rect.has_point(rect.position + Vector2(1, -1)), |
| 247 |
"has_point() with positive size should not include `position + (1, -1)`."); |
| 248 |
CHECK_MESSAGE( |
| 249 |
!rect.has_point(rect.position + rect.size), |
| 250 |
"has_point() with positive size should not include `position + size`."); |
| 251 |
CHECK_MESSAGE( |
| 252 |
!rect.has_point(rect.position + rect.size + Vector2(1, 1)), |
| 253 |
"has_point() with positive size should not include `position + size + (1, 1)`."); |
| 254 |
CHECK_MESSAGE( |
| 255 |
rect.has_point(rect.position + rect.size + Vector2(-1, -1)), |
| 256 |
"has_point() with positive size should include `position + size + (-1, -1)`."); |
| 257 |
CHECK_MESSAGE( |
| 258 |
!rect.has_point(rect.position + rect.size + Vector2(-1, 1)), |
| 259 |
"has_point() with positive size should not include `position + size + (-1, 1)`."); |
| 260 |
|
| 261 |
CHECK_MESSAGE( |
| 262 |
rect.has_point(rect.position + Vector2(0, 10)), |
| 263 |
"has_point() with point located on left edge should return true."); |
| 264 |
CHECK_MESSAGE( |
| 265 |
!rect.has_point(rect.position + Vector2(rect.size.x, 10)), |
| 266 |
"has_point() with point located on right edge should return false."); |
| 267 |
CHECK_MESSAGE( |
| 268 |
rect.has_point(rect.position + Vector2(10, 0)), |
| 269 |
"has_point() with point located on top edge should return true."); |
| 270 |
CHECK_MESSAGE( |
| 271 |
!rect.has_point(rect.position + Vector2(10, rect.size.y)), |
| 272 |
"has_point() with point located on bottom edge should return false."); |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
rect = Rect2(-4000, -200, 1280, 720); |
| 287 |
CHECK_MESSAGE( |
| 288 |
rect.has_point(rect.position + Vector2(0, 10)), |
| 289 |
"has_point() with negative position and point located on left edge should return true."); |
| 290 |
CHECK_MESSAGE( |
| 291 |
!rect.has_point(rect.position + Vector2(rect.size.x, 10)), |
| 292 |
"has_point() with negative position and point located on right edge should return false."); |
| 293 |
CHECK_MESSAGE( |
| 294 |
rect.has_point(rect.position + Vector2(10, 0)), |
| 295 |
"has_point() with negative position and point located on top edge should return true."); |
| 296 |
CHECK_MESSAGE( |
| 297 |
!rect.has_point(rect.position + Vector2(10, rect.size.y)), |
| 298 |
"has_point() with negative position and point located on bottom edge should return false."); |
| 299 |
} |
| 300 |
|
| 301 |
TEST_CASE("[Rect2] Intersection") { |
| 302 |
CHECK_MESSAGE( |
| 303 |
Rect2(0, 100, 1280, 720).intersects(Rect2(0, 300, 100, 100)), |
| 304 |
"intersects() with fully enclosed Rect2 should return the expected result."); |
| 305 |
CHECK_MESSAGE( |
| 306 |
Rect2(0, 100, 1280, 720).intersects(Rect2(1200, 700, 100, 100)), |
| 307 |
"intersects() with partially enclosed Rect2 should return the expected result."); |
| 308 |
CHECK_MESSAGE( |
| 309 |
!Rect2(0, 100, 1280, 720).intersects(Rect2(-4000, -4000, 100, 100)), |
| 310 |
"intersects() with non-enclosed Rect2 should return the expected result."); |
| 311 |
} |
| 312 |
|
| 313 |
TEST_CASE("[Rect2] Merging") { |
| 314 |
CHECK_MESSAGE( |
| 315 |
Rect2(0, 100, 1280, 720).merge(Rect2(0, 300, 100, 100)).is_equal_approx(Rect2(0, 100, 1280, 720)), |
| 316 |
"merge() with fully enclosed Rect2 should return the expected result."); |
| 317 |
CHECK_MESSAGE( |
| 318 |
Rect2(0, 100, 1280, 720).merge(Rect2(1200, 700, 100, 100)).is_equal_approx(Rect2(0, 100, 1300, 720)), |
| 319 |
"merge() with partially enclosed Rect2 should return the expected result."); |
| 320 |
CHECK_MESSAGE( |
| 321 |
Rect2(0, 100, 1280, 720).merge(Rect2(-4000, -4000, 100, 100)).is_equal_approx(Rect2(-4000, -4000, 5280, 4820)), |
| 322 |
"merge() with non-enclosed Rect2 should return the expected result."); |
| 323 |
} |
| 324 |
|
| 325 |
TEST_CASE("[Rect2] Finite number checks") { |
| 326 |
constexpr Vector2 x(0, 1); |
| 327 |
constexpr Vector2 infinite(Math::NaN, Math::NaN); |
| 328 |
|
| 329 |
CHECK_MESSAGE( |
| 330 |
Rect2(x, x).is_finite(), |
| 331 |
"Rect2 with all components finite should be finite"); |
| 332 |
|
| 333 |
CHECK_FALSE_MESSAGE( |
| 334 |
Rect2(infinite, x).is_finite(), |
| 335 |
"Rect2 with one component infinite should not be finite."); |
| 336 |
CHECK_FALSE_MESSAGE( |
| 337 |
Rect2(x, infinite).is_finite(), |
| 338 |
"Rect2 with one component infinite should not be finite."); |
| 339 |
|
| 340 |
CHECK_FALSE_MESSAGE( |
| 341 |
Rect2(infinite, infinite).is_finite(), |
| 342 |
"Rect2 with two components infinite should not be finite."); |
| 343 |
} |
| 344 |
|
| 345 |
} |