Advance TestTimeSource more precisely

Change durations in its test so that they hold precise number
of nanoseconds
This commit is contained in:
Ilya Gorbunov
2021-03-27 21:36:57 +03:00
parent fa7460ba48
commit 4fd2254f3f
2 changed files with 12 additions and 12 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -84,14 +84,14 @@ public class TestTimeSource : AbstractLongTimeSource(unit = DurationUnit.NANOSEC
* @throws IllegalStateException when the reading value overflows as the result of this operation.
*/
public operator fun plusAssign(duration: Duration) {
val delta = duration.toDouble(unit)
val longDelta = delta.toLong()
val longDelta = duration.toLong(unit)
reading = if (longDelta != Long.MIN_VALUE && longDelta != Long.MAX_VALUE) {
// when delta fits in long, add it as long
val newReading = reading + longDelta
if (reading xor longDelta >= 0 && reading xor newReading < 0) overflow(duration)
newReading
} else {
val delta = duration.toDouble(unit)
// when delta is greater than long, add it as double
val newReading = reading + delta
if (newReading > Long.MAX_VALUE || newReading < Long.MIN_VALUE) overflow(duration)