SlideShare a Scribd company logo
QuickCheck: 
A Lightweight Tool for Random Testing 
of Haskell Programs 
Koen Claessen, John Hughes ICFP ‘00 
CMSC 737 Software Testing 
Fang Cheng javran@cs.umd.edu
Prelude - Haskell 
● Functional Programming 
○ Functions as First-class Citizens 
○ More Controls over Side-effects 
○ Implicit Control Flow 
● Strong Static Typing 
○ Reasoning about Types 
○ Rule out “bad programs” at compile time 
○ If it compiles, it works!
Prelude - Haskell 
● Functional Programming 
○ First-class citizen 
■ g . f = λx -> g (f x) 
○ Side effects 
■ nextRandom :: () -> Int 
■ nextRandom :: Seed -> (Int, Seed) 
○ Implicit control flow 
■ sum . map (^2) . filter (< 4) $ [1..10]
Prelude - Haskell 
● Strong Static Typing 
○ Reasoning about Types 
■ f1 :: ∀a . a -> a 
■ f2 :: ∀a b . a -> b 
■ f3 :: ∀a . [a] -> [a]
Prelude - Haskell 
● Strong Static Typing 
○ Reasoning about Types 
■ f1 :: ∀a . a -> a 
● Can only be the identity function 
■ f2 :: ∀a b . a -> b 
● Impossible 
■ f3 :: ∀a . [a] -> [a] 
● Theorems for free! [Wadler 1989] 
● map f . f3 == f3 . map f
Prelude - Haskell 
● Strong Static Typing 
○ f: plus 2 
○ f3: swap the first two element if possible 
map f 
[1,2,3,4] [3,4,5,6] 
f3 f3 
map f 
[2,1,3,4] [4,3,5,6]
Overview - QuickCheck 
● Test Case Generation 
○ Value Generation 
○ Function Generation 
● Output Verification 
○ Describing Desired Properties 
● Case studies 
● Discussion
Test Case Generation - Type Class 
● Type class: ad hoc polymorphism support 
○ in Prolog: 
eq(bool). 
eq(char). 
… 
ord(X) :- eq(X).
Test Case Generation - Arbitrary 
● newtype Gen a = Gen (Rand -> a) 
○ reads “some computation(Gen) that will give you a” 
class Arbitrary a where 
arbitrary :: Gen a 
instance Arbitrary Int where 
arbitrary = choose (-20,20) 
instance (Arbitrary a, Arbitrary b) => Arbitrary (a,b) where 
arbitrary = do {v1 <- arbitrary; v2 <- arbitrary; return (v1,v2)} 
We split a random seed (using a primitive function) into two independent 
seeds, notation “<-” and “return” will take care of that.
Test Case Generation - Arbitrary 
● newtype Gen a = Gen (Rand -> a) 
○ reads “some computation(Gen) that will give you a” 
instance (Arbitrary a, Arbitrary b) => Arbitrary (a,b) where 
arbitrary = liftM2 (,) arbitrary arbitrary 
-- shorthand: 
-- liftM f m1 = do { v1 <- m1; return (f v1) } 
-- liftM2 g m1 m2 = do { v1 <- m1; v2 <- m2; return (g v1 v2) }
Test Case Generation - Arbitrary 
● Generate Recursive Data Structure 
○ data Tree a = Branch (Tree a) (Tree a) 
| Leaf a 
deriving (Show) 
instance (Arbitrary a) => Arbitrary (Tree a) where 
arbitrary = frequency 
[ (1, liftM Leaf arbitrary) 
, (2, liftM2 Branch arbitrary arbitrary) 
]
Test Case Generation - Arbitrary 
● Generate Recursive Data Structure 
○ data Tree a = Branch (Tree a) (Tree a) 
| Leaf a 
deriving (Show) 
instance (Arbitrary a) => Arbitrary (Tree a) where 
arbitrary = frequency 
[ (1, liftM Leaf arbitrary) 
, (2, liftM2 Branch arbitrary arbitrary) 
] 
● The generator might not terminate 
● Likely to produce huge trees
Test Case Generation - Arbitrary 
● Generate Recursive Data Structure 
○ Limit the size 
○ The notion of “size” is hard to define, leave it to 
programmers 
○ newtype Gen a = Gen (Int -> Rand -> a) 
○ sized :: (Int -> Gen a) -> Gen a
Test Case Generation - Arbitrary 
● Generate Recursive Data Structure 
○ data Tree a = Branch (Tree a) (Tree a) 
| Leaf a 
deriving (Show) 
instance (Arbitrary a) => Arbitrary (Tree a) where 
arbitrary = sized arbTree 
arbTree :: Int -> Gen a 
arbTree 0 = liftM Leaf arbitrary 
arbTree n = frequency 
[ (1, liftM Leaf arbitrary) 
, (4, liftM2 Branch (arbTree (n `div` 2) ) 
(arbTree (n `div` 2) ) 
]
Test Case Generation - CoArbitrary 
● Generate Functions 
○ Use input value to perturb the random generator Gen b 
■ class CoArbitrary a where 
■ coarbitrary :: a -> Gen b -> Gen b 
○ We should be able to generate an arbitrary b using Gen b 
○ Use input value a to perturb Gen b so that we will have a modified 
Gen b 
○ This is a pure function because given the same generator, the output 
depends merely on input value.
Output Verification - DSL 
● a Domain Specific Language (DSL) 
embedded in Haskell 
○ Property 
○ Function Equality 
○ Conditional Laws 
○ Classify Input 
○ Custom Data Generator
Output Verification - DSL 
prop_RevUnit x = 
reverse [x] == [x] 
prop_RevApp xs ys = 
reverse (xs++ys) == reverse ys++reverse xs 
prop_RevRev xs = 
reverse (reverse xs) == xs 
● Property 
● Function Equality (===) 
type Endo a = a -> a 
prop_CompAssoc :: Endo Int -> Endo Int -> Endo Int -> Int -> Bool 
prop_CompAssoc f g h = f . (g . h) === (f . g) . h
Output Verification - DSL 
● Conditional Laws (==>) 
○ “A ==> B” is different from “not A || B” 
○ try checking it for 100 test cases satisfying the 
condition 
○ generate only a limited number of test cases 
prop_MaxLe :: Int -> Int -> Property 
prop_MaxLe x y = x <= y ==> max x y == y
Output Verification - DSL 
● Classify Input: classify, collect 
● Custom Data Generator: forAll 
prop_Insert :: Int -> Property 
prop_Insert x = 
forAll orderedList $ xs -> 
classify (null xs) “trivial” $ 
ordered (insert x xs)
Case studies 
● Unification 
○ Random generated terms are unlikely to be unified 
○ New types are introduced to produce a different input distribution 
● Lava 
○ Provide symbolic input and use external theorem prover 
● Pretty Printing 
○ Extending Arbitrary type class to include: 
○ shrink :: a -> [a]
Discussion 
● On Random Testing 
○ a Haskell test framework written in Haskell 
■ Lightweight 
■ Doesn’t tie to a particular implementation 
○ Many criteria need reinterpretation 
■ Reachable statement? 
■ Constraint solving in Haskell program is hard
Discussion 
● Correctness Criteria 
○ a Haskell test framework written in Haskell 
■ Property Language is much more general 
● Function Equality (e.g. current vs. known correct version) 
● Result Checking (e.g. mathematical properties) 
● Conditional Properties (e.g. insert on sorted list) 
● Testing High-Order Functions (e.g. function composition) 
○ No published work on automatic testing of functional 
programs against specification
Discussion 
● Test Data Generation 
○ a Haskell test framework written in Haskell 
■ another DSL for describing test data generation 
● Grammar inherited from Haskell 
● Minimum learning effort 
○ Controlling sizes to guarantee termination 
■ Need developer to 
● Interpret the meaning of “size” 
● Specify generators for his/her own new types
Discussion 
● Some Reflections 
○ Formulating formal specification improves our 
understanding of our programs 
○ Three types of errors divided evenly 
■ Errors in test generators 
■ Errors in the specification 
■ Errors in the program
References 
● Claessen, Koen, and John Hughes. "QuickCheck: a lightweight tool for 
random testing of Haskell programs." Acm sigplan notices 46.4 (2011): 53- 
64. 
● Wadler, Philip. "Theorems for free!." Proceedings of the fourth international 
conference on Functional programming languages and computer 
architecture. ACM, 1989. 
● Weyuker, Elaine J. "On testing non-testable programs." The Computer 
Journal 25.4 (1982): 465-470.

More Related Content

What's hot

Web futures
Web futuresWeb futures
Web futures
Brendan Eich
 
Queue
QueueQueue
Extensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptExtensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScript
Brendan Eich
 
JS Responsibilities
JS ResponsibilitiesJS Responsibilities
JS Responsibilities
Brendan Eich
 
Deriving Scalaz
Deriving ScalazDeriving Scalaz
Deriving Scalaz
nkpart
 
Intro to functional programming - Confoo
Intro to functional programming - ConfooIntro to functional programming - Confoo
Intro to functional programming - Confoo
felixtrepanier
 
Zhongl scala summary
Zhongl scala summaryZhongl scala summary
Zhongl scala summarylunfu zhong
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics
Hitesh-Java
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Keyur Vadodariya
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Brendan Eich
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scaladjspiewak
 
Priority queues
Priority queuesPriority queues
Priority queues
Yeela Mehroz
 
Value objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressValue objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progress
Brendan Eich
 
Knolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in ScalaKnolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in ScalaAyush Mishra
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scala
Ruslan Shevchenko
 
Session 4#
Session 4#Session 4#
Session 4#
Mohamed Samir
 
Ponies and Unicorns With Scala
Ponies and Unicorns With ScalaPonies and Unicorns With Scala
Ponies and Unicorns With Scala
Tomer Gabel
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
jessitron
 

What's hot (20)

Web futures
Web futuresWeb futures
Web futures
 
Queue
QueueQueue
Queue
 
Extensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptExtensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScript
 
JS Responsibilities
JS ResponsibilitiesJS Responsibilities
JS Responsibilities
 
Deriving Scalaz
Deriving ScalazDeriving Scalaz
Deriving Scalaz
 
Slides
SlidesSlides
Slides
 
Intro to functional programming - Confoo
Intro to functional programming - ConfooIntro to functional programming - Confoo
Intro to functional programming - Confoo
 
Zhongl scala summary
Zhongl scala summaryZhongl scala summary
Zhongl scala summary
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Scala - core features
Scala - core featuresScala - core features
Scala - core features
 
Value objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressValue objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progress
 
Knolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in ScalaKnolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in Scala
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scala
 
Session 4#
Session 4#Session 4#
Session 4#
 
Ponies and Unicorns With Scala
Ponies and Unicorns With ScalaPonies and Unicorns With Scala
Ponies and Unicorns With Scala
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
 

Similar to QuickCheck - Software Testing

Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Franklin Chen
 
Scheme 核心概念(一)
Scheme 核心概念(一)Scheme 核心概念(一)
Scheme 核心概念(一)
維然 柯維然
 
Good functional programming is good programming
Good functional programming is good programmingGood functional programming is good programming
Good functional programming is good programming
kenbot
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
Prashant Kalkar
 
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data science
John Cant
 
Functional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersFunctional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 Developers
Jayaram Sankaranarayanan
 
Java 8
Java 8Java 8
Java 8
vilniusjug
 
IIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into RIIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into R
Kevin Smith
 
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
Qbeast
 
Python to scala
Python to scalaPython to scala
Python to scala
kao kuo-tung
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in Scala
Ayush Mishra
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
John De Goes
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
Jordan Open Source Association
 
Meetup C++ A brief overview of c++17
Meetup C++  A brief overview of c++17Meetup C++  A brief overview of c++17
Meetup C++ A brief overview of c++17
Daniel Eriksson
 
002. Introducere in type script
002. Introducere in type script002. Introducere in type script
002. Introducere in type script
Dmitrii Stoian
 
Towards hasktorch 1.0
Towards hasktorch 1.0Towards hasktorch 1.0
Towards hasktorch 1.0
Junji Hashimoto
 
Learn JavaScript From Scratch
Learn JavaScript From ScratchLearn JavaScript From Scratch
Learn JavaScript From Scratch
Mohd Manzoor Ahmed
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)
allanh0526
 
Ruslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingRuslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testing
Ievgenii Katsan
 

Similar to QuickCheck - Software Testing (20)

Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheck
 
Scheme 核心概念(一)
Scheme 核心概念(一)Scheme 核心概念(一)
Scheme 核心概念(一)
 
Good functional programming is good programming
Good functional programming is good programmingGood functional programming is good programming
Good functional programming is good programming
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data science
 
Functional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersFunctional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 Developers
 
ppopoff
ppopoffppopoff
ppopoff
 
Java 8
Java 8Java 8
Java 8
 
IIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into RIIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into R
 
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
 
Python to scala
Python to scalaPython to scala
Python to scala
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in Scala
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
 
Meetup C++ A brief overview of c++17
Meetup C++  A brief overview of c++17Meetup C++  A brief overview of c++17
Meetup C++ A brief overview of c++17
 
002. Introducere in type script
002. Introducere in type script002. Introducere in type script
002. Introducere in type script
 
Towards hasktorch 1.0
Towards hasktorch 1.0Towards hasktorch 1.0
Towards hasktorch 1.0
 
Learn JavaScript From Scratch
Learn JavaScript From ScratchLearn JavaScript From Scratch
Learn JavaScript From Scratch
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)
 
Ruslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingRuslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testing
 

Recently uploaded

A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 

Recently uploaded (20)

A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 

QuickCheck - Software Testing

  • 1. QuickCheck: A Lightweight Tool for Random Testing of Haskell Programs Koen Claessen, John Hughes ICFP ‘00 CMSC 737 Software Testing Fang Cheng javran@cs.umd.edu
  • 2. Prelude - Haskell ● Functional Programming ○ Functions as First-class Citizens ○ More Controls over Side-effects ○ Implicit Control Flow ● Strong Static Typing ○ Reasoning about Types ○ Rule out “bad programs” at compile time ○ If it compiles, it works!
  • 3. Prelude - Haskell ● Functional Programming ○ First-class citizen ■ g . f = λx -> g (f x) ○ Side effects ■ nextRandom :: () -> Int ■ nextRandom :: Seed -> (Int, Seed) ○ Implicit control flow ■ sum . map (^2) . filter (< 4) $ [1..10]
  • 4. Prelude - Haskell ● Strong Static Typing ○ Reasoning about Types ■ f1 :: ∀a . a -> a ■ f2 :: ∀a b . a -> b ■ f3 :: ∀a . [a] -> [a]
  • 5. Prelude - Haskell ● Strong Static Typing ○ Reasoning about Types ■ f1 :: ∀a . a -> a ● Can only be the identity function ■ f2 :: ∀a b . a -> b ● Impossible ■ f3 :: ∀a . [a] -> [a] ● Theorems for free! [Wadler 1989] ● map f . f3 == f3 . map f
  • 6. Prelude - Haskell ● Strong Static Typing ○ f: plus 2 ○ f3: swap the first two element if possible map f [1,2,3,4] [3,4,5,6] f3 f3 map f [2,1,3,4] [4,3,5,6]
  • 7. Overview - QuickCheck ● Test Case Generation ○ Value Generation ○ Function Generation ● Output Verification ○ Describing Desired Properties ● Case studies ● Discussion
  • 8. Test Case Generation - Type Class ● Type class: ad hoc polymorphism support ○ in Prolog: eq(bool). eq(char). … ord(X) :- eq(X).
  • 9. Test Case Generation - Arbitrary ● newtype Gen a = Gen (Rand -> a) ○ reads “some computation(Gen) that will give you a” class Arbitrary a where arbitrary :: Gen a instance Arbitrary Int where arbitrary = choose (-20,20) instance (Arbitrary a, Arbitrary b) => Arbitrary (a,b) where arbitrary = do {v1 <- arbitrary; v2 <- arbitrary; return (v1,v2)} We split a random seed (using a primitive function) into two independent seeds, notation “<-” and “return” will take care of that.
  • 10. Test Case Generation - Arbitrary ● newtype Gen a = Gen (Rand -> a) ○ reads “some computation(Gen) that will give you a” instance (Arbitrary a, Arbitrary b) => Arbitrary (a,b) where arbitrary = liftM2 (,) arbitrary arbitrary -- shorthand: -- liftM f m1 = do { v1 <- m1; return (f v1) } -- liftM2 g m1 m2 = do { v1 <- m1; v2 <- m2; return (g v1 v2) }
  • 11. Test Case Generation - Arbitrary ● Generate Recursive Data Structure ○ data Tree a = Branch (Tree a) (Tree a) | Leaf a deriving (Show) instance (Arbitrary a) => Arbitrary (Tree a) where arbitrary = frequency [ (1, liftM Leaf arbitrary) , (2, liftM2 Branch arbitrary arbitrary) ]
  • 12. Test Case Generation - Arbitrary ● Generate Recursive Data Structure ○ data Tree a = Branch (Tree a) (Tree a) | Leaf a deriving (Show) instance (Arbitrary a) => Arbitrary (Tree a) where arbitrary = frequency [ (1, liftM Leaf arbitrary) , (2, liftM2 Branch arbitrary arbitrary) ] ● The generator might not terminate ● Likely to produce huge trees
  • 13. Test Case Generation - Arbitrary ● Generate Recursive Data Structure ○ Limit the size ○ The notion of “size” is hard to define, leave it to programmers ○ newtype Gen a = Gen (Int -> Rand -> a) ○ sized :: (Int -> Gen a) -> Gen a
  • 14. Test Case Generation - Arbitrary ● Generate Recursive Data Structure ○ data Tree a = Branch (Tree a) (Tree a) | Leaf a deriving (Show) instance (Arbitrary a) => Arbitrary (Tree a) where arbitrary = sized arbTree arbTree :: Int -> Gen a arbTree 0 = liftM Leaf arbitrary arbTree n = frequency [ (1, liftM Leaf arbitrary) , (4, liftM2 Branch (arbTree (n `div` 2) ) (arbTree (n `div` 2) ) ]
  • 15. Test Case Generation - CoArbitrary ● Generate Functions ○ Use input value to perturb the random generator Gen b ■ class CoArbitrary a where ■ coarbitrary :: a -> Gen b -> Gen b ○ We should be able to generate an arbitrary b using Gen b ○ Use input value a to perturb Gen b so that we will have a modified Gen b ○ This is a pure function because given the same generator, the output depends merely on input value.
  • 16. Output Verification - DSL ● a Domain Specific Language (DSL) embedded in Haskell ○ Property ○ Function Equality ○ Conditional Laws ○ Classify Input ○ Custom Data Generator
  • 17. Output Verification - DSL prop_RevUnit x = reverse [x] == [x] prop_RevApp xs ys = reverse (xs++ys) == reverse ys++reverse xs prop_RevRev xs = reverse (reverse xs) == xs ● Property ● Function Equality (===) type Endo a = a -> a prop_CompAssoc :: Endo Int -> Endo Int -> Endo Int -> Int -> Bool prop_CompAssoc f g h = f . (g . h) === (f . g) . h
  • 18. Output Verification - DSL ● Conditional Laws (==>) ○ “A ==> B” is different from “not A || B” ○ try checking it for 100 test cases satisfying the condition ○ generate only a limited number of test cases prop_MaxLe :: Int -> Int -> Property prop_MaxLe x y = x <= y ==> max x y == y
  • 19. Output Verification - DSL ● Classify Input: classify, collect ● Custom Data Generator: forAll prop_Insert :: Int -> Property prop_Insert x = forAll orderedList $ xs -> classify (null xs) “trivial” $ ordered (insert x xs)
  • 20. Case studies ● Unification ○ Random generated terms are unlikely to be unified ○ New types are introduced to produce a different input distribution ● Lava ○ Provide symbolic input and use external theorem prover ● Pretty Printing ○ Extending Arbitrary type class to include: ○ shrink :: a -> [a]
  • 21. Discussion ● On Random Testing ○ a Haskell test framework written in Haskell ■ Lightweight ■ Doesn’t tie to a particular implementation ○ Many criteria need reinterpretation ■ Reachable statement? ■ Constraint solving in Haskell program is hard
  • 22. Discussion ● Correctness Criteria ○ a Haskell test framework written in Haskell ■ Property Language is much more general ● Function Equality (e.g. current vs. known correct version) ● Result Checking (e.g. mathematical properties) ● Conditional Properties (e.g. insert on sorted list) ● Testing High-Order Functions (e.g. function composition) ○ No published work on automatic testing of functional programs against specification
  • 23. Discussion ● Test Data Generation ○ a Haskell test framework written in Haskell ■ another DSL for describing test data generation ● Grammar inherited from Haskell ● Minimum learning effort ○ Controlling sizes to guarantee termination ■ Need developer to ● Interpret the meaning of “size” ● Specify generators for his/her own new types
  • 24. Discussion ● Some Reflections ○ Formulating formal specification improves our understanding of our programs ○ Three types of errors divided evenly ■ Errors in test generators ■ Errors in the specification ■ Errors in the program
  • 25. References ● Claessen, Koen, and John Hughes. "QuickCheck: a lightweight tool for random testing of Haskell programs." Acm sigplan notices 46.4 (2011): 53- 64. ● Wadler, Philip. "Theorems for free!." Proceedings of the fourth international conference on Functional programming languages and computer architecture. ACM, 1989. ● Weyuker, Elaine J. "On testing non-testable programs." The Computer Journal 25.4 (1982): 465-470.