Skip to content

Commit

Permalink
Merge pull request #782 from ashawley/choose-cleanup
Browse files Browse the repository at this point in the history
Further cleanup of ChooseSpecification
  • Loading branch information
larsrh authored Apr 29, 2021
2 parents cb8a89a + e21cd3d commit 68a3172
Showing 1 changed file with 65 additions and 90 deletions.
155 changes: 65 additions & 90 deletions jvm/src/test/scala/org/scalacheck/ChooseSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,37 @@ object ChooseSpecification extends Properties("Choose") with time.OrderingVersio
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) => Prop.forAll(g) { x => l <= x && x <= h }
case Failure(_) => l > h
case Success(g) => Prop.forAll(g) { x => l <= x && x <= h }
case Failure(e: Choose.IllegalBoundsError[_]) => l > h
case Failure(e) => throw e
}
}

property("choose-duration") = chooseProp[Duration]
property("choose[Duration]") = chooseProp[Duration]

property("choose-instant") = chooseProp[Instant]
property("choose[Instant]") = chooseProp[Instant]

property("choose-month") = chooseProp[Month]
property("choose[Month]") = chooseProp[Month]

property("choose-year") = chooseProp[Year]
property("choose[Year]") = chooseProp[Year]

property("choose-localTime") = chooseProp[LocalTime]
property("choose[LocalTime]") = chooseProp[LocalTime]

property("choose-localDate") = chooseProp[LocalDate]
property("choose[LocalDate]") = chooseProp[LocalDate]

property("choose-localDateTime") = chooseProp[LocalDateTime]
property("choose[LocalDateTime]") = chooseProp[LocalDateTime]

property("choose-monthDay") = chooseProp[MonthDay]
property("choose[MonthDay]") = chooseProp[MonthDay]

property("choose-zoneOffset") = chooseProp[ZoneOffset]
property("choose[ZoneOffset]") = chooseProp[ZoneOffset]

property("choose-offsetTime") = chooseProp[OffsetTime]
property("choose[OffsetTime]") = chooseProp[OffsetTime]

property("choose-offsetDateTime") = chooseProp[OffsetDateTime]
property("choose[OffsetDateTime]") = chooseProp[OffsetDateTime]

property("choose-yearMonth") = chooseProp[YearMonth]
property("choose[YearMonth]") = chooseProp[YearMonth]

property("choose-zonedDateTime") = chooseProp[ZonedDateTime]
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
Expand All @@ -66,16 +67,17 @@ object ChooseSpecification extends Properties("Choose") with time.OrderingVersio
Gen.choose(Duration.ofSeconds(Long.MinValue), Duration.ofSeconds(Long.MaxValue - 1L, 999999999L))

// https://github.com/typelevel/scalacheck/issues/762
property("handle-min-nanos-duration") = Prop.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)
}
property("choose[Duration](min, max.withNanos") =
Prop.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
Expand All @@ -85,113 +87,86 @@ object ChooseSpecification extends Properties("Choose") with time.OrderingVersio
Gen.choose(Instant.MIN, Instant.MAX.minusSeconds(1L))

// https://github.com/typelevel/scalacheck/issues/762
property("handle-min-nanos-instant") = Prop.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)
}
property("choose[Instant](min, max.withNanos)") =
Prop.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)
}

property("choose-int") = Prop.forAll { (l: Int, h: Int) =>
Try(choose(l, h)) match {
case Success(g) => Prop.forAll(g) { x => l <= x && x <= h }
case Failure(_) => Prop(l > h)
property("choose[Int]") =
Prop.forAll { (l: Int, h: Int) =>
checkChoose(l, h)
}
}

property("choose-long") = Prop.forAll { (l: Long, h: Long) =>
Try(choose(l, h)) match {
case Success(g) => Prop.forAll(g) { x => l <= x && x <= h }
case Failure(_) => Prop(l > h)
property("choose[Long]") =
Prop.forAll { (l: Long, h: Long) =>
checkChoose(l, h)
}
}

property("choose-double") = Prop.forAll { (l: Double, h: Double) =>
Try(choose(l, h)) match {
case Success(g) => Prop.forAll(g) { x => l <= x && x <= h }
case Failure(_) => Prop(l > h)
property("choose[Double]") =
Prop.forAll { (l: Double, h: Double) =>
checkChoose(l, h)
}
}

import Double.{MinValue, MaxValue}
property("choose-large-double") = Prop.forAll(choose(MinValue, MaxValue)) { x =>
MinValue <= x && x <= MaxValue && !x.isNaN
}
property("choose(MinValue, MaxValue)") =
Prop.forAll(choose(MinValue, MaxValue)) { x =>
MinValue <= x && x <= MaxValue && !x.isNaN
}

import Double.{NegativeInfinity, PositiveInfinity}
property("choose-infinite-double") = {
property("choose(NegativeInfinity, PositiveInfinity)") =
Prop.forAll(Gen.choose(NegativeInfinity, PositiveInfinity)) { x =>
NegativeInfinity <= x && x <= PositiveInfinity && !x.isNaN
}
}

property("choose-infinite-double-fix-zero-defect-379") = {
property("choose(NegativeInfinity, PositiveInfinity)") =
Prop.forAllNoShrink(listOfN(3, choose(NegativeInfinity, PositiveInfinity))) { xs =>
xs.exists(_ != 0d)
}
}

val manualBigInt: Gen[BigInt] =
nonEmptyContainerOf[Array, Byte](Arbitrary.arbitrary[Byte]).map(BigInt(_))

property("choose-big-int") =
property("choose[BigInt]") =
Prop.forAll(manualBigInt, manualBigInt) { (l: BigInt, h: BigInt) =>
Try(choose(l, h)) match {
case Success(g) => Prop.forAll(g) { x => l <= x && x <= h }
case Failure(e: Choose.IllegalBoundsError[_]) => Prop(l > h)
case Failure(e) => throw e
}
checkChoose(l, h)
}

property("choose-java-big-int") =
property("choose[BigInteger]") =
Prop.forAll(manualBigInt, manualBigInt) { (x0: BigInt, y0: BigInt) =>
val (x, y) = (x0.bigInteger, y0.bigInteger)
Try(choose(x, y)) match {
case Success(g) => Prop.forAll(g) { n => x.compareTo(n) <= 0 && y.compareTo(n) >= 0 }
case Failure(e: Choose.IllegalBoundsError[_]) => Prop(x.compareTo(y) > 0)
case Failure(e) => throw e
}
checkChoose(x, y)
}

property("Gen.choose(BigInt( 2^(2^18 - 1)), BigInt(-2^(2^18 - 1)))") = {
property("choose(BigInt( 2^(2^18 - 1)), BigInt(-2^(2^18 - 1)))") = {
val (l, h) = (BigInt(-2).pow(262143), BigInt(2).pow(262143))
Prop.forAllNoShrink(Gen.choose(l, h)) { x =>
l <= x && x <= h
}
checkChoose(l, h)
}

property("choose-big-decimal") =
property("choose[BigDecimal]") =
Prop.forAll { (x0: Double, y0: Double) =>
val (x, y) = (BigDecimal(x0), BigDecimal(y0))
Try(choose(x, y)) match {
case Success(g) => Prop.forAll(g) { n => x <= n && n <= y }
case Failure(e: Choose.IllegalBoundsError[_]) => Prop(x > y)
case Failure(e) => throw e
}
checkChoose(x, y)
}

property("choose-java-big-decimal") =
property("choose[BigDecimal]") =
Prop.forAll { (x0: Double, y0: Double) =>
val (x, y) = (BigDecimal(x0).bigDecimal, BigDecimal(y0).bigDecimal)
Try(choose(x, y)) match {
case Success(g) => Prop.forAll(g) { n => x.compareTo(n) <= 0 && y.compareTo(n) >= 0 }
case Failure(e: Choose.IllegalBoundsError[_]) => Prop(x.compareTo(y) > 0)
case Failure(e) => throw e
}
checkChoose(x, y)
}

property("choose-xmap") = {
property("xmap[Long,Date]") = {
implicit val chooseDate: Choose[Date] =
Choose.xmap[Long, Date](new Date(_), _.getTime)
Prop.forAll { (l: Date, h: Date) =>
Try(choose(l, h)) match {
case Success(g) => Prop.forAll(g) { x => x.compareTo(l) >= 0 && x.compareTo(h) <= 0 }
case Failure(_) => Prop(l.after(h))
}
checkChoose(l, h)
}
}
}

0 comments on commit 68a3172

Please sign in to comment.