Merge pull request #33 from thomie/master
[packages/random.git] / tests / rangeTest.hs
1 {-# LANGUAGE CPP #-}
2 import Control.Monad
3 import System.Random
4 import Data.Int
5 import Data.Word
6 import Data.Bits
7 import Foreign.C.Types
8
9 -- Take many measurements and record the max/min/average random values.
10 approxBounds :: (RandomGen g, Random a, Ord a, Num a) =>
11 (g -> (a,g)) -> Int -> a -> (a,a) -> g -> ((a,a,a),g)
12 -- Here we do a little hack to essentiall pass in the type in the last argument:
13 approxBounds nxt iters unused (explo,exphi) initrng =
14 if False
15 then ((unused,unused,unused),undefined)
16 -- else loop initrng iters 100 (-100) 0 -- Oops, can't use minBound/maxBound here.
17 else loop initrng iters exphi explo 0 -- Oops, can't use minBound/maxBound here.
18 where
19 loop rng 0 mn mx sum = ((mn,mx,sum),rng)
20 loop rng n mn mx sum =
21 case nxt rng of
22 (x, rng') -> loop rng' (n-1) (min x mn) (max x mx) (x+sum)
23
24
25 -- We check that:
26 -- (1) all generated numbers are in bounds
27 -- (2) we get "close" to the bounds
28 -- The with (2) is that we do enough trials to ensure that we can at
29 -- least hit the 90% mark.
30 checkBounds:: (Real a, Show a, Ord a) =>
31 String -> (Bool, a, a) -> ((a,a) -> StdGen -> ((a, a, t), StdGen)) -> IO ()
32 checkBounds msg (exclusive,lo,hi) fun =
33 -- (lo,hi) is [inclusive,exclusive)
34 do putStr$ msg
35 -- ++ ", expected range " ++ show (lo,hi)
36 ++ ": "
37 (mn,mx,sum) <- getStdRandom (fun (lo,hi))
38 when (mn < lo)$ error$ "broke lower bound: " ++ show mn
39 when (mx > hi) $ error$ "broke upper bound: " ++ show mx
40 when (exclusive && mx >= hi)$ error$ "hit upper bound: " ++ show mx
41
42 let epsilon = 0.1 * (toRational hi - toRational lo)
43
44 when (toRational (hi - mx) > epsilon)$ error$ "didn't get close enough to upper bound: "++ show mx
45 when (toRational (mn - lo) > epsilon)$ error$ "didn't get close enough to lower bound: "++ show mn
46 putStrLn "Passed"
47
48 boundedRange :: (Num a, Bounded a) => (Bool, a, a)
49 boundedRange = ( False, minBound, maxBound )
50
51 trials = 5000
52
53 -- Keep in mind here that on some architectures (e.g. ARM) CChar, CWchar, and CSigAtomic
54 -- are unsigned
55
56 main =
57 do
58 checkBounds "Int" boundedRange (approxBounds random trials (undefined::Int))
59 checkBounds "Integer" (False, fromIntegral (minBound::Int), fromIntegral (maxBound::Int))
60 (approxBounds random trials (undefined::Integer))
61 checkBounds "Int8" boundedRange (approxBounds random trials (undefined::Int8))
62 checkBounds "Int16" boundedRange (approxBounds random trials (undefined::Int16))
63 checkBounds "Int32" boundedRange (approxBounds random trials (undefined::Int32))
64 checkBounds "Int64" boundedRange (approxBounds random trials (undefined::Int64))
65 checkBounds "Word" boundedRange (approxBounds random trials (undefined::Word))
66 checkBounds "Word8" boundedRange (approxBounds random trials (undefined::Word8))
67 checkBounds "Word16" boundedRange (approxBounds random trials (undefined::Word16))
68 checkBounds "Word32" boundedRange (approxBounds random trials (undefined::Word32))
69 checkBounds "Word64" boundedRange (approxBounds random trials (undefined::Word64))
70 checkBounds "Double" (True,0.0,1.0) (approxBounds random trials (undefined::Double))
71 checkBounds "Float" (True,0.0,1.0) (approxBounds random trials (undefined::Float))
72
73 checkBounds "CChar" boundedRange (approxBounds random trials (undefined:: CChar))
74 checkBounds "CSChar" boundedRange (approxBounds random trials (undefined:: CSChar))
75 checkBounds "CUChar" boundedRange (approxBounds random trials (undefined:: CUChar))
76 checkBounds "CShort" boundedRange (approxBounds random trials (undefined:: CShort))
77 checkBounds "CUShort" boundedRange (approxBounds random trials (undefined:: CUShort))
78 checkBounds "CInt" boundedRange (approxBounds random trials (undefined:: CInt))
79 checkBounds "CUInt" boundedRange (approxBounds random trials (undefined:: CUInt))
80 checkBounds "CLong" boundedRange (approxBounds random trials (undefined:: CLong))
81 checkBounds "CULong" boundedRange (approxBounds random trials (undefined:: CULong))
82 checkBounds "CPtrdiff" boundedRange (approxBounds random trials (undefined:: CPtrdiff))
83 checkBounds "CSize" boundedRange (approxBounds random trials (undefined:: CSize))
84 checkBounds "CWchar" boundedRange (approxBounds random trials (undefined:: CWchar))
85 checkBounds "CSigAtomic" boundedRange (approxBounds random trials (undefined:: CSigAtomic))
86 checkBounds "CLLong" boundedRange (approxBounds random trials (undefined:: CLLong))
87 checkBounds "CULLong" boundedRange (approxBounds random trials (undefined:: CULLong))
88 checkBounds "CIntPtr" boundedRange (approxBounds random trials (undefined:: CIntPtr))
89 checkBounds "CUIntPtr" boundedRange (approxBounds random trials (undefined:: CUIntPtr))
90 checkBounds "CIntMax" boundedRange (approxBounds random trials (undefined:: CIntMax))
91 checkBounds "CUIntMax" boundedRange (approxBounds random trials (undefined:: CUIntMax))
92
93 -- Then check all the range-restricted versions:
94 checkBounds "Int R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined::Int))
95 checkBounds "Integer R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined::Integer))
96 checkBounds "Int8 R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined::Int8))
97 checkBounds "Int8 Rsmall" (False,-50,50) (approxBounds (randomR (-50,50)) trials (undefined::Int8))
98 checkBounds "Int8 Rmini" (False,3,4) (approxBounds (randomR (3,4)) trials (undefined::Int8))
99 checkBounds "Int8 Rtrivial" (False,3,3) (approxBounds (randomR (3,3)) trials (undefined::Int8))
100
101 checkBounds "Int16 R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined::Int16))
102 checkBounds "Int32 R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined::Int32))
103 checkBounds "Int64 R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined::Int64))
104 checkBounds "Word R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined::Word))
105 checkBounds "Word8 R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined::Word8))
106 checkBounds "Word16 R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined::Word16))
107 checkBounds "Word32 R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined::Word32))
108 checkBounds "Word64 R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined::Word64))
109 checkBounds "Double R" (True,10.0,77.0) (approxBounds (randomR (10,77)) trials (undefined::Double))
110 checkBounds "Float R" (True,10.0,77.0) (approxBounds (randomR (10,77)) trials (undefined::Float))
111
112 checkBounds "CChar R" (False,0,100) (approxBounds (randomR (0,100)) trials (undefined:: CChar))
113 checkBounds "CSChar R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined:: CSChar))
114 checkBounds "CUChar R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined:: CUChar))
115 checkBounds "CShort R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined:: CShort))
116 checkBounds "CUShort R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined:: CUShort))
117 checkBounds "CInt R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined:: CInt))
118 checkBounds "CUInt R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined:: CUInt))
119 checkBounds "CLong R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined:: CLong))
120 checkBounds "CULong R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined:: CULong))
121 checkBounds "CPtrdiff R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined:: CPtrdiff))
122 checkBounds "CSize R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined:: CSize))
123 checkBounds "CWchar R" (False,0,100) (approxBounds (randomR (0,100)) trials (undefined:: CWchar))
124 checkBounds "CSigAtomic R" (False,0,100) (approxBounds (randomR (0,100)) trials (undefined:: CSigAtomic))
125 checkBounds "CLLong R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined:: CLLong))
126 checkBounds "CULLong R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined:: CULLong))
127 checkBounds "CIntPtr R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined:: CIntPtr))
128 checkBounds "CUIntPtr R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined:: CUIntPtr))
129 checkBounds "CIntMax R" (False,-100,100) (approxBounds (randomR (-100,100)) trials (undefined:: CIntMax))
130 checkBounds "CUIntMax R" (False,0,200) (approxBounds (randomR (0,200)) trials (undefined:: CUIntMax))
131
132 -- Untested:
133 -- instance Random Char where
134 -- instance Random Bool where
135 -- instance Random Integer where