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] #631

Closed
dmurvihill opened this issue Feb 28, 2020 · 3 comments
Closed

Add Choose[BigInt] #631

dmurvihill opened this issue Feb 28, 2020 · 3 comments

Comments

@dmurvihill
Copy link
Contributor

ScalaCheck's arbitrary BigInt is bounded, which can be very limiting for some applications and requires e.g. scientific and cryptographic users to implement custom generators. Providing a Choose[BigInt] implementation could save considerable boilerplate. This is my implementation:

  implicit object chooseBigInt extends Gen.Choose[BigInt] {
    @tailrec
    def choose(min: BigInt, max: BigInt): Gen[BigInt] = {
      val n = min + BigInt((max - min).bitLength, Random)
      if (n > max) choose(min, max) else n
    }
  }

Any interest in a PR?

@ashawley
Copy link
Contributor

Yeah, the bounds of arbitrary BigInt is by design. An implementation of Gen.choose[BigInt](min,max) is missing, however. Yes, a contribution would be welcome for BigInt, and for BigDecimal if you're interested.

@ashawley
Copy link
Contributor

ashawley commented Mar 9, 2020

A ticket for BigDecimal is #637. I thought there might be some commonality, but it seems like BigInt may be a dependency of BigDecimal.

@ashawley
Copy link
Contributor

ashawley commented May 6, 2020

@dmurvihill has an open PR in #636. We began to realize that there are practical limits of what ScalaCheck can offer in coverage of BigInt values in terms of performance. As @non pointed out, there are other practical issues, including printing large failing BigInt values. Given these limits, I wonder what safe guards ScalaCheck should provide to Gen.choose[BigInt]?

@ashawley ashawley added this to the 1.15.0 milestone Jun 4, 2020
non added a commit to non/scalacheck that referenced this issue Jul 1, 2020
In addition to implicit Choose instances for scala.math.BigDecimal and
java.math.BigDecimal we also include explicit constructor methods, since users
may wish to be explicit about the scale they want. We may want to put those
methods directly on Gen, currently the ergonomics of using this are a bit bad:

    Gen.Choose.chooseBigDecimalScale(100).choose(0, 1)

The BigDecimal generation is not yet tested. That will also be added in a
follow up. This also optimizes the BigInt generator a bit and generalizes it to
java.math.BigInteger to support that as well.

Addresses typelevel#631, typelevel#637, and typelevel#664
@larsrh larsrh closed this as completed Oct 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants