11module Test.Data.Map where
22
33import Prelude
4-
4+ import Data.List.NonEmpty as NEL
5+ import Data.Map as M
56import Control.Alt ((<|>))
67import Control.Monad.Eff (Eff )
78import Control.Monad.Eff.Console (log , CONSOLE )
89import Control.Monad.Eff.Exception (EXCEPTION )
910import Control.Monad.Eff.Random (RANDOM )
10-
1111import Data.Foldable (foldl , for_ , all )
1212import Data.Function (on )
13- import Data.List (List (..), groupBy , length , nubBy , sortBy , singleton )
14- import Data.List.NonEmpty as NEL
15- import Data.Map as M
13+ import Data.List (List (Cons), groupBy , length , nubBy , singleton , sort , sortBy )
1614import Data.Maybe (Maybe (..), fromMaybe )
1715import Data.Tuple (Tuple (..), fst )
18-
1916import Partial.Unsafe (unsafePartial )
20-
2117import Test.QuickCheck ((<?>), (===), quickCheck , quickCheck' )
2218import Test.QuickCheck.Arbitrary (class Arbitrary , arbitrary )
2319
@@ -194,21 +190,26 @@ mapTests = do
194190 quickCheck (M .lookup 1 nums == Just 2 <?> " invalid lookup - 1" )
195191 quickCheck (M .lookup 2 nums == Nothing <?> " invalid lookup - 2" )
196192
197- log " toList . fromFoldable = id"
198- quickCheck $ \arr -> let f x = M .toList (M .fromFoldable x)
199- in f (f arr) == f (arr :: List (Tuple SmallKey Int )) <?> show arr
193+ log " sort . toList . fromFoldable = sort (on lists without key-duplicates)"
194+ quickCheck $ \(list :: List (Tuple SmallKey Int )) ->
195+ let nubbedList = nubBy ((==) `on` fst) list
196+ f x = M .toList (M .fromFoldable x)
197+ in sort (f nubbedList) == sort nubbedList <?> show nubbedList
200198
201199 log " fromFoldable . toList = id"
202- quickCheck $ \(TestMap m) -> let f m' = M .fromFoldable (M .toList m') in
203- M .toList (f m) == M .toList (m :: M.Map SmallKey Int ) <?> show m
200+ quickCheck $ \(TestMap (m :: M.Map SmallKey Int )) ->
201+ let f m' = M .fromFoldable (M .toList m')
202+ in f m == m <?> show m
204203
205204 log " fromFoldable . toUnfoldable = id"
206- quickCheck $ \(TestMap m) -> let f m' = M .fromFoldable (M .toUnfoldable m' :: List (Tuple SmallKey Int )) in
207- f m == (m :: M.Map SmallKey Int ) <?> show m
205+ quickCheck $ \(TestMap (m :: M.Map SmallKey Int )) ->
206+ let f m' = M .fromFoldable (M .toUnfoldable m' :: List (Tuple SmallKey Int ))
207+ in f m == m <?> show m
208208
209209 log " fromFoldableWith const = fromFoldable"
210- quickCheck $ \arr -> M .fromFoldableWith const arr ==
211- M .fromFoldable (arr :: List (Tuple SmallKey Int )) <?> show arr
210+ quickCheck $ \arr ->
211+ M .fromFoldableWith const arr ==
212+ M .fromFoldable (arr :: List (Tuple SmallKey Int )) <?> show arr
212213
213214 log " fromFoldableWith (<>) = fromFoldable . collapse with (<>) . group on fst"
214215 quickCheck $ \arr ->
@@ -218,10 +219,10 @@ mapTests = do
218219 groupBy ((==) `on` fst) <<< sortBy (compare `on` fst) in
219220 M .fromFoldableWith (<>) arr === f (arr :: List (Tuple String String ))
220221
221- log " toAscList is sorted version of toList "
222+ log " toAscUnfoldable is sorted version of toUnfoldable "
222223 quickCheck $ \(TestMap m) ->
223- let list = M .toList (m :: M.Map SmallKey Int )
224- ascList = M .toAscList m
224+ let list = M .toUnfoldable (m :: M.Map SmallKey Int )
225+ ascList = M .toAscUnfoldable m
225226 in ascList === sortBy (compare `on` fst) list
226227
227228 log " Lookup from union"
0 commit comments