Control Duration init block assertions on JVM with -ea option

On other platforms enable them unconditionally
This commit is contained in:
Ilya Gorbunov
2021-09-23 12:56:20 +03:00
committed by Space
parent 21a9198fff
commit 7666377565
5 changed files with 15 additions and 6 deletions
+8 -6
View File
@@ -38,12 +38,13 @@ public value class Duration internal constructor(private val rawValue: Long) : C
private val storageUnit get() = if (isInNanos()) DurationUnit.NANOSECONDS else DurationUnit.MILLISECONDS
init {
// TODO: disable assertions in final version
if (isInNanos()) {
if (value !in -MAX_NANOS..MAX_NANOS) throw AssertionError("$value ns is out of nanoseconds range")
} else {
if (value !in -MAX_MILLIS..MAX_MILLIS) throw AssertionError("$value ms is out of milliseconds range")
if (value in -MAX_NANOS_IN_MILLIS..MAX_NANOS_IN_MILLIS) throw AssertionError("$value ms is denormalized")
if (durationAssertionsEnabled) {
if (isInNanos()) {
if (value !in -MAX_NANOS..MAX_NANOS) throw AssertionError("$value ns is out of nanoseconds range")
} else {
if (value !in -MAX_MILLIS..MAX_MILLIS) throw AssertionError("$value ms is out of milliseconds range")
if (value in -MAX_NANOS_IN_MILLIS..MAX_NANOS_IN_MILLIS) throw AssertionError("$value ms is denormalized")
}
}
}
@@ -1421,6 +1422,7 @@ private fun durationOfMillisNormalized(millis: Long) =
durationOfMillis(millis.coerceIn(-MAX_MILLIS, MAX_MILLIS))
}
internal expect val durationAssertionsEnabled: Boolean
internal expect fun formatToExactDecimals(value: Double, decimals: Int): String
internal expect fun formatUpToDecimals(value: Double, decimals: Int): String