-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuickCheck.purs
More file actions
93 lines (83 loc) · 2.87 KB
/
Copy pathQuickCheck.purs
File metadata and controls
93 lines (83 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
module Test.TestQuickCheck where
import Prelude
import Effect (Effect)
import Effect.Console (log, logShow)
import Effect.Exception (try)
import Control.Monad.Gen.Class as MGen
import Data.Array.Partial (head)
import Data.Either (isLeft)
import Data.Foldable (sum)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.Tuple (fst)
import Partial.Unsafe (unsafePartial)
import Test.Assert (assert)
import Test.QuickCheck (class Testable, quickCheck, (/=?), (<=?), (<?), (==?), (>=?), (>?))
import Test.QuickCheck.Arbitrary (arbitrary, genericArbitrary, class Arbitrary)
import Test.QuickCheck.Gen (Gen, vectorOf, randomSample', resize, Size, runGen, sized)
import Random.LCG (mkSeed)
data Foo a
= F0 a
| F1 a a
| F2 { foo :: a, bar :: Array a }
derive instance genericFoo :: Generic (Foo a) _
instance showFoo :: Show a => Show (Foo a) where
show = genericShow
instance arbitraryFoo :: Arbitrary a => Arbitrary (Foo a) where
arbitrary = genericArbitrary
quickCheckFail :: forall t. Testable t => t -> Effect Unit
quickCheckFail = assert <=< map isLeft <<< try <<< quickCheck
testResize :: (forall a. Size -> Gen a -> Gen a) -> Boolean
testResize resize' =
let
initialSize = 2
gen = do
s1 <- sized pure
s2 <- resize' 1 (sized pure)
s3 <- sized pure
pure $ [ s1, s2, s3 ] == [ initialSize, 1, initialSize ]
in
fst $ runGen gen { newSeed: mkSeed 0, size: initialSize }
testQuickCheck :: Effect Unit
testQuickCheck = do
log "MonadGen.resize"
assert (testResize (MGen.resize <<< const))
log "Gen.resize"
assert (testResize (resize))
log "Try with some little Gens first"
logShow =<< go 10
logShow =<< go 100
logShow =<< go 1000
logShow =<< go 10000
log "Testing stack safety of Gen"
logShow =<< go 20000
logShow =<< go 100000
log "Generating via Generic"
logShow =<< randomSample' 10 (arbitrary :: Gen (Foo Int))
log "Arbitrary instance for records"
listOfRecords ← randomSample' 10 (arbitrary :: Gen { foo :: Int, nested :: { bar :: Boolean } })
let
toString rec = "{ foo: " <> show rec.foo <> "; nested.bar: " <> show rec.nested.bar <> " }"
logShow (toString <$> listOfRecords)
quickCheck \(x :: Int) -> x <? x + 1
quickCheck \(x :: Int) -> x <=? x + 1
quickCheck \(x :: Int) -> x >=? x - 1
quickCheck \(x :: Int) -> x >? x - 1
quickCheck \(x :: Int) -> x + x ==? x * 2
quickCheck \(x :: Int) -> x + x /=? x * 3
quickCheck $ 1 ==? 1
quickCheckFail $ 1 /=? 1
quickCheck $ 1 <? 2
quickCheckFail $ 1 >=? 2
quickCheck $ 3 <=? 3
quickCheckFail $ 3 >? 3
quickCheck $ 3 >=? 3
quickCheckFail $ 3 <? 3
quickCheck $ 4 /=? 3
quickCheckFail $ 4 ==? 3
quickCheck $ 4 >? 3
quickCheckFail $ 4 <=? 3
where
go n = map (sum <<< unsafeHead) $ randomSample' 1 (vectorOf n (arbitrary :: Gen Int))
unsafeHead :: forall x. Array x -> x
unsafeHead xs = unsafePartial (head xs)