QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one...

37
QuickCheck Koen Lindström Claessen

Transcript of QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one...

Page 1: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

QuickCheck Koen Lindström Claessen

Page 2: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

QuickCheck

●  A testing library for Haskell in 2000 o  Koen Claessen o  John Hughes

●  Now the “standard” way of testing Haskell

programs

●  Also: Erlang, C, C++, Java, OCaml, Python, Isabelle, Coq, ...

Page 3: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Quviq AB

●  A testing company founded in the early 2000s

●  Commercial version of Erlang QuickCheck o  State machines o  Property libraries for industrial applications o  ...

Page 4: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

QuickCheck Success Stories

PULSE - Concurrent Software

XMonad, darcs

Compiler testing

model checkers

Page 5: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

QuickCheck

●  Properties o  one aspect of functionality of the code

●  Random test data

o  Each time you get a new test case o  Library for crafting generators

●  Shrinking

o  Understanding the failing case o  (Avoid getting to the same failing case every time!)

Page 6: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Testing Implementations of Complex Algorithms

using contrapositive testing, inductive testing, and co-inductive testing

Koen Lindström Claessen

Chalmers, Gothenburg, Sweden and Utrecht University

Page 7: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

System Under Test

test cases

Oracle

ok?

Page 8: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Oracle

●  Simple o  Simpler than the implementation

●  Practically runnable

o  May need to run many tests

●  Oracle should be “complete” o  For any faulty implementation, there should exist

inputs that trigger the oracle to say “no”

Page 9: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Shortest Path Algorithms

Page 10: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

type  Map  type  Point  type  Path      shortest  :  (Map,  Point,  Point)  -­‐>  Maybe  Path  

(  solve  :  Problem  -­‐>  Maybe  Solution  )  

Page 11: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

●  The oracle needs to know what the shortest path is

●  We can be simple, but it is too slow o  Not practical when testing o  (Non-termination!)

●  We can be fast, but it is too complex

o  We may not trust our test results

Problem

Page 12: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Property-based Testing

(a la QuickCheck)

Page 13: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Sound - If an answer is produced, it should be an actual solution

Complete - If no answer is produced, there indeed was no actual solution

Optimal - If an answer is produced, there is no actual solution that is better

Page 14: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Complete - If no answer is produced, there indeed was no actual solution

Complete’ - If there is a solution, some answer will be produced

logically equivalent

testable

Page 15: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

ForAll x . A(x) ==> B(x)

ForAll x in “A”. B(x)

Page 16: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

ForAll mp,a,b . hasPath mp a b ==> isJust (shortest (mp, a, b))

ForAll mp,a,b in hasPathMap . isJust (shortest (mp, a, b))

Page 17: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

logically equivalent

testable

Optimal - If an answer is produced, there is no actual solution that is better

Optimal’ - If there is a solution, then no worse answer will be produced

?

Page 18: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Contrapositive testing

●  Change your viewpoint o  From: Stimuli / System Under Test / Oracle

o  To: Logical implication

●  And take the contrapositive view to get new

inspiration

●  Sometimes, you have a choice! (How to make it?)

Page 19: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Contrapositive Testing

?

Page 20: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Shortest Distance Algorithms

Page 21: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

type  Map  type  Point  data  Distance  =  Inf  |  Fin  Int      distance  :  (Map,  Point,  Point)  -­‐>  Distance  

Page 22: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Sound - If an answer is produced, it should be an actual solution

Complete - If no answer is produced, there indeed was no actual solution

Optimal - If an answer is produced, there is no actual solution that is better

Page 23: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

ForAll  mp,a,a  .      distance(mp,a,a)  ==  Fin  0  

ForAll  mp,a,b  .      distance(mp,a,b)  ==          minimum  [  distance(mp,a’,b)  +  d                          |  (a’,d)  <-­‐  neighbors(mp,a)                          ]  

Page 24: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

●  Correctness: by induction o  soundness: induction over actual distance o  completeness: induction over function answer

●  Induction principle

o  choose this for enabling testing o  independent of implementation (unlike proving)

●  Induction vs. recursion in implementation

o  too slow to use directly (even non-terminating) o  Plotkin induction

Inductive Testing What happens

to fault distribution?

Page 25: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Testing SAT-solvers

Page 26: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

●  If model and proof are generated o  Direct soundness o  Direct completeness

●  If only model is generated when found

o  Direct soundness o  Contrapositive testing for completeness

●  If only yes/no answer

o  Inductive testing o  Base case: no variables o  Step case: branch on a variable

Testing SAT-solvers

Page 27: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Testing Sorting

Page 28: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

●  Write down the simplest sorting function you can think of o  You trust this code

●  Show that the function you want to test has

the same behavior o  How?

Testing sorting functions

Page 29: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Testing FFT implementations

Page 30: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

●  Using exact arithmetic o  Implementation is still fast o  Specification is extremely slow

●  Base cases

o  vectors [0,..,0,1,0,..,0]

●  Step cases o  a * fft v = fft (a*v) o  fft v + fft w = fft (v + w)

Testing FFT

Page 31: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Testing Model Checkers for Safety Properties

Page 32: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Circuit

bad

s s’

s0

Page 33: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

check : (State, Circuit) -> Bool

False: The circuit is not safe; often

produces a trace

True: The circuit is safe; (produces nothing)

Page 34: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

ForAll s, C . check(s, C) ==> ForAll inp . let (ok, s’) = step(s, C, inp) in ok && check(s’, C)

step : (State, Circuit, Input) -> (Bool, State)

Page 35: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

a ≤ F(a)

a ≤ gfp x . F(x)

●  Correctness o  Safety is defined as greatest fixpoint o  Most natural is to use coinduction

o  Can also use induction (over the length of the shortest missed countertrace)

●  Efficiency o  Model checker is called twice for each test

Page 36: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

●  Break away from the stimuli / system under test / oracle view

●  Look at the logical meaning of the property ●  Use proof techniques to “break up” into

smaller properties o  Together, they imply the original property o  They may be easier to test o  The system may be run several times

●  What happens to the distribution of faulty test cases?

Inductive Testing

Page 37: QuickCheck - ChalmersXMonad, darcs Compiler testing model checkers . QuickCheck Properties o one aspect of functionality of the code Random test data o Each time you get a new test

Ongoing Work

●  More examples o  Testing compilers / interpreters o  Theorem provers for decidable logics o  Theorem provers for semi-decidable logics o  Unification algorithm o  Distributed systems o  …

●  Develop “testing logic” o  Logical equivalence o  Testing non-equivalence o  Cost of testing o  Predict which testing ways are most effective