Page 1

Test Data Generators

Page 2

Remember IO?

Last time we looked at how IO works in Haskell

Page 3

Random testing with QuickCheck

Page 4

Test data generators

Page 5

Sampling test data

Page 6

Sampling Bool

Page 7

Sampling Lists

Page 8

Functions for creating generators

Page 9

Sampling generators

Page 10

Generating a Suit

Page 11

Generating a Rank

Remember the type Rank?

Page 12

Generating a Card?

Remember the type
Card
?

Page 13

Gen vs IO

Page 14

Generating a Card

rCard :: Gen Card
rCard = do r <- rRank
           s <- rSuit
           return (Card r s)
Page 15

More examples

Page 16

More examples 2

Page 17

Generating a Hand

Remember the type Hand?

Page 18

Telling QuickCheck which generator to use

Page 19
Telling QuickCheck which generator to use

Two ways to test if all generated cards are valid:

Page 20

Arbitrary

Page 21

Test Data Distribution

Page 22

Distribution of Hands

instance Arbitrary Hand where
  arbitrary = rHand

size Empty = 0
size (Add c h) = 1+size h

prop_Hand h = collect (size h) True
Page 23
Distribution of Hands
Page 24

More big Hands

We can increase the probability that a non-empty hand is generated.

rHand :: Gen Hand
rHand = frequency [(1,return Empty),
                   (4,do c <- rCard
                         h <- rHand
                         return (Add c h))]
Page 25
More big Hands
Page 26

Testing properties of algorithms

Page 27

Testing insert

Page 28

Testing insert, first try

Page 29

Testing insert, second try

Page 30

Testing ordered lists

Page 31

OrderedList

Page 32

Summary