@@ -4,6 +4,7 @@ import Prelude
4
4
5
5
import Control.Alt ((<|>))
6
6
import Data.Array as A
7
+ import Data.Array.NonEmpty as NEA
7
8
import Data.Foldable (foldl , for_ , all , and )
8
9
import Data.FoldableWithIndex (foldrWithIndex )
9
10
import Data.Function (on )
@@ -44,7 +45,7 @@ instance showSmallKey :: Show SmallKey where
44
45
show J = " J"
45
46
46
47
instance arbSmallKey :: Arbitrary SmallKey where
47
- arbitrary = elements $ A :| [B , C , D , E , F , G , H , I , J ]
48
+ arbitrary = elements $ NEA .fromNonEmpty $ A :| [B , C , D , E , F , G , H , I , J ]
48
49
49
50
data Instruction k v = Insert k v | Delete k
50
51
@@ -53,7 +54,7 @@ instance showInstruction :: (Show k, Show v) => Show (Instruction k v) where
53
54
show (Delete k) = " Delete (" <> show k <> " )"
54
55
55
56
instance arbInstruction :: (Arbitrary k , Arbitrary v ) => Arbitrary (Instruction k v ) where
56
- arbitrary = oneOf $ (Insert <$> arbitrary <*> arbitrary) :| [Delete <$> arbitrary]
57
+ arbitrary = oneOf $ NEA .fromNonEmpty $ (Insert <$> arbitrary <*> arbitrary) :| [Delete <$> arbitrary]
57
58
58
59
runInstructions :: forall k v . Ord k => List (Instruction k v ) -> M.Map k v -> M.Map k v
59
60
runInstructions instrs t0 = foldl step t0 instrs
@@ -240,6 +241,12 @@ mapTests = do
240
241
Nothing -> not (M .member k m1 && M .member k m2)
241
242
Just v -> Just v == (op <$> M .lookup k m1 <*> M .lookup k m2)
242
243
244
+ log " map-apply is equivalent to intersectionWith"
245
+ for_ [(+), (*)] $ \op ->
246
+ quickCheck $ \(TestMap m1) (TestMap m2) ->
247
+ let u = M .intersectionWith op m1 m2 :: M.Map SmallKey Int
248
+ in u == (op <$> m1 <*> m2)
249
+
243
250
log " difference"
244
251
quickCheck $ \(TestMap m1) (TestMap m2) ->
245
252
let d = M .difference (m1 :: M.Map SmallKey Int ) (m2 :: M.Map SmallKey String )
@@ -315,23 +322,23 @@ mapTests = do
315
322
316
323
log " filterWithKey keeps those keys for which predicate is true"
317
324
quickCheck $ \(TestMap s :: TestMap String Int ) p ->
318
- A . all (uncurry p) (M .toUnfoldable (M .filterWithKey p s) :: Array (Tuple String Int ))
325
+ all (uncurry p) (M .toUnfoldable (M .filterWithKey p s) :: Array (Tuple String Int ))
319
326
320
327
log " filterKeys gives submap"
321
328
quickCheck $ \(TestMap s :: TestMap String Int ) p ->
322
329
M .isSubmap (M .filterKeys p s) s
323
330
324
331
log " filterKeys keeps those keys for which predicate is true"
325
332
quickCheck $ \(TestMap s :: TestMap String Int ) p ->
326
- A . all p (M .keys (M .filterKeys p s))
333
+ all p (M .keys (M .filterKeys p s))
327
334
328
335
log " filter gives submap"
329
336
quickCheck $ \(TestMap s :: TestMap String Int ) p ->
330
337
M .isSubmap (M .filter p s) s
331
338
332
339
log " filter keeps those values for which predicate is true"
333
340
quickCheck $ \(TestMap s :: TestMap String Int ) p ->
334
- A . all p (M .values (M .filter p s))
341
+ all p (M .values (M .filter p s))
335
342
336
343
log " submap with no bounds = id"
337
344
quickCheck \(TestMap m :: TestMap SmallKey Int ) ->
@@ -371,6 +378,17 @@ mapTests = do
371
378
let outList = foldrWithIndex (\i a b -> (Tuple i a) : b) Nil m
372
379
in outList == sort outList
373
380
381
+ log " bind"
382
+ quickCheck $ \(TestMap m1) (TestMap m2 :: TestMap SmallKey Int ) (TestMap m3) k ->
383
+ let
384
+ u = do
385
+ v <- m1
386
+ if v then m2 else m3
387
+ in case M .lookup k m1 of
388
+ Just true -> M .lookup k m2 == M .lookup k u
389
+ Just false -> M .lookup k m3 == M .lookup k u
390
+ Nothing -> not $ M .member k u
391
+
374
392
log " catMaybes creates a new map of size less than or equal to the original"
375
393
quickCheck \(TestMap m :: TestMap Int (Maybe Int )) -> do
376
394
let result = M .catMaybes m
0 commit comments