zell
zell

Reputation: 10204

How to express "implies" in ScalaCheck, say, "if an integer n * n = 0 then n = 0"?

I would like to use Scala's property-based testing tool ScalaCheck to express a property

if an integer n * n = 0 then n = 0 

How can I write this property in ScalaCheck? I know for example

val myprop = forAll {(n: Int) => n + 1 - 1 = n}

But I do not know how to express "A implies B" in ScalaCheck (without reducing it to Not-A or B, which can look clumsy).

Upvotes: 0

Views: 145

Answers (1)

phongnt
phongnt

Reputation: 505

Use ==> (implication operator)

val prop = forAll { n: Int =>
  (n * n == 0) ==> n == 0
}

(see their User Guide ) the catch is: in this particular example the condition is very hard to satisfy so ScalaCheck will give up after several tries (but at least it does tell you so, otherwise you get a false positive because your necessary condition was never checked). In that case you can provide a custom generator so that it will generate values that satisfy your condition.

Upvotes: 1

Related Questions