5

I'm trying to implement a simple property check but Scalacheck is ignoring my generators. What I'm doing wrong here?

object AlgorithmTest extends Properties("Algorithm") { property("Test") = forAll (Gen.choose(0,10)) (n => n>=0 & n<10) } 

and this is the result in SBT

[info] ! Algorithm.Test: Falsified after 12 passed tests. [info] > ARG_0: -1 [error] Failed: : Total 1, Failed 1, Errors 0, Passed 0, Skipped 0 
1
  • The issue has now been fixed (for built-in generators) via the use of suchThat postconditions - see github.com/rickynils/scalacheck/issues/8 Commented Jul 23, 2014 at 15:15

1 Answer 1

5

It looks like the Shrink instance which is passed to the forAll method is not using the generator when searching for smaller counter-examples. If you change your property to:

property("Test") = Prop.forAllNoShrink(Gen.choose(1, 10)) (n => n >= 0 && n < 10) 

Then it should properly fail with:

[info] ! Algorithm.Test: Falsified after 7 passed tests. [info] > ARG_0: 10 [error] Failed: : Total 1, Failed 1, Errors 0, Passed 0, Skipped 0 

One way to visualize the Shrink values is to use the Prop.collect method:

property("Test") = Prop.forAll(Gen.choose(1, 10)) { n => Prop.collect(n) { n >= 0 && n < 10 } } 

Then the collected values look like:

[info] ! Algorithm.Test: Falsified after 40 passed tests. [info] > ARG_0: -1 [info] > Collected test data: [info] 17% 3 [info] 17% 1 [info] 15% 6 [info] 12% 9 [info] 10% 2 [info] 10% 5 [info] 7% 4 [info] 7% 8 [info] 2% -1 [info] 2% 7 

Where you can see that -1 has been used during the shrinking process.

Sign up to request clarification or add additional context in comments.

4 Comments

But this is not the documented behaviour. If I use a generator between 1 and 10 all the values must be in the range [1,10[ . Never should appear a -1
@ancechu The -1 appears after the test failed, as ScalaCheck tries to find simpler input that fails the test -- that's the "shrinking". That phase doesn't respect the generation process -- whether that's a bug or an intrinsic limitation, I don't know.
I should have searched yesterday but this issue is actually discussed here
The issue has now been fixed (for built-in generators) via the use of suchThat postconditions - see github.com/rickynils/scalacheck/issues/8

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.