on main help sign in
[#8] Add property tests for insert lookup (#16)

* Add property tests for insert lookup Remove Tree structure and convert sorted list directly to the list with the required order. Fix work with 8.0.2 * Add insert.insert test, fix CI

commite3462514ff6800c967fd09ed6b9763958ea74166 authorVeronika Romashkina <vrom911@gmail.com> dateSun 8 Jul 2018, 22:58 (8 years ago) parente64c000
4 files changed+107−36
.travis.yml+2−2 internal/Data/TypeRep/CacheMap.hs+36−33 test/Test/TypeRep/MapProperty.hs+61 typerep-map.cabal+8−1
.travis.yml+2−2
@@ -16,7 +16,7 @@ matrix:
1616 include:
1717
1818 - ghc: 8.0.2
19− env: GHCVER='8.0.2' CABALVER='head'
19+ env: GHCVER='8.0.2' CABALVER='head' ALLOWNEWER='--allow-newer'
2020 os: linux
2121 addons:
2222 apt:
@@ -77,7 +77,7 @@ install:
7777script:
7878 - |
7979 if [ -z "$STACK_YAML" ]; then
80− cabal new-test
80+ cabal new-test $ALLOWNEWER
8181 else
8282 stack build --test --bench --no-terminal
8383 fi
internal/Data/TypeRep/CacheMap.hs+36−33
@@ -1,4 +1,5 @@
11{-# LANGUAGE BangPatterns #-}
2+{-# LANGUAGE DataKinds #-}
23{-# LANGUAGE GADTs #-}
34{-# LANGUAGE KindSignatures #-}
45{-# LANGUAGE MagicHash #-}
@@ -20,12 +21,17 @@ module Data.TypeRep.CacheMap
2021 -- * Helpful testing functions
2122 , TF (..)
2223 , fromList
24+ , toFps
2325 ) where
2426
2527import Prelude hiding (lookup)
2628
2729import Control.Arrow ((&&&))
30+import Data.Function (on)
31+import Data.IntMap.Strict (IntMap)
2832import Data.Kind (Type)
33+import Data.List (nubBy)
34+import Data.Maybe (fromJust)
2935import Data.Proxy (Proxy (..))
3036import Data.Typeable (Typeable, typeRep, typeRepFingerprint)
3137import Data.Word (Word64)
@@ -36,6 +42,7 @@ import GHC.Prim (eqWord#, ltWord#)
3642import GHC.Word (Word64 (..))
3743import Unsafe.Coerce (unsafeCoerce)
3844
45+import qualified Data.IntMap.Strict as IM
3946import qualified Data.Vector as V
4047import qualified Data.Vector.Unboxed as Unboxed
4148
@@ -45,6 +52,14 @@ data TypeRepMap (f :: k -> Type) = TypeRepMap
4552 , anys :: V.Vector Any
4653 }
4754
55+instance Show (TypeRepMap f) where
56+ show = show . toFps
57+
58+toFps :: TypeRepMap f -> [Fingerprint]
59+toFps TypeRepMap{..} = zipWith Fingerprint
60+ (Unboxed.toList fingerprintAs)
61+ (Unboxed.toList fingerprintBs)
62+
4863fromAny :: Any -> f a
4964fromAny = unsafeCoerce
5065
@@ -57,10 +72,7 @@ insert :: forall a f . Typeable a => f a -> TypeRepMap f -> TypeRepMap f
5772insert x = fromListPairs . addX . toPairList
5873 where
5974 toPairList :: TypeRepMap f -> [(Fingerprint, Any)]
60− toPairList (TypeRepMap as bs ans) = zip (zipWith Fingerprint
61− (Unboxed.toList as)
62− (Unboxed.toList bs))
63− (V.toList ans)
75+ toPairList trMap = zip (toFps trMap) (V.toList $ anys trMap)
6476
6577 pairX :: (Fingerprint, Any)
6678 pairX@(fpX, _) = (calcFp x, unsafeCoerce x)
@@ -114,7 +126,10 @@ cachedBinarySearch (Fingerprint (W64# a) (W64# b)) fpAs fpBs = inline (go 0#)
114126----------------------------------------------------------------------------
115127
116128data TF f where
117− TF :: Typeable a => f a -> TF f
129+ TF :: Typeable a => f a -> TF f
130+
131+instance Show (TF f) where
132+ show (TF tf) = show $ calcFp tf
118133
119134fromF :: Typeable a => f a -> Proxy a
120135fromF _ = Proxy
@@ -126,7 +141,7 @@ fromListPairs :: [(Fingerprint, Any)] -> TypeRepMap f
126141fromListPairs kvs = TypeRepMap (Unboxed.fromList fpAs) (Unboxed.fromList fpBs) (V.fromList ans)
127142 where
128143 (fpAs, fpBs) = unzip $ map (\(Fingerprint a b) -> (a, b)) fps
129− (fps, ans) = unzip $ breadthFirst $ fromListToTree $ sortWith fst kvs
144+ (fps, ans) = unzip $ fromSortedList $ sortWith fst $ nubPairs kvs
130145
131146fromList :: forall f . [TF f] -> TypeRepMap f
132147fromList = fromListPairs . map (fp &&& an)
@@ -137,34 +152,22 @@ fromList = fromListPairs . map (fp &&& an)
137152 an :: TF f -> Any
138153 an (TF x) = unsafeCoerce x
139154
155+nubPairs :: (Eq a) => [(a, b)] -> [(a, b)]
156+nubPairs = nubBy ((==) `on` fst)
157+
140158----------------------------------------------------------------------------
141−-- Tree
159+-- Tree-like conversion
142160----------------------------------------------------------------------------
143161
144−data Tree a = Leaf | Node a (Tree a) (Tree a)
145− deriving (Show)
146−
147−fromListToTree :: [a] -> Tree a
148−fromListToTree [] = Leaf
149−fromListToTree xs =
150− let len = length xs
151− in case splitAt (len `div` 2) xs of
152− ([], []) -> Leaf
153− (ls, []) -> Node (last ls) (fromListToTree $ init ls) Leaf
154− ([], (r:rs)) -> Node r Leaf $ fromListToTree rs
155− (ls, r:rs) -> Node r (fromListToTree ls) (fromListToTree rs)
156−
157−breadthFirst :: Tree a -> [a]
158−breadthFirst tree = bf [tree]
162+fromSortedList :: forall a . [a] -> [a]
163+fromSortedList l = IM.elems $ fst $ go 0 0 mempty (IM.fromList $ zip [0..] l)
159164 where
160− bf :: [Tree a] -> [a]
161− bf [] = []
162− bf xs = mapValue xs ++ bf (concatMap children xs)
163−
164− mapValue :: [Tree a] -> [a]
165− mapValue [] = []
166− mapValue (Leaf : xs) = mapValue xs
167− mapValue (Node a _ _ : xs) = a : mapValue xs
168−
169− children Leaf = []
170− children (Node _ l r) = [l, r]
165+ -- state monad could be used here, but it's another dependency
166+ go :: Int -> Int -> IntMap a -> IntMap a -> (IntMap a, Int)
167+ go i first result vector =
168+ if i >= IM.size vector
169+ then (result, first)
170+ else do
171+ let (newResult, newFirst) = go (2 * i + 1) first result vector
172+ let withCur = IM.insert i (fromJust $ IM.lookup newFirst vector) newResult
173+ go (2 * i + 2) (newFirst + 1) withCur vector
test/Test/TypeRep/MapProperty.hsnew file+61
@@ -0,0 +1,61 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE KindSignatures #-}
3+{-# LANGUAGE PolyKinds #-}
4+
5+{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}
6+
7+module Test.TypeRep.MapProperty where
8+
9+import Prelude hiding (lookup)
10+
11+import Data.Proxy (Proxy (..))
12+import GHC.Stack (HasCallStack)
13+import GHC.TypeLits (Nat, SomeNat (..), someNatVal)
14+import Hedgehog (MonadGen, PropertyT, forAll, property, (===))
15+import Test.Tasty (TestName, TestTree)
16+import Test.Tasty.Hedgehog (testProperty)
17+
18+import Data.TypeRep.CacheMap (TF (..), TypeRepMap, fromList, insert, lookup)
19+
20+import qualified Hedgehog.Gen as Gen
21+import qualified Hedgehog.Range as Range
22+
23+type PropertyTest = [TestTree]
24+
25+prop :: HasCallStack => TestName -> PropertyT IO () -> PropertyTest
26+prop testName = pure . testProperty testName . property
27+
28+test_InsertLookup :: PropertyTest
29+test_InsertLookup = prop "lookup k (insert k v m) == Just v" $ do
30+ m <- forAll genMap
31+ TF (proxy :: IntProxy n) <- forAll genTF
32+
33+ lookup @n @IntProxy (insert proxy m) === Just proxy
34+
35+test_InsertInsert :: PropertyTest
36+test_InsertInsert = prop "insert k b . insert k a == insert k b" $ do
37+ m <- forAll genMap
38+ TF a@(IntProxy (proxy :: Proxy n) i) <- forAll genTF
39+ let b = IntProxy proxy (i + 1)
40+ lookup @n @IntProxy (insert b $ insert a m) === Just b
41+
42+
43+----------------------------------------------------------------------------
44+-- Generators
45+----------------------------------------------------------------------------
46+
47+data IntProxy (n :: Nat) = IntProxy (Proxy n) Int
48+ deriving (Show, Eq)
49+
50+
51+genMap :: MonadGen m => m (TypeRepMap IntProxy)
52+genMap = fromList <$> Gen.list (Range.linear 0 1000) genTF
53+
54+
55+genTF :: MonadGen m => m (TF IntProxy)
56+genTF = do
57+ randNat :: Integer <- Gen.integral (Range.linear 0 10000)
58+ randInt <- Gen.int Range.constantBounded
59+ case someNatVal randNat of
60+ Just (SomeNat proxyNat) -> pure $ TF $ IntProxy proxyNat randInt
61+ Nothing -> error "Invalid test generator"
typerep-map.cabal+8−1
@@ -52,16 +52,23 @@ test-suite typerep-map-test
5252 main-is: Test.hs
5353 other-modules: Test.TypeRep.CMap
5454 , Test.TypeRep.CacheMap
55+ , Test.TypeRep.MapProperty
5556 , Test.TypeRep.Vector
5657 , Test.TypeRep.VectorOpt
5758 build-depends: base
59+ , ghc-typelits-knownnat
60+ , hedgehog
5861 , typerep-map
5962 , typerep-map-internal
6063 , tasty
6164 , tasty-discover >= 4.1.1
65+ , tasty-hedgehog
6266 , tasty-hspec
6367 build-tool-depends: tasty-discover:tasty-discover
6468 ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
69+ default-extensions: ScopedTypeVariables
70+ TypeApplications
71+
6572 default-language: Haskell2010
6673
6774benchmark typerep-map-benchmark
@@ -76,11 +83,11 @@ benchmark typerep-map-benchmark
7683 , OptimalVector
7784 build-depends: base
7885 , criterion
79− , typerep-map-internal
8086 , deepseq
8187 , dependent-map
8288 , dependent-sum
8389 , ghc-typelits-knownnat
90+ , typerep-map-internal
8491 default-extensions: OverloadedStrings
8592 RecordWildCards
8693 ScopedTypeVariables