Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ScalaCheck

 ScalaCheck

Presented at Scaladores, August/2013

Pedro Matiello

August 28, 2013
Tweet

More Decks by Pedro Matiello

Other Decks in Programming

Transcript

  1. PROPERTIES def sum(x:Int, y:Int) = x + y # Property:

    ∀x,y : sum(x,y) = sum(y,x) val commutativity = forAll { (x:Int, y:Int) => sum(x,y) == sum(y,x) }
  2. PROPERTIES def sum(x:Int, y:Int) = x + y # Property:

    ∀x,y : sum(x,y) = sum(y,x) val commutativity = forAll { (x:Int, y:Int) => sum(x,y) == sum(y,x) }
  3. PROPERTIES def sum(x:Int, y:Int) = x + y # Property:

    ∀x,y : sum(x,y) = sum(y,x) val commutativity = forAll { (x:Int, y:Int) => sum(x,y) == sum(y,x) } org.scalacheck.Prop.forAll
  4. PROPERTIES def sum(x:Int, y:Int) = x + y # Property:

    ∀x,y : sum(x,y) = sum(y,x) val commutativity = forAll { (x:Int, y:Int) => sum(x,y) == sum(y,x) } commutativity.check
  5. PROPERTIES def sum(x:Int, y:Int) = x + y # Property:

    ∀x,y : sum(x,y) = sum(y,x) val commutativity = forAll { (x:Int, y:Int) => sum(x,y) == sum(y,x) } commutativity.check + OK, passed 100 tests
  6. CONDITIONAL PROPERTIES def sum(x:Int, y:Int) = x + y #

    Property: ∀x,y>0 : sum(x,y) > x
  7. CONDITIONAL PROPERTIES def sum(x:Int, y:Int) = x + y #

    Property: ∀x,y>0 : sum(x,y) > x val greater = forAll { (x:Int, y:Int) => y > 0 ==> sum(x,y) > x }
  8. CONDITIONAL PROPERTIES def sum(x:Int, y:Int) = x + y #

    Property: ∀x,y>0 : sum(x,y) > x val greater = forAll { (x:Int, y:Int) => y > 0 ==> sum(x,y) > x }
  9. CONDITIONAL PROPERTIES def sum(x:Int, y:Int) = x + y #

    Property: ∀x,y>0 : sum(x,y) > x val greater = forAll { (x:Int, y:Int) => y > 0 ==> sum(x,y) > x } greater.check
  10. CONDITIONAL PROPERTIES def sum(x:Int, y:Int) = x + y #

    Property: ∀x,y>0 : sum(x,y) > x val greater = forAll { (x:Int, y:Int) => y > 0 ==> sum(x,y) > x } greater.check ! Falsified after 2 passed tests. > ARG_0: 51047075 > ARG_1: 2096436573
  11. CONDITIONAL PROPERTIES scala> 51047075 + 2096436573 res0: Int = -2147483648

    scala> 51047075l + 2096436573l res1: Long = 2147483648
  12. GENERATORS def sum(x:Int, y:Int) = x + y # ∀x,y,

    x%2=0,y%2=0 : sum(x,y)%2 = 0 val evenInt = arbitrary[Int] suchThat (_ % 2 == 0)
  13. GENERATORS def sum(x:Int, y:Int) = x + y # ∀x,y,

    x%2=0,y%2=0 : sum(x,y)%2 = 0 val evenInt = arbitrary[Int] suchThat (_ % 2 == 0) org.scalacheck.Arbitrary.arbitrary
  14. GENERATORS def sum(x:Int, y:Int) = x + y # ∀x,y,

    x%2=0,y%2=0 : sum(x,y)%2 = 0 val evenInt = arbitrary[Int] suchThat (_ % 2 == 0) forAll(evenInt, evenInt) { (x:Int, y:Int) => sum(x,y) % 2 == 0 }.check
  15. GENERATORS def sum(x:Int, y:Int) = x + y # ∀x,y,

    x%2=0,y%2=0 : sum(x,y)%2 = 0 val evenInt = arbitrary[Int] suchThat (_ % 2 == 0) forAll(evenInt, evenInt) { (x:Int, y:Int) => sum(x,y) % 2 == 0 }.check
  16. GENERATORS def sum(x:Int, y:Int) = x + y # ∀x,y,

    x%2=0,y%2=0 : sum(x,y)%2 = 0 val evenInt = arbitrary[Int] suchThat (_ % 2 == 0) forAll(evenInt, evenInt) { (x:Int, y:Int) => sum(x,y) % 2 == 0 }.check + OK, passed 100 tests