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
|
module Data.TrieSpec (main, spec) where
import Control.Applicative ((<$>))
import Data.List (nub, sort)
import Data.Trie
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck
newtype T = T Trie deriving (Show, Eq)
instance Arbitrary T where
arbitrary = anything >>= \s -> return . T $ insert s empty
-- Just for the 0-255 range.
newtype ASCII = ASCII Trie deriving (Show, Eq)
newtype Anything = Anything { _unA :: String } deriving (Show, Eq)
instance Arbitrary Anything where
arbitrary = Anything <$> anything
instance Arbitrary ASCII where
arbitrary = arbitrary >>= \s -> return . ASCII $ insert s empty
-- | Generates chars from the full allowed range then sticks them
-- together. This lets us check that our tree works for any garbage
-- thrown at it.
anything :: Gen String
anything = listOf $ choose (minBound, maxBound)
main :: IO ()
main = hspec spec
-- Resizes a property to 10000 runs
size :: Spec -> Spec
size = modifyMaxSuccess (const 10000)
spec :: Spec
spec = do
describe "fromList . toList ≡ id" $ do
size $ prop "anything" $ \(T x) ->
fromList (toList x) `shouldBe` x
size $ prop "ASCII" $ \(ASCII x) ->
fromList (toList x) `shouldBe` x
describe "sort . nub . toList . fromList ≡ sort . nub" $ do
size $ prop "anything" $ \xs ->
let ys = sort . nub $ map _unA xs
in toList (fromList ys) `shouldBe` ys
size $ prop "ASCII" $ \xs ->
let ys = sort $ nub xs
in toList (fromList ys) `shouldBe` ys
describe "toList (fromString x) ≡ [x]" $ do
size $ prop "anything" $ \(Anything x) ->
toList (fromString x) `shouldBe` [x]
size $ prop "ASCII" $ \x ->
toList (fromString x) `shouldBe` [x]
|