File: TrieSpec.hs

package info (click to toggle)
haskell-word-trie 0.3.0-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 84 kB
  • sloc: haskell: 94; makefile: 6
file content (62 lines) | stat: -rw-r--r-- 1,718 bytes parent folder | download | duplicates (5)
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]