Massive overhaul to testsuite structure
[darcs-mirrors/vector.git] / tests / Utilities.hs
1 module Utilities where
2
3 import Test.QuickCheck
4
5 import qualified Data.Vector as DV
6 import qualified Data.Vector.IVector as DVI
7 import qualified Data.Vector.Unboxed as DVU
8 import qualified Data.Vector.Unboxed.Unbox as DVUU
9 import qualified Data.Vector.Fusion.Stream as S
10
11
12 instance Show a => Show (S.Stream a) where
13 show s = "Data.Vector.Fusion.Stream.fromList " ++ show (S.toList s)
14
15
16 instance Arbitrary a => Arbitrary (DV.Vector a) where
17 arbitrary = fmap DV.fromList arbitrary
18 coarbitrary = coarbitrary . DV.toList
19
20 instance (Arbitrary a, DVUU.Unbox a) => Arbitrary (DVU.Vector a) where
21 arbitrary = fmap DVU.fromList arbitrary
22 coarbitrary = coarbitrary . DVU.toList
23
24 instance Arbitrary a => Arbitrary (S.Stream a) where
25 arbitrary = fmap S.fromList arbitrary
26 coarbitrary = coarbitrary . S.toList
27
28
29 class Model a b | a -> b where
30 -- | Convert a concrete value into an abstract model
31 model :: a -> b
32
33 -- The meat of the models
34 instance Model (DV.Vector a) [a] where model = DV.toList
35 instance DVUU.Unbox a => Model (DVU.Vector a) [a] where model = DVU.toList
36
37 -- Identity models
38 instance Model Bool Bool where model = id
39 instance Model Int Int where model = id
40 instance Model Float Float where model = id
41 instance Model Double Double where model = id
42 instance Model Ordering Ordering where model = id
43
44 -- Functorish models
45 -- All of these need UndecidableInstances although they are actually well founded. Oh well.
46 instance Model a b => Model (Maybe a) (Maybe b) where model = fmap model
47 instance (Model a c, Model b d) => Model (a, b) (c, d) where model (a, b) = (model a, model b)
48 instance (Model c a, Model b d) => Model (a -> b) (c -> d) where model f = model . f . model
49
50
51 eq0 f g = model f == g
52 eq1 f g = \a -> model (f a) == g (model a)
53 eq2 f g = \a b -> model (f a b) == g (model a) (model b)
54 eq3 f g = \a b c -> model (f a b c) == g (model a) (model b) (model c)
55
56 eqNotNull1 f g = \x -> (not (DVI.null x)) ==> eq1 f g x
57 eqNotNull2 f g = \x y -> (not (DVI.null y)) ==> eq2 f g x y
58 eqNotNull3 f g = \x y z -> (not (DVI.null z)) ==> eq3 f g x y z