310

# ScalaCheck August 28, 2013

## Transcript

1. SCALACHECK

2. SCALACHECK
Pedro Matiello
[email protected]
@pmatiello

3. SCALACHECK
• Property-based testing
• Scala

4. PROPERTIES

5. PROPERTIES
def sum(x:Int, y:Int) = x + y

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

7. 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)
}

8. 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)
}

9. 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

10. 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

11. 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

12. CONDITIONAL PROPERTIES

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

14. 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
}

15. 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
}

16. 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

17. 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

18. CONDITIONAL PROPERTIES
scala> 51047075 + 2096436573
res0: Int = -2147483648

19. CONDITIONAL PROPERTIES
scala> 51047075 + 2096436573
res0: Int = -2147483648
scala> 51047075l + 2096436573l
res1: Long = 2147483648

20. GENERATORS

21. GENERATORS
def sum(x:Int, y:Int) = x + y
# ∀x,y, x%2=0,y%2=0 : sum(x,y)%2 = 0

22. 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)

23. 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

24. 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

25. 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

26. 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

27. SCALACHECK
Pedro Matiello
[email protected]
@pmatiello