Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
fix: Incomplete domain of Java Instant generation (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmiikkkkaa authored Oct 20, 2020
1 parent 76e7ad5 commit 6ed57fc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public fun Generator.Companion.instants(
else 0

val maxNano =
if (instant.epochSecond == instant.epochSecond) max.nano
if (instant.epochSecond == max.epochSecond) max.nano
else MAX_NANOSECONDS

instant.with(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ class InstantGeneratorTest : AbstractGeneratorTest() {
val max = min.plusSeconds(50)
assertTrue(Generator.instants(min = min, max = max).randomSequence(0).take(50).any { it == min })
}

@Test
fun `generate values within complete nanos range if epochsecond is not min boundary`() {
val minNanoBoundary = MAX_NANOSECONDS - 10
val min = Instant.ofEpochSecond(100, minNanoBoundary)
val max = Instant.ofEpochSecond(102, MAX_NANOSECONDS)
assertTrue(Generator.instants(min = min, max = max)
.randomSequence(0)
.take(50)
.filter { it.epochSecond > min.epochSecond }
.any { it.nano < minNanoBoundary })
}

@Test
fun `generate values within complete nanos range if epochsecond is not max boundary`() {
val maxNanoBoundary = 10L
val min = Instant.ofEpochSecond(100, 0)
val max = Instant.ofEpochSecond(102, maxNanoBoundary)
assertTrue(Generator.instants(min = min, max = max)
.randomSequence(0)
.take(50)
.filter { it.epochSecond < max.epochSecond }
.any { it.nano > maxNanoBoundary })
}

}

class LocalTimeGeneratorTest : AbstractGeneratorTest() {
Expand Down

0 comments on commit 6ed57fc

Please sign in to comment.