Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false negatives in IntMap.isProperSubmapOfBy #1008

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions containers-tests/tests/intmap-properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,9 @@ test_isProperSubmapOfBy = do
isProperSubmapOfBy (==) (fromList [(-1,1),(2,2)]) (fromList [(-1,1)]) @?= False
isProperSubmapOfBy (<) (fromList [(-1,1)]) (fromList [(-1,1),(2,2)]) @?= False

-- See Github #1007
isProperSubmapOfBy (==) (fromList [(-3,1),(-1,1)]) (fromList [(-3,1),(-1,1),(0,1)]) @?= True

test_isProperSubmapOf :: Assertion
test_isProperSubmapOf = do
isProperSubmapOf (fromList [(1,1)]) (fromList [(1,1),(2,2)]) @?= True
Expand All @@ -1024,6 +1027,9 @@ test_isProperSubmapOf = do
isProperSubmapOf (fromList [(-1,1),(2,2)]) (fromList [(-1,1),(2,2)]) @?= False
isProperSubmapOf (fromList [(-1,1),(2,2)]) (fromList [(-1,1)]) @?= False

-- See Github #1007
isProperSubmapOf (fromList [(-3,1),(-1,1)]) (fromList [(-3,1),(-1,1),(0,1)]) @?= True

----------------------------------------------------------------
-- Min/Max

Expand Down
14 changes: 14 additions & 0 deletions containers-tests/tests/intset-properties.hs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a property test for sets and maps that catches this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The property test is how konsumlamm caught this (#1007).

But it's random and such cases are apparently unlikely, so it didn't show up when the test was added (in #995).

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ main = defaultMain $ testGroup "intset-properties"
, testCase "lookupLE" test_lookupLE
, testCase "lookupGE" test_lookupGE
, testCase "split" test_split
, testCase "isProperSubsetOf" test_isProperSubsetOf
, testProperty "prop_Valid" prop_Valid
, testProperty "prop_EmptyValid" prop_EmptyValid
, testProperty "prop_SingletonValid" prop_SingletonValid
Expand Down Expand Up @@ -109,6 +110,19 @@ test_split :: Assertion
test_split = do
split 3 (fromList [1..5]) @?= (fromList [1,2], fromList [4,5])

test_isProperSubsetOf :: Assertion
test_isProperSubsetOf = do
isProperSubsetOf (fromList [1]) (fromList [1,2]) @?= True
isProperSubsetOf (fromList [1,2]) (fromList [1,2]) @?= False
isProperSubsetOf (fromList [1,2]) (fromList [1]) @?= False

isProperSubsetOf (fromList [-1]) (fromList [-1,2]) @?= True
isProperSubsetOf (fromList [-1,2]) (fromList [-1,2]) @?= False
isProperSubsetOf (fromList [-1,2]) (fromList [-1]) @?= False

-- See Github #1007
isProperSubsetOf (fromList [-65,-1]) (fromList [-65,-1,0]) @?= True

{--------------------------------------------------------------------
Arbitrary, reasonably balanced trees
--------------------------------------------------------------------}
Expand Down
7 changes: 5 additions & 2 deletions containers/src/Data/IntMap/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2367,11 +2367,14 @@ submapCmp :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Ordering
submapCmp predicate t1@(Bin p1 l1 r1) (Bin p2 l2 r2) = case mapMapBranch p1 p2 of
ABL -> GT
ABR -> GT
BAL -> submapCmp predicate t1 l2
BAR -> submapCmp predicate t1 r2
BAL -> submapCmpLt l2
BAR -> submapCmpLt r2
EQL -> submapCmpEq
NOM -> GT -- disjoint
where
submapCmpLt t = case submapCmp predicate t1 t of
GT -> GT
_ -> LT
submapCmpEq = case (submapCmp predicate l1 l2, submapCmp predicate r1 r2) of
(GT,_ ) -> GT
(_ ,GT) -> GT
Expand Down
Loading