Fiddle with test data generation
[darcs-mirrors/vector.git] / tests / Utilities.hs
1 {-# LANGUAGE FlexibleInstances, GADTs #-}
2 module Utilities where
3
4 import Test.QuickCheck
5
6 import qualified Data.Vector as DV
7 import qualified Data.Vector.Generic as DVG
8 import qualified Data.Vector.Primitive as DVP
9 import qualified Data.Vector.Fusion.Stream as S
10
11 import Data.List ( sortBy )
12
13
14 instance Show a => Show (S.Stream a) where
15 show s = "Data.Vector.Fusion.Stream.fromList " ++ show (S.toList s)
16
17
18 instance Arbitrary a => Arbitrary (DV.Vector a) where
19 arbitrary = fmap DV.fromList arbitrary
20
21 instance CoArbitrary a => CoArbitrary (DV.Vector a) where
22 coarbitrary = coarbitrary . DV.toList
23
24 instance (Arbitrary a, DVP.Prim a) => Arbitrary (DVP.Vector a) where
25 arbitrary = fmap DVP.fromList arbitrary
26
27 instance (CoArbitrary a, DVP.Prim a) => CoArbitrary (DVP.Vector a) where
28 coarbitrary = coarbitrary . DVP.toList
29
30 instance Arbitrary a => Arbitrary (S.Stream a) where
31 arbitrary = fmap S.fromList arbitrary
32
33 instance CoArbitrary a => CoArbitrary (S.Stream a) where
34 coarbitrary = coarbitrary . S.toList
35
36 class (Testable (Pty a), Conclusion (Pty a)) => TestData a where
37 type Pty a
38 type Model a
39 model :: a -> Model a
40 unmodel :: Model a -> a
41
42 instance TestData (DV.Vector a) where
43 type Pty (DV.Vector a) = Property
44 type Model (DV.Vector a) = [a]
45 model = DV.toList
46 unmodel = DV.fromList
47
48 instance DVP.Prim a => TestData (DVP.Vector a) where
49 type Pty (DVP.Vector a) = Property
50 type Model (DVP.Vector a) = [a]
51 model = DVP.toList
52 unmodel = DVP.fromList
53
54 -- Identity models
55
56 #define id_TestData(ty) \
57 instance TestData ty where { type Pty ty = Property; type Model ty = ty; model = id; unmodel = id }
58
59 id_TestData(Bool)
60 id_TestData(Int)
61 id_TestData(Float)
62 id_TestData(Double)
63 id_TestData(Ordering)
64
65 -- Functorish models
66 -- All of these need UndecidableInstances although they are actually well founded. Oh well.
67 instance TestData a => TestData (Maybe a) where
68 type Pty (Maybe a) = Property
69 type Model (Maybe a) = Maybe (Model a)
70 model = fmap model
71 unmodel = fmap unmodel
72
73 instance TestData a => TestData [a] where
74 type Pty [a] = Property
75 type Model [a] = [Model a]
76 model = fmap model
77 unmodel = fmap unmodel
78
79 instance (TestData a, TestData b) => TestData (a,b) where
80 type Pty (a,b) = Property
81 type Model (a,b) = (Model a, Model b)
82 model (a,b) = (model a, model b)
83 unmodel (a,b) = (unmodel a, unmodel b)
84
85 instance (TestData a, TestData b, TestData c) => TestData (a,b,c) where
86 type Pty (a,b,c) = Property
87 type Model (a,b,c) = (Model a, Model b, Model c)
88 model (a,b,c) = (model a, model b, model c)
89 unmodel (a,b,c) = (unmodel a, unmodel b, unmodel c)
90
91 instance (Arbitrary a, Show a, TestData a, TestData b) => TestData (a -> b) where
92 type Pty (a -> b) = a -> Pty b
93 type Model (a -> b) = Model a -> Model b
94 model f = model . f . unmodel
95 unmodel f = unmodel . f . model
96
97 data P a where
98 P :: TestData a => Pty a -> P a
99
100 unP :: P a -> Pty a
101 unP (P p) = p
102
103 instance Testable (P a) where
104 property (P a) = property a
105
106 class TestData a => EqTestable a p where
107 equal :: a -> a -> p
108
109 instance (Eq a, TestData a) => EqTestable a Property where
110 equal x y = property (x==y)
111
112 instance (Arbitrary a, Show a, TestData a, EqTestable b p) => EqTestable (a -> b) (a -> p) where
113 equal f g = \x -> equal (f x) (g x)
114
115 infix 4 `eq`
116 eq :: EqTestable a (Pty a) => a -> Model a -> P a
117 eq x y = P (equal x (unmodel y))
118
119 class Conclusion p where
120 type Predicate p
121
122 predicate :: Predicate p -> p -> p
123
124 instance Conclusion Property where
125 type Predicate Property = Bool
126
127 predicate = (==>)
128
129 instance Conclusion p => Conclusion (a -> p) where
130 type Predicate (a -> p) = a -> Predicate p
131
132 predicate f p = \x -> predicate (f x) (p x)
133
134 infixr 0 ===>
135 (===>) :: Predicate (Pty a) -> P a -> P a
136 p ===> P a = P (predicate p a)
137
138 notNull2 _ xs = not $ DVG.null xs
139
140 -- Generators
141 index_value_pairs :: Arbitrary a => Int -> Gen [(Int,a)]
142 index_value_pairs 0 = return []
143 index_value_pairs m = sized $ \n ->
144 do
145 len <- choose (0,n)
146 is <- sequence [choose (0,m-1) | i <- [1..len]]
147 xs <- vector len
148 return $ zip is xs
149
150 indices :: Int -> Gen [Int]
151 indices 0 = return []
152 indices m = sized $ \n ->
153 do
154 len <- choose (0,n)
155 sequence [choose (0,m-1) | i <- [1..len]]
156
157
158 -- Additional list functions
159 singleton x = [x]
160 snoc xs x = xs ++ [x]
161 slice xs i n = take n (drop i xs)
162 backpermute xs is = map (xs!!) is
163 prescanl f z = init . scanl f z
164 postscanl f z = tail . scanl f z
165
166 accum :: (a -> b -> a) -> [a] -> [(Int,b)] -> [a]
167 accum f xs ps = go xs ps' 0
168 where
169 ps' = sortBy (\p q -> compare (fst p) (fst q)) ps
170
171 go (x:xs) ((i,y) : ps) j
172 | i == j = go (f x y : xs) ps j
173 go (x:xs) ps j = x : go xs ps (j+1)
174 go [] _ _ = []
175
176 (//) :: [a] -> [(Int, a)] -> [a]
177 xs // ps = go xs ps' 0
178 where
179 ps' = sortBy (\p q -> compare (fst p) (fst q)) ps
180
181 go (x:xs) ((i,y) : ps) j
182 | i == j = go (y:xs) ps j
183 go (x:xs) ps j = x : go xs ps (j+1)
184 go [] _ _ = []
185