summaryrefslogtreecommitdiff
path: root/tests/Tests/Helpers.hs
blob: eb3511511642958665176a1231c3565ac2aeb882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{-# LANGUAGE ScopedTypeVariables #-}
-- | Helpers for testing
module Tests.Helpers (
    -- * helpers
    T(..)
  , typeName
  , Double01(..)
    -- * IEEE 754
  , isDenorm
    -- * Generic QC tests
  , monotonicallyIncreases
  , monotonicallyIncreasesIEEE
    -- * HUnit helpers
  , testAssertion
  , testEquality
    -- * QC helpers
  , small
  , unsquare
  , shrinkFixedList
  ) where

import Data.Typeable
import Numeric.MathFunctions.Constants (m_tiny)
import Test.Framework
import Test.Framework.Providers.HUnit
import Test.QuickCheck
import qualified Numeric.IEEE as IEEE
import qualified Test.HUnit as HU

-- | Phantom typed value used to select right instance in QC tests
data T a = T

-- | String representation of type name
typeName :: Typeable a => T a -> String
typeName = show . typeOf . typeParam
  where
    typeParam :: T a -> a
    typeParam _ = undefined

-- | Check if Double denormalized
isDenorm :: Double -> Bool
isDenorm x = let ax = abs x in ax > 0 && ax < m_tiny

-- | Generates Doubles in range [0,1]
newtype Double01 = Double01 Double
                   deriving (Show)
instance Arbitrary Double01 where
  arbitrary = do
    (_::Int, x) <- fmap properFraction arbitrary
    return $ Double01 x

----------------------------------------------------------------
-- Generic QC
----------------------------------------------------------------

-- Check that function is nondecreasing
monotonicallyIncreases :: (Ord a, Ord b) => (a -> b) -> a -> a -> Bool
monotonicallyIncreases f x1 x2 = f (min x1 x2) <= f (max x1 x2)

-- Check that function is nondecreasing taking rounding errors into
-- account.
--
-- In fact funstion is allowed to decrease less than one ulp in order
-- to guard againist problems with excess precision. On x86 FPU works
-- with 80-bit numbers but doubles are 64-bit so rounding happens
-- whenever values are moved from registers to memory
monotonicallyIncreasesIEEE :: (Ord a, IEEE.IEEE b)  => (a -> b) -> a -> a -> Bool
monotonicallyIncreasesIEEE f x1 x2 =
  y1 <= y2 || (y1 - y2) < y2 * IEEE.epsilon
  where
    y1 = f (min x1 x2)
    y2 = f (max x1 x2)

----------------------------------------------------------------
-- HUnit helpers
----------------------------------------------------------------

testAssertion :: String -> Bool -> Test
testAssertion str cont = testCase str $ HU.assertBool str cont

testEquality :: (Show a, Eq a) => String -> a -> a -> Test
testEquality msg a b = testCase msg $ HU.assertEqual msg a b

unsquare :: (Arbitrary a, Show a, Testable b) => (a -> b) -> Property
unsquare = forAll (small arbitrary)

small :: Gen a -> Gen a
small act = sized $ \n -> resize (smallish n) act
  where smallish = round . (sqrt :: Double -> Double) . fromIntegral . abs

shrinkFixedList :: (a -> [a]) -> [a] -> [[a]]
shrinkFixedList shr (x:xs) = map (:xs) (shr x) ++ map (x:) (shrinkFixedList shr xs)
shrinkFixedList _   []     = []