Skip to content

Commit 7cb250b

Browse files
committed
Fix false negatives in IntMap.isProperSubmapOfBy
1 parent c651094 commit 7cb250b

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

containers-tests/tests/intmap-properties.hs

+6
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,9 @@ test_isProperSubmapOfBy = do
10141014
isProperSubmapOfBy (==) (fromList [(-1,1),(2,2)]) (fromList [(-1,1)]) @?= False
10151015
isProperSubmapOfBy (<) (fromList [(-1,1)]) (fromList [(-1,1),(2,2)]) @?= False
10161016

1017+
-- See Github #1007
1018+
isProperSubmapOfBy (==) (fromList [(-3,1),(-1,1)]) (fromList [(-3,1),(-1,1),(0,1)]) @?= True
1019+
10171020
test_isProperSubmapOf :: Assertion
10181021
test_isProperSubmapOf = do
10191022
isProperSubmapOf (fromList [(1,1)]) (fromList [(1,1),(2,2)]) @?= True
@@ -1024,6 +1027,9 @@ test_isProperSubmapOf = do
10241027
isProperSubmapOf (fromList [(-1,1),(2,2)]) (fromList [(-1,1),(2,2)]) @?= False
10251028
isProperSubmapOf (fromList [(-1,1),(2,2)]) (fromList [(-1,1)]) @?= False
10261029

1030+
-- See Github #1007
1031+
isProperSubmapOf (fromList [(-3,1),(-1,1)]) (fromList [(-3,1),(-1,1),(0,1)]) @?= True
1032+
10271033
----------------------------------------------------------------
10281034
-- Min/Max
10291035

containers-tests/tests/intset-properties.hs

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ main = defaultMain $ testGroup "intset-properties"
2020
, testCase "lookupLE" test_lookupLE
2121
, testCase "lookupGE" test_lookupGE
2222
, testCase "split" test_split
23+
, testCase "isProperSubsetOf" test_isProperSubsetOf
2324
, testProperty "prop_Valid" prop_Valid
2425
, testProperty "prop_EmptyValid" prop_EmptyValid
2526
, testProperty "prop_SingletonValid" prop_SingletonValid
@@ -109,6 +110,19 @@ test_split :: Assertion
109110
test_split = do
110111
split 3 (fromList [1..5]) @?= (fromList [1,2], fromList [4,5])
111112

113+
test_isProperSubsetOf :: Assertion
114+
test_isProperSubsetOf = do
115+
isProperSubsetOf (fromList [1]) (fromList [1,2]) @?= True
116+
isProperSubsetOf (fromList [1,2]) (fromList [1,2]) @?= False
117+
isProperSubsetOf (fromList [1,2]) (fromList [1]) @?= False
118+
119+
isProperSubsetOf (fromList [-1]) (fromList [-1,2]) @?= True
120+
isProperSubsetOf (fromList [-1,2]) (fromList [-1,2]) @?= False
121+
isProperSubsetOf (fromList [-1,2]) (fromList [-1]) @?= False
122+
123+
-- See Github #1007
124+
isProperSubsetOf (fromList [-65,-1]) (fromList [-65,-1,0]) @?= True
125+
112126
{--------------------------------------------------------------------
113127
Arbitrary, reasonably balanced trees
114128
--------------------------------------------------------------------}

containers/src/Data/IntMap/Internal.hs

+5-2
Original file line numberDiff line numberDiff line change
@@ -2367,11 +2367,14 @@ submapCmp :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
23672367
submapCmp predicate t1@(Bin p1 l1 r1) (Bin p2 l2 r2) = case mapMapBranch p1 p2 of
23682368
ABL -> GT
23692369
ABR -> GT
2370-
BAL -> submapCmp predicate t1 l2
2371-
BAR -> submapCmp predicate t1 r2
2370+
BAL -> submapCmpLt l2
2371+
BAR -> submapCmpLt r2
23722372
EQL -> submapCmpEq
23732373
NOM -> GT -- disjoint
23742374
where
2375+
submapCmpLt t = case submapCmp predicate t1 t of
2376+
GT -> GT
2377+
_ -> LT
23752378
submapCmpEq = case (submapCmp predicate l1 l2, submapCmp predicate r1 r2) of
23762379
(GT,_ ) -> GT
23772380
(_ ,GT) -> GT

0 commit comments

Comments
 (0)