QuickCheck - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

QuickCheck

Description:

??? ???????? ??????? ?????? ? ?? ??????????? ??????????. ... import Control.Monad. import Test.QuickCheck. data Color = R | B deriving (Eq, Show) ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 25
Provided by: spbhugFol
Category:
Tags: quickcheck | monad

less

Transcript and Presenter's Notes

Title: QuickCheck


1
???????? ? QuickCheck
  • ??????? ????????
  • 13 ??????? 2007 ?.
  • ?????-????????????? ?????? ????????????? Haskell

2
????????? ? ??????
  • ????? ????????? ????? ????????? ??????
  • ?????????????? ??????
  • ?????? ??????????????? ???????????.
  • ?????? ?????
  • ?????? ????????? ???????? ?????, ???????
    ????????.
  • ?????? ? ?????? ?????? ?????????
  • ??? ?????? ?????????? ?????? ????????????.

3
????? ?????????? ??????
  • ???????
  • ???????????? ? ???????
  • ???????????
  • ??? ???????? ??????? ?????? ? ?? ???????????
    ??????????.
  • ???????????? ? ??????? ????? ?????? ????????
    ??????????.
  • ?????????? ????????????? ????????????.

4
???????????? ? Haskell
  • REPL
  • Hat
  • Debug.Trace, print, unsafePerformIO
  • HUnit
  • QuickCheck

5
QuickCheck
  • ????????? ????????? ???????? ?????? ??? ??????
    ????? ??????.
  • ????????????? ?????????? ??????.
  • ???????????? ??????????? ??????? ????? Haskell.

6
???????? ???????
  • QuickCheck ?????????? ???????? ?????? ?
    ?????????, ??????????? ?? ????????? ????????.
  • ???????????? ?????? ?? ????????????
  • prop_RevRev xs reverse (reverse xs) xs
  • where types xsInt
  • gt quickCheck prop_RevRev
  • OK, passed 100 tests.

7
prop_RevRev
prop_RevRev xs reverse (reverse xs)
xs where types xsInt
??????? ??? ???? ??????? ?????????? ??????????
reverse ?????????? ???????? ??????.
??? ?????? ? ???????? ???????? ?????? ?????
?????????????? ????????? ?????? ????? ?????.
8
??????? ??????? ???????
  • ???????? ??? ???????, ???????????? ????????
    ???? Bool.
  • ???????? ?????? ????? ??????????? ??? ??????????
    ???????????? ???????? ?????? ???????????? ????.

9
???? ???????
  • ???????? ????????
  • lt???????gt gt lt????????gt

ordered xs and (zipWith (lt) xs (drop 1
xs)) insert x x insert x (yys)
x lt y x y ys otherwise y insert x
ys prop_Insert x xs ordered xs gt ordered
(insert x xs) where types xInt
10
???? ???????
  • ?????????????????? ????????
  • forAll lt?????????gt \x -gt lt????????gt

prop_Insert2 x forAll orderedList \xs -gt
ordered (insert x xs) where types xInt
  • ??? ???????? ????????? ???????? ??????
    ???????????? ? ?????????????, ???? ??????? ??
    ???????????.
  • ??? ?????????????????? ??????? ????????????
    ?????? ?????????? ???????.

11
??????????
  • QuickCheck ?????????? ??????????? ?????????? ???
    ???????? ????? ??????.
  • ??? ???? ?? ????? ?????? ????? ?????? ???????????
    ??????????.
  • ????? ?????????? ???? ?????????? ??? ????? ?????
    ??????.
  • ????????? ??? ???? a ?????? Gen a.
  • choose Random a gt (a, a) -gt Gen a
  • choose (0, n) gtgt \i -gt return (list !! i)

12
???????????
  • oneof lt?????? ???????????gt
  • oneof Gen a -gt Gen a
  • oneof return a, return b, return c
  • ?????????? ?????????? ?????????????.
  • frequency lt?????? ???????????gt
  • frequency (Int, Gen a) -gt Gen a
  • frequency (2, return a), (3, return b)
  • ?????????? ?????????? ? ????????? ????????.

13
???????????
  • sized ?????????? ???????? ??????
  • sized (Int -gt Gen a) -gt Gen a
  • resize ????????? ??????? ???????? ??????
  • resize Int -gt Gen a -gt Gen a
  • ??? ???????????????? ?????? ?????? ????
    ????????????.

14
????????? ???????? ??????
  • ????????? ???????????? ? ?????? Gen.

data IntList Nil Cons Int IntList listGen
oneof return Nil, liftM2 Cons arbitrary
listGen
  • ????? ??????? ? ??????? ??????.
  • ??????????? ??????????
  • two, three, four, vector

15
Arbitrary
class Arbitrary a where arbitrary Gen
a coarbitrary a -gt Gen b -gt Gen b
  • arbitrary ????????? ????????? ????????
  • coarbitrary ???????????? ??? ????????? ?????????
    ???????

16
?????? ??????-?????? ???????
  • ????????????? ???????? ? ??????? ??????-??????
    ????????
  • ?????????? Chris Okasaki, 1993 (Functional
    Pearls)

17
????????
  • ?????? ?????? ?????? ??????? ? ?????? ????
  • ????????? ??????? ????? ????? ???? ?????? ??????
    ????
  • ???????? ???????????? ?????????????????? ??????
    ?, ??? ?????????, ????????????? ??????

18
??????????
import Data.List hiding (insert) import
Control.Monad import Test.QuickCheck data Color
R B deriving (Eq, Show) data Tree elt E T
Color (Tree elt) elt (Tree elt) deriving
Show type Set elt Tree elt empty Set
elt empty E member Ord elt gt elt -gt Set
elt -gt Bool member x E False member x (T _ a y
b) x lt y member x a x y True x
gt y member x b
19
??????????
insert Ord elt gt elt -gt Set elt -gt Set
elt insert x s makeBlack (ins s) where ins E
T R E x E ins (T color a
y b) x lt y balance color (ins a) y b
x y T
color a y b
x gt y balance color a y (ins
b) makeBlack (T _ a y b) T B a y
b balance Ord elt gt Color -gt Tree elt -gt elt
-gt Tree elt -gt Tree elt balance B (T R (T R a x
b) y c) z d T R (T B a x b) y (T B c z
d) balance B (T R a x (T R b y c)) z d T R (T B
a x b) y (T B c z d) balance B a x (T R (T R b y
c) z d) T R (T B a x b) y (T B c z d) balance B
a x (T R b y (T R c z d)) T R (T B a x b) y (T
B c z d) balance color a x b T color a x b
20
??????????????? ???????
setFromList Ord elt gt elt -gt Set
elt setFromList empty setFromList (xxs)
insert x setFromList xs listFromSet Ord elt
gt Set elt -gt elt listFromSet E
listFromSet (T _ a y b) listFromSet a y
listFromSet b
21
Arbitrary Tree
instance (Arbitrary a, Ord a) gt Arbitrary (Tree
a) where arbitrary sized \n -gt
liftM setFromList arbitrary
coarbitrary tree coarbitrary (listFromSet tree)
22
???????? ????????????????????-?????????
  • prop_listConvert Int -gt Bool
  • prop_listConvert list
  • (listFromSet . setFromList) list (nub
    . sort) list
  • -- ??????? ???????? ???? ????????????
  • prop_listConvertBad Int -gt Bool
  • prop_listConvertBad list
  • (listFromSet . setFromList) list sort
    list
  • gt quickCheck prop_listConvert
  • Falsifiable, after 10 tests
  • 2,2

23
???????? ??????
prop_blackRoot Tree Int -gt Bool prop_blackRoot
E True prop_blackRoot (T color _ _ _) color
B -- All children of a red node must be
black prop_blackChildren Tree Int -gt
Bool prop_blackChildren E True prop_blackChildre
n (T color left _ right) color R
isNotBlack left isNotBlack right False
otherwise prop_blackChildren left
prop_blackChildren right where
isNotBlack Tree Int -gt Bool isNotBlack
(T R _ _ _) True isNotBlack _ False
24
?????
Write a Comment
User Comments (0)
About PowerShow.com