Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
    Keir Lawson
    Hello, I'm wondering how I can create a generator of a list of values sourced from two other generators, but guaranteeing that one of the generators is used at least once?
    Ie I have a genMobilePhone and a genHomePhone andI want to make a genPhoneListWithAtLeastOneMobile
    Obviously I can do Gen.nonEmptyList(Gen.oneOf(genMobilePhone, genHomePhone)) but that wont guarantee I have at least one mobile...
    Katherine Prevost
    I'd generate that list then add one mobile to it and optionally shuffle.
    (Or generate a list, a single required item, and a position in the list, would be a way to do that completely in terms of generating a tuple of existing things.)
    Keir Lawson
    ok, that makes sense, thanks :)
    Ben Spencer
    I'm looking for a way to use magnolia to derive scalacheck generators and google has turned up at least 3 projects, 2 of which have relatively recent commits - is there a preferred / endorsed one?
    Deenar Toraskar
    Hi I was looking to a string generator based on regular expressions, there seems to be a couple of options that turn up https://github.com/wolfendale/scalacheck-gen-regexp and https://github.com/non/antimirov/#scalacheck-support. Has anyone used either of these? Is there a preferred option?
    How can I generate an arbitrary instance of final case class VisitActivityQuestionAssignmentStudyEnvironment( id: UUID, description: String, orderIndex: OrderIndex, methodAssignments: Seq[MethodAssignment] ) { require(description.trim.nonEmpty, "Description parameter should not be empty") }
    Aaron S. Hawley
    Hi! Is scalacheck scaladoc available online ? The link on http://www.scalacheck.org/documentation.html seems to be broken and I cant find any other
    Sergey Torgashov
    @jhnsmth , I guess you can find some info here: https://github.com/typelevel/scalacheck/blob/master/doc/UserGuide.md
    Jakub Kozłowski
    Hi, is there a way to limit the size of an Arbitrary[Int], without "overriding" the implicit instance for it?
    I was trying to change the sizeRange parameter (I'm using scalatest's ScalaCheckPropertyChecks) but that doesn't seem to have an effect.
    ah, apparently numeric generators don't ask for size
    Julien Truffaut
    Hi all, as anyone tried to generate a Gen for ExecutionContext?
    Cédric Gourlay

    Hi all, I tried the 1.15.2 with scala 3.0.0-M3 and got a weird error message

    object Gen: org.scalacheck.Gen
    value foreach is not a member of org.scalacheck.Gen[Int]

    it is something new ? I may have missed something...

    Aaron S. Hawley
    @kidi That's strange. I haven't worked with Scala 3 much, but I also don't recall using foreach with Gen. Would you like to open an issue it so we can study it further?
    Cédric Gourlay
    @ashawley thx for the reply; I tried in 2.13.4 and I actually probably don't use it correctly but with the .sample the option can provide it. Let me know if this kind of usage need an issue ?
    val pw = new PrintWriter(f)
        for {
          sample <- Gen.choose(5000, 100000).sample
          _ <- 0 until sample
          t <- Gen.choose(-500f, 500f).sample
        } {
    Nicolas Rinaudo

    I have a question about the default Shrink[Int] instance.

    From what I can see, it "simply" halves its input it reaches 0. This obviously works, but isn't very precise. Let's say, for example, that you have the following property:

      val propTest = forAll { i: Int =>
        i < 156

    If you get an input of, say, 482, it'll get halved to 241, then get stuck there - it has no way of getting back "up" from (241 / 2) to 156.

    An alternative implementation would be to halve the input, but then explore the larger part of the halved space:

      implicit val intShrink: Shrink[Int] = {
        def halves(inf: Int, sup: Int): Stream[Int] = {
          if(math.abs(sup - inf) <= 1) Stream.empty
          else {
            val mid = inf + (sup - inf) / 2
            mid #:: halves(mid, sup)
        Shrink(i => halves(0, i))

    I've ran some tests and this seems to consistently find the most precise value possible.

    Was there any reason it wasn't implemented that way? For example, "hoping" for a value closer to 0 and avoiding getting stuck in a local optimum?

    Aaron S. Hawley
    @nrinaudo Well, complexity would be a concern, but I presume it was implemented this way since it was the simplest thing. Others have raised making this improvement of shrinking on a known range and not just zero. It seems worth pursuing.
    Fortunately, not a lot of property testing is numerical it's structural or algebraic. Not that it's worth improving, but might explain why this doesn't come up, often.
    To be fair to the current implementation, numerical test generators are supposed to run with increasing values so a failure would cause shrinking to use decreasing values, but it's not hard to find trivial cases, as you have, where that doesn't work optimally.
    Nicolas Rinaudo

    @ashawley yeah I've actually pushed the experience further and it's not actually a good idea. Binary search works because you keep a running context - [a, b]. With shrinking, you do have that context while looking for the next failing test case, but you lose it as soon as you find that test case.

    That is, you can be looking for your local minimum in [100, 150], and you'll end up searching in [0, 125].

    You end up spending far more time looking at values you've already explored, and I'm not at all convinced it's safe - there *must be some infinite loop scenario

    Basavaraj Kalloli
    Hello i have just updated Scala Version to 2.13.4, trying to get rid off these Stream warnings:
    def noShrink[T]: Shrink[T] = Shrink(_ => Stream.empty)
    But I see the scalacheck library still expects a Stream, whats the recommendation here?
    João Costa
    you might have to use @nowarn, not sure if that's the only way
    Aaron S. Hawley
    @scalolli Doesn't the warning suggest using LazyList?
    Actually, we couldn't overload that method to support LazyList. We had to add a new method withLazyList in #627.
    3 replies
    Aaron S. Hawley
    If you're having to support 2.12 or earlier, then there may be nothing you can do about the warning.
    Artem Nikiforov

    Hi, I want to include some properties into ordinary AnyFunSuite, for example

    MyAwesomeTest extends AnyFunSuite {
      test("ordinary test") {
      test("scalacheck test" {
        val prop = forAll { b: Boolean =>

    How can I launch property check in that scenario? I want to have a red failed test instead of

    ! Falsified after 0 passed tests.
    > ARG_0: 0
    > ARG_0_ORIGINAL: 71

    I haven't found the answer in https://github.com/typelevel/scalacheck/blob/master/doc/UserGuide.md

    Aaron S. Hawley
    @nikiforo That looks like Scalatest. You might refer to their docs https://www.scalatest.org/user_guide/writing_scalacheck_style_properties
    You can probably just write
    test("scalacheck test") {
      forAll { b: Boolean =>
    Keir Lawson
    Hey, I'm wanting to generate a string up to a given number of characters, what's the best way to do this? I see I can use listOfN for a fixed length, but not sure how to get the "up to"
    Dmitry Polienko
    Gen.choose(0, max)
      .flatMap(Gen.stringOfN(_, arbitrary[Character]))
    Basavaraj Kalloli
    Another deprecation warnings I am trying to fix with Scala 2.13.4 is with these:
    Auto-application to `()` is deprecated. Supply the empty argument list `()` explicitly to invoke method dispatch,
    or remove the empty argument list from its definition (Java-defined methods are exempt).
    In Scala 3, an unapplied method like this will be eta-expanded into a function.
          forAll {}
    I cannot find an alternative to the forAll {} syntax, is there any obvious alternative that I am missing in the library?
    2 replies
    Aaron S. Hawley
    @scalolli What are you doing with forAll that produces that warning?
    2 replies
    Nicolas Rinaudo
    @keirlawson i think that’s what the sized combinator is meant for
    Nicolas Rinaudo
    The resize combinator, I mean
    Nicolas Rinaudo

    Here's a different attempt at shrinking ints (basically a Scala version of the QuickCheck implementation) which seems to have better results than the default one: https://scastie.scala-lang.org/XbfRtWfFRG6rAKy6mUVbjg

    Note that, contrary to the default ScalaCheck instance, it does not explore negative values, simply because QuickCheck doesn't.

    Is there any reason the QuickCheck implementation wasn't ported over as is?

    If not, I'm happy to make a more generic version and submit a PR...

    Guillaume Martres
    that looks nice!
    João Costa
    fwiw, you have a typo in your example (you have written "Old shrink" twice) :P
    Nicolas Rinaudo
    yeah I realised afterwards, and was kind of hoping no one would pick up on it. Clearly, that was in vain :)
    a couple of important things to note:
    • this implementation can be centered on any number, not just 0, which can be useful when you're working with, say, ints between 50 and 100.
    • it starts by attempting the destination (0, here), because that's the largest possible shrink and will often end up short-circuiting the entire process
    • I take exactly 0 credit for it, it's really just me ripping it from Hedgehog (which, itself, ripped it from QuickCheck)
    Nicolas Rinaudo
    I'd have pasted a sample shrink tree, but since this is so much more thorough than the default shrink, they end up being huge and not terribly useful.

    Hi everyone. I'm trying to generate an ADT that is previously validated. So, instead of returning the actual ADT it is returning a Validate[ADT]. This is the generator:

    val coordinatesGen: Gen[ValidationResult[Coordinates]] = for {
        latitude <- arbitrary[Double].suchThat(BigDecimal(_).scale <= 8).suchThat(_.abs < 90)
        longitude <- arbitrary[Double].suchThat(BigDecimal(_).scale <= 8).suchThat(_.abs < 180)
      } yield {
          latitude = latitude,
          longitude = longitude
      implicit val arbCoordinatesGen: Arbitrary[ValidationResult[Coordinates]] = Arbitrary(coordinatesGen)

    And this is the test:

    property("coordinates") = forAll { coordinates: ValidationResult[Coordinates] =>

    This is not working tho, it seems like no test is passing: Generating coordinates.coordinates: Gave up after only 0 passed tests. 501 tests were discarded.. Is this related to the fact that I'm returning a ValidationResult instead of the actual ADT?

    Christopher Davenport
    You want to use restricted generators rather than using filters like that .