Skip to content

Commit b2a54c8

Browse files
authored
Fix false negatives in IntMap.isProperSubmapOfBy (#1008)
1 parent 8562003 commit b2a54c8

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

containers-tests/tests/intmap-properties.hs

Lines changed: 6 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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
@@ -107,6 +108,19 @@ test_split :: Assertion
107108
test_split = do
108109
split 3 (fromList [1..5]) @?= (fromList [1,2], fromList [4,5])
109110

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

containers/src/Data/IntMap/Internal.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,11 +2339,14 @@ submapCmp :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
23392339
submapCmp predicate t1@(Bin p1 l1 r1) (Bin p2 l2 r2) = case treeTreeBranch p1 p2 of
23402340
ABL -> GT
23412341
ABR -> GT
2342-
BAL -> submapCmp predicate t1 l2
2343-
BAR -> submapCmp predicate t1 r2
2342+
BAL -> submapCmpLt l2
2343+
BAR -> submapCmpLt r2
23442344
EQL -> submapCmpEq
23452345
NOM -> GT -- disjoint
23462346
where
2347+
submapCmpLt t = case submapCmp predicate t1 t of
2348+
GT -> GT
2349+
_ -> LT
23472350
submapCmpEq = case (submapCmp predicate l1 l2, submapCmp predicate r1 r2) of
23482351
(GT,_ ) -> GT
23492352
(_ ,GT) -> GT

0 commit comments

Comments
 (0)