reddit-pub/test/Test/Data/Trie.hs

33 lines
980 B
Haskell

{-# LANGUAGE NamedFieldPuns #-}
module Test.Data.Trie where
import Test.Hspec
import Test.Hspec.Hedgehog
import Data.ByteString (ByteString)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import qualified Data.Map.Strict as M
import Control.Monad.State (evalStateT, foldM, execStateT, StateT (runStateT))
import qualified Data.Foldable as F
import qualified Data.Trie as Trie
import Data.Monoid
genByteString :: Gen ByteString
genByteString = Gen.bytes (Range.linear 0 50)
genModel :: Gen (M.Map ByteString (Sum Int))
genModel = Gen.map (Range.linear 0 30) genT
where
genT = (,) <$> genByteString <*> (Sum <$> Gen.integral (Range.linear 0 1000))
propInsert :: PropertyT IO ()
propInsert = do
model <- forAll genModel
let got = F.foldl' (\acc (k,v) -> Trie.insert k v acc) Trie.empty $ M.toList model
model === M.fromList (Trie.toList got)
spec :: Spec
spec = describe "Data.Trie" $
it "inserts as with Map" $ hedgehog propInsert