Debian Codegraph @

Test/ChasingBottoms/ContinuousFunctions/Tests.hs

raw
1 {-# LANGUAGE CPP, DeriveDataTypeable, MonoLocalBinds, NamedFieldPuns,
2 ScopedTypeVariables #-}
3
4 -- TODO: Tests passed even though for finiteTreeOf and finiteListOf
5 -- transform was only applied once at the top-level!
6
7 -- | Tests for "Test.ChasingBottoms.ContinuousFunctions". So far the
8 -- tests are rather weak.
9
10 module Test.ChasingBottoms.ContinuousFunctions.Tests (tests) where
11
12 import Test.ChasingBottoms.ContinuousFunctions
13 import Test.ChasingBottoms.IsBottom
14 import Test.ChasingBottoms.SemanticOrd
15 import Test.ChasingBottoms.TestUtilities
16 import qualified Test.ChasingBottoms.TestUtilities.Generators as Gen
17 import Test.ChasingBottoms.TestUtilities.Generators (Tree(..))
18 import Test.ChasingBottoms.ApproxShow
19 import Data.Generics
20 import Test.QuickCheck
21 import Test.ChasingBottoms.TestUtilities
22 import Control.Arrow
23 import Control.Monad
24 import Data.List
25 #if MIN_VERSION_QuickCheck(2,12,0)
26 import qualified Data.Map.Strict as Map
27 #endif
28 import Data.Ratio
29
30 ------------------------------------------------------------------------
31 -- Example data type
32
33 finiteTreeOf :: MakeResult a -> MakeResult (Tree a)
34 finiteTreeOf makeResult = sized' tree
35 where
36 tree size = transform $
37 if size == 0 then
38 baseCase
39 else
40 frequency' [ (1, baseCase)
41 , (1, liftM2 Branch tree' tree')
42 ]
43 where
44 tree' = tree (size `div` 2)
45
46 baseCase =
47 frequency' [ (1, return bottom)
48 , (2, liftM Leaf makeResult)
49 ]
50
51 ------------------------------------------------------------------------
52 -- Helpers
53
54 integer :: Gen Integer
55 integer = frequency [ (1, return bottom)
56 , (9, arbitrary)
57 ]
58
59 length' :: Num b => [a] -> b
60 length' xs | isBottom xs = 0
61 length' [] = 1
62 length' (x:xs) = 1 + length' xs
63
64 depth :: (Ord b, Num b) => Tree a -> b
65 depth t | isBottom t = 0
66 depth (Leaf {}) = 1
67 depth (Branch l r) = 1 + (depth l `max` depth r)
68
69 ------------------------------------------------------------------------
70 -- Tests
71
72 -- Interesting properties for the function generators:
73 --
74 -- * Surjectivity.
75 --
76 -- * Decent distribution.
77 --
78 -- How do we test these properties?
79
80 type DistributionTest = Int -> [(String, Double)] -> (Bool, String)
81
82 testDistribution :: Testable a => DistributionTest -> a -> IO Bool
83 testDistribution test t = do
84 result <- run t
85 let (ok, msg) = apply test result
86 unless ok $ putStrLn msg
87 return ok
88 where
89 #if MIN_VERSION_QuickCheck(2,12,0)
90 convert numTests labels =
91 map (\(x, f) -> (x, fromIntegral f / fromIntegral numTests)) $
92 Map.toList $
93 Map.fromListWith (+)
94 [ (l, n) | (ls, n) <- Map.toList labels, l <- ls ]
95 #else
96 convert _ labels = labels
97 #endif
98
99 apply test Success{numTests, labels} = test numTests
100 (convert numTests labels)
101 apply _ _ = (False, "Test failed.")
102
103 spread :: DistributionTest
104 spread numTests labels =
105 (uniqueShare >= 3%4, "uniqueShare: " ++ show uniqueShare)
106 where
107 noUniqueArgs = length labels
108 uniqueShare = noUniqueArgs % numTests
109
110 len :: Integer -> Double -> Integer -> DistributionTest
111 len max avg short numTests labels =
112 ( maxLen >= max && averageLen >= avg && shortShare >= 0.1
113 , "maxLen: " ++ show maxLen ++
114 ", averageLen: " ++ show averageLen ++
115 ", shortShare: " ++ show shortShare
116 )
117 where
118 lengths = map (read *** id) labels :: [(Integer, Double)]
119 maxLen = maximum $ map fst lengths
120 averageLen = sum $ map (\(n, f) -> fromInteger n * f) lengths
121 shortShare = sum . map snd . filter ((<= short) . fst) $ lengths
122
123 -- | We want to make sure that we can generate many different kinds of
124 -- lazy functions.
125
126 prop_many_functions_rather_lazy = testDistribution spread $
127 forAll (functionTo (finiteTreeOf (finiteTreeOf flat))) $
128 \(f :: Tree Integer -> Tree (Tree Bool)) ->
129 f bottom /=! bottom && f (Leaf bottom) <! f (Leaf 1) ==>
130 collect (map (approxShow 100 . f) [bottom, Leaf bottom, Leaf 1]) $
131 True
132
133 -- | The generated lists should not be too short.
134
135 prop_lists_have_decent_length = testDistribution (len 20 5 5) $
136 forAll (functionTo (finiteListOf flat)) $ \(f :: Integer -> [Bool]) ->
137 forAll integer $ \(i :: Integer) ->
138 collect (length' (f i) :: Integer) $
139 True
140
141 -- | The generated trees should not be too shallow.
142
143 prop_trees_have_decent_depth = testDistribution (len 6 2 2) $
144 forAll (functionTo (finiteTreeOf flat)) $ \(f :: Integer -> Tree Bool) ->
145 forAll integer $ \(i :: Integer) ->
146 collect (depth (f i) :: Integer) $
147 True
148
149 -- | In one version of Data.Generics the following equations were
150 -- valid:
151 --
152 -- * @'toConstr' ('bottom' :: ()) = 'toConstr' ()@
153 --
154 -- * @'toConstr' ('bottom' :: One) = _|_@
155 --
156 -- 'toConstr' should be strict. There is a workaround for this (using
157 -- seq) in "Test.ChasingBottoms.ContinuousFunctions", and the
158 -- following two tests check that this workaround works.
159
160 data One
161 = One
162 deriving (Typeable, Data)
163
164 prop_some_lazy_unit =
165 forAll (functionTo (finiteTreeOf flat)) $ \(f :: () -> Tree Bool) ->
166 f bottom <! f () ==>
167 True
168
169 prop_some_lazy_One =
170 forAll (functionTo (finiteTreeOf flat)) $ \(f :: One -> Tree Bool) ->
171 f bottom <! f One ==>
172 True
173
174 -- | Example from documentation. Here mostly to check that it type
175 -- checks.
176
177 prop_example_works =
178 forAll (functionTo (finiteTreeOf flat)) $
179 \(f :: Bool -> Tree Integer) ->
180 f bottom <=! f True && f bottom <=! f False
181
182 -- | Generated functions should be monotone.
183
184 prop_functions_monotone =
185 forAll (functionTo (finiteListOf flat))
186 $ \(f :: Tree Integer -> [Bool]) ->
187 forAll (pair (Gen.finiteTreeOf Gen.integer)
188 (Gen.geTreeOf Gen.integer Gen.geInteger Gen.finiteTreeOf))
189 $ \(x, y) ->
190 x <=! y && f x <=! f y
191
192 ------------------------------------------------------------------------
193 -- | All tests collected together.
194
195 tests :: IO Bool
196 tests = do
197 b1 <- fmap and $ sequence theIOTests
198 b2 <- runQuickCheckTests $ map run $ concat theTests
199 return (b1 && b2)
200 where
201 theIOTests :: [IO Bool]
202 theIOTests = []
203
204 -- Disabled, because occasionally one or more of the tests failed,
205 -- and (at the time of writing in 2015 and 2017) I have no
206 -- interest in fixing test suite bugs in old, unfinished and
207 -- experimental code. Known problems (in code that has later been
208 -- changed due to changes to QuickCheck):
209 -- * Division by zero, presumably because noArgs is 0.
210 -- * After reducing maxSuccess from 1000 to 100 I once observed
211 -- that "averageLen" was 199 % 100, but if I am not mistaken the
212 -- test requires it to be >= 2.
213
214 -- theIOTests = [ prop_many_functions_rather_lazy
215 -- , prop_lists_have_decent_length
216 -- , prop_trees_have_decent_depth
217 -- ]
218
219 theTests :: [[Property]]
220 theTests = [ [ prop_example_works
221 , prop_some_lazy_unit
222 , prop_some_lazy_One
223 , prop_functions_monotone
224 ]
225 ]
226
227 ------------------------------------------------------------------------
228 -- Manual inspection of function tables
229
230 viewFun :: (ApproxShow b, Data a) => MakeResult b -> [a] -> IO ()
231 viewFun (makeResult :: MakeResult b) (inputs :: [a]) = quickCheck $
232 forAll (functionTo makeResult) $ \(f :: a -> b) ->
233 collect (map (approxShow 5 . f) inputs) $ True
234
235 bool = undefined :: Bool
236 int = undefined :: Int
237 float = undefined :: Float
238 treeOfBool = undefined :: Tree Bool
239
240 test0 = viewFun (flat :: MakeResult Bool) [bottom, False, True]
241 test1 = viewFun (finiteTreeOf flat :: MakeResult (Tree Bool))
242 [bottom, False, True]
243 test2 = viewFun (finiteTreeOf flat :: MakeResult (Tree Bool))
244 [bottom, Leaf bottom, Leaf False]
245
246 test4 = viewFun (flat :: MakeResult Int) [bottom, False, True]
247 test5 = viewFun (flat :: MakeResult Float) [bottom, False, True]