Layout
[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 (EqTest a), Conclusion (EqTest a)) => TestData a where
37 type Model a
38 model :: a -> Model a
39 unmodel :: Model a -> a
40
41 type EqTest a
42 equal :: a -> a -> EqTest a
43
44 instance Eq a => TestData (DV.Vector a) where
45 type Model (DV.Vector a) = [a]
46 model = DV.toList
47 unmodel = DV.fromList
48
49 type EqTest (DV.Vector a) = Property
50 equal x y = property (x == y)
51
52 instance (Eq a, DVP.Prim a) => TestData (DVP.Vector a) where
53 type Model (DVP.Vector a) = [a]
54 model = DVP.toList
55 unmodel = DVP.fromList
56
57 type EqTest (DVP.Vector a) = Property
58 equal x y = property (x == y)
59
60 #define id_TestData(ty) \
61 instance TestData ty where { \
62 type Model ty = ty; \
63 model = id; \
64 unmodel = id; \
65 \
66 type EqTest ty = Property; \
67 equal x y = property (x == y) }
68
69 id_TestData(Bool)
70 id_TestData(Int)
71 id_TestData(Float)
72 id_TestData(Double)
73 id_TestData(Ordering)
74
75 -- Functorish models
76 -- All of these need UndecidableInstances although they are actually well founded. Oh well.
77 instance (Eq a, TestData a) => TestData (Maybe a) where
78 type Model (Maybe a) = Maybe (Model a)
79 model = fmap model
80 unmodel = fmap unmodel
81
82 type EqTest (Maybe a) = Property
83 equal x y = property (x == y)
84
85 instance (Eq a, TestData a) => TestData [a] where
86 type Model [a] = [Model a]
87 model = fmap model
88 unmodel = fmap unmodel
89
90 type EqTest [a] = Property
91 equal x y = property (x == y)
92
93 instance (Eq a, Eq b, TestData a, TestData b) => TestData (a,b) where
94 type Model (a,b) = (Model a, Model b)
95 model (a,b) = (model a, model b)
96 unmodel (a,b) = (unmodel a, unmodel b)
97
98 type EqTest (a,b) = Property
99 equal x y = property (x == y)
100
101 instance (Eq a, Eq b, Eq c, TestData a, TestData b, TestData c) => TestData (a,b,c) where
102 type Model (a,b,c) = (Model a, Model b, Model c)
103 model (a,b,c) = (model a, model b, model c)
104 unmodel (a,b,c) = (unmodel a, unmodel b, unmodel c)
105
106 type EqTest (a,b,c) = Property
107 equal x y = property (x == y)
108
109 instance (Arbitrary a, Show a, TestData a, TestData b) => TestData (a -> b) where
110 type Model (a -> b) = Model a -> Model b
111 model f = model . f . unmodel
112 unmodel f = unmodel . f . model
113
114 type EqTest (a -> b) = a -> EqTest b
115 equal f g x = equal (f x) (g x)
116
117 newtype P a = P { unP :: EqTest a }
118
119 instance TestData a => Testable (P a) where
120 property (P a) = property a
121
122 infix 4 `eq`
123 eq :: TestData a => a -> Model a -> P a
124 eq x y = P (equal x (unmodel y))
125
126 class Conclusion p where
127 type Predicate p
128
129 predicate :: Predicate p -> p -> p
130
131 instance Conclusion Property where
132 type Predicate Property = Bool
133
134 predicate = (==>)
135
136 instance Conclusion p => Conclusion (a -> p) where
137 type Predicate (a -> p) = a -> Predicate p
138
139 predicate f p = \x -> predicate (f x) (p x)
140
141 infixr 0 ===>
142 (===>) :: TestData a => Predicate (EqTest a) -> P a -> P a
143 p ===> P a = P (predicate p a)
144
145 notNull2 _ xs = not $ DVG.null xs
146
147 -- Generators
148 index_value_pairs :: Arbitrary a => Int -> Gen [(Int,a)]
149 index_value_pairs 0 = return []
150 index_value_pairs m = sized $ \n ->
151 do
152 len <- choose (0,n)
153 is <- sequence [choose (0,m-1) | i <- [1..len]]
154 xs <- vector len
155 return $ zip is xs
156
157 indices :: Int -> Gen [Int]
158 indices 0 = return []
159 indices m = sized $ \n ->
160 do
161 len <- choose (0,n)
162 sequence [choose (0,m-1) | i <- [1..len]]
163
164
165 -- Additional list functions
166 singleton x = [x]
167 snoc xs x = xs ++ [x]
168 slice xs i n = take n (drop i xs)
169 backpermute xs is = map (xs!!) is
170 prescanl f z = init . scanl f z
171 postscanl f z = tail . scanl f z
172
173 accum :: (a -> b -> a) -> [a] -> [(Int,b)] -> [a]
174 accum f xs ps = go xs ps' 0
175 where
176 ps' = sortBy (\p q -> compare (fst p) (fst q)) ps
177
178 go (x:xs) ((i,y) : ps) j
179 | i == j = go (f x y : xs) ps j
180 go (x:xs) ps j = x : go xs ps (j+1)
181 go [] _ _ = []
182
183 (//) :: [a] -> [(Int, a)] -> [a]
184 xs // ps = go xs ps' 0
185 where
186 ps' = sortBy (\p q -> compare (fst p) (fst q)) ps
187
188 go (x:xs) ((i,y) : ps) j
189 | i == j = go (y:xs) ps j
190 go (x:xs) ps j = x : go xs ps (j+1)
191 go [] _ _ = []
192