-
Notifications
You must be signed in to change notification settings - Fork 99
Generating expr #157
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
Closed
Closed
Generating expr #157
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
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,105 @@ | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
-- | Test utilities | ||
module Utils | ||
( Key (..) | ||
, Expr (..) | ||
, evalExprStrict | ||
, evalExprLazy | ||
, evalExprSet | ||
, evalExprOrdSet | ||
) where | ||
|
||
import Control.Applicative (liftA2, liftA3) | ||
import Data.Hashable (Hashable(hashWithSalt)) | ||
import Test.QuickCheck (Arbitrary(arbitrary,shrink), oneof, sized) | ||
|
||
import qualified Data.HashMap.Strict as HMS | ||
import qualified Data.HashMap.Lazy as HML | ||
import qualified Data.HashSet as HS | ||
import qualified Data.Set as Set | ||
|
||
-- | A key with collision-prone Hashable instance | ||
------------------------------------------------------------------------ | ||
|
||
-- Key type that generates more hash collisions. | ||
newtype Key = K { unK :: Int } | ||
deriving (Arbitrary, Enum, Eq, Ord, Read, Show, Integral, Num, Real) | ||
|
||
instance Hashable Key where | ||
hashWithSalt salt k = hashWithSalt salt (unK k) `mod` 20 | ||
|
||
-- | A HashMap/HashSet building expression | ||
------------------------------------------------------------------------ | ||
|
||
data Expr k v | ||
= ExprEmpty | ||
| ExprSingleton k v | ||
| ExprInsert k v (Expr k v) | ||
| ExprDelete k (Expr k v) | ||
| ExprUnion (Expr k v) (Expr k v) | ||
deriving Show | ||
|
||
instance (Arbitrary k, Arbitrary v) => Arbitrary (Expr k v) where | ||
arbitrary = sized arb | ||
where | ||
arb n | n <= 0 = oneof leafs | ||
arb n = oneof $ leafs ++ | ||
[ liftA3 ExprInsert arbitrary arbitrary (arb (n - 1)) | ||
, liftA2 ExprDelete arbitrary (arb (n - 1)) | ||
, liftA2 ExprUnion (arb (n `div` 2)) (arb (n - n `div` 2)) | ||
] | ||
|
||
leafs = | ||
[ return ExprEmpty | ||
, liftA2 ExprSingleton arbitrary arbitrary | ||
] | ||
|
||
shrink ExprEmpty | ||
= [] | ||
shrink (ExprSingleton k v) = | ||
ExprEmpty : uncurry ExprSingleton `map` shrink (k, v) | ||
shrink (ExprInsert k v e) = | ||
ExprEmpty : e : uncurry3 ExprInsert `map` shrink (k, v, e) | ||
shrink (ExprDelete k e) = | ||
ExprEmpty : e : uncurry ExprDelete `map` shrink (k, e) | ||
shrink (ExprUnion a b) = | ||
ExprEmpty : a : b : uncurry ExprUnion `map` shrink (a, b) | ||
|
||
uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d | ||
uncurry3 f (a, b, c) = f a b c | ||
|
||
evalExprStrict :: (Eq k, Hashable k) => Expr k v -> HMS.HashMap k v | ||
evalExprStrict = go | ||
where | ||
go ExprEmpty = HMS.empty | ||
go (ExprSingleton k v) = HMS.singleton k v | ||
go (ExprInsert k v e) = HMS.insert k v (go e) | ||
go (ExprDelete k e) = HMS.delete k (go e) | ||
go (ExprUnion a b) = HMS.union (go a) (go b) | ||
|
||
evalExprLazy :: (Eq k, Hashable k) => Expr k v -> HML.HashMap k v | ||
evalExprLazy = go | ||
where | ||
go ExprEmpty = HML.empty | ||
go (ExprSingleton k v) = HML.singleton k v | ||
go (ExprInsert k v e) = HML.insert k v (go e) | ||
go (ExprDelete k e) = HML.delete k (go e) | ||
go (ExprUnion a b) = HML.union (go a) (go b) | ||
|
||
evalExprSet :: (Eq k, Hashable k) => Expr k () -> HS.HashSet k | ||
evalExprSet = go | ||
where | ||
go ExprEmpty = HS.empty | ||
go (ExprSingleton k _) = HS.singleton k | ||
go (ExprInsert k _ e) = HS.insert k (go e) | ||
go (ExprDelete k e) = HS.delete k (go e) | ||
go (ExprUnion a b) = HS.union (go a) (go b) | ||
|
||
evalExprOrdSet :: (Ord k) => Expr k () -> Set.Set k | ||
evalExprOrdSet = go | ||
where | ||
go ExprEmpty = Set.empty | ||
go (ExprSingleton k _) = Set.singleton k | ||
go (ExprInsert k _ e) = Set.insert k (go e) | ||
go (ExprDelete k e) = Set.delete k (go e) | ||
go (ExprUnion a b) = Set.union (go a) (go b) |
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.
Why
div
and notquot
?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.