Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Off By One Error In JavaTimeChoose #769

Merged
merged 3 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions jvm/src/main/scala/org/scalacheck/time/JavaTimeChoose.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private[scalacheck] trait JavaTimeChoose {
if(seconds == minSeconds) {
min.getNano
} else {
1
0
}
val maxNanos: Int =
if (seconds == maxSeconds) {
Expand Down Expand Up @@ -57,7 +57,7 @@ private[scalacheck] trait JavaTimeChoose {
if (epochSecond == min.getEpochSecond) {
min.getNano
} else {
1
0
}
val maxNano: Int =
if (epochSecond == max.getEpochSecond) {
Expand Down
51 changes: 47 additions & 4 deletions jvm/src/test/scala/org/scalacheck/time/GenSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import org.scalacheck.Prop._
import org.scalacheck.Shrink._
import org.scalacheck._
import scala.util._
import java.time.temporal.TemporalUnit
import java.time.temporal.ChronoUnit

object GenSpecification extends Properties("java.time Gen") with OrphanInstances {

private[this] def chooseProp[A](implicit C: Choose[A], A: Arbitrary[A], O: Ordering[A]): Prop = {
import O.mkOrderingOps
private[this] def chooseProp[A](implicit C: Choose[A], A: Arbitrary[A], O: Ordering[A]): Prop =
forAll { (l: A, h: A) =>
Try(choose(l, h)) match {
checkChoose(l, h)
}

private[this] def checkChoose[A](l: A, h: A)(implicit C: Choose[A], O: Ordering[A]): Prop = {
import O.mkOrderingOps
Try(choose(l, h)) match {
case Success(g) => forAll(g) { x => l <= x && x <= h }
case Failure(_) => Prop(l > h)
}
}
}

Expand Down Expand Up @@ -44,4 +49,42 @@ object GenSpecification extends Properties("java.time Gen") with OrphanInstances
property("choose-yearMonth") = chooseProp[YearMonth]

property("choose-zonedDateTime") = chooseProp[ZonedDateTime]

/** Generate a duration which is at least 1 second smaller than the max
* duration the type can support. We use this to avoid the incredibly
* unlikely event of overflowing in the handle-min-nanos-duration test.
*/
private[this] lazy val genOneSecondLessThanMaxDuration: Gen[Duration] =
Gen.choose(Duration.ofSeconds(Long.MinValue), Duration.ofSeconds(Long.MaxValue - 1L, 999999999L))

// https://github.com/typelevel/scalacheck/issues/762
property("handle-min-nanos-duration") = forAllNoShrink(genOneSecondLessThanMaxDuration){(min: Duration) =>
// At most one second larger, with 0 in nanos.
val max: Duration =
min.plusSeconds(1L).withNanos(0)

// We either select the min second value or the max second value. In the
// case where we select the max second value the valid nano range is [0,0]
// (was incorrectly [1,0] in the past which caused an exception).
checkChoose(min, max)
}

/** Generate an Instant which is at least 1 second smaller than the max
* Instant the type can support. We use this to avoid the incredibly
* unlikely event of overflowing in the handle-min-nanos-instant test.
*/
private[this] lazy val genOneSecondLessThanMaxInstant: Gen[Instant] =
Gen.choose(Instant.MIN, Instant.MAX.minusSeconds(1L))

// https://github.com/typelevel/scalacheck/issues/762
property("handle-min-nanos-instant") = forAllNoShrink(genOneSecondLessThanMaxInstant){(min: Instant) =>
// At most one second later, with 0 in nanos.
val max: Instant =
min.plusSeconds(1L).truncatedTo(ChronoUnit.NANOS)

// We either select the min second value or the max second value. In the
// case where we select the max second value the valid nano range is [0,0]
// (was incorrectly [1,0] in the past which caused an exception).
checkChoose(min, max)
}
}