-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Innmind/fix-elapsed-period-precision
Fix precision of elapsed periods
- Loading branch information
Showing
17 changed files
with
370 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
use Innmind\TimeContinuum\{ | ||
PointInTime\HighResolution, | ||
Period, | ||
}; | ||
use Fixtures\Innmind\TimeContinuum as Fixtures; | ||
use Innmind\BlackBox\Set; | ||
|
||
return static function() { | ||
yield proof( | ||
'High resolution elapsed period', | ||
given( | ||
Set\Integers::above(0), | ||
Set\Integers::above(0), | ||
Set\Integers::between(0, 999_999_999), | ||
Set\Integers::between(0, 999_999_999), | ||
)->filter(static fn($start, $end) => $end > $start), | ||
static function( | ||
$assert, | ||
$startSeconds, | ||
$endSeconds, | ||
$startNanoseconds, | ||
$endNanoseconds, | ||
) { | ||
$start = HighResolution::of($startSeconds, $startNanoseconds); | ||
$end = HighResolution::of($endSeconds, $endNanoseconds); | ||
|
||
$assert->true( | ||
$start | ||
->elapsedSince($start) | ||
->equals( | ||
Period::microsecond(0)->asElapsedPeriod(), | ||
), | ||
); | ||
$assert->true( | ||
$end | ||
->elapsedSince($start) | ||
->longerThan( | ||
Period::microsecond(1)->asElapsedPeriod(), | ||
), | ||
); | ||
}, | ||
); | ||
|
||
yield proof( | ||
'High resolution elapsed period within same second', | ||
given( | ||
Set\Integers::between(0, 999_999_999), | ||
Set\Integers::between(0, 999_999_999), | ||
)->filter(static fn($start, $end) => $end > $start), | ||
static function( | ||
$assert, | ||
$startNanoseconds, | ||
$endNanoseconds, | ||
) { | ||
$start = HighResolution::of(0, $startNanoseconds); | ||
$end = HighResolution::of(0, $endNanoseconds); | ||
|
||
$assert->true( | ||
$start | ||
->elapsedSince($start) | ||
->equals( | ||
Period::microsecond(0)->asElapsedPeriod(), | ||
), | ||
); | ||
$assert->true( | ||
$end | ||
->elapsedSince($start) | ||
->longerThan( | ||
Period::microsecond(1)->asElapsedPeriod(), | ||
), | ||
); | ||
}, | ||
); | ||
|
||
yield proof( | ||
'Elapsed period', | ||
given( | ||
Fixtures\PointInTime::any(), | ||
Set\Integers::above(1), | ||
), | ||
static function($assert, $start, $microsecond) { | ||
$assert->true( | ||
$start | ||
->elapsedSince($start) | ||
->equals( | ||
Period::microsecond(0)->asElapsedPeriod(), | ||
), | ||
); | ||
$assert->true( | ||
$start | ||
->goForward(Period::microsecond($microsecond)) | ||
->elapsedSince($start) | ||
->equals( | ||
Period::microsecond($microsecond)->asElapsedPeriod(), | ||
), | ||
); | ||
$assert->true( | ||
$start | ||
->goForward(Period::microsecond($microsecond)) | ||
->elapsedSince($start) | ||
->longerThan( | ||
Period::microsecond(0)->asElapsedPeriod(), | ||
), | ||
); | ||
}, | ||
); | ||
|
||
yield proof( | ||
'Negative elapsed periods throws', | ||
given( | ||
Fixtures\PointInTime::any(), | ||
Set\Integers::above(1), | ||
), | ||
static function($assert, $start, $microsecond) { | ||
$assert->throws( | ||
static fn() => $start | ||
->goBack(Period::microsecond($microsecond)) | ||
->elapsedSince($start), | ||
); | ||
}, | ||
); | ||
|
||
yield proof( | ||
'Negative high resolution elapsed periods throws', | ||
given( | ||
Set\Integers::above(0), | ||
Set\Integers::above(0), | ||
Set\Integers::between(0, 999_999_999), | ||
Set\Integers::between(0, 999_999_999), | ||
)->filter(static fn($start, $end) => $end > $start), | ||
static function( | ||
$assert, | ||
$startSeconds, | ||
$endSeconds, | ||
$startNanoseconds, | ||
$endNanoseconds, | ||
) { | ||
$start = HighResolution::of($startSeconds, $startNanoseconds); | ||
$end = HighResolution::of($endSeconds, $endNanoseconds); | ||
|
||
$assert->throws( | ||
static fn() => $start->elapsedSince($end), | ||
); | ||
}, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.