-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPaperExample1.hs
More file actions
177 lines (148 loc) · 4.33 KB
/
PaperExample1.hs
File metadata and controls
177 lines (148 loc) · 4.33 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
{-# LANGUAGE CPP #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveGeneric #-}
-- | Try to generate a very large counterexample.
module Main where
#if defined(qcjh) || defined(qcNone) || defined(qc10) || defined(qc20) || defined(feat) || defined(qcGen) || defined(smart) || defined(small)
import Test
import System.Environment
#endif
import Test.SmartCheck
import Test.QuickCheck
#ifdef small
import Test.LazySmallCheck hiding (Property, test, (==>))
import qualified Test.LazySmallCheck as S
#endif
import GHC.Generics hiding (P, C)
import Data.Typeable
import Data.Int
import Control.Monad
#ifdef feat
import Test.Feat
#endif
-----------------------------------------------------------------
#if defined(qcjh) || defined(qcNone) || defined(qc10) || defined(qc20)
-- So that Int16s aren't shrunk by default arbitrary instances.
newtype J = J { getInt :: Int16 } deriving (Show, Read)
type I = [J]
instance Arbitrary J where
arbitrary = fmap J arbitrary
#else
type I = [Int16]
#endif
data T = T I I I I I
deriving (Read, Show, Typeable, Generic)
-- SmallCheck --------------------------
#ifdef small
enum :: (Enum b, Integral a, Num b) => a -> [b]
enum d = [(-d')..d']
where d' = fromIntegral d
instance Serial Int16 where
series = drawnFrom . enum
instance Serial Word8 where
series = drawnFrom . enum
instance Serial T where
series = cons5 T
#endif
-- SmallCheck --------------------------
-- SmartCheck --------------------------
#ifdef smart
instance SubTypes I
instance SubTypes T
#endif
-- SmartCheck --------------------------
-- qc/shrink takes over 1m seconds
instance Arbitrary T where
#ifdef feat
arbitrary = sized uniform
#else
arbitrary = liftM5 T arbitrary arbitrary
arbitrary arbitrary arbitrary
#endif
#if defined(qcNone) || defined(feat)
shrink _ = []
#endif
#if defined(qcjh)
shrink (T i0 i1 i2 i3 i4) = map go xs
where xs = shrink (i0, i1, i2, i3, i4)
go (i0', i1', i2', i3', i4') = T i0' i1' i2' i3' i4'
#endif
#if defined(qc10) || defined(qc20)
shrink (T i0 i1 i2 i3 i4) =
[ T a b c d e | a <- tk i0
, b <- tk i1, c <- tk i2
, d <- tk i3, e <- tk i4 ]
where
#ifdef qc10
sz = 10
#endif
#ifdef qc20
sz = 20
#endif
tk x = take sz (shrink x)
#endif
#if defined(qcNaive)
shrink (T i0 i1 i2 i3 i4) =
[ T a b c d e | a <- shrink i0
, b <- shrink i1, c <- shrink i2
, d <- shrink i3, e <- shrink i4 ]
#endif
#if defined(qcGen)
shrink = genericShrink
#endif
-- Feat --------------------------------
#ifdef feat
deriveEnumerable ''T
#endif
-- Feat --------------------------------
toList :: T -> [[Int16]]
toList (T i0 i1 i2 i3 i4) =
#if defined(qcjh) || defined(qcNone) || defined(qc10) || defined(qc20)
(map . map) (fromIntegral . getInt) [i0, i1, i2, i3, i4]
#else
[i0, i1, i2, i3, i4]
#endif
pre :: T -> Bool
pre t = all ((< 256) . sum) (toList t)
post :: T -> Bool
post t = (sum . concat) (toList t) < 5 * 256
prop :: T -> Property
prop t = pre t ==> post t
-- Smallcheck --------------------------
#ifdef small
prop_small :: T -> Bool
prop_small t = pre t S.==> post t
#endif
-- Smallcheck --------------------------
--------------------------------------------------------------------------------
-- Testing
--------------------------------------------------------------------------------
size :: T -> Int
size t = sum $ map length (toList t)
#if defined(qcjh) || defined(qcNone) || defined(qc10) || defined(qc20) || defined(feat) || defined(qcGen) || defined(smart) || defined(small)
main :: IO ()
main = do
[file', rnds'] <- getArgs
let rnds = read rnds' :: Int
let file = read file' :: String
#ifdef feat
test file rnds $ runQC' proxy stdArgs {maxSuccess = 10000} prop size
#endif
#ifdef smart
test file rnds $ runSC scStdArgs prop size
#endif
#if defined(qcNone) || defined(qc10) || defined(qc20) || defined(qcjh) || defined(qcNaive) || defined(qcGen)
test file rnds $ runQC' proxy stdArgs prop size
#endif
#endif
#ifdef smart
-- Tester (not part of the benchmark).
smtChk :: IO ()
smtChk = smartCheck scStdArgs { scMaxForall = 20
, runForall = True
, scMinForall = 25
, format = PrintString
} prop
#endif