@@ -7,12 +7,13 @@ import Control.Monad.Eff.Console (log, CONSOLE)
7
7
import Control.Monad.Eff.Exception (EXCEPTION )
8
8
import Control.Monad.Eff.Random (RANDOM )
9
9
10
+ import Data.Array as A
10
11
import Data.Foldable (foldl )
11
12
import Data.Function (on )
12
- import Data.List ( List (..), groupBy , sortBy , singleton , fromFoldable , zipWith )
13
+ import Data.List as L
13
14
import Data.List.NonEmpty as NEL
14
- import Data.NonEmpty ((:|))
15
15
import Data.Maybe (Maybe (..))
16
+ import Data.NonEmpty ((:|))
16
17
import Data.StrMap as M
17
18
import Data.Tuple (Tuple (..), fst )
18
19
@@ -25,7 +26,7 @@ import Test.QuickCheck.Gen as Gen
25
26
newtype TestStrMap v = TestStrMap (M.StrMap v )
26
27
27
28
instance arbTestStrMap :: (Arbitrary v ) => Arbitrary (TestStrMap v ) where
28
- arbitrary = TestStrMap <<< (M .fromFoldable :: List (Tuple String v ) -> M.StrMap v ) <$> arbitrary
29
+ arbitrary = TestStrMap <<< (M .fromFoldable :: L. List (Tuple String v ) -> M.StrMap v ) <$> arbitrary
29
30
30
31
data Instruction k v = Insert k v | Delete k
31
32
@@ -36,15 +37,15 @@ instance showInstruction :: (Show k, Show v) => Show (Instruction k v) where
36
37
instance arbInstruction :: (Arbitrary v ) => Arbitrary (Instruction String v ) where
37
38
arbitrary = do
38
39
b <- arbitrary
39
- k <- Gen .frequency $ Tuple 10.0 (pure " hasOwnProperty" ) :| Tuple 50.0 arbitrary ` Cons ` Nil
40
+ k <- Gen .frequency $ Tuple 10.0 (pure " hasOwnProperty" ) :| pure ( Tuple 50.0 arbitrary)
40
41
case b of
41
42
true -> do
42
43
v <- arbitrary
43
44
pure (Insert k v)
44
45
false -> do
45
46
pure (Delete k)
46
47
47
- runInstructions :: forall v . List (Instruction String v ) -> M.StrMap v -> M.StrMap v
48
+ runInstructions :: forall v . L. List (Instruction String v ) -> M.StrMap v -> M.StrMap v
48
49
runInstructions instrs t0 = foldl step t0 instrs
49
50
where
50
51
step tree (Insert k v) = M .insert k v tree
@@ -101,7 +102,7 @@ strMapTests = do
101
102
in M .lookup k tree == Just v <?> (" instrs:\n " <> show instrs <> " \n k:\n " <> show k <> " \n v:\n " <> show v)
102
103
103
104
log " Singleton to list"
104
- quickCheck $ \k v -> M .toList (M .singleton k v :: M.StrMap Int ) == singleton (Tuple k v)
105
+ quickCheck $ \k v -> M .toUnfoldable (M .singleton k v :: M.StrMap Int ) == L . singleton (Tuple k v)
105
106
106
107
log " fromFoldable [] = empty"
107
108
quickCheck (M .fromFoldable [] == (M .empty :: M.StrMap Unit )
@@ -125,26 +126,26 @@ strMapTests = do
125
126
quickCheck (M .lookup " 1" nums == Just 2 <?> " invalid lookup - 1" )
126
127
quickCheck (M .lookup " 2" nums == Nothing <?> " invalid lookup - 2" )
127
128
128
- log " toList . fromFoldable = id"
129
- quickCheck $ \arr -> let f x = M .toList (M .fromFoldable x)
130
- in f (f arr) == f (arr :: List (Tuple String Int )) <?> show arr
129
+ log " toUnfoldable . fromFoldable = id"
130
+ quickCheck $ \arr -> let f x = M .toUnfoldable (M .fromFoldable x)
131
+ in f (f arr) == f (arr :: L. List (Tuple String Int )) <?> show arr
131
132
132
- log " fromFoldable . toList = id"
133
+ log " fromFoldable . toUnfoldable = id"
133
134
quickCheck $ \(TestStrMap m) ->
134
- let f m1 = M .fromFoldable (M .toList m1) in
135
- M .toList (f m) == M .toList ( m :: M.StrMap Int ) <?> show m
135
+ let f m1 = M .fromFoldable (( M .toUnfoldable m1) :: L.List ( Tuple String Int ) ) in
136
+ M .toUnfoldable (f m) == ( M .toUnfoldable m :: L.List ( Tuple String Int ) ) <?> show m
136
137
137
138
log " fromFoldableWith const = fromFoldable"
138
139
quickCheck $ \arr -> M .fromFoldableWith const arr ==
139
- M .fromFoldable (arr :: List (Tuple String Int )) <?> show arr
140
+ M .fromFoldable (arr :: L. List (Tuple String Int )) <?> show arr
140
141
141
142
log " fromFoldableWith (<>) = fromFoldable . collapse with (<>) . group on fst"
142
143
quickCheck $ \arr ->
143
144
let combine (Tuple s a) (Tuple t b) = (Tuple s $ b <> a)
144
- foldl1 g = unsafePartial \(Cons x xs) -> foldl g x xs
145
+ foldl1 g = unsafePartial \(L. Cons x xs) -> foldl g x xs
145
146
f = M .fromFoldable <<< map (foldl1 combine <<< NEL .toList) <<<
146
- groupBy ((==) `on` fst) <<< sortBy (compare `on` fst) in
147
- M .fromFoldableWith (<>) arr == f (arr :: List (Tuple String String )) <?> show arr
147
+ L . groupBy ((==) `on` fst) <<< L. sortBy (compare `on` fst) in
148
+ M .fromFoldableWith (<>) arr == f (arr :: L. List (Tuple String String )) <?> show arr
148
149
149
150
log " Lookup from union"
150
151
quickCheck $ \(TestStrMap m1) (TestStrMap m2) k ->
@@ -157,13 +158,13 @@ strMapTests = do
157
158
(m1 `M.union` m2) == ((m1 `M.union` m2) `M.union` (m2 :: M.StrMap Int)) <?> (show (M.size (m1 `M.union` m2)) <> " != " <> show (M.size ((m1 `M.union` m2) `M.union` m2)))
158
159
159
160
log " fromFoldable = zip keys values"
160
- quickCheck $ \(TestStrMap m) -> M .toList m == zipWith Tuple (fromFoldable $ M .keys m) (M .values m :: List Int )
161
+ quickCheck $ \(TestStrMap m) -> M .toUnfoldable m == A . zipWith Tuple (M .keys m) (M .values m :: Array Int )
161
162
162
163
log " mapWithKey is correct"
163
164
quickCheck $ \(TestStrMap m :: TestStrMap Int ) -> let
164
165
f k v = k <> show v
165
166
resultViaMapWithKey = m # M .mapWithKey f
166
- resultViaLists = m # M .toList # map (\(Tuple k v) → Tuple k (f k v)) # M .fromFoldable
167
+ resultViaLists = m # M .toUnfoldable # map (\(Tuple k v) → Tuple k (f k v)) # ( M .fromFoldable :: forall a . L.List ( Tuple String a ) -> M.StrMap a )
167
168
in resultViaMapWithKey === resultViaLists
168
169
169
170
log " Bug #63: accidental observable mutation in foldMap"
0 commit comments