|
| 1 | +/* |
| 2 | + * Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | +package kotlinx.coroutines |
| 5 | + |
| 6 | +import kotlin.test.* |
| 7 | +import kotlin.time.* |
| 8 | +import kotlin.time.Duration.Companion.milliseconds |
| 9 | +import kotlin.time.Duration.Companion.nanoseconds |
| 10 | +import kotlin.time.Duration.Companion.seconds |
| 11 | + |
| 12 | +class DurationToMillisTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + fun testNegativeDurationCoercedToZeroMillis() { |
| 16 | + assertEquals(0L, (-1).seconds.toDelayMillis()) |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + fun testZeroDurationCoercedToZeroMillis() { |
| 21 | + assertEquals(0L, 0.seconds.toDelayMillis()) |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + fun testOneNanosecondCoercedToOneMillisecond() { |
| 26 | + assertEquals(1L, 1.nanoseconds.toDelayMillis()) |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + fun testOneSecondCoercedTo1000Milliseconds() { |
| 31 | + assertEquals(1_000L, 1.seconds.toDelayMillis()) |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun testMixedComponentDurationRoundedUpToNextMillisecond() { |
| 36 | + assertEquals(999L, (998.milliseconds + 75909.nanoseconds).toDelayMillis()) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun testOneExtraNanosecondRoundedUpToNextMillisecond() { |
| 41 | + assertEquals(999L, (998.milliseconds + 1.nanoseconds).toDelayMillis()) |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun testInfiniteDurationCoercedToLongMaxValue() { |
| 46 | + assertEquals(Long.MAX_VALUE, Duration.INFINITE.toDelayMillis()) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun testNegativeInfiniteDurationCoercedToZero() { |
| 51 | + assertEquals(0L, (-Duration.INFINITE).toDelayMillis()) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun testNanosecondOffByOneInfinityDoesNotOverflow() { |
| 56 | + assertEquals(Long.MAX_VALUE / 1_000_000, (Long.MAX_VALUE - 1L).nanoseconds.toDelayMillis()) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun testMillisecondOffByOneInfinityDoesNotIncrement() { |
| 61 | + assertEquals((Long.MAX_VALUE / 2) - 1, ((Long.MAX_VALUE / 2) - 1).milliseconds.toDelayMillis()) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun testOutOfBoundsNanosecondsButFiniteDoesNotIncrement() { |
| 66 | + val milliseconds = Long.MAX_VALUE / 10 |
| 67 | + assertEquals(milliseconds, milliseconds.milliseconds.toDelayMillis()) |
| 68 | + } |
| 69 | +} |
0 commit comments