Skip to content

Commit d1d0151

Browse files
authored
#254, add a regression test
1 parent 647f6af commit d1d0151

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tests/Regressions.hs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
module Main where
22

33
import Control.Applicative ((<$>))
4+
import Control.Exception (evaluate)
45
import Control.Monad (replicateM)
6+
import Data.Hashable (Hashable(..))
57
import qualified Data.HashMap.Strict as HM
8+
import qualified Data.HashMap.Lazy as HML
69
import Data.List (delete)
710
import Data.Maybe
11+
import System.Mem (performGC)
12+
import System.Mem.Weak (mkWeakPtr, deRefWeak)
813
import Test.HUnit (Assertion, assert)
914
import Test.Framework (Test, defaultMain)
1015
import Test.Framework.Providers.HUnit (testCase)
@@ -71,6 +76,48 @@ propEqAfterDelete (Keys keys) =
7176
mapFromKeys :: [Int] -> HM.HashMap Int ()
7277
mapFromKeys keys = HM.fromList (zip keys (repeat ()))
7378

79+
------------------------------------------------------------------------
80+
-- Issue #254
81+
82+
data KC = KC Int
83+
deriving (Eq, Ord, Show)
84+
instance Hashable KC where
85+
hashWithSalt salt _ = salt
86+
87+
issue254Lazy :: Assertion
88+
issue254Lazy = issue254LazyLambda 2
89+
90+
-- Important that oldV is not hoisted out by optimisation, so use NOINLINE
91+
{-# NOINLINE issue254LazyLambda #-}
92+
issue254LazyLambda :: Int -> Assertion
93+
issue254LazyLambda i = do
94+
_ <- return ()
95+
let oldV = show i
96+
weakV <- mkWeakPtr oldV Nothing
97+
let mp = HML.insert (KC 1) "3" $ HML.fromList [(KC 0, "1"), (KC 1, oldV)]
98+
_ <- evaluate mp
99+
performGC
100+
res <- deRefWeak weakV
101+
_ <- evaluate mp
102+
assert $ isNothing res
103+
104+
issue254Strict :: Assertion
105+
issue254Strict = issue254StrictLambda 2
106+
107+
-- Important that oldV is not hoisted out by optimisation, so use NOINLINE
108+
{-# NOINLINE issue254StrictLambda #-}
109+
issue254StrictLambda :: Int -> Assertion
110+
issue254StrictLambda i = do
111+
_ <- return ()
112+
let oldV = show i
113+
weakV <- mkWeakPtr oldV Nothing
114+
let mp = HM.insert (KC 1) "3" $ HM.fromList [(KC 0, "1"), (KC 1, oldV)]
115+
_ <- evaluate mp
116+
performGC
117+
res <- deRefWeak weakV
118+
_ <- evaluate mp
119+
assert $ isNothing res
120+
74121
------------------------------------------------------------------------
75122
-- * Test list
76123

@@ -80,6 +127,8 @@ tests =
80127
testCase "issue32" issue32
81128
, testCase "issue39a" issue39
82129
, testProperty "issue39b" propEqAfterDelete
130+
, testCase "issue254 lazy" issue254Lazy
131+
, testCase "issue254 strict" issue254Strict
83132
]
84133

85134
------------------------------------------------------------------------

0 commit comments

Comments
 (0)