KT-58046 Use saturated math in LongTimeMark implementation

- keep offset in range (-1..+1) of time source units
- when adding big or infinite offset, saturate startedAt instead
- displace initial reading to zero, similar to MonotonicTimeSource
- fix the zero reading in TestTimeSource by calling markNow in constructor
- use more precise reading adjustment in TestTimeSource for big durations
- add a note about comparable time marks for AbstractLongTimeSource and TestTimeSource
This commit is contained in:
Ilya Gorbunov
2023-04-10 18:04:11 +02:00
committed by Space Team
parent 7a6947ad35
commit 1014434475
5 changed files with 193 additions and 120 deletions
@@ -41,8 +41,10 @@ class TestTimeSourceTest {
run {
val timeSource = TestTimeSource()
timeSource += moderatePositiveDuration
val mark = timeSource.markNow()
// does not overflow even if duration doesn't fit in long, but the result fits
timeSource += -moderatePositiveDuration - Long.MAX_VALUE.nanoseconds
assertEquals(-(moderatePositiveDuration + Long.MAX_VALUE.nanoseconds), mark.elapsedNow())
}
}