| 1 |
{ |
| 2 |
Copyright 2015-2021 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Useful assertions for CGE types. |
| 17 |
Adjusted from tests/code/common/castletestcase.pas (which is only for FPC now). |
| 18 |
|
| 19 |
All these routines fail with exception when the condition is not satisfied. |
| 20 |
They are @italic(not) based on built-in Assert routine, and are not affected |
| 21 |
by the $assertions on/off compiler mode. They will check (and raise exception |
| 22 |
if needed) regardless of the $assertions on/off compiler mode. |
| 23 |
} |
| 24 |
unit CastleAssertions; |
| 25 |
|
| 26 |
interface |
| 27 |
|
| 28 |
uses CastleVectors, CastleRectangles, CastleBoxes; |
| 29 |
|
| 30 |
procedure AssertEquals(const Expected, Actual: Int64); overload; |
| 31 |
procedure AssertEquals(const Expected, Actual: String); overload; |
| 32 |
|
| 33 |
procedure AssertMatrixEquals(const Expected, Actual: TMatrix4; |
| 34 |
const Epsilon: Single); |
| 35 |
|
| 36 |
procedure AssertVectorEquals(const Expected, Actual: TVector2Byte); overload; |
| 37 |
procedure AssertVectorEquals(const Expected, Actual: TVector3Byte); overload; |
| 38 |
procedure AssertVectorEquals(const Expected, Actual: TVector4Byte); overload; |
| 39 |
|
| 40 |
procedure AssertVectorEquals(const Expected, Actual: TVector2); overload; |
| 41 |
procedure AssertVectorEquals(const Expected, Actual: TVector3); overload; |
| 42 |
procedure AssertVectorEquals(const Expected, Actual: TVector4); overload; |
| 43 |
procedure AssertVectorEquals(const Expected, Actual: TVector2; const Epsilon: Single); overload; |
| 44 |
procedure AssertVectorEquals(const Expected, Actual: TVector3; const Epsilon: Single); overload; |
| 45 |
procedure AssertVectorEquals(const Expected, Actual: TVector4; const Epsilon: Single); overload; |
| 46 |
|
| 47 |
procedure AssertSameValue(const Expected, Actual: Single); overload; |
| 48 |
procedure AssertSameValue(const Expected, Actual: Single; const Epsilon: Single); overload; |
| 49 |
|
| 50 |
{ TODO: Need to have different names to avoid FPC errors "duplicate ASM label", |
| 51 |
see https://bugs.freepascal.org/view.php?id=32188 } |
| 52 |
procedure AssertVectorEqualsDouble(const Expected, Actual: TVector2Double); overload; |
| 53 |
procedure AssertVectorEqualsDouble(const Expected, Actual: TVector3Double); overload; |
| 54 |
procedure AssertVectorEqualsDouble(const Expected, Actual: TVector4Double); overload; |
| 55 |
procedure AssertVectorEqualsDouble(const Expected, Actual: TVector2Double; const Epsilon: Single); overload; |
| 56 |
procedure AssertVectorEqualsDouble(const Expected, Actual: TVector3Double; const Epsilon: Single); overload; |
| 57 |
procedure AssertVectorEqualsDouble(const Expected, Actual: TVector4Double; const Epsilon: Single); overload; |
| 58 |
procedure AssertSameValue(const Expected, Actual: Double); overload; |
| 59 |
procedure AssertSameValue(const Expected, Actual: Double; const Epsilon: Double); overload; |
| 60 |
|
| 61 |
procedure AssertBoxesEqual(const Expected, Actual: TBox3D); overload; |
| 62 |
procedure AssertBoxesEqual(const Expected, Actual: TBox3D; const Epsilon: Double); overload; |
| 63 |
procedure AssertFilenamesEqual(const Expected, Actual: string); overload; |
| 64 |
//procedure AssertImagesEqual(const Expected, Actual: TRGBAlphaImage); overload; |
| 65 |
procedure AssertRectsEqual(const Expected, Actual: TRectangle); overload; |
| 66 |
procedure AssertRectsEqual(const Expected, Actual: TFloatRectangle); overload; |
| 67 |
|
| 68 |
implementation |
| 69 |
|
| 70 |
uses Math, SysUtils, |
| 71 |
CastleUtils; |
| 72 |
|
| 73 |
procedure Fail(const Message: String); |
| 74 |
begin |
| 75 |
raise Exception.Create(Message); |
| 76 |
end; |
| 77 |
|
| 78 |
procedure AssertEquals(const Expected, Actual: Int64); |
| 79 |
begin |
| 80 |
if Expected <> Actual then |
| 81 |
Fail(Format('Expected integer: %d, actual: %d', [ |
| 82 |
Expected, |
| 83 |
Actual |
| 84 |
])); |
| 85 |
end; |
| 86 |
|
| 87 |
procedure AssertEquals(const Expected, Actual: String); |
| 88 |
begin |
| 89 |
if Expected <> Actual then |
| 90 |
Fail(Format('Expected string: "%s", actual: "%s"', [ |
| 91 |
Expected, |
| 92 |
Actual |
| 93 |
])); |
| 94 |
end; |
| 95 |
|
| 96 |
procedure AssertMatrixEquals( |
| 97 |
const Expected, Actual: TMatrix4; const Epsilon: Single); |
| 98 |
var |
| 99 |
DifferenceEpsilon: Single; |
| 100 |
I, J: TMatrix4.TIndex; |
| 101 |
begin |
| 102 |
if not TMatrix4.Equals(Expected, Actual, Epsilon) then |
| 103 |
begin |
| 104 |
DifferenceEpsilon := 0; |
| 105 |
for I := 0 to 3 do |
| 106 |
for J := 0 to 3 do |
| 107 |
MaxVar(DifferenceEpsilon, Abs(Expected[I, J] - Actual[I, J])); |
| 108 |
|
| 109 |
Fail(Format('Matrices (TMatrix4) are not equal:' + LineEnding + |
| 110 |
' Expected:' + LineEnding + |
| 111 |
'%s' + LineEnding + |
| 112 |
' Actual:' + LineEnding + |
| 113 |
'%s' + LineEnding + |
| 114 |
' The epsilon to ignore the difference would need to be >= %.10f, but is %.10f', |
| 115 |
[Expected.ToRawString(' '), |
| 116 |
Actual.ToRawString(' '), |
| 117 |
DifferenceEpsilon, |
| 118 |
Epsilon |
| 119 |
])); |
| 120 |
end; |
| 121 |
end; |
| 122 |
|
| 123 |
procedure AssertVectorEquals( |
| 124 |
const Expected, Actual: TVector2Byte); |
| 125 |
begin |
| 126 |
if not TVector2Byte.Equals(Expected, Actual) then |
| 127 |
Fail(Format('Vectors (TVector2Byte) are not equal: expected: %s, actual: %s', |
| 128 |
[Expected.ToString, Actual.ToString])); |
| 129 |
end; |
| 130 |
|
| 131 |
procedure AssertVectorEquals( |
| 132 |
const Expected, Actual: TVector3Byte); |
| 133 |
begin |
| 134 |
if not TVector3Byte.Equals(Expected, Actual) then |
| 135 |
Fail(Format('Vectors (TVector3Byte) are not equal: expected: %s, actual: %s', |
| 136 |
[Expected.ToString, Actual.ToString])); |
| 137 |
end; |
| 138 |
|
| 139 |
procedure AssertVectorEquals( |
| 140 |
const Expected, Actual: TVector4Byte); |
| 141 |
begin |
| 142 |
if not TVector4Byte.Equals(Expected, Actual) then |
| 143 |
Fail(Format('Vectors (TVector4Byte) are not equal: expected: %s, actual: %s', |
| 144 |
[Expected.ToString, Actual.ToString])); |
| 145 |
end; |
| 146 |
|
| 147 |
procedure AssertVectorEquals( |
| 148 |
const Expected, Actual: TVector2); |
| 149 |
begin |
| 150 |
AssertVectorEquals(Expected, Actual, SingleEpsilon); |
| 151 |
end; |
| 152 |
|
| 153 |
procedure AssertVectorEquals( |
| 154 |
const Expected, Actual: TVector3); |
| 155 |
begin |
| 156 |
AssertVectorEquals(Expected, Actual, SingleEpsilon); |
| 157 |
end; |
| 158 |
|
| 159 |
procedure AssertVectorEquals( |
| 160 |
const Expected, Actual: TVector4); |
| 161 |
begin |
| 162 |
AssertVectorEquals(Expected, Actual, SingleEpsilon); |
| 163 |
end; |
| 164 |
|
| 165 |
procedure AssertVectorEquals( |
| 166 |
const Expected, Actual: TVector2; const Epsilon: Single); |
| 167 |
begin |
| 168 |
if not TVector2.Equals(Expected, Actual, Epsilon) then |
| 169 |
Fail(Format('Vectors (TVector2) are not equal: expected: %s, actual: %s', |
| 170 |
[Expected.ToRawString, Actual.ToRawString])); |
| 171 |
end; |
| 172 |
|
| 173 |
procedure AssertVectorEquals( |
| 174 |
const Expected, Actual: TVector3; const Epsilon: Single); |
| 175 |
begin |
| 176 |
if not TVector3.Equals(Expected, Actual, Epsilon) then |
| 177 |
Fail(Format('Vectors (TVector3) are not equal: expected: %s, actual: %s', |
| 178 |
[Expected.ToRawString, Actual.ToRawString])); |
| 179 |
end; |
| 180 |
|
| 181 |
procedure AssertVectorEquals( |
| 182 |
const Expected, Actual: TVector4; const Epsilon: Single); |
| 183 |
begin |
| 184 |
if not TVector4.Equals(Expected, Actual, Epsilon) then |
| 185 |
Fail(Format('Vectors (TVector4) are not equal: expected: %s, actual: %s', |
| 186 |
[Expected.ToRawString, Actual.ToRawString])); |
| 187 |
end; |
| 188 |
|
| 189 |
procedure AssertSameValue(const Expected, Actual: Single); |
| 190 |
begin |
| 191 |
AssertSameValue(Expected, Actual, SingleEpsilon); |
| 192 |
end; |
| 193 |
|
| 194 |
procedure AssertSameValue(const Expected, Actual: Single; |
| 195 |
const Epsilon: Single); |
| 196 |
begin |
| 197 |
if not SameValue(Expected, Actual, Epsilon) then |
| 198 |
Fail(Format('Floats (Single) are not equal: expected: %g, actual: %g', |
| 199 |
[Expected, Actual])); |
| 200 |
end; |
| 201 |
|
| 202 |
procedure AssertVectorEqualsDouble( |
| 203 |
const Expected, Actual: TVector2Double); |
| 204 |
begin |
| 205 |
AssertVectorEqualsDouble(Expected, Actual, DoubleEpsilon); |
| 206 |
end; |
| 207 |
|
| 208 |
procedure AssertVectorEqualsDouble( |
| 209 |
const Expected, Actual: TVector3Double); |
| 210 |
begin |
| 211 |
AssertVectorEqualsDouble(Expected, Actual, DoubleEpsilon); |
| 212 |
end; |
| 213 |
|
| 214 |
procedure AssertVectorEqualsDouble( |
| 215 |
const Expected, Actual: TVector4Double); |
| 216 |
begin |
| 217 |
AssertVectorEqualsDouble(Expected, Actual, DoubleEpsilon); |
| 218 |
end; |
| 219 |
|
| 220 |
procedure AssertVectorEqualsDouble( |
| 221 |
const Expected, Actual: TVector2Double; const Epsilon: Single); |
| 222 |
begin |
| 223 |
if not TVector2Double.Equals(Expected, Actual, Epsilon) then |
| 224 |
Fail(Format('Vectors (TVector2Double) are not equal: expected: %s, actual: %s', |
| 225 |
[Expected.ToRawString, Actual.ToRawString])); |
| 226 |
end; |
| 227 |
|
| 228 |
procedure AssertVectorEqualsDouble( |
| 229 |
const Expected, Actual: TVector3Double; const Epsilon: Single); |
| 230 |
begin |
| 231 |
if not TVector3Double.Equals(Expected, Actual, Epsilon) then |
| 232 |
Fail(Format('Vectors (TVector3Double) are not equal: expected: %s, actual: %s', |
| 233 |
[Expected.ToRawString, Actual.ToRawString])); |
| 234 |
end; |
| 235 |
|
| 236 |
procedure AssertVectorEqualsDouble( |
| 237 |
const Expected, Actual: TVector4Double; const Epsilon: Single); |
| 238 |
begin |
| 239 |
if not TVector4Double.Equals(Expected, Actual, Epsilon) then |
| 240 |
Fail(Format('Vectors (TVector4Double) are not equal: expected: %s, actual: %s', |
| 241 |
[Expected.ToRawString, Actual.ToRawString])); |
| 242 |
end; |
| 243 |
|
| 244 |
procedure AssertSameValue(const Expected, Actual: Double); |
| 245 |
begin |
| 246 |
AssertSameValue(Expected, Actual, DoubleEpsilon); |
| 247 |
end; |
| 248 |
|
| 249 |
procedure AssertSameValue(const Expected, Actual: Double; |
| 250 |
const Epsilon: Double); |
| 251 |
begin |
| 252 |
if not SameValue(Expected, Actual, Epsilon) then |
| 253 |
Fail(Format('Floats (Double) are not equal: expected: %g, actual: %g', |
| 254 |
[Expected, Actual])); |
| 255 |
end; |
| 256 |
|
| 257 |
procedure AssertBoxesEqual(const Expected, Actual: TBox3D); |
| 258 |
begin |
| 259 |
AssertBoxesEqual(Expected, Actual, SingleEpsilon); |
| 260 |
end; |
| 261 |
|
| 262 |
procedure AssertBoxesEqual(const Expected, Actual: TBox3D; |
| 263 |
const Epsilon: Double); |
| 264 |
var |
| 265 |
I: Integer; |
| 266 |
begin |
| 267 |
if Expected.IsEmpty and Actual.IsEmpty then |
| 268 |
Exit; // OK |
| 269 |
|
| 270 |
if Expected.IsEmpty then |
| 271 |
Fail(Format('Expected empty box, actual box is NOT empty (%s)', |
| 272 |
[Actual.ToRawString])); |
| 273 |
|
| 274 |
if Actual.IsEmpty then |
| 275 |
Fail(Format('Expected NOT empty box (%s), actual box is empty', |
| 276 |
[Expected.ToRawString])); |
| 277 |
|
| 278 |
for I := 0 to 2 do |
| 279 |
if (not SameValue(Expected.Data[0][I], Actual.Data[0][I], Epsilon)) or |
| 280 |
(not SameValue(Expected.Data[1][I], Actual.Data[1][I], Epsilon)) then |
| 281 |
Fail(Format('Boxes are not equal: expected: %s, actual: %s', |
| 282 |
[Expected.ToRawString, Actual.ToRawString])); |
| 283 |
end; |
| 284 |
|
| 285 |
procedure AssertFilenamesEqual(const Expected, Actual: string); |
| 286 |
begin |
| 287 |
if not SameFileName(Expected, Actual) then |
| 288 |
Fail(Format('Expected filename "%s", got "%s"', [Expected, Actual])); |
| 289 |
end; |
| 290 |
|
| 291 |
(* |
| 292 |
procedure AssertImagesEqual(const Expected, Actual: TRGBAlphaImage); |
| 293 |
var |
| 294 |
ExpectedPtr, ActualPtr: PVector4Byte; |
| 295 |
I: Integer; |
| 296 |
begin |
| 297 |
// Overloaded version with AErrorAddrs is missing for fpcunit AssertEquals |
| 298 |
AssertEquals(Expected.Width, Actual.Width{}); |
| 299 |
AssertEquals(Expected.Height, Actual.Height{}); |
| 300 |
AssertEquals(Expected.Depth, Actual.Depth{}); |
| 301 |
ExpectedPtr := Expected.Pixels; |
| 302 |
ActualPtr := Actual.Pixels; |
| 303 |
for I := 1 to Actual.Width * Actual.Height * Actual.Depth do |
| 304 |
begin |
| 305 |
AssertVectorEquals(ExpectedPtr^, ActualPtr^); |
| 306 |
Inc(ExpectedPtr); |
| 307 |
Inc(ActualPtr); |
| 308 |
end; |
| 309 |
end; |
| 310 |
*) |
| 311 |
|
| 312 |
procedure AssertRectsEqual(const Expected, Actual: TRectangle); |
| 313 |
begin |
| 314 |
if not Expected.Equals(Actual) then |
| 315 |
Fail(Format('Expected rect (%s) does not equal actual (%s)', |
| 316 |
[Expected.ToString, Actual.ToString])); |
| 317 |
end; |
| 318 |
|
| 319 |
procedure AssertRectsEqual(const Expected, Actual: TFloatRectangle); |
| 320 |
begin |
| 321 |
if not Expected.Equals(Actual) then |
| 322 |
Fail(Format('Expected rect (%s) does not equal actual (%s)', |
| 323 |
[Expected.ToString, Actual.ToString])); |
| 324 |
end; |
| 325 |
|
| 326 |
end. |