KT-58058 Duration truncation internal helper function
This commit is contained in:
committed by
Space Team
parent
b3decace6b
commit
17d977977c
@@ -496,6 +496,50 @@ class DurationTest {
|
||||
assertTrue((Duration.ZERO / Duration.ZERO).isNaN())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun truncation() {
|
||||
fun expect(expected: Duration, value: Duration, unit: DurationUnit) {
|
||||
assertEquals(expected, value.truncateTo(unit))
|
||||
assertEquals(-expected, (-value).truncateTo(unit))
|
||||
}
|
||||
for (unit in units) {
|
||||
expect(Duration.ZERO, Duration.ZERO, unit)
|
||||
expect(Duration.INFINITE, Duration.INFINITE, unit)
|
||||
expect(Duration.ZERO, 1.toDuration(unit) - 1.nanoseconds, unit)
|
||||
repeat(100) {
|
||||
val whole = Random.nextInt(100_000).toDuration(unit)
|
||||
expect(whole, whole, unit)
|
||||
if (unit > DurationUnit.NANOSECONDS) {
|
||||
val part = Random.nextLong(1, 1.toDuration(unit).inWholeNanoseconds).nanoseconds
|
||||
expect(Duration.ZERO, part, unit)
|
||||
expect(whole, whole + part, unit)
|
||||
}
|
||||
}
|
||||
}
|
||||
repeat(10) {
|
||||
val d = Random.nextLong().nanoseconds
|
||||
expect(d, d, DurationUnit.NANOSECONDS)
|
||||
}
|
||||
expect(12.microseconds, 12998.nanoseconds, DurationUnit.MICROSECONDS)
|
||||
expect(1503.milliseconds, 1503_889_404.nanoseconds, DurationUnit.MILLISECONDS)
|
||||
expect(340.seconds, 340_990_567_444L.nanoseconds, DurationUnit.SECONDS)
|
||||
expect(3.minutes, 200.seconds, DurationUnit.MINUTES)
|
||||
expect(4.hours, 250.minutes, DurationUnit.HOURS)
|
||||
expect(1.days, 30.hours, DurationUnit.DAYS)
|
||||
|
||||
// big durations
|
||||
run {
|
||||
val d = (Long.MAX_VALUE / 4).milliseconds
|
||||
for (unit in units) {
|
||||
if (unit <= DurationUnit.MILLISECONDS) {
|
||||
expect(d, d, unit)
|
||||
} else {
|
||||
expect(d.toLong(unit).toDuration(unit), d, unit)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseAndFormatIsoString() {
|
||||
fun test(duration: Duration, vararg isoStrings: String) {
|
||||
|
||||
Reference in New Issue
Block a user