From a9854025078612571280af24d9ff31da248fc93a Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 15 Aug 2019 19:12:36 +0300 Subject: [PATCH] Introduce ClockMark.hasPassedNow, hasNotPassedNow functions Makes it more clear to use them than comparing elapsed with Duration.ZERO. --- libraries/stdlib/src/kotlin/time/Clock.kt | 17 ++++++++++++ libraries/stdlib/test/time/ClockMarkTest.kt | 26 +++++++++++++++---- .../kotlin-stdlib-runtime-merged.txt | 2 ++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/libraries/stdlib/src/kotlin/time/Clock.kt b/libraries/stdlib/src/kotlin/time/Clock.kt index f7468616a3e..e20c1c7e5de 100644 --- a/libraries/stdlib/src/kotlin/time/Clock.kt +++ b/libraries/stdlib/src/kotlin/time/Clock.kt @@ -52,6 +52,23 @@ public abstract class ClockMark { * The returned clock mark is more _early_ when the [duration] is positive, and more _late_ when the [duration] is negative. */ public open operator fun minus(duration: Duration): ClockMark = plus(-duration) + + + /** + * Returns true if this clock mark has passed according to the clock from which this mark was taken. + * + * Note that the value returned by this function can change on subsequent invocations. + * If the clock is monotonic, it can change only from `false` to `true`, namely, when the clock mark becomes behind the current point of the clock. + */ + public fun hasPassedNow(): Boolean = !elapsedNow().isNegative() + + /** + * Returns false if this clock mark has not passed according to the clock from which this mark was taken. + * + * Note that the value returned by this function can change on subsequent invocations. + * If the clock is monotonic, it can change only from `true` to `false`, namely, when the clock mark becomes behind the current point of the clock. + */ + public fun hasNotPassedNow(): Boolean = elapsedNow().isNegative() } @ExperimentalTime diff --git a/libraries/stdlib/test/time/ClockMarkTest.kt b/libraries/stdlib/test/time/ClockMarkTest.kt index b188a1f90ce..28e905093af 100644 --- a/libraries/stdlib/test/time/ClockMarkTest.kt +++ b/libraries/stdlib/test/time/ClockMarkTest.kt @@ -14,12 +14,19 @@ class ClockMarkTest { fun adjustment() { val clock = TestClock() - val mark = clock.markNow() - val markFuture1 = mark + 1.milliseconds - val markFuture2 = mark - (-1).milliseconds + fun ClockMark.assertHasPassed(hasPassed: Boolean) { + assertEquals(!hasPassed, this.hasNotPassedNow(), "Expected mark in the future") + assertEquals(hasPassed, this.hasPassedNow(), "Expected mark in the past") - val markPast1 = mark - 1.milliseconds - val markPast2 = markFuture1 + (-2).milliseconds + assertEquals(!hasPassed, this.elapsedNow() < Duration.ZERO, "Mark elapsed: ${this.elapsedNow()}, expected hasPassed: $hasPassed") + } + + val mark = clock.markNow() + val markFuture1 = (mark + 1.milliseconds).apply { assertHasPassed(false) } + val markFuture2 = (mark - (-1).milliseconds).apply { assertHasPassed(false) } + + val markPast1 = (mark - 1.milliseconds).apply { assertHasPassed(true) } + val markPast2 = (markFuture1 + (-2).milliseconds).apply { assertHasPassed(true) } clock += 500_000.nanoseconds @@ -33,5 +40,14 @@ class ClockMarkTest { assertEquals(elapsedFromPast, markPast1.elapsedNow()) assertEquals(elapsedFromPast, markPast2.elapsedNow()) + + markFuture1.assertHasPassed(false) + markPast1.assertHasPassed(true) + + clock += 1.milliseconds + + markFuture1.assertHasPassed(true) + markPast1.assertHasPassed(true) + } } \ No newline at end of file diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index e3c8f0825a5..ce9ef01e313 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -5288,6 +5288,8 @@ public abstract interface class kotlin/time/Clock { public abstract class kotlin/time/ClockMark { public fun ()V public abstract fun elapsedNow ()D + public final fun hasNotPassedNow ()Z + public final fun hasPassedNow ()Z public fun minus-LRDsOJo (D)Lkotlin/time/ClockMark; public fun plus-LRDsOJo (D)Lkotlin/time/ClockMark; }