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

Add choose[BigInt] #636

Merged
merged 8 commits into from
May 26, 2020
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
19 changes: 19 additions & 0 deletions jvm/src/test/scala/org/scalacheck/GenSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp
}
}

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

property("Gen.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
}
}

property("choose-xmap") = {
implicit val chooseDate: Choose[Date] =
Choose.xmap[Long, Date](new Date(_), _.getTime)
Expand Down
19 changes: 19 additions & 0 deletions src/main/scala/org/scalacheck/Gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import scala.collection.mutable.ArrayBuffer
import scala.concurrent.duration.{Duration, FiniteDuration}

import java.util.{ Calendar, UUID }
import java.nio.ByteBuffer

sealed abstract class Gen[+T] extends Serializable { self =>

Expand Down Expand Up @@ -454,6 +455,24 @@ object Gen extends GenArities with GenVersionSpecific {
implicit val chooseFiniteDuration: Choose[FiniteDuration] =
Choose.xmap[Long, FiniteDuration](Duration.fromNanos, _.toNanos)

implicit object chooseBigInt extends Choose[BigInt] {
def choose(low: BigInt, high: BigInt): Gen[BigInt] =
if (low > high) throw new IllegalBoundsError(low, high)
else if (low == high) low
else {
val range = high - low
Gen.containerOfN[Array, Long](
(range.bitLength + 64 - 1) / 64,
Gen.choose(Long.MinValue, Long.MaxValue)
).map(longs => {
longs(0) = longs(0) & (-1L >>> (64 - range.bitLength % 64))
val bb = ByteBuffer.allocate(longs.length * 8)
longs.foreach(bb.putLong)
BigInt(1, bb.array()) + low
}).filter(n => high >= n)
}
}

/** Transform a Choose[T] to a Choose[U] where T and U are two isomorphic
* types whose relationship is described by the provided transformation
* functions. (exponential functor map) */
Expand Down