1
1
module Main where
2
2
3
3
import Control.Applicative ((<$>) )
4
+ import Control.Exception (evaluate )
4
5
import Control.Monad (replicateM )
6
+ import Data.Hashable (Hashable (.. ))
5
7
import qualified Data.HashMap.Strict as HM
8
+ import qualified Data.HashMap.Lazy as HML
6
9
import Data.List (delete )
7
10
import Data.Maybe
11
+ import System.Mem (performGC )
12
+ import System.Mem.Weak (mkWeakPtr , deRefWeak )
8
13
import Test.HUnit (Assertion , assert )
9
14
import Test.Framework (Test , defaultMain )
10
15
import Test.Framework.Providers.HUnit (testCase )
@@ -71,6 +76,48 @@ propEqAfterDelete (Keys keys) =
71
76
mapFromKeys :: [Int ] -> HM. HashMap Int ()
72
77
mapFromKeys keys = HM. fromList (zip keys (repeat () ))
73
78
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
+
74
121
------------------------------------------------------------------------
75
122
-- * Test list
76
123
@@ -80,6 +127,8 @@ tests =
80
127
testCase " issue32" issue32
81
128
, testCase " issue39a" issue39
82
129
, testProperty " issue39b" propEqAfterDelete
130
+ , testCase " issue254 lazy" issue254Lazy
131
+ , testCase " issue254 strict" issue254Strict
83
132
]
84
133
85
134
------------------------------------------------------------------------
0 commit comments