-
-
Notifications
You must be signed in to change notification settings - Fork 39
[#32] Add InsertLookup property test for PrefixTree #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| resolver: lts-11.0 | ||
| resolver: lts-11.7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,8 @@ | ||
| module Main where | ||
|
|
||
| import Test.Tasty (defaultMain) | ||
|
|
||
| import Test.Toml.PrefixTree (prefixTreeTests) | ||
|
|
||
| main :: IO () | ||
| main = putStrLn "Test suite not yet implemented" | ||
| main = prefixTreeTests >>= defaultMain |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| module Test.Toml.PrefixTree | ||
| ( prefixTreeTests | ||
| ) where | ||
|
|
||
| import Test.Tasty (TestTree, testGroup) | ||
|
|
||
| import Test.Toml.PrefixTree.Property (propertyTests) | ||
| import Test.Toml.PrefixTree.Unit (unitTests) | ||
|
|
||
| prefixTreeTests :: IO TestTree | ||
| prefixTreeTests = do | ||
| uTests <- unitTests | ||
| pure $ testGroup "Prefix Tree tests" $ uTests : propertyTests | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| {-# LANGUAGE PatternSynonyms #-} | ||
| {-# LANGUAGE ScopedTypeVariables #-} | ||
|
|
||
| module Test.Toml.PrefixTree.Property | ||
| ( propertyTests | ||
| ) where | ||
|
|
||
| import Control.Monad (forM) | ||
|
|
||
| import Hedgehog (MonadGen, Property, forAll, property, (===)) | ||
| import Test.Tasty (TestTree) | ||
| import Test.Tasty.Hedgehog (testProperty) | ||
|
|
||
| import Toml.PrefixTree (pattern (:||), Key (..), Piece (..), PrefixMap, PrefixTree (..)) | ||
|
|
||
| import qualified Data.HashMap.Strict as HashMap | ||
| import qualified Hedgehog.Gen as Gen | ||
| import qualified Hedgehog.Range as Range | ||
| import qualified Toml.PrefixTree as Prefix | ||
|
|
||
| propertyTests :: [TestTree] | ||
| propertyTests = [insertLookup, insertInsert] | ||
|
|
||
| insertLookup, insertInsert :: TestTree | ||
| insertLookup = testProperty "lookup k (insert k v m) == Just v" prop_InsertLookup | ||
| insertInsert = testProperty "insert x a . insert x b == insert x a" prop_InsertInsert | ||
|
|
||
| ---------------------------------------------------------------------------- | ||
| -- Common generators | ||
| ---------------------------------------------------------------------------- | ||
|
|
||
| type V = Int | ||
|
|
||
| genVal :: MonadGen m => m V | ||
| genVal = Gen.int (Range.constant 0 256) | ||
|
|
||
| genPiece :: MonadGen m => m Piece | ||
| genPiece = Piece <$> Gen.text (Range.constant 1 50) Gen.unicode | ||
|
|
||
| genKey :: MonadGen m => m Key | ||
| genKey = Key <$> Gen.nonEmpty (Range.constant 1 10) genPiece | ||
|
|
||
| -- Generates key-value pair for PrefixMap | ||
| genEntry :: MonadGen m => m (Piece, Key) | ||
| genEntry = do | ||
| key@(piece :|| _) <- genKey | ||
| pure (piece, key) | ||
|
|
||
| genPrefixMap :: MonadGen m => m (PrefixMap V) | ||
| genPrefixMap = do | ||
| entries <- Gen.list (Range.linear 0 10) genEntry | ||
| kvps <- forM entries $ \(piece, key) -> do | ||
| tree <- genPrefixTree key | ||
| pure (piece, tree) | ||
|
|
||
| pure $ HashMap.fromList kvps | ||
|
|
||
| genPrefixTree :: forall m . MonadGen m => Key -> m (PrefixTree V) | ||
| genPrefixTree key = Gen.recursive | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's nice to see this library feature to work for us! 👏 |
||
| -- list picker generator combinator | ||
| Gen.choice | ||
| -- non-recursive generators | ||
| [ Leaf key <$> genVal ] | ||
| -- recursive generators | ||
| [ genPrefixMap >>= genBranch ] | ||
| where | ||
| genBranch :: PrefixMap V -> m (PrefixTree V) | ||
| genBranch prefMap = do | ||
| prefVal <- Gen.maybe genVal | ||
| pure $ Branch key prefVal prefMap | ||
|
|
||
| ---------------------------------------------------------------------------- | ||
| -- InsertLookup | ||
| ---------------------------------------------------------------------------- | ||
|
|
||
| prop_InsertLookup :: Property | ||
| prop_InsertLookup = property $ do | ||
| t <- forAll genPrefixMap | ||
| key <- forAll genKey | ||
| val <- forAll genVal | ||
|
|
||
| Prefix.lookup key (Prefix.insert key val t) === Just val | ||
|
|
||
| -- DEBUG: ensures that trees of depth at least 5 are generated | ||
| -- assert $ depth prefMap < 5 | ||
|
|
||
| ---------------------------------------------------------------------------- | ||
| -- InsertInsert | ||
| ---------------------------------------------------------------------------- | ||
|
|
||
| prop_InsertInsert :: Property | ||
| prop_InsertInsert = property $ do | ||
| t <- forAll genPrefixMap | ||
| x <- forAll genKey | ||
| a <- forAll genVal | ||
| b <- forAll genVal | ||
|
|
||
| Prefix.lookup x (Prefix.insert x a $ Prefix.insert x b t) === Just a | ||
|
|
||
| ---------------------------------------------------------------------------- | ||
| -- DEBUG | ||
| ---------------------------------------------------------------------------- | ||
|
|
||
| -- useful functions to test generators | ||
| -- TODO: commented to avoid warnings | ||
|
|
||
| -- depth :: PrefixMap a -> Int | ||
| -- depth = HashMap.foldl' (\acc t -> max acc (depthT t)) 0 | ||
| -- | ||
| -- depthT :: PrefixTree a -> Int | ||
| -- depthT (Leaf _ _) = 1 | ||
| -- depthT (Branch _ _ prefMap) = 1 + depth prefMap | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| {-# LANGUAGE PatternSynonyms #-} | ||
| {-# LANGUAGE TypeApplications #-} | ||
|
|
||
| module Test.Toml.PrefixTree.Unit | ||
| ( unitTests | ||
| ) where | ||
|
|
||
| import Test.Tasty (TestTree) | ||
| import Test.Tasty.Hspec (Spec, describe, it, shouldBe, testSpec) | ||
|
|
||
| import Toml.PrefixTree (pattern (:||)) | ||
|
|
||
| import qualified Toml.PrefixTree as Prefix | ||
|
|
||
| unitTests :: IO TestTree | ||
| unitTests = testSpec "PrefixTree unit tests" spec_PrefixTree | ||
|
|
||
| spec_PrefixTree :: Spec | ||
| spec_PrefixTree = do | ||
| -- some test keys | ||
| let a = "a" :|| [] | ||
| let b = "b" :|| [] | ||
| let c = "c" :|| [] | ||
| let ab = "a" :|| ["b"] | ||
|
|
||
| describe "Insert and lookup unit tests" $ do | ||
| it "Lookup on empty map returns Nothing" $ | ||
| Prefix.lookup @Bool a mempty `shouldBe` Nothing | ||
| it "Lookup in single map returns this element" $ do | ||
| let t = Prefix.single a True | ||
| Prefix.lookup a t `shouldBe` Just True | ||
| Prefix.lookup b t `shouldBe` Nothing | ||
| it "Lookup after insert returns this element" $ do | ||
| let t = Prefix.insert a True mempty | ||
| Prefix.lookup a t `shouldBe` Just True | ||
| Prefix.lookup b t `shouldBe` Nothing | ||
| it "Lookup after multiple non-overlapping inserts" $ do | ||
| let t = Prefix.insert a True $ Prefix.insert b False mempty | ||
| Prefix.lookup a t `shouldBe` Just True | ||
| Prefix.lookup b t `shouldBe` Just False | ||
| Prefix.lookup c t `shouldBe` Nothing | ||
| it "Prefix lookup" $ do | ||
| let t = Prefix.insert ab True mempty | ||
| Prefix.lookup a t `shouldBe` Nothing | ||
| Prefix.lookup ab t `shouldBe` Just True | ||
| it "Composite key lookup" $ do | ||
| let t = Prefix.insert a True $ Prefix.insert ab False mempty | ||
| Prefix.lookup a t `shouldBe` Just True | ||
| Prefix.lookup ab t `shouldBe` Just False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like this structure! 👍 It makes code easier to read with this test separation