Skip to content

Commit

Permalink
add a testcase for Data.IntMap.Strict.fromAscList strictness
Browse files Browse the repository at this point in the history
- the function is rather peculiar in that in the presence of
  duplicate keys, the first value is not evaluated, but all
  the others are evaluated. See also #473.
  • Loading branch information
int-e authored and treeowl committed Aug 14, 2020
1 parent 4ac960a commit 9765edd
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions containers-tests/tests/intmap-strictness.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Test.QuickCheck.Function (Fun(..), apply)

import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as M
import qualified Data.IntMap as L
import Data.Containers.ListUtils

instance Arbitrary v => Arbitrary (IntMap v) where
arbitrary = M.fromList `fmap` arbitrary
Expand Down Expand Up @@ -76,6 +78,25 @@ pInsertLookupWithKeyValueStrict f k v m
not (isBottom $ M.insertLookupWithKey (const3 1) k bottom m)
| otherwise = isBottom $ M.insertLookupWithKey (apply3 f) k bottom m

------------------------------------------------------------------------
-- test a corner case of fromAscList
--
-- If the list contains duplicate keys, then (only) the first of the
-- given values is not evaluated. This may change in the future, see
-- also https://github.com/haskell/containers/issues/473

pFromAscListLazy :: [Int] -> Bool
pFromAscListLazy ks = not . isBottom $ M.fromAscList elems
where
elems = [(k, v) | k <- nubInt ks, v <- [undefined, ()]]

pFromAscListStrict :: [Int] -> Bool
pFromAscListStrict ks
| null ks = not . isBottom $ M.fromAscList elems
| otherwise = isBottom $ M.fromAscList elems
where
elems = [(k, v) | k <- nubInt ks, v <- [undefined, undefined, ()]]

------------------------------------------------------------------------
-- * Test list

Expand Down Expand Up @@ -103,6 +124,8 @@ tests =
pInsertLookupWithKeyKeyStrict
, testProperty "insertLookupWithKey is value-strict"
pInsertLookupWithKeyValueStrict
, testProperty "fromAscList is somewhat value-lazy" pFromAscListLazy
, testProperty "fromAscList is somewhat value-strict" pFromAscListStrict
]
]

Expand Down

0 comments on commit 9765edd

Please sign in to comment.