Advance TestTimeSource more precisely
Change durations in its test so that they hold precise number of nanoseconds
This commit is contained in:
@@ -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.
|
* 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.
|
* @throws IllegalStateException when the reading value overflows as the result of this operation.
|
||||||
*/
|
*/
|
||||||
public operator fun plusAssign(duration: Duration) {
|
public operator fun plusAssign(duration: Duration) {
|
||||||
val delta = duration.toDouble(unit)
|
val longDelta = duration.toLong(unit)
|
||||||
val longDelta = delta.toLong()
|
|
||||||
reading = if (longDelta != Long.MIN_VALUE && longDelta != Long.MAX_VALUE) {
|
reading = if (longDelta != Long.MIN_VALUE && longDelta != Long.MAX_VALUE) {
|
||||||
// when delta fits in long, add it as long
|
// when delta fits in long, add it as long
|
||||||
val newReading = reading + longDelta
|
val newReading = reading + longDelta
|
||||||
if (reading xor longDelta >= 0 && reading xor newReading < 0) overflow(duration)
|
if (reading xor longDelta >= 0 && reading xor newReading < 0) overflow(duration)
|
||||||
newReading
|
newReading
|
||||||
} else {
|
} else {
|
||||||
|
val delta = duration.toDouble(unit)
|
||||||
// when delta is greater than long, add it as double
|
// when delta is greater than long, add it as double
|
||||||
val newReading = reading + delta
|
val newReading = reading + delta
|
||||||
if (newReading > Long.MAX_VALUE || newReading < Long.MIN_VALUE) overflow(duration)
|
if (newReading > Long.MAX_VALUE || newReading < Long.MIN_VALUE) overflow(duration)
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ class TestTimeSourceTest {
|
|||||||
assertFailsWith<IllegalStateException>((-enormousDuration).toString()) { TestTimeSource() += -enormousDuration }
|
assertFailsWith<IllegalStateException>((-enormousDuration).toString()) { TestTimeSource() += -enormousDuration }
|
||||||
}
|
}
|
||||||
|
|
||||||
val moderatePositiveDuration = Duration.nanoseconds(Long.MAX_VALUE.takeHighestOneBit())
|
val moderatePositiveDuration = Duration.nanoseconds(Long.MAX_VALUE) / 1.5
|
||||||
val borderlinePositiveDuration = Duration.nanoseconds(Long.MAX_VALUE) // rounded to 2.0^63, which is slightly more than Long.MAX_VALUE
|
val borderlineQuarterPositiveDuration = Duration.nanoseconds(Long.MAX_VALUE / 4) // precise number of ns
|
||||||
val borderlineNegativeDuration = Duration.nanoseconds(Long.MIN_VALUE)
|
val borderlineQuarterNegativeDuration = Duration.nanoseconds(Long.MIN_VALUE / 4)
|
||||||
run {
|
run {
|
||||||
val timeSource = TestTimeSource()
|
val timeSource = TestTimeSource()
|
||||||
timeSource += moderatePositiveDuration
|
timeSource += moderatePositiveDuration
|
||||||
@@ -28,20 +28,20 @@ class TestTimeSourceTest {
|
|||||||
}
|
}
|
||||||
run {
|
run {
|
||||||
val timeSource = TestTimeSource()
|
val timeSource = TestTimeSource()
|
||||||
timeSource += borderlinePositiveDuration
|
repeat(4) { timeSource += borderlineQuarterPositiveDuration }
|
||||||
assertFailsWith<IllegalStateException>("Should overflow positive") { timeSource += Duration.nanoseconds(1) }
|
assertFailsWith<IllegalStateException>("Should overflow positive") { timeSource += Duration.nanoseconds(4) }
|
||||||
}
|
}
|
||||||
run {
|
run {
|
||||||
val timeSource = TestTimeSource()
|
val timeSource = TestTimeSource()
|
||||||
timeSource += borderlineNegativeDuration
|
repeat(4) { timeSource += borderlineQuarterNegativeDuration }
|
||||||
assertFailsWith<IllegalStateException>("Should overflow negative") { timeSource += -Duration.nanoseconds(1) }
|
assertFailsWith<IllegalStateException>("Should overflow negative") { timeSource += -Duration.nanoseconds(4) }
|
||||||
}
|
}
|
||||||
|
|
||||||
run {
|
run {
|
||||||
val timeSource = TestTimeSource()
|
val timeSource = TestTimeSource()
|
||||||
timeSource += moderatePositiveDuration
|
timeSource += moderatePositiveDuration
|
||||||
// does not overflow event if duration doesn't fit in long
|
// does not overflow even if duration doesn't fit in long, but the result fits
|
||||||
timeSource += -moderatePositiveDuration + borderlineNegativeDuration
|
timeSource += -moderatePositiveDuration - Duration.nanoseconds(Long.MAX_VALUE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user