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