| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestCastleTimeUtils" -*- |
| 2 |
{ |
| 3 |
Copyright 2016-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 CastleTimeUtils unit. } |
| 18 |
unit TestCastleTimeUtils; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses CastleTester; |
| 23 |
|
| 24 |
type |
| 25 |
TTestCastleTimeUtils = class(TCastleTestCase) |
| 26 |
published |
| 27 |
procedure TestGetTickCount64; |
| 28 |
end; |
| 29 |
|
| 30 |
implementation |
| 31 |
|
| 32 |
uses SysUtils, Classes, Math, CastleTimeUtils; |
| 33 |
|
| 34 |
{ TTestCastleTimeUtils ---------------------------------------------------------- } |
| 35 |
|
| 36 |
{$warnings off} // knowingly using deprecated, to check they are working |
| 37 |
|
| 38 |
procedure TTestCastleTimeUtils.TestGetTickCount64; |
| 39 |
var |
| 40 |
G1, G2: TMilisecTime; |
| 41 |
begin |
| 42 |
G1 := CastleGetTickCount64; |
| 43 |
Sleep(1000); |
| 44 |
G2 := CastleGetTickCount64; |
| 45 |
|
| 46 |
{ It should be that G2 is larger than G1 by 1000 milisecons. |
| 47 |
To allow wild imprecision, we check something larger. |
| 48 |
Also, to work even when 32-bit GetTickCount wraps, |
| 49 |
use TimeTickSecondLater instead of MilisecTimesSubtract. } |
| 50 |
|
| 51 |
// Writeln(MilisecTimesSubtract(G2, G1)); |
| 52 |
// Writeln(G2 - G1); |
| 53 |
|
| 54 |
AssertTrue(TimeTickSecondLater(G1, G2, 100)); |
| 55 |
AssertFalse(TimeTickSecondLater(G1, G2, 10000)); |
| 56 |
|
| 57 |
{ test on prepared values } |
| 58 |
AssertTrue(TimeTickSecondLater(1000, 2001, 1000)); |
| 59 |
AssertFalse(TimeTickSecondLater(1000, 1999, 1000)); |
| 60 |
|
| 61 |
{ test on prepared values simulating wrap with 32-bit GetTickCount } |
| 62 |
AssertTrue(TimeTickSecondLater(High(UInt32) - 500, 501, 1000)); |
| 63 |
AssertFalse(TimeTickSecondLater(High(UInt32) - 500, 499, 1000)); |
| 64 |
end; |
| 65 |
|
| 66 |
{$warnings on} |
| 67 |
|
| 68 |
initialization |
| 69 |
RegisterTest(TTestCastleTimeUtils); |
| 70 |
end. |