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 1 commit
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
7 changes: 7 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,13 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp
}
}

property("choose-big-int") = forAll { (l: BigInt, h: BigInt) =>
Try(choose(l, h)) match {
case Success(g) => forAll(g) { l <= x && x <= h }
ashawley marked this conversation as resolved.
Show resolved Hide resolved
case Failure(_) => l > h
}
}

property("choose-xmap") = {
implicit val chooseDate: Choose[Date] =
Choose.xmap[Long, Date](new Date(_), _.getTime)
Expand Down
11 changes: 11 additions & 0 deletions src/main/scala/org/scalacheck/Gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,12 @@ object Gen extends GenArities with GenVersionSpecific {
}
}

@tailrec
private def chBigInt(l: BigInt, h: BigInt)(p: P, seed: Seed): R[BigInt] = {
val (x, seed2) = seed.bigInt((h - l).bitLength)
if (x > h) chBigInt(l, h)(p, seed2) else r(Some(x), seed2)
}

implicit val chooseLong: Choose[Long] =
new Choose[Long] {
def choose(low: Long, high: Long): Gen[Long] =
Expand Down Expand Up @@ -454,6 +460,11 @@ 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] =
gen(chBigInt(low, high))
}

/** 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
20 changes: 20 additions & 0 deletions src/main/scala/org/scalacheck/rng/Seed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ sealed abstract class Seed extends Serializable {
* The values will be uniformly distributed, and will be contained
* in the interval [0.0, 1.0). */
def double: (Double, Seed) = ((d >>> 11) * 1.1102230246251565e-16, next)

/**
* Generates a big integer value.
*
* The values will be uniformly distributed, and will be contained in
* the interval [0, 2 ^ bitLength).
* */
def bigInt(bitLength: Int): (BigInt, Seed) = bigInt(0, bitLength)

@tailrec
private def bigInt(acc: BigInt, bitLength: Int): (BigInt, Seed) = {
val blockLength = 64
val (raw, next) = long
val block = (BigInt(raw) - Long.MinValue) >> (blockLength - bitLength)
if (bitLength <= blockLength) {
(acc + block, next)
} else {
next.bigInt(block << blockLength, bitLength - blockLength)
}
}
}

object Seed {
Expand Down