Advance TestTimeSource more precisely
Change durations in its test so that they hold precise number of nanoseconds
This commit is contained in:
@@ -18,9 +18,9 @@ class TestTimeSourceTest {
|
||||
assertFailsWith<IllegalStateException>((-enormousDuration).toString()) { TestTimeSource() += -enormousDuration }
|
||||
}
|
||||
|
||||
val moderatePositiveDuration = Duration.nanoseconds(Long.MAX_VALUE.takeHighestOneBit())
|
||||
val borderlinePositiveDuration = Duration.nanoseconds(Long.MAX_VALUE) // rounded to 2.0^63, which is slightly more than Long.MAX_VALUE
|
||||
val borderlineNegativeDuration = Duration.nanoseconds(Long.MIN_VALUE)
|
||||
val moderatePositiveDuration = Duration.nanoseconds(Long.MAX_VALUE) / 1.5
|
||||
val borderlineQuarterPositiveDuration = Duration.nanoseconds(Long.MAX_VALUE / 4) // precise number of ns
|
||||
val borderlineQuarterNegativeDuration = Duration.nanoseconds(Long.MIN_VALUE / 4)
|
||||
run {
|
||||
val timeSource = TestTimeSource()
|
||||
timeSource += moderatePositiveDuration
|
||||
@@ -28,20 +28,20 @@ class TestTimeSourceTest {
|
||||
}
|
||||
run {
|
||||
val timeSource = TestTimeSource()
|
||||
timeSource += borderlinePositiveDuration
|
||||
assertFailsWith<IllegalStateException>("Should overflow positive") { timeSource += Duration.nanoseconds(1) }
|
||||
repeat(4) { timeSource += borderlineQuarterPositiveDuration }
|
||||
assertFailsWith<IllegalStateException>("Should overflow positive") { timeSource += Duration.nanoseconds(4) }
|
||||
}
|
||||
run {
|
||||
val timeSource = TestTimeSource()
|
||||
timeSource += borderlineNegativeDuration
|
||||
assertFailsWith<IllegalStateException>("Should overflow negative") { timeSource += -Duration.nanoseconds(1) }
|
||||
repeat(4) { timeSource += borderlineQuarterNegativeDuration }
|
||||
assertFailsWith<IllegalStateException>("Should overflow negative") { timeSource += -Duration.nanoseconds(4) }
|
||||
}
|
||||
|
||||
run {
|
||||
val timeSource = TestTimeSource()
|
||||
timeSource += moderatePositiveDuration
|
||||
// does not overflow event if duration doesn't fit in long
|
||||
timeSource += -moderatePositiveDuration + borderlineNegativeDuration
|
||||
// does not overflow even if duration doesn't fit in long, but the result fits
|
||||
timeSource += -moderatePositiveDuration - Duration.nanoseconds(Long.MAX_VALUE)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user