| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestRectangles" -*- |
| 2 |
{ |
| 3 |
Copyright 2013-2021 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Test CastleRectangles unit. } |
| 18 |
unit TestCastleRectangles; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses |
| 23 |
Classes, SysUtils, |
| 24 |
CastleTester, CastleRectangles; |
| 25 |
|
| 26 |
type |
| 27 |
TTestRectangles = class(TCastleTestCase) |
| 28 |
published |
| 29 |
procedure TestRectangles; |
| 30 |
procedure TestScaleEmpty; |
| 31 |
procedure TestCollidesDisc; |
| 32 |
procedure TestAdd; |
| 33 |
// procedure TestRightTop; |
| 34 |
procedure TestAlign; |
| 35 |
end; |
| 36 |
|
| 37 |
implementation |
| 38 |
|
| 39 |
uses CastleVectors; |
| 40 |
|
| 41 |
procedure TTestRectangles.TestRectangles; |
| 42 |
var |
| 43 |
R: TRectangle; |
| 44 |
begin |
| 45 |
R := Rectangle(-100, -200, 20, 30); |
| 46 |
AssertTrue(R.Contains(-100, -200)); |
| 47 |
AssertTrue(R.Contains(-100 + 19, -200 + 29)); |
| 48 |
AssertTrue(not R.Contains(-100 + 20, -200 + 30)); |
| 49 |
|
| 50 |
R.Grow(5); |
| 51 |
AssertEquals(20, R.Width); |
| 52 |
R := R.Grow(5); |
| 53 |
AssertEquals( 30, R.Width ); |
| 54 |
AssertEquals( 40, R.Height); |
| 55 |
AssertEquals(-105, R.Left ); |
| 56 |
AssertEquals(-205, R.Bottom); |
| 57 |
R := R.Grow(-5); |
| 58 |
AssertEquals( 20, R.Width ); |
| 59 |
AssertEquals( 30, R.Height); |
| 60 |
AssertEquals(-100, R.Left ); |
| 61 |
AssertEquals(-200, R.Bottom); |
| 62 |
R := R.Grow(-10); |
| 63 |
AssertEquals( 0, R.Width ); |
| 64 |
AssertEquals( 10, R.Height); |
| 65 |
AssertEquals( -90, R.Left ); |
| 66 |
AssertEquals(-190, R.Bottom); |
| 67 |
AssertFalse(R.Contains(-100, -200)); |
| 68 |
AssertFalse(R.Contains(-100 + 19, -200 + 29)); |
| 69 |
AssertFalse(R.Contains(-100 + 20, -200 + 30)); |
| 70 |
end; |
| 71 |
|
| 72 |
procedure TTestRectangles.TestScaleEmpty; |
| 73 |
var |
| 74 |
R: TRectangle; |
| 75 |
begin |
| 76 |
R := Rectangle(10, 20, 0, 50); |
| 77 |
AssertEquals(0, R.ScaleWidthAround0(2)); |
| 78 |
AssertEquals(100, R.ScaleHeightAround0(2)); // correctly scaled, even though R.Width = 0 |
| 79 |
AssertEquals(0, R.ScaleAround0(2).Width); |
| 80 |
AssertEquals(100, R.ScaleAround0(2).Height); // correctly scaled, even though R.Width = 0 |
| 81 |
AssertEquals(10, R.ScaleAround0(2).Left); // untouched by ScaleAround0, since R.Width = 0 |
| 82 |
AssertEquals(10, R.ScaleAround0(123).Left); // untouched by ScaleAround0, since R.Width = 0 |
| 83 |
AssertEquals(40, R.ScaleAround0(2).Bottom); // correctly scaled, even though R.Width = 0 |
| 84 |
|
| 85 |
// analogous to above test, but swap Width and Height |
| 86 |
R := Rectangle(20, 10, 50, 0); |
| 87 |
AssertEquals(100, R.ScaleWidthAround0(2)); // correctly scaled, even though R.Height = 0 |
| 88 |
AssertEquals(0, R.ScaleHeightAround0(2)); |
| 89 |
AssertEquals(100, R.ScaleAround0(2).Width); // correctly scaled, even though R.Height = 0 |
| 90 |
AssertEquals(0, R.ScaleAround0(2).Height); |
| 91 |
AssertEquals(10, R.ScaleAround0(2).Bottom); // untouched by ScaleAround0, since R.Height = 0 |
| 92 |
AssertEquals(10, R.ScaleAround0(123).Bottom); // untouched by ScaleAround0, since R.Height = 0 |
| 93 |
AssertEquals(40, R.ScaleAround0(2).Left); // correctly scaled, even though R.Height = 0 |
| 94 |
|
| 95 |
R := Rectangle(10, 20, 0, 50); |
| 96 |
AssertEquals(0, R.ScaleAroundCenter(2).Width); |
| 97 |
AssertEquals(100, R.ScaleAroundCenter(2).Height); // correctly scaled, even though R.Width = 0 |
| 98 |
AssertEquals(10, R.ScaleAroundCenter(2).Left); // untouched by ScaleAroundCenter, since R.Width = 0 |
| 99 |
AssertEquals(10, R.ScaleAroundCenter(123).Left); // untouched by ScaleAroundCenter, since R.Width = 0 |
| 100 |
AssertEquals(-5, R.ScaleAroundCenter(2).Bottom); // correctly scaled, even though R.Width = 0 |
| 101 |
end; |
| 102 |
|
| 103 |
procedure TTestRectangles.TestCollidesDisc; |
| 104 |
var |
| 105 |
R: TFloatRectangle; |
| 106 |
begin |
| 107 |
R := FloatRectangle(10, 20, 30, 40); |
| 108 |
// left = 10, right = 40, bottom = 20, top = 60 |
| 109 |
|
| 110 |
{ circles far outside } |
| 111 |
|
| 112 |
AssertFalse(R.CollidesDisc(Vector2(0 , 0), 1)); |
| 113 |
AssertFalse(R.CollidesDisc(Vector2(20, 0), 1)); |
| 114 |
AssertFalse(R.CollidesDisc(Vector2(50, 0), 1)); |
| 115 |
|
| 116 |
AssertFalse(R.CollidesDisc(Vector2(0, 100), 1)); |
| 117 |
AssertFalse(R.CollidesDisc(Vector2(20, 100), 1)); |
| 118 |
AssertFalse(R.CollidesDisc(Vector2(50, 100), 1)); |
| 119 |
|
| 120 |
AssertFalse(R.CollidesDisc(Vector2(0, 10), 1)); |
| 121 |
AssertFalse(R.CollidesDisc(Vector2(0, 40), 1)); |
| 122 |
AssertFalse(R.CollidesDisc(Vector2(0, 70), 1)); |
| 123 |
|
| 124 |
AssertFalse(R.CollidesDisc(Vector2(100, 10), 1)); |
| 125 |
AssertFalse(R.CollidesDisc(Vector2(100, 40), 1)); |
| 126 |
AssertFalse(R.CollidesDisc(Vector2(100, 70), 1)); |
| 127 |
|
| 128 |
{ circles collide, when one range inside } |
| 129 |
|
| 130 |
AssertFalse(R.CollidesDisc(Vector2(-10, 10), 15)); |
| 131 |
AssertTrue(R.CollidesDisc(Vector2(20, 10), 15)); |
| 132 |
AssertFalse(R.CollidesDisc(Vector2(60, 10), 15)); |
| 133 |
|
| 134 |
AssertFalse(R.CollidesDisc(Vector2(-10, 70), 15)); |
| 135 |
AssertTrue(R.CollidesDisc(Vector2(20, 70), 15)); |
| 136 |
AssertFalse(R.CollidesDisc(Vector2(60, 70), 15)); |
| 137 |
|
| 138 |
AssertFalse(R.CollidesDisc(Vector2(0, 0), 15)); |
| 139 |
AssertTrue(R.CollidesDisc(Vector2(0, 40), 15)); |
| 140 |
AssertFalse(R.CollidesDisc(Vector2(0, 80), 15)); |
| 141 |
|
| 142 |
AssertFalse(R.CollidesDisc(Vector2(50, 0), 15)); |
| 143 |
AssertTrue(R.CollidesDisc(Vector2(50, 40), 15)); |
| 144 |
AssertFalse(R.CollidesDisc(Vector2(50, 80), 15)); |
| 145 |
|
| 146 |
{ circles collide, both ranges inside } |
| 147 |
|
| 148 |
AssertTrue(R.CollidesDisc(Vector2(20, 40), 1)); |
| 149 |
|
| 150 |
R := FloatRectangle(0, 0, 10, 10); |
| 151 |
|
| 152 |
AssertFalse(R.CollidesDisc(Vector2(-1, -1), 0.9)); |
| 153 |
AssertFalse(R.CollidesDisc(Vector2(-1, 5), 0.9)); |
| 154 |
AssertFalse(R.CollidesDisc(Vector2(-1, 11), 0.9)); |
| 155 |
|
| 156 |
AssertFalse(R.CollidesDisc(Vector2(-1, -1), 1.1)); |
| 157 |
AssertTrue(R.CollidesDisc(Vector2(-1, 5), 1.1)); |
| 158 |
AssertFalse(R.CollidesDisc(Vector2(-1, 11), 1.1)); |
| 159 |
end; |
| 160 |
|
| 161 |
procedure TTestRectangles.TestAdd; |
| 162 |
var |
| 163 |
R: TFloatRectangle; |
| 164 |
begin |
| 165 |
R := TFloatRectangle.Empty; |
| 166 |
|
| 167 |
R.Include(Vector2(10, 20)); // without assignment, R.Add does nothing |
| 168 |
AssertTrue(R.IsEmpty); |
| 169 |
|
| 170 |
R := R.Include(Vector2(10, 20)); |
| 171 |
AssertFalse(R.IsEmpty); |
| 172 |
AssertSameValue(10, R.Left); |
| 173 |
AssertSameValue(20, R.Bottom); |
| 174 |
AssertSameValue(0, R.Width); |
| 175 |
AssertSameValue(0, R.Height); |
| 176 |
|
| 177 |
R.Include(Vector2(0, 40)); // without assignment, R.Add does nothing |
| 178 |
AssertSameValue(10, R.Left); |
| 179 |
AssertSameValue(20, R.Bottom); |
| 180 |
AssertSameValue(0, R.Width); |
| 181 |
AssertSameValue(0, R.Height); |
| 182 |
|
| 183 |
R := R.Include(Vector2(0, 40)); |
| 184 |
AssertSameValue(0, R.Left); |
| 185 |
AssertSameValue(20, R.Bottom); |
| 186 |
AssertSameValue(10, R.Width); |
| 187 |
AssertSameValue(20, R.Height); |
| 188 |
|
| 189 |
R := R.Include(Vector2(5, 30)); // does not change R, since already inside |
| 190 |
AssertSameValue(0, R.Left); |
| 191 |
AssertSameValue(20, R.Bottom); |
| 192 |
AssertSameValue(10, R.Width); |
| 193 |
AssertSameValue(20, R.Height); |
| 194 |
|
| 195 |
R := R.Include(Vector2(-10, 30)); // changes R only horizontally |
| 196 |
AssertSameValue(-10, R.Left); |
| 197 |
AssertSameValue(20, R.Bottom); |
| 198 |
AssertSameValue(20, R.Width); |
| 199 |
AssertSameValue(20, R.Height); |
| 200 |
|
| 201 |
R := R.Include(Vector2(5, -50)); // changes R only vertically |
| 202 |
AssertSameValue(-10, R.Left); |
| 203 |
AssertSameValue(-50, R.Bottom); |
| 204 |
AssertSameValue(20, R.Width); |
| 205 |
AssertSameValue(90, R.Height); |
| 206 |
|
| 207 |
R := R.Include(Vector2(5, -25)); // should not change R |
| 208 |
AssertSameValue(-10, R.Left); |
| 209 |
AssertSameValue(-50, R.Bottom); |
| 210 |
AssertSameValue(20, R.Width); |
| 211 |
AssertSameValue(90, R.Height); |
| 212 |
end; |
| 213 |
|
| 214 |
{ |
| 215 |
procedure TTestRectangles.TestRightTop; |
| 216 |
var |
| 217 |
RInt: TRectangle; |
| 218 |
R: TFloatRectangle; |
| 219 |
begin |
| 220 |
RInt.Left := 10; |
| 221 |
RInt.Bottom := 50; |
| 222 |
RInt.Right := 100; |
| 223 |
RInt.Top := 1000; |
| 224 |
AssertEquals(90, RInt.Width); |
| 225 |
AssertEquals(950, RInt.Height); |
| 226 |
AssertEquals(10, RInt.Left); |
| 227 |
AssertEquals(50, RInt.Bottom); |
| 228 |
AssertEquals(100, RInt.Right); |
| 229 |
AssertEquals(1000, RInt.Top); |
| 230 |
|
| 231 |
R.Left := 10; |
| 232 |
R.Bottom := 50; |
| 233 |
R.Right := 100; |
| 234 |
R.Top := 1000; |
| 235 |
AssertSameValue(90, R.Width); |
| 236 |
AssertSameValue(950, R.Height); |
| 237 |
AssertSameValue(10, R.Left); |
| 238 |
AssertSameValue(50, R.Bottom); |
| 239 |
AssertSameValue(100, R.Right); |
| 240 |
AssertSameValue(1000, R.Top); |
| 241 |
end; |
| 242 |
} |
| 243 |
|
| 244 |
procedure TTestRectangles.TestAlign; |
| 245 |
var |
| 246 |
WindowRect, ImageRect, TextRect: TRectangle; |
| 247 |
UIScale: Single; |
| 248 |
begin |
| 249 |
WindowRect := Rectangle(0, 0, 1920, 1080); |
| 250 |
ImageRect := Rectangle(0, 0, 237, 50); |
| 251 |
UIScale := 1.2; |
| 252 |
TextRect := ImageRect. |
| 253 |
ScaleAroundCenter(UIScale). |
| 254 |
Align(hpMiddle, WindowRect, hpMiddle). |
| 255 |
Align(vpMiddle, WindowRect, vpMiddle); |
| 256 |
// Writeln(TextRect.ToString); // 818x510 284x60 |
| 257 |
AssertEquals(818, TextRect.Left); |
| 258 |
AssertEquals(510, TextRect.Bottom); |
| 259 |
AssertEquals(284, TextRect.Width); |
| 260 |
AssertEquals(60, TextRect.Height); |
| 261 |
end; |
| 262 |
|
| 263 |
initialization |
| 264 |
RegisterTest(TTestRectangles); |
| 265 |
end. |