Skip to content

Commit 4dc074d

Browse files
oisdktreeowl
authored andcommitted
test stability of sort and sortOn (#514)
The test may not be necessary for `sortOn`, but that's okay.
1 parent 0e4a010 commit 4dc074d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tests/seq-properties.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ main = defaultMain
9797
, testProperty "partition" prop_partition
9898
, testProperty "filter" prop_filter
9999
, testProperty "sort" prop_sort
100+
, testProperty "sortStable" prop_sortStable
100101
, testProperty "sortBy" prop_sortBy
101102
, testProperty "sortOn" prop_sortOn
103+
, testProperty "sortOnStable" prop_sortOnStable
102104
, testProperty "unstableSort" prop_unstableSort
103105
, testProperty "unstableSortBy" prop_unstableSortBy
104106
, testProperty "unstableSortOn" prop_unstableSortOn
@@ -560,6 +562,30 @@ prop_sort :: Seq OrdA -> Bool
560562
prop_sort xs =
561563
toList' (sort xs) ~= Data.List.sort (toList xs)
562564

565+
data UnstableOrd = UnstableOrd
566+
{ ordKey :: OrdA
567+
, _ignored :: A
568+
} deriving (Show)
569+
570+
instance Eq UnstableOrd where
571+
x == y = compare x y == EQ
572+
573+
instance Ord UnstableOrd where
574+
compare (UnstableOrd x _) (UnstableOrd y _) = compare x y
575+
576+
instance Arbitrary UnstableOrd where
577+
arbitrary = liftA2 UnstableOrd arbitrary arbitrary
578+
shrink (UnstableOrd x y) =
579+
[ UnstableOrd x' y'
580+
| (x',y') <- shrink (x, y) ]
581+
582+
prop_sortStable :: Seq UnstableOrd -> Bool
583+
prop_sortStable xs =
584+
(fmap . fmap) unignore (toList' (sort xs)) ~=
585+
fmap unignore (Data.List.sort (toList xs))
586+
where
587+
unignore (UnstableOrd x y) = (x, y)
588+
563589
prop_sortBy :: Seq (OrdA, B) -> Bool
564590
prop_sortBy xs =
565591
toList' (sortBy f xs) ~= Data.List.sortBy f (toList xs)
@@ -575,6 +601,16 @@ prop_sortOn (Fun _ f) xs =
575601
listSortOn k = Data.List.sortBy (compare `on` k)
576602
#endif
577603

604+
prop_sortOnStable :: Fun A UnstableOrd -> Seq A -> Bool
605+
prop_sortOnStable (Fun _ f) xs =
606+
toList' (sortOn f xs) ~= listSortOn f (toList xs)
607+
where
608+
#if MIN_VERSION_base(4,8,0)
609+
listSortOn = Data.List.sortOn
610+
#else
611+
listSortOn k = Data.List.sortBy (compare `on` k)
612+
#endif
613+
578614
prop_unstableSort :: Seq OrdA -> Bool
579615
prop_unstableSort xs =
580616
toList' (unstableSort xs) ~= Data.List.sort (toList xs)

0 commit comments

Comments
 (0)