Debian Codegraph @

tests/code/tester-castle/castletester.pas

raw
1 {
2 Copyright 2022-2024 Andrzej Kilijański, Dean Zobec, Michael Van Canneyt, 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 { Running tests, compatible with fpcunit, using some code from fpcunit
17 (part of Free Component Library (FCL)) by Dean Zobec, Michael Van Canneyt.
18 See https://wiki.lazarus.freepascal.org/fpcunit for more info. }
19 unit CastleTester;
20
21 interface
22
23 // FPC: Do not warn that Rtti is experimental
24 {$ifdef FPC} {$warnings off} {$endif}
25
26 uses SysUtils, Classes, Generics.Collections, Rtti, CastleVectors, CastleBoxes,
27 CastleFrustum, CastleImages, CastleRectangles, CastleWindow, CastleViewport;
28
29 {$ifdef FPC} {$warnings on} {$endif}
30
31 const
32 { Epsilon used by default when compating Single (Single-precision float values).
33 Compatible with Math unit value, used by standard routines like Math.SameValue
34 and Math.IsZero. }
35 SingleEpsilon = 1E-4;
36
37 { Epsilon used by default when compating Double (Double-precision float values).
38 Compatible with Math unit value, used by standard routines like Math.SameValue
39 and Math.IsZero. }
40 DoubleEpsilon = 1E-12;
41
42 type
43
44 EAssertionFailedError = class(Exception);
45
46 TNotifyTestFail = procedure (const TestName, Msg: String) of object;
47 TNotifyTestExecuted = procedure (const Name: String) of object;
48 TNotifyTestCaseExecuted = procedure (const Name: String) of object;
49 TNotifyTestCountChanged = procedure (const TestCount: Integer) of object;
50
51 TCastleTester = class;
52
53 { Generate type info for TCastleTestCase and descendants.
54 Delphi expects the forward class declaration to have the same $M state
55 as an actual class declaration. }
56 {$M+}
57 TCastleTestCase = class;
58 {$M-}
59
60 TCastleTest = class
61 strict private
62 FTestCase: TCastleTestCase;
63 {$ifdef FPC}
64 FMethodPointer: CodePointer;
65 {$else}
66 FRttiMethod: TRttiMethod;
67 {$endif}
68 FEnabled: Boolean;
69
70 procedure SetEnabled(NewValue: Boolean);
71 public
72 Name: String;
73
74 constructor Create(const ATestCase: TCastleTestCase; const AName: String;
75 {$ifdef FPC}const AMethodPointer: CodePointer{$else}
76 const ARttiMethod: TRttiMethod{$endif});
77
78 procedure Run;
79 { Runs TestCase.Setup }
80 procedure Setup;
81 { Runs TestCase.TearDown }
82 procedure TearDown;
83
84 { Test case name with test name }
85 function GetFullName: String;
86
87 property Enabled: Boolean read FEnabled write SetEnabled;
88 end;
89
90 {$M+} // Generate type info for TCastleTestCase and descendants
91 TCastleTestCase = class
92 strict private
93 FName: String;
94 SavedApplicationMainWindow: TCastleWindow;
95
96 { Object list for tests }
97 FTestList: {$ifdef FPC}specialize{$endif} TObjectList<TCastleTest>;
98
99 function GetTest(const Index: Integer): TCastleTest;
100 private
101 FNotifyTestFail: TNotifyTestFail;
102 FCurrentTestName: String;
103
104 FWindowForTest: TCastleWindow;
105 { Viewport from Application.MainWindow in non-console mode,
106 or viewport from FWindowForViewportTest when Console Mode. }
107 FViewportForTest: TCastleViewport;
108 { Window for Viewport when tester in Console mode }
109 FWindowForViewportTest: TCastleWindow;
110 { TCastleTester that runs test case }
111 FCastleTester: TCastleTester;
112
113 function PrepareCustomMsg(const Msg: String): String;
114 protected
115 procedure Setup; virtual;
116 procedure TearDown; virtual;
117 public
118 Enabled: Boolean;
119
120 constructor Create;
121 destructor Destroy; override;
122
123 procedure Fail(const Msg: String; const AddrOfError: Pointer = nil);
124 procedure AssertTrue(const ACondition: Boolean); overload;
125 procedure AssertTrue(const Msg: String; const ACondition: Boolean;
126 AddrOfError: Pointer = nil); overload;
127 procedure AssertFalse(const ACondition: Boolean); overload;
128 procedure AssertFalse(const Msg: String; const ACondition: Boolean;
129 AddrOfError: Pointer = nil); overload;
130 procedure AssertFilenamesEqual(const Expected, Actual: String);
131 procedure AssertEquals(const Expected, Actual: String); overload;
132 procedure AssertEquals(const Expected, Actual: Boolean); overload;
133 procedure AssertEquals(const Expected, Actual: Single); overload;
134 procedure AssertEquals(const Expected, Actual: Integer); overload;
135 procedure AssertEquals(const Expected, Actual: Cardinal); overload;
136 procedure AssertEquals(const Expected, Actual: Int64); overload;
137 procedure AssertEquals(const Msg: String; const Expected, Actual: Integer;
138 AddrOfError: Pointer = nil); overload;
139 procedure AssertEquals(const Msg, Expected, Actual: String;
140 AddrOfError: Pointer = nil); overload;
141 procedure AssertEquals(const Msg: String; const Expected, Actual: Boolean;
142 AddrOfError: Pointer = nil); overload;
143 procedure AssertEquals(const Msg: String; const Expected, Actual: Cardinal;
144 AddrOfError: Pointer = nil); overload;
145 procedure AssertEquals(const Msg: String; const Expected, Actual: Int64;
146 AddrOfError: Pointer = nil); overload;
147
148 procedure AssertSameValue(const Expected, Actual: Single;
149 AddrOfError: Pointer = nil); overload;
150 procedure AssertSameValue(const Expected, Actual: Single;
151 const Epsilon: Single; AddrOfError: Pointer = nil); overload;
152
153 procedure AssertMatrixEquals(const Expected, Actual: TMatrix4;
154 const Epsilon: Single; AddrOfError: Pointer = nil); overload;
155 procedure AssertMatrixEquals(const Expected, Actual: TMatrix4;
156 AddrOfError: Pointer = nil); overload;
157
158 procedure AssertVectorEquals(const Expected, Actual: TVector2Byte;
159 AddrOfError: Pointer = nil); overload;
160 procedure AssertVectorEquals(const Expected, Actual: TVector3Byte;
161 AddrOfError: Pointer = nil); overload;
162 procedure AssertVectorEquals(const Expected, Actual: TVector4Byte;
163 AddrOfError: Pointer = nil); overload;
164
165 procedure AssertVectorEquals(const Expected, Actual: TVector2;
166 AddrOfError: Pointer = nil); overload;
167 procedure AssertVectorEquals(const Expected, Actual: TVector3;
168 AddrOfError: Pointer = nil); overload;
169 procedure AssertVectorEquals(const Expected, Actual: TVector4;
170 AddrOfError: Pointer = nil); overload;
171 procedure AssertVectorEquals(const Expected, Actual: TVector2;
172 const Epsilon: Single; AddrOfError: Pointer = nil); overload;
173 procedure AssertVectorEquals(const Expected, Actual: TVector3;
174 const Epsilon: Single; AddrOfError: Pointer = nil); overload;
175 procedure AssertVectorEquals(const Expected, Actual: TVector4;
176 const Epsilon: Single; AddrOfError: Pointer = nil); overload;
177
178 { Check that 3D planes (defined by equation Ax+By+Cz+D=0) are equal.
179 The vectors must be a component-wise multiplication of each other.
180
181 @param(CheckEqualDirection
182 The plane direction (ABC) must point in the same direction
183 (because our planes in practice often determine a half-space,
184 not only a plane, so their direction matters).
185 ) }
186 procedure AssertPlaneEquals(const Expected, Actual: TVector4;
187 const CheckEqualDirection: Boolean;
188 const Epsilon: Single; AddrOfError: Pointer = nil); overload;
189 procedure AssertPlaneEquals(const Expected, Actual: TVector4;
190 const CheckEqualDirection: Boolean;
191 AddrOfError: Pointer = nil); overload;
192
193 { TODO: Need to have different names to avoid FPC errors "duplicate ASM label",
194 see https://bugs.freepascal.org/view.php?id=32188 }
195 procedure AssertVectorEqualsDouble(const Expected, Actual: TVector2Double;
196 AddrOfError: Pointer = nil); overload;
197 procedure AssertVectorEqualsDouble(const Expected, Actual: TVector3Double;
198 AddrOfError: Pointer = nil); overload;
199 procedure AssertVectorEqualsDouble(const Expected, Actual: TVector4Double;
200 AddrOfError: Pointer = nil); overload;
201 procedure AssertVectorEqualsDouble(const Expected, Actual: TVector2Double;
202 const Epsilon: Single; AddrOfError: Pointer = nil); overload;
203 procedure AssertVectorEqualsDouble(const Expected, Actual: TVector3Double;
204 const Epsilon: Single; AddrOfError: Pointer = nil); overload;
205 procedure AssertVectorEqualsDouble(const Expected, Actual: TVector4Double;
206 const Epsilon: Single; AddrOfError: Pointer = nil); overload;
207 procedure AssertSameValue(const Expected, Actual: Double;
208 AddrOfError: Pointer = nil); overload;
209 procedure AssertSameValue(const Expected, Actual: Double;
210 const Epsilon: Double; AddrOfError: Pointer = nil); overload;
211
212 procedure AssertBoxesEqual(const Expected, Actual: TBox3D;
213 AddrOfError: Pointer = nil); overload;
214 procedure AssertBoxesEqual(const Expected, Actual: TBox3D;
215 const Epsilon: Double; AddrOfError: Pointer = nil); overload;
216 procedure AssertBoxesEqual(const Msg: String; const Expected, Actual: TBox3D;
217 AddrOfError: Pointer = nil); overload;
218 procedure AssertBoxesEqual(const Msg: String; const Expected, Actual: TBox3D;
219 const Epsilon: Double; AddrOfError: Pointer = nil); overload;
220
221 procedure AssertImagesEqual(const Expected, Actual: TCastleImage;
222 AddrOfError: Pointer = nil);
223
224 procedure AssertRectsEqual(const Expected, Actual: TRectangle;
225 AddrOfError: Pointer = nil); overload;
226 procedure AssertRectsEqual(const Expected, Actual: TFloatRectangle;
227 AddrOfError: Pointer = nil); overload;
228
229 procedure AssertFrustumNormalized(const F: TFrustum);
230
231 procedure AssertFrustumEquals(const Expected, Actual: TFrustum;
232 const Epsilon: Single; AddrOfError: Pointer = nil); overload;
233 procedure AssertFrustumEquals(const Expected, Actual: TFrustum;
234 AddrOfError: Pointer = nil); overload;
235
236 function CompareFileName(Expected, Actual: String): Boolean;
237
238 { Get temporary directory, implementation that works for both Delphi and FPC. }
239 function GetTempDirectory: String;
240
241 procedure TestLog(Text: String);
242
243 procedure OnWarningRaiseException(const Category, S: string);
244
245 { Create TCastleWindow for test purposes.
246
247 Only one such window may exist at given time,
248 it is also automatically set as Application.MainWindow. }
249 function CreateWindowForTest: TCastleWindow;
250
251 { Destroy TCastleWindow created by CreateWindowForTest.
252 This method also checks Window value and sets it to @nil.
253
254 It is often not necessary to actually use it,
255 because the window will be automatically freed when test method ends anyway. }
256 procedure DestroyWindowForTest(var Window: TCastleWindow);
257
258 { If you need a TCastleViewport for testing, you can use this one.
259 This viewport is automatically cleaned when test method ends. }
260 //function GetTestingViewport: TCastleViewport;
261
262 { Used by TCastleTester.Scan to add tests }
263 function AddTest(const AName: String;
264 {$ifdef FPC}const AMethodPointer: CodePointer{$else}
265 const ARttiMethod: TRttiMethod{$endif}): TCastleTest;
266
267 { Does the current platform allow to create new TCastleWindow during tests.
268
269 In case you do manually TCastleWindow.Create call,
270 you should also honour this method, do not create TCastleWindow instance
271 when this is @false. Abort the test (without any failure) in this case.
272
273 All test windows should be created using CreateWindowForTest now,
274 and CreateWindowForTest will actually raise exception if this is true.
275
276 This is @false on mobile or when run with --no-window-create . }
277 function CanCreateWindowForTest: Boolean;
278
279 { Clears test list }
280 procedure ClearTests;
281
282 function TestCount: Integer;
283 function EnabledTestCount: Integer;
284
285 property Test[const Index: Integer]: TCastleTest read GetTest;
286
287 property CurrentTestName: String read FCurrentTestName;
288
289 published
290
291 end;
292 {$M-}
293
294 TCastleTestCaseClass = class of TCastleTestCase;
295
296 TCastleTester = class (TComponent)
297 strict private
298 { Rtti context to run all test methods }
299 FRttiContext: TRttiContext;
300
301 { Object list for test cases }
302 FTestCaseList: {$ifdef FPC}specialize{$endif} TObjectList<TCastleTestCase>;
303
304 { Object list for test that should be executed }
305 FTestsToRun: {$ifdef FPC}specialize{$endif} TList<TCastleTest>;
306 FLastRunningTestIndex: Integer;
307
308 { Test app window when tester runs in UI mode or nil in console mode }
309 FUIWindow: TCastleWindow;
310
311 { Flag to stop on first test fail }
312 FStopOnFirstFail: Boolean;
313
314 FTestsCount: Integer;
315 FTestPassedCount: Integer;
316 FTestFailedCount: Integer;
317
318 procedure SetNotifyTestFail(const ANotifyTestFail: TNotifyTestFail);
319
320 { Scans test case using RTTI }
321 procedure ScanTestCase(TestCase: TCastleTestCase);
322
323 procedure SetTestCount(const NewTestCount: Integer);
324
325 procedure RunTest(Test: TCastleTest);
326
327 procedure SetTestPassedCount(const NewTestCount: Integer);
328 procedure SetTestFailedCount(const NewTestCount: Integer);
329
330 private
331 { Callbacks to change UI }
332 FNotifyTestExecuted: TNotifyTestExecuted;
333 FNotifyTestCaseExecuted: TNotifyTestCaseExecuted;
334 FNotifyTestFail: TNotifyTestFail;
335 FNotifyTestCountChanged: TNotifyTestCountChanged;
336 FNotifyTestPassedChanged: TNotifyTestCountChanged;
337 FNotifyTestFailedChanged: TNotifyTestCountChanged;
338 FNotifyEnabledTestCountChanged: TNotifyEvent;
339
340 public
341 constructor Create(AOwner: TComponent); override;
342 destructor Destroy; override;
343
344 { Adds testcase to tester }
345 procedure AddTestCase(const TestCase: TCastleTestCase);
346 { Adds registered testcases to tester }
347 procedure AddRegisteredTestCases;
348
349 { Scans added test cases }
350 procedure Scan;
351
352 { Prepares list of tests to run - choosen test case or all for '' }
353 procedure PrepareTestListToRun(const ATestCaseName: String = '');
354
355 { Running all test in one loop - good for console because blocks UI }
356 procedure Run;
357
358 { Returns true when there is next test to run }
359 function IsNextTestToRun: Boolean;
360 { Runs next test, good for UI apps, can raise exceptions }
361 procedure RunNextTest;
362
363 function EnabledTestCount: Integer;
364
365 { Stop testing on first fail or run all tests.
366
367 This also means that unhandled exception is just "let through",
368 which is nice to let FPC print backtrace of exception to console,
369 if outside code will just let unhandled exception to break the program.
370
371 Default @true is suitable for console version. }
372 property StopOnFirstFail: Boolean read FStopOnFirstFail
373 write FStopOnFirstFail default true;
374
375 property TestsCount: Integer read FTestsCount;
376 property TestPassedCount: Integer read FTestPassedCount
377 write SetTestPassedCount;
378 property TestFailedCount: Integer read FTestFailedCount
379 write SetTestFailedCount;
380
381 property NotifyTestExecuted: TNotifyTestExecuted read FNotifyTestExecuted
382 write FNotifyTestExecuted;
383
384 property NotifyTestCaseExecuted: TNotifyTestCaseExecuted
385 read FNotifyTestCaseExecuted write FNotifyTestCaseExecuted;
386
387 { Callback after test fail }
388 property NotifyTestFail: TNotifyTestFail read FNotifyTestFail
389 write SetNotifyTestFail;
390
391 { Callback after test count changed }
392 property NotifyTestCountChanged: TNotifyTestCountChanged
393 read FNotifyTestCountChanged write FNotifyTestCountChanged;
394
395 { Callback after passed test count changed }
396 property NotifyTestPassedChanged: TNotifyTestCountChanged
397 read FNotifyTestPassedChanged write FNotifyTestPassedChanged;
398
399 { Callback after failed test count changed }
400 property NotifyTestFailedChanged: TNotifyTestCountChanged
401 read FNotifyTestFailedChanged write FNotifyTestFailedChanged;
402
403 { Callback after enabled test count changed }
404 property NotifyEnabledTestCountChanged: TNotifyEvent
405 read FNotifyEnabledTestCountChanged write FNotifyEnabledTestCountChanged;
406
407 { Set Enabled of all tests, to reflect whether they match Filter.
408 Filter can use wildcards (* and ?) and is compared with
409 the test GetFullName.
410 See ../../README.md for detailed description and examples. }
411 procedure EnableFilter(const Filter: String);
412 end;
413
414 procedure RegisterTest(CastleTestCaseClass: TCastleTestCaseClass);
415
416 { Like CompareMem, but slower,
417 and when the memory is different, log the difference:
418 position and the 2 different bytes.
419
420 Note: Size and I are Integer, not Int64.
421 This is good enough for current purposes, and it's easier for implementation
422 (iterating with In64 doesn't compile with FPC 3.2.2 on Linux/Arm (32-bit)
423 now (Raspberry Pi).) }
424 function CompareMemDebug(const P1, P2: Pointer; const Size: Integer): Boolean;
425
426 implementation
427
428 uses TypInfo, Math, {$ifdef FPC}testutils,{$else}IOUtils,{$endif} StrUtils,
429 CastleLog, CastleUtils, CastleStringUtils, CastleTesterParameters;
430
431 { routines ------------------------------------------------------------------- }
432
433 var
434 FRegisteredTestCaseList: {$ifdef FPC}specialize{$endif} TList<TCastleTestCaseClass>;
435
436 procedure RegisterTest(CastleTestCaseClass: TCastleTestCaseClass);
437 begin
438 FRegisteredTestCaseList.Add(CastleTestCaseClass);
439 end;
440
441 function CompareMemDebug(const P1, P2: Pointer; const Size: Integer): Boolean;
442 var
443 I: Integer;
444 P1B, P2B: PByte;
445 begin
446 Result := true;
447 P1B := P1;
448 P2B := P2;
449 for I := 0 to Size - 1 do
450 begin
451 if P1B^ <> P2B^ then
452 begin
453 WritelnLog('Difference at %d: %d <> %d', [I, P1B^, P2B^]);
454 Exit(false);
455 end;
456 Inc(P1B);
457 Inc(P2B);
458 end;
459 end;
460
461 { TCastleTester -------------------------------------------------------------- }
462
463 procedure TCastleTester.AddRegisteredTestCases;
464 var
465 I: Integer;
466 begin
467 for I := 0 to FRegisteredTestCaseList.Count - 1 do
468 begin
469 AddTestCase(FRegisteredTestCaseList[I].Create);
470 end;
471 end;
472
473 procedure TCastleTester.AddTestCase(const TestCase: TCastleTestCase);
474 begin
475 FTestCaseList.Add(TestCase);
476 TestCase.FNotifyTestFail := FNotifyTestFail;
477 TestCase.FCastleTester := Self;
478 end;
479
480 constructor TCastleTester.Create(AOwner: TComponent);
481 begin
482 inherited;
483
484 StopOnFirstFail := true;
485 FRttiContext := TRttiContext.Create;
486 FTestCaseList := {$ifdef FPC}specialize{$endif} TObjectList<TCastleTestCase>.Create;
487 FTestsToRun := {$ifdef FPC}specialize{$endif} TList<TCastleTest>.Create;
488 end;
489
490 destructor TCastleTester.Destroy;
491 begin
492 FreeAndNil(FTestsToRun);
493 FreeAndNil(FTestCaseList);
494 FRttiContext.Free;
495 inherited;
496 end;
497
498 function TCastleTester.IsNextTestToRun: Boolean;
499 begin
500 Result := (FLastRunningTestIndex < FTestsToRun.Count);
501 end;
502
503 procedure TCastleTester.PrepareTestListToRun(const ATestCaseName: String);
504 var
505 I, J: Integer;
506 TestCase: TCastleTestCase;
507 Test: TCastleTest;
508 begin
509 FLastRunningTestIndex := 0;
510 FUIWindow := Application.MainWindow;
511 FTestsToRun.Clear;
512
513 for I := 0 to FTestCaseList.Count -1 do
514 begin
515 TestCase := FTestCaseList[I];
516
517 if ATestCaseName <> '' then
518 if UpperCase(TestCase.ClassName) <> UpperCase(ATestCaseName) then
519 continue;
520
521 if TestCase.Enabled then
522 begin
523 for J := 0 to TestCase.TestCount -1 do
524 begin
525 Test := TestCase.Test[J];
526 if Test.Enabled then
527 FTestsToRun.Add(Test);
528 end;
529 end;
530 end;
531
532 TestPassedCount := 0;
533 TestFailedCount := 0;
534 if Assigned(FNotifyEnabledTestCountChanged) then
535 NotifyEnabledTestCountChanged(Self);
536 end;
537
538 procedure TCastleTester.Run;
539 var
540 I: Integer;
541 begin
542 FUIWindow := Application.MainWindow;
543 FLastRunningTestIndex := 0;
544
545 for I := 0 to FTestsToRun.Count -1 do
546 RunTest(FTestsToRun[I]);
547 end;
548
549 procedure TCastleTester.RunNextTest;
550 begin
551 RunTest(FTestsToRun[FLastRunningTestIndex]);
552 Inc(FLastRunningTestIndex);
553 end;
554
555 procedure TCastleTester.RunTest(Test: TCastleTest);
556 var
557 FailMsg: String;
558 begin
559 // raise Exception.Create('Test Return Adress') at {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
560 if not Test.Enabled then
561 Exit;
562
563 if Assigned(FNotifyTestCaseExecuted) then
564 FNotifyTestCaseExecuted(Test.GetFullName);
565
566 try
567 Test.Setup;
568 try
569 Test.Run;
570 TestPassedCount := TestPassedCount + 1;
571 finally
572 Test.TearDown;
573 end;
574 except
575 { Notes:
576
577 - We don't treat EAssertionFailedError
578 any differently from other exceptions here.
579
580 (EAssertionFailedError is raised by our Fail
581 and our AssertXxx methods; *not* raised by standard Pascal Assert.)
582
583 Reason: When the test raises (and doesn't catch) any exception
584 (explicitly like "raise ..", or because of check failed
585 e.g. range check error, or because of standard Pascal Assert failed)
586 it should be reported as a failure, just like a failure in our Fail
587 or AssertXxx.
588
589 Trying to differentiate between EAssertionFailedError and others
590 actually caused bugs in display of castle-tester in the past.
591 It seems simpler and more consistent to treat all exceptions the same.
592
593 - We capture all TObject exceptions here, not just Exception,
594 to be sure to really catch everything. Though I know of no real-life
595 cases when something in standard Pascal libraries raises an exception
596 that is not Exception.
597 }
598 on E: TObject do
599 begin
600 // call FNotifyTestFail
601 if Assigned(FNotifyTestFail) then
602 begin
603 if E is Exception then
604 FailMsg := Exception(E).Message
605 else
606 FailMsg := ''; // exception class not Exception, no better message
607 FNotifyTestFail(Test.GetFullName, FailMsg);
608 end;
609
610 // handle TestFailedCount
611 TestFailedCount := TestFailedCount + 1;
612
613 // handle FStopOnFirstFail
614 if FStopOnFirstFail then
615 raise;
616 end;
617 end;
618 end;
619
620 procedure TCastleTester.Scan;
621 var
622 I: Integer;
623 TestCase: TCastleTestCase;
624 begin
625 FTestsCount := 0;
626
627 for I := 0 to FTestCaseList.Count -1 do
628 begin
629 TestCase := FTestCaseList[I];
630 TestCase.ClearTests;
631 ScanTestCase(TestCase);
632 SetTestCount(TestsCount + TestCase.TestCount);
633 end;
634 end;
635
636 procedure TCastleTester.ScanTestCase(TestCase: TCastleTestCase);
637
638 function TestMethod(const MethodName: String): Boolean;
639 begin
640 Result := IsPrefix('test', MethodName, true) and
641 { Avoid adding internal method TestCount }
642 not SameText(MethodName, 'TestCount');
643 end;
644
645 var
646 {$ifdef FPC}
647 MethodList: TStringList;
648 AMethodName: String;
649 {$else}
650 RttiType: TRttiType;
651 RttiMethod: TRttiMethod;
652 {$endif}
653 begin
654 {$ifdef FPC}
655 MethodList := TStringList.Create;
656 try
657 GetMethodList(TestCase, MethodList);
658
659 for AMethodName in MethodList do
660 begin
661 if TestMethod(AMethodName) then
662 TestCase.AddTest(AMethodName, TestCase.MethodAddress(AMethodName));
663 end;
664
665 finally
666 FreeAndNil(MethodList);
667 end;
668 {$else}
669 RttiType := FRttiContext.GetType(TestCase.ClassInfo);
670
671 for RttiMethod in RttiType.GetMethods do
672 begin
673 if (RttiMethod.MethodKind in [mkProcedure, mkFunction]) and
674 (Length(RttiMethod.GetParameters) = 0) and
675 TestMethod(RttiMethod.Name) then
676 begin
677 TestCase.AddTest(RttiMethod.Name, RttiMethod);
678 end;
679 end;
680 {$endif}
681 end;
682
683 procedure TCastleTester.SetNotifyTestFail(
684 const ANotifyTestFail: TNotifyTestFail);
685 var
686 TestCase: TCastleTestCase;
687 begin
688 if @FNotifyTestFail = @ANotifyTestFail then
689 Exit;
690
691 FNotifyTestFail := ANotifyTestFail;
692 for TestCase in FTestCaseList do
693 begin
694 TestCase.FNotifyTestFail := ANotifyTestFail;
695 end;
696 end;
697
698 procedure TCastleTester.SetTestCount(const NewTestCount: Integer);
699 begin
700 FTestsCount := NewTestCount;
701
702 if Assigned(FNotifyTestCountChanged) then
703 FNotifyTestCountChanged(FTestsCount);
704 end;
705
706 procedure TCastleTester.SetTestFailedCount(const NewTestCount: Integer);
707 begin
708 FTestFailedCount := NewTestCount;
709
710 if Assigned(FNotifyTestFailedChanged) then
711 FNotifyTestFailedChanged(FTestFailedCount);
712 end;
713
714 procedure TCastleTester.SetTestPassedCount(const NewTestCount: Integer);
715 begin
716 FTestPassedCount := NewTestCount;
717
718 if Assigned(FNotifyTestPassedChanged) then
719 FNotifyTestPassedChanged(FTestPassedCount);
720 end;
721
722 function TCastleTester.EnabledTestCount: Integer;
723 var
724 I: Integer;
725 TestCase: TCastleTestCase;
726 begin
727 Result := 0;
728
729 for I := 0 to FTestCaseList.Count -1 do
730 begin
731 TestCase := FTestCaseList[I];
732 if TestCase.Enabled then
733 Inc(Result, TestCase.EnabledTestCount);
734 end;
735 end;
736
737 procedure TCastleTester.EnableFilter(const Filter: String);
738 var
739 TestCase: TCastleTestCase;
740 Test: TCastleTest;
741 I: Integer;
742 begin
743 for TestCase in FTestCaseList do
744 for I := 0 to TestCase.TestCount - 1 do
745 begin
746 Test := TestCase.Test[I];
747 Test.Enabled := IsWild(Test.GetFullName, Filter, true);
748 end;
749 end;
750
751 { TCastleTestCase }
752
753 procedure TCastleTestCase.AssertEquals(const Expected, Actual: String);
754 begin
755 AssertEquals('', Expected, Actual,
756 {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif});
757 end;
758
759 procedure TCastleTestCase.AssertEquals(const Expected, Actual: Boolean);
760 begin
761 AssertEquals('', Expected, Actual,
762 {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif});
763 end;
764
765 function TCastleTestCase.AddTest(const AName: String;
766 {$ifdef FPC}const AMethodPointer: CodePointer{$else}
767 const ARttiMethod: TRttiMethod{$endif}): TCastleTest;
768 begin
769 Result := TCastleTest.Create(Self, AName, {$ifdef FPC}AMethodPointer{$else}
770 ARttiMethod{$endif});
771 FTestList.Add(Result);
772 end;
773
774 procedure TCastleTestCase.AssertBoxesEqual(const Expected, Actual: TBox3D;
775 const Epsilon: Double; AddrOfError: Pointer);
776 begin
777 if AddrOfError = nil then
778 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
779
780 AssertBoxesEqual('', Expected, Actual, Epsilon, AddrOfError);
781 end;
782
783 procedure TCastleTestCase.AssertBoxesEqual(const Expected, Actual: TBox3D;
784 AddrOfError: Pointer);
785 begin
786 if AddrOfError = nil then
787 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
788
789 AssertBoxesEqual('', Expected, Actual, AddrOfError);
790 end;
791
792 procedure TCastleTestCase.AssertBoxesEqual(const Msg: String;
793 const Expected, Actual: TBox3D;
794 const Epsilon: Double; AddrOfError: Pointer);
795 var
796 I: Integer;
797 begin
798 if AddrOfError = nil then
799 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
800
801 if Expected.IsEmpty and Actual.IsEmpty then
802 Exit; // OK
803
804 if Expected.IsEmpty then
805 Fail(Format('Expected empty box, actual box is NOT empty (%s). ' + Msg,
806 [Actual.ToRawString]));
807
808 if Actual.IsEmpty then
809 Fail(Format('Expected NOT empty box (%s), actual box is empty. ' + Msg,
810 [Expected.ToRawString]), AddrOfError);
811
812 for I := 0 to 2 do
813 if (not SameValue(Expected.Data[0][I], Actual.Data[0][I], Epsilon)) or
814 (not SameValue(Expected.Data[1][I], Actual.Data[1][I], Epsilon)) then
815 Fail(Format('Boxes are not equal: expected: %s, actual: %s. ' + Msg,
816 [Expected.ToRawString, Actual.ToRawString]), AddrOfError);
817 end;
818
819 procedure TCastleTestCase.AssertBoxesEqual(const Msg: String;
820 const Expected, Actual: TBox3D;
821 AddrOfError: Pointer);
822 begin
823 if AddrOfError = nil then
824 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
825
826 AssertBoxesEqual(Msg, Expected, Actual, SingleEpsilon, AddrOfError);
827 end;
828
829 procedure TCastleTestCase.AssertEquals(const Expected, Actual: Single);
830 begin
831 AssertTrue('Expected: ' + FloatToStr(Expected) + ' Actual: ' +
832 FloatToStr(Actual), SameValue(Expected, Actual, SingleEpsilon),
833 {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif});
834 end;
835
836 procedure TCastleTestCase.AssertFalse(const Msg: String;
837 const ACondition: Boolean; AddrOfError: Pointer);
838 begin
839 if not Assigned(AddrOfError) then
840 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
841
842
843 AssertTrue(Msg, not ACondition, AddrOfError);
844 end;
845
846 procedure TCastleTestCase.AssertFalse(const ACondition: Boolean);
847 begin
848 AssertFalse('', ACondition,
849 {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif});
850 end;
851
852 procedure TCastleTestCase.AssertFilenamesEqual(const Expected, Actual: String);
853 begin
854 AssertTrue('Expected: ' + Expected + ' Actual: ' + Actual,
855 CompareFileName(Expected, Actual),
856 {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif});
857 end;
858
859 procedure TCastleTestCase.AssertFrustumEquals(const Expected, Actual: TFrustum;
860 AddrOfError: Pointer);
861 begin
862 if AddrOfError = nil then
863 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
864 AssertFrustumEquals(Expected, Actual, SingleEpsilon, AddrOfError);
865 end;
866
867 procedure TCastleTestCase.AssertImagesEqual(
868 const Expected, Actual: TCastleImage; AddrOfError: Pointer);
869 begin
870 if AddrOfError = nil then
871 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
872
873 AssertEquals(Expected.Width, Actual.Width);
874 AssertEquals(Expected.Height, Actual.Height);
875 AssertEquals(Expected.Depth, Actual.Depth);
876 AssertEquals(Expected.Size, Actual.Size);
877
878 AssertTrue(CompareMemDebug(Expected.RawPixels, Actual.RawPixels, Expected.Size));
879 AssertTrue(CompareMem (Expected.RawPixels, Actual.RawPixels, Expected.Size));
880 end;
881
882 procedure TCastleTestCase.AssertFrustumNormalized(const F: TFrustum);
883
884 procedure AssertPlaneNormalized(const P: TVector4);
885 var
886 PDir: TVector3 absolute P;
887 begin
888 if not SameValue(PDir.Length, 1.0, SingleEpsilon) then
889 Fail('Plane is not normalized (frustum planes have to be normalized): ' + P.ToString);
890 end;
891
892 var
893 I: TFrustumPlane;
894 begin
895 if F.FarInfinity then
896 begin
897 for I := Low(I) to Pred(High(I)) do
898 AssertPlaneNormalized(F.Planes[I]);
899 end else
900 begin
901 for I := Low(I) to High(I) do
902 AssertPlaneNormalized(F.Planes[I]);
903 end;
904 end;
905
906 procedure TCastleTestCase.AssertFrustumEquals(const Expected, Actual: TFrustum;
907 const Epsilon: Single; AddrOfError: Pointer);
908 var
909 I: TFrustumPlane;
910 begin
911 if AddrOfError = nil then
912 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
913
914 // by the way, check frustums have normalized planes
915 AssertFrustumNormalized(Expected);
916 AssertFrustumNormalized(Actual);
917
918 try
919 AssertEquals(Expected.FarInfinity, Actual.FarInfinity);
920
921 if Expected.FarInfinity then
922 begin
923 for I := Low(I) to Pred(High(I)) do
924 AssertPlaneEquals(Expected.Planes[I], Actual.Planes[I], true, Epsilon);
925 end else
926 begin
927 for I := Low(I) to High(I) do
928 AssertPlaneEquals(Expected.Planes[I], Actual.Planes[I], true, Epsilon);
929 end;
930 except
931 on E: Exception do
932 begin
933 Fail(Format('Expected frustum (%s) does not equal actual (%s). The underlying difference exception: %s', [
934 Expected.ToString(' '),
935 Actual.ToString(' '),
936 E.Message
937 ]), AddrOfError);
938 end;
939 end;
940 end;
941
942 procedure TCastleTestCase.AssertMatrixEquals(const Expected, Actual: TMatrix4;
943 const Epsilon: Single; AddrOfError: Pointer);
944 var
945 DifferenceEpsilon: Single;
946 I, J: TMatrix4.TIndex;
947 begin
948 if not Assigned(AddrOfError) then
949 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
950
951 if not TMatrix4.Equals(Expected, Actual, Epsilon) then
952 begin
953 DifferenceEpsilon := 0;
954 for I := Low(TMatrix4.TIndex) to High(TMatrix4.TIndex) do
955 for J := Low(TMatrix4.TIndex) to High(TMatrix4.TIndex) do
956 MaxVar(DifferenceEpsilon, Abs(Expected[I, J] - Actual[I, J]));
957
958 Fail(Format('Matrices (TMatrix4) are not equal:' + LineEnding +
959 ' Expected:' + LineEnding +
960 '%s' + LineEnding +
961 ' Actual:' + LineEnding +
962 '%s' + LineEnding +
963 ' The epsilon to ignore the difference would need to be >= %.10f, but is %.10f',
964 [Expected.ToRawString(' '),
965 Actual.ToRawString(' '),
966 DifferenceEpsilon,
967 Epsilon
968 ]), AddrOfError);
969 end;
970 end;
971
972 procedure TCastleTestCase.AssertMatrixEquals(const Expected, Actual: TMatrix4;
973 AddrOfError: Pointer);
974 begin
975 if AddrOfError = nil then
976 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
977
978 AssertMatrixEquals(Expected, Actual, SingleEpsilon, AddrOfError);
979 end;
980
981 procedure TCastleTestCase.AssertPlaneEquals(const Expected, Actual: TVector4;
982 const CheckEqualDirection: Boolean;
983 AddrOfError: Pointer);
984 begin
985 if AddrOfError = nil then
986 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
987 AssertPlaneEquals(Expected, Actual, CheckEqualDirection, SingleEpsilon, AddrOfError);
988 end;
989
990 procedure TCastleTestCase.AssertVectorEqualsDouble(const Expected,
991 Actual: TVector2Double; AddrOfError: Pointer);
992 begin
993 if AddrOfError = nil then
994 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
995
996 AssertVectorEqualsDouble(Expected, Actual, DoubleEpsilon, AddrOfError);
997 end;
998
999 procedure TCastleTestCase.AssertVectorEqualsDouble(const Expected,
1000 Actual: TVector3Double; AddrOfError: Pointer);
1001 begin
1002 if AddrOfError = nil then
1003 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1004
1005 AssertVectorEqualsDouble(Expected, Actual, DoubleEpsilon, AddrOfError);
1006 end;
1007
1008 procedure TCastleTestCase.AssertVectorEqualsDouble(const Expected,
1009 Actual: TVector4Double; AddrOfError: Pointer);
1010 begin
1011 if AddrOfError = nil then
1012 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1013
1014 AssertVectorEqualsDouble(Expected, Actual, DoubleEpsilon, AddrOfError);
1015 end;
1016
1017 procedure TCastleTestCase.AssertVectorEqualsDouble(const Expected,
1018 Actual: TVector2Double; const Epsilon: Single; AddrOfError: Pointer);
1019 begin
1020 if AddrOfError = nil then
1021 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1022
1023 if not TVector2Double.Equals(Expected, Actual, Epsilon) then
1024 Fail(Format('Vectors (TVector2Double) are not equal: expected: %s, actual: %s',
1025 [Expected.ToRawString, Actual.ToRawString]), AddrOfError);
1026 end;
1027
1028 procedure TCastleTestCase.AssertVectorEqualsDouble(const Expected,
1029 Actual: TVector3Double; const Epsilon: Single; AddrOfError: Pointer);
1030 begin
1031 if AddrOfError = nil then
1032 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1033
1034 if not TVector3Double.Equals(Expected, Actual, Epsilon) then
1035 Fail(Format('Vectors (TVector3Double) are not equal: expected: %s, actual: %s',
1036 [Expected.ToRawString, Actual.ToRawString]), AddrOfError);
1037 end;
1038
1039 procedure TCastleTestCase.AssertVectorEqualsDouble(const Expected,
1040 Actual: TVector4Double; const Epsilon: Single; AddrOfError: Pointer);
1041 begin
1042 if AddrOfError = nil then
1043 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1044
1045 if not TVector4Double.Equals(Expected, Actual, Epsilon) then
1046 Fail(Format('Vectors (TVector4Double) are not equal: expected: %s, actual: %s',
1047 [Expected.ToRawString, Actual.ToRawString]), AddrOfError);
1048 end;
1049
1050 procedure TCastleTestCase.AssertSameValue(const Expected, Actual: Double;
1051 AddrOfError: Pointer);
1052 begin
1053 if not Assigned(AddrOfError) then
1054 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
1055
1056 AssertSameValue(Expected, Actual, DoubleEpsilon, AddrOfError);
1057 end;
1058
1059 procedure TCastleTestCase.AssertSameValue(const Expected, Actual: Double;
1060 const Epsilon: Double; AddrOfError: Pointer);
1061 begin
1062 if not Assigned(AddrOfError) then
1063 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
1064
1065 if not SameValue(Expected, Actual, Epsilon) then
1066 Fail(Format('Floats (Double) are not equal: expected: %g, actual: %g',
1067 [Expected, Actual]), AddrOfError);
1068 end;
1069
1070 procedure TCastleTestCase.AssertRectsEqual(const Expected,
1071 Actual: TFloatRectangle; AddrOfError: Pointer);
1072 begin
1073 if AddrOfError = nil then
1074 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
1075
1076 if not Expected.Equals(Actual) then
1077 Fail(Format('Expected rect (%s) does not equal actual (%s)',
1078 [Expected.ToString, Actual.ToString]), AddrOfError);
1079 end;
1080
1081 procedure TCastleTestCase.AssertRectsEqual(const Expected, Actual: TRectangle;
1082 AddrOfError: Pointer);
1083 begin
1084 if AddrOfError = nil then
1085 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
1086
1087 if not Expected.Equals(Actual) then
1088 Fail(Format('Expected rect (%s) does not equal actual (%s)',
1089 [Expected.ToString, Actual.ToString]), AddrOfError);
1090 end;
1091
1092 procedure TCastleTestCase.AssertPlaneEquals(const Expected, Actual: TVector4;
1093 const CheckEqualDirection: Boolean;
1094 const Epsilon: Single; AddrOfError: Pointer);
1095
1096 function VectorsDifference(const V1, V2: TVector4): Single;
1097 var
1098 I: Integer;
1099 begin
1100 I := MaxAbsVectorCoord(V1 - V2);
1101 Result := Abs(V1[I] - V2[I]);
1102 end;
1103
1104 var
1105 MaxE, MaxA: Integer;
1106 begin
1107 if AddrOfError = nil then
1108 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
1109
1110 MaxE := MaxAbsVectorCoord(Expected);
1111 MaxA := MaxAbsVectorCoord(Actual);
1112
1113 if MaxE <> MaxA then
1114 Fail(Format('Planes (TVector4) are not equal, their maximum component index differs. Expected: %s, actual: %s',
1115 [Expected.ToRawString, Actual.ToRawString]), AddrOfError);
1116
1117 if IsZero(Expected[MaxE], Epsilon) and
1118 IsZero( Actual[MaxA], Epsilon) then
1119 begin
1120 if not (Expected.IsZero(Epsilon) and Actual.IsZero(Epsilon)) then
1121 Fail(Format('Planes (TVector4) are not equal, they should be both zero since maximum component is zero. Expected: %s, actual: %s',
1122 [Expected.ToRawString, Actual.ToRawString]), AddrOfError);
1123 end else
1124 if IsZero(Expected[MaxE], Epsilon) or
1125 IsZero( Actual[MaxA], Epsilon) then
1126 begin
1127 Fail(Format('Planes (TVector4) are not equal, one of them has zero maximum component, the other not. Expected: %s, actual: %s',
1128 [Expected.ToRawString, Actual.ToRawString]), AddrOfError);
1129 end else
1130 if CheckEqualDirection and
1131 ((Expected[MaxE] >= 0) <> (Actual[MaxA] >= 0)) then
1132 begin
1133 Fail(Format('Planes (TVector4) are not equal, point in different directions (this matters e.g. for frustum calculations). Expected: %s, actual: %s',
1134 [Expected.ToRawString, Actual.ToRawString]), AddrOfError);
1135 end else
1136 begin
1137 if not TVector4.Equals(
1138 Expected,
1139 Actual * (Expected[MaxE] / Actual[MaxA]),
1140 Epsilon
1141 ) then
1142 Fail(Format('Planes (TVector4) are not equal, they are not multiplied version of each other. Expected: %s, actual: %s. After trying to bring them closer, actual is %s, max difference %f', [
1143 Expected.ToRawString,
1144 Actual.ToRawString,
1145 (Actual * (Expected[MaxE] / Actual[MaxA])).ToRawString,
1146 VectorsDifference(Expected, Actual * (Expected[MaxE] / Actual[MaxA]))
1147 ]), AddrOfError);
1148 end;
1149 end;
1150
1151 procedure TCastleTestCase.AssertSameValue(const Expected, Actual: Single;
1152 const Epsilon: Single; AddrOfError: Pointer);
1153 begin
1154 if not Assigned(AddrOfError) then
1155 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
1156
1157 if not SameValue(Expected, Actual, Epsilon) then
1158 Fail(Format('Floats (Single) are not equal: expected: %g, actual: %g',
1159 [Expected, Actual]), AddrOfError);
1160 end;
1161
1162 procedure TCastleTestCase.AssertSameValue(const Expected, Actual: Single;
1163 AddrOfError: Pointer);
1164 begin
1165 if not Assigned(AddrOfError) then
1166 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif};
1167
1168 AssertSameValue(Expected, Actual, SingleEpsilon, AddrOfError);
1169 end;
1170
1171 procedure TCastleTestCase.AssertTrue(const Msg: String;
1172 const ACondition: Boolean; AddrOfError: Pointer);
1173 begin
1174 if not Assigned(AddrOfError) then
1175 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1176
1177 if not ACondition then
1178 Fail(Msg, AddrOfError);
1179 end;
1180
1181 procedure TCastleTestCase.AssertVectorEquals(const Expected,
1182 Actual: TVector4Byte; AddrOfError: Pointer);
1183 begin
1184 if AddrOfError = nil then
1185 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1186
1187 if not TVector4Byte.Equals(Expected, Actual) then
1188 Fail(Format('Vectors (TVector4Byte) are not equal: expected: %s, actual: %s',
1189 [Expected.ToString, Actual.ToString]), AddrOfError);
1190 end;
1191
1192 procedure TCastleTestCase.AssertVectorEquals(const Expected,
1193 Actual: TVector3Byte; AddrOfError: Pointer);
1194 begin
1195 if AddrOfError = nil then
1196 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1197
1198 if not TVector3Byte.Equals(Expected, Actual) then
1199 Fail(Format('Vectors (TVector3Byte) are not equal: expected: %s, actual: %s',
1200 [Expected.ToString, Actual.ToString]), AddrOfError);
1201 end;
1202
1203 procedure TCastleTestCase.AssertVectorEquals(const Expected,
1204 Actual: TVector2Byte; AddrOfError: Pointer);
1205 begin
1206 if AddrOfError = nil then
1207 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1208
1209 if not TVector2Byte.Equals(Expected, Actual) then
1210 Fail(Format('Vectors (TVector2Byte) are not equal: expected: %s, actual: %s',
1211 [Expected.ToString, Actual.ToString]), AddrOfError);
1212 end;
1213
1214 procedure TCastleTestCase.AssertVectorEquals(const Expected, Actual: TVector3;
1215 AddrOfError: Pointer);
1216 begin
1217 if AddrOfError = nil then
1218 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1219
1220 AssertVectorEquals(Expected, Actual, SingleEpsilon, AddrOfError);
1221 end;
1222
1223 procedure TCastleTestCase.AssertVectorEquals(const Expected, Actual: TVector4;
1224 const Epsilon: Single; AddrOfError: Pointer);
1225 begin
1226 if AddrOfError = nil then
1227 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1228
1229 if not TVector4.Equals(Expected, Actual, Epsilon) then
1230 Fail(Format('Vectors (TVector4) are not equal: expected: %s, actual: %s',
1231 [Expected.ToRawString, Actual.ToRawString]), AddrOfError);
1232 end;
1233
1234 procedure TCastleTestCase.AssertVectorEquals(const Expected, Actual: TVector3;
1235 const Epsilon: Single; AddrOfError: Pointer);
1236 begin
1237 if AddrOfError = nil then
1238 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1239
1240 if not TVector3.Equals(Expected, Actual, Epsilon) then
1241 Fail(Format('Vectors (TVector3) are not equal: expected: %s, actual: %s',
1242 [Expected.ToRawString, Actual.ToRawString]), AddrOfError);
1243 end;
1244
1245 procedure TCastleTestCase.AssertVectorEquals(const Expected, Actual: TVector2;
1246 const Epsilon: Single; AddrOfError: Pointer);
1247 begin
1248 if AddrOfError = nil then
1249 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1250
1251 if not TVector2.Equals(Expected, Actual, Epsilon) then
1252 Fail(Format('Vectors (TVector2) are not equal: expected: %s, actual: %s',
1253 [Expected.ToRawString, Actual.ToRawString]), AddrOfError);
1254 end;
1255
1256 procedure TCastleTestCase.AssertVectorEquals(const Expected, Actual: TVector4;
1257 AddrOfError: Pointer);
1258 begin
1259 if AddrOfError = nil then
1260 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1261
1262 AssertVectorEquals(Expected, Actual, SingleEpsilon, AddrOfError);
1263 end;
1264
1265 procedure TCastleTestCase.AssertVectorEquals(const Expected, Actual: TVector2;
1266 AddrOfError: Pointer);
1267 begin
1268 if AddrOfError = nil then
1269 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1270
1271 AssertVectorEquals(Expected, Actual, SingleEpsilon, AddrOfError);
1272 end;
1273
1274 procedure TCastleTestCase.AssertTrue(const ACondition: Boolean);
1275 begin
1276 AssertTrue('', ACondition, {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif});
1277 end;
1278
1279 procedure TCastleTestCase.ClearTests;
1280 begin
1281 FTestList.Clear;
1282 end;
1283
1284 function TCastleTestCase.CompareFileName(Expected, Actual: String): Boolean;
1285 begin
1286 {$ifdef MSWINDOWS}
1287 Result := UpperCase(Expected) = UpperCase(Actual);
1288 {$else}
1289 Result := Expected = Actual;
1290 {$endif}
1291 end;
1292
1293 constructor TCastleTestCase.Create;
1294 begin
1295 FName := ClassName;
1296 FTestList := {$ifdef FPC}specialize{$endif} TObjectList<TCastleTest>.Create;
1297 Enabled := true;
1298 FWindowForTest := nil;
1299 end;
1300
1301 destructor TCastleTestCase.Destroy;
1302 begin
1303 FreeAndNil(FTestList);
1304 inherited;
1305 end;
1306
1307 function TCastleTestCase.CreateWindowForTest: TCastleWindow;
1308 begin
1309 if FWindowForTest <> nil then
1310 raise Exception.Create('CreateWindowForTest called twice without DestroyWindowForTest');
1311 if not CanCreateWindowForTest then
1312 raise Exception.Create('CreateWindowForTest called when CanCreateWindowForTest = false');
1313
1314 FWindowForTest := TCastleWindow.Create(nil);
1315 SavedApplicationMainWindow := Application.MainWindow;
1316 Application.MainWindow := FWindowForTest;
1317 Result := FWindowForTest;
1318 end;
1319
1320 procedure TCastleTestCase.DestroyWindowForTest(var Window: TCastleWindow);
1321 begin
1322 if FWindowForTest = nil then
1323 raise Exception.Create('DestroyWindowForTest called without CreateWindowForTest');
1324
1325 Assert(FWindowForTest = Window);
1326 Window := nil;
1327
1328 FreeAndNil(FWindowForTest);
1329 Application.MainWindow := SavedApplicationMainWindow;
1330 end;
1331
1332 function TCastleTestCase.EnabledTestCount: Integer;
1333 var
1334 I: Integer;
1335 begin
1336 Result := 0;
1337
1338 for I := 0 to FTestList.Count -1 do
1339 begin
1340 if FTestList[I].Enabled then
1341 Inc(Result);
1342 end;
1343 end;
1344
1345 procedure TCastleTestCase.Fail(const Msg: String; const AddrOfError: Pointer);
1346 begin
1347 if Assigned(AddrOfError) then
1348 raise EAssertionFailedError.Create(Msg) at AddrOfError
1349 else
1350 begin
1351 raise EAssertionFailedError.Create(Msg) at
1352 {$ifdef FPC}
1353 // FPC https://www.freepascal.org/docs-html/ref/refse118.html#x244-26800017.1
1354 get_caller_addr(get_frame)
1355 {$else}
1356 // Delphi: https://stackoverflow.com/questions/8950513/what-does-at-returnaddress-mean-in-delphi
1357 ReturnAddress
1358 {$endif};
1359 end;
1360 end;
1361
1362 function TCastleTestCase.GetTempDirectory: String;
1363 begin
1364 {$ifdef FPC}
1365 Result := GetTempDir;
1366 {$else}
1367 Result := TPath.GetTempPath;
1368 {$endif}
1369 end;
1370
1371 function TCastleTestCase.GetTest(const Index: Integer): TCastleTest;
1372 begin
1373 Result := FTestList[Index];
1374 end;
1375
1376 { TODO:
1377 function TCastleTestCase.GetTestingViewport: TCastleViewport;
1378 begin
1379 raise Exception.Create('Not implemented');
1380 end;
1381 }
1382
1383 function TCastleTestCase.CanCreateWindowForTest: Boolean;
1384 begin
1385 Result :=
1386 {$if defined(ANDROID) or
1387 defined(iPHONESIM) or
1388 defined(iOS) or
1389 defined(CASTLE_NINTENDO_SWITCH)}
1390 // On these platforms, we cannot create a window, so we cannot test
1391 false
1392 {$else}
1393 not ParamNoWindowCreate
1394 {$endif};
1395 end;
1396
1397 procedure TCastleTestCase.OnWarningRaiseException(const Category, S: string);
1398 begin
1399 raise Exception.CreateFmt(ClassName +
1400 ': received a warning, and any warning here is an error: %s: %s',
1401 [Category,
1402 S]
1403 );
1404 end;
1405
1406 function TCastleTestCase.PrepareCustomMsg(const Msg: String): String;
1407 begin
1408 Result := Trim(Msg);
1409 if Length(Result) > 0 then
1410 Result := '"' + Result + '" ';
1411 end;
1412
1413 procedure TCastleTestCase.Setup;
1414 begin
1415
1416 end;
1417
1418 procedure TCastleTestCase.TearDown;
1419 begin
1420
1421 end;
1422
1423 function TCastleTestCase.TestCount: Integer;
1424 begin
1425 Result := FTestList.Count;
1426 end;
1427
1428 procedure TCastleTestCase.TestLog(Text: String);
1429 begin
1430 WritelnLog(Text);
1431 end;
1432
1433 procedure TCastleTestCase.AssertEquals(const Expected, Actual: Integer);
1434 begin
1435 AssertEquals('', Expected, Actual,
1436 {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif});
1437 end;
1438
1439 procedure TCastleTestCase.AssertEquals(const Expected, Actual: Cardinal);
1440 begin
1441 AssertEquals('', Expected, Actual,
1442 {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif});
1443 end;
1444
1445 procedure TCastleTestCase.AssertEquals(const Expected, Actual: Int64);
1446 begin
1447 AssertEquals('', Expected, Actual,
1448 {$ifdef FPC}get_caller_addr(get_frame){$else}ReturnAddress{$endif});
1449 end;
1450
1451 procedure TCastleTestCase.AssertEquals(const Msg: String; const Expected,
1452 Actual: Integer; AddrOfError: Pointer = nil);
1453 begin
1454 if AddrOfError = nil then
1455 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1456
1457 AssertTrue(PrepareCustomMsg(Msg) + 'Expected: ' + IntToStr(Expected) +
1458 ' Actual: ' + IntToStr(Actual), Expected = Actual, AddrOfError);
1459 end;
1460
1461 procedure TCastleTestCase.AssertEquals(const Msg, Expected, Actual: String;
1462 AddrOfError: Pointer = nil);
1463 begin
1464 if AddrOfError = nil then
1465 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1466
1467 AssertTrue(PrepareCustomMsg(Msg) + 'Expected: ' + Expected +
1468 ' Actual: ' + Actual, Expected = Actual, AddrOfError);
1469 end;
1470
1471 procedure TCastleTestCase.AssertEquals(const Msg: String; const Expected,
1472 Actual: Boolean; AddrOfError: Pointer);
1473 begin
1474 if AddrOfError = nil then
1475 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1476
1477 AssertTrue(PrepareCustomMsg(Msg) + 'Expected: ' + BoolToStr(Expected, true) + ' Actual: ' +
1478 BoolToStr(Actual, true), Expected = Actual, AddrOfError);
1479 end;
1480
1481 procedure TCastleTestCase.AssertEquals(const Msg: String; const Expected,
1482 Actual: Cardinal; AddrOfError: Pointer);
1483 begin
1484 if AddrOfError = nil then
1485 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1486
1487 AssertTrue(PrepareCustomMsg(Msg) + 'Expected: ' + IntToStr(Expected) +
1488 ' Actual: ' + IntToStr(Actual), Expected = Actual, AddrOfError);
1489 end;
1490
1491 procedure TCastleTestCase.AssertEquals(const Msg: String; const Expected,
1492 Actual: Int64; AddrOfError: Pointer);
1493 begin
1494 if AddrOfError = nil then
1495 AddrOfError := {$ifdef FPC}get_caller_addr(get_frame){$else}System.ReturnAddress{$endif};
1496
1497 AssertTrue(PrepareCustomMsg(Msg) + 'Expected: ' + IntToStr(Expected) +
1498 ' Actual: ' + IntToStr(Actual), Expected = Actual, AddrOfError);
1499 end;
1500
1501 { TCastleTest }
1502
1503 constructor TCastleTest.Create(const ATestCase: TCastleTestCase;
1504 const AName: String; {$ifdef FPC}const AMethodPointer: CodePointer{$else}
1505 const ARttiMethod: TRttiMethod{$endif});
1506 begin
1507 FTestCase := ATestCase;
1508 Name := AName;
1509 FEnabled := true;
1510 {$ifdef FPC}
1511 FMethodPointer := AMethodPointer;
1512 {$else}
1513 FRttiMethod := ARttiMethod;
1514 {$endif}
1515 end;
1516
1517 function TCastleTest.GetFullName: String;
1518 begin
1519 Result:= FTestCase.ClassName + '.' + Name;
1520 end;
1521
1522 procedure TCastleTest.Run;
1523 {$ifdef FPC}
1524 var
1525 Method: TMethod;
1526 type
1527 TCastleTestFunc = procedure() of object;
1528 {$endif}
1529 var
1530 DummyWindowForTest: TCastleWindow;
1531 begin
1532 FTestCase.FCurrentTestName := GetFullName;
1533 try
1534 {$ifdef FPC}
1535 Method.Code := FMethodPointer;
1536 Method.Data := Pointer(FTestCase);
1537 TCastleTestFunc(Method);
1538 {$else}
1539 FRttiMethod.Invoke(FTestCase, []);
1540 {$endif}
1541 finally
1542 { Automatically free window created by CreateWindowForTest. }
1543 if FTestCase.FWindowForTest <> nil then
1544 begin
1545 { Use DummyWindowForTest that will be set to nil by DestroyWindowForTest. }
1546 DummyWindowForTest := FTestCase.FWindowForTest;
1547 FTestCase.DestroyWindowForTest(DummyWindowForTest);
1548 end;
1549 end;
1550 end;
1551
1552 procedure TCastleTest.Setup;
1553 begin
1554 FTestCase.Setup;
1555 end;
1556
1557 procedure TCastleTest.TearDown;
1558 begin
1559 FTestCase.TearDown;
1560 end;
1561
1562 procedure TCastleTest.SetEnabled(NewValue: Boolean);
1563 begin
1564 if FEnabled = NewValue then
1565 Exit;
1566
1567 FEnabled := NewValue;
1568
1569 if Assigned(FTestCase.FCastleTester.FNotifyEnabledTestCountChanged) then
1570 FTestCase.FCastleTester.FNotifyEnabledTestCountChanged(Self);
1571 end;
1572
1573 initialization
1574 FRegisteredTestCaseList := {$ifdef FPC}specialize{$endif} TList<TCastleTestCaseClass>.Create;
1575 finalization
1576 FreeAndNil(FRegisteredTestCaseList);
1577 end.