PageRenderTime 131ms CodeModel.GetById 71ms app.highlight 14ms RepoModel.GetById 44ms app.codeStats 0ms

/tests/tests.hs

http://github.com/exclipy/pdata
Haskell | 105 lines | 91 code | 14 blank | 0 comment | 25 complexity | f8efec8c395f3210365eda4e8bcd4e5d MD5 | raw file
  1import Test.QuickCheck
  2import Test.QuickCheck.Batch
  3import Data.Hashable
  4import Data.HamtMap as HM
  5import Data.Int
  6import Data.List (foldl', sort)
  7import Data.Maybe (isNothing)
  8import Prelude as P
  9
 10newtype Inty = Inty Int deriving (Eq, Show, Ord)
 11
 12instance Hashable Inty where
 13    hash (Inty i) = fromIntegral (hash i `mod` 2)
 14
 15instance Arbitrary Inty where
 16    arbitrary = do
 17        x <- arbitrary
 18        return $ Inty x
 19
 20ldelete :: (Eq k) => k -> [(k, v)] -> [(k, v)]
 21ldelete _ [] = []
 22ldelete k ((k', v'):xs) | k' == k   = ldelete k xs
 23                        | otherwise = (k', v') : ldelete k xs
 24
 25lset :: (Eq k) => k -> v -> [(k, v)] -> [(k, v)]
 26lset k v [] = []
 27lset k v ((k', v'):xs) | k' == k   = (k, v) : lset k v xs
 28                       | otherwise = (k', v') : lset k v xs
 29
 30prop_insert :: (Eq k, Hashable k, Eq v) => k -> v -> [(k, v)] -> Bool
 31prop_insert k v lm =
 32    let hm  = fromList lm
 33        hm' = insert k v hm
 34        in    member k hm'
 35           && not (notMember k hm')
 36           && HM.lookup k hm' == Just v
 37           && hm' ! k == v
 38           && (if member k hm
 39                  then toList hm == lset k (hm ! k) (toList hm')
 40                  else toList hm == ldelete k (toList hm')
 41                  )
 42
 43prop_delete :: (Eq k, Hashable k, Eq v) => k -> [(k, v)] -> Bool
 44prop_delete k lm =
 45    let hm  = fromList lm
 46        hm' = delete k hm
 47        in    not (member k hm')
 48           && notMember k hm'
 49           && isNothing (HM.lookup k hm')
 50           && (if member k hm
 51                  then toList hm' == ldelete k (toList hm)
 52                  else toList hm' == toList hm
 53                  )
 54
 55prop_fromList :: (Eq k, Hashable k, Ord k, Eq v, Ord v) => [(k, v)] -> Bool
 56prop_fromList lm =
 57    let hm = fromList lm
 58        hm' = foldl' (\hm (k,v) -> insert k v hm) empty lm
 59        in sort (toList hm) == sort (toList hm')
 60
 61prop_toList :: (Eq k, Hashable k, Eq v) => [(k, v)] -> Bool
 62prop_toList lm =
 63    let hm = fromList lm
 64        lm' = toList hm
 65        ks = keys hm
 66        ks' = P.map fst lm'
 67        els = elems hm
 68        els' = P.map snd lm'
 69        els'' = P.map (\k -> hm ! k) ks
 70        in    ks == ks'
 71           && els == els'
 72           && els == els''
 73
 74prop_map :: (Eq k, Hashable k, Integral v) => [(k, v)] -> Bool
 75prop_map lm =
 76    let hm = fromList lm
 77        lm' = toList hm
 78        in toList (HM.map (*2) hm) == P.map (\(x,y) -> (x, y*2)) lm'
 79
 80prop_filter :: (Eq k, Hashable k, Integral v) => [(k, v)] -> Bool
 81prop_filter lm =
 82    let hm = fromList lm
 83        lm' = toList hm
 84        in toList (HM.filter even hm) == P.filter (\(x,y) -> even y) lm'
 85
 86
 87options = TestOptions
 88      { no_of_tests         = 200
 89      , length_of_tests     = 2 -- seconds
 90      , debug_tests         = False }
 91
 92main = runTests "tests" options
 93    [ run (prop_insert :: Int -> Int -> [(Int,Int)] -> Bool)
 94    , run (prop_insert :: Inty -> Int -> [(Inty,Int)] -> Bool)
 95    , run (prop_delete :: Int -> [(Int, Int)] -> Bool)
 96    , run (prop_delete :: Inty -> [(Inty, Int)] -> Bool)
 97    , run (prop_fromList :: [(Int, Int)] -> Bool)
 98    , run (prop_fromList :: [(Inty, Int)] -> Bool)
 99    , run (prop_toList :: [(Int, Int)] -> Bool)
100    , run (prop_toList :: [(Inty, Int)] -> Bool)
101    , run (prop_map :: [(Int, Int)] -> Bool)
102    , run (prop_map :: [(Inty, Int)] -> Bool)
103    , run (prop_filter :: [(Int, Int)] -> Bool)
104    , run (prop_filter :: [(Inty, Int)] -> Bool)
105    ]