Skip to content

Commit 9765edd

Browse files
int-etreeowl
authored andcommitted
add a testcase for Data.IntMap.Strict.fromAscList strictness
- 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.
1 parent 4ac960a commit 9765edd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

containers-tests/tests/intmap-strictness.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import Test.QuickCheck.Function (Fun(..), apply)
1010

1111
import Data.IntMap.Strict (IntMap)
1212
import qualified Data.IntMap.Strict as M
13+
import qualified Data.IntMap as L
14+
import Data.Containers.ListUtils
1315

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

81+
------------------------------------------------------------------------
82+
-- test a corner case of fromAscList
83+
--
84+
-- If the list contains duplicate keys, then (only) the first of the
85+
-- given values is not evaluated. This may change in the future, see
86+
-- also https://github.com/haskell/containers/issues/473
87+
88+
pFromAscListLazy :: [Int] -> Bool
89+
pFromAscListLazy ks = not . isBottom $ M.fromAscList elems
90+
where
91+
elems = [(k, v) | k <- nubInt ks, v <- [undefined, ()]]
92+
93+
pFromAscListStrict :: [Int] -> Bool
94+
pFromAscListStrict ks
95+
| null ks = not . isBottom $ M.fromAscList elems
96+
| otherwise = isBottom $ M.fromAscList elems
97+
where
98+
elems = [(k, v) | k <- nubInt ks, v <- [undefined, undefined, ()]]
99+
79100
------------------------------------------------------------------------
80101
-- * Test list
81102

@@ -103,6 +124,8 @@ tests =
103124
pInsertLookupWithKeyKeyStrict
104125
, testProperty "insertLookupWithKey is value-strict"
105126
pInsertLookupWithKeyValueStrict
127+
, testProperty "fromAscList is somewhat value-lazy" pFromAscListLazy
128+
, testProperty "fromAscList is somewhat value-strict" pFromAscListStrict
106129
]
107130
]
108131

0 commit comments

Comments
 (0)