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

Simple check leads to infinite loop #244

Closed
danielkarch opened this issue Jun 3, 2016 · 0 comments · Fixed by #278
Closed

Simple check leads to infinite loop #244

danielkarch opened this issue Jun 3, 2016 · 0 comments · Fixed by #278

Comments

@danielkarch
Copy link

Hi,

I just started playing around with ScalaCheck and encountered a problem. The following does not terminate on my machine with ScalaCheck 1.13.[0|1]:

import org.scalacheck.Properties
import org.scalacheck.Prop.forAll

object HelloSpec extends Properties("Hello") {

  def replaceLettersWithX(str: String) = {

    def replaceLetter(ch: Char): Char = {
      if (ch.isLetter) {
        'X'
      } else {
        ch
      }
    }

    str map replaceLetter
  }

  property("is actually the identity function") = forAll { (str: String) =>
    val withX: String = replaceLettersWithX(str)
    withX == str
  }

}

With version 1.12.5, a counter-example is produced quickly. With the newer versions, it gets stuck in an infinite loop. I tried to debug it and it seems like once ScalaCheck has found a counter-example, it is trying to shrink it. The shrinking however only keeps replacing one 1-character string with another.

non pushed a commit to non/scalacheck that referenced this issue Oct 17, 2016
This was reported as typelevel#244. The root cause was that shrinking certain
values would produce an infinite stream. There were several ways this
could happen -- in the reported case, the fact that Char was unsigned
meant that the strategy of including inverses was able to produce
loops.

The fix was to detect when Integral[T] cannot support negative values,
and disabling negation.

While working on this, I noticed that shrinking floating point
sentinels (i.e. infinities and NaN) would cause a similar issue. I
added code to detect those cases as well.

Fixes typelevel#244.
jonaskoelker added a commit to jonaskoelker/scalacheck that referenced this issue Dec 30, 2020
We all know that we shrink towards 0 and from negative to positive.
Formalizing this is simple and strengthens the typelevel#244 regression tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant