Skip to content

Commit b480f9b

Browse files
committed
Tests for Bifoldable default implementations
1 parent 9be5ba7 commit b480f9b

File tree

1 file changed

+60
-12
lines changed

1 file changed

+60
-12
lines changed

test/Main.purs

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,19 @@ main = do
4747
testSequenceDefault 20
4848

4949
log "Test Bifoldable on `inclusive or`"
50-
testBifoldableIOrWith 10 100 42
50+
testBifoldableIOrWith id 10 100 42
51+
52+
log "Test bifoldMapDefaultL"
53+
testBifoldableIOrWith BFML 10 100 42
54+
55+
log "Test bifoldMapDefaultR"
56+
testBifoldableIOrWith BFMR 10 100 42
57+
58+
log "Test bifoldlDefault"
59+
testBifoldableIOrWith BFLD 10 100 42
60+
61+
log "Test bifoldrDefault"
62+
testBifoldableIOrWith BFRD 10 100 42
5163

5264
log "Test Bitraversable on `inclusive or`"
5365
testBitraversableIOrWith id
@@ -199,19 +211,22 @@ instance bitraversableIOr :: Bitraversable IOr where
199211
bisequence (Fst fst) = Fst <$> fst
200212
bisequence (Snd snd) = Snd <$> snd
201213

202-
testBifoldableIOrWith :: forall e. Int -> Int -> Int -> Eff (assert :: ASSERT | e) Unit
203-
testBifoldableIOrWith fst snd u = do
204-
assert $ bifoldr (+) (*) u (Both fst snd) == fst + (snd * u)
205-
assert $ bifoldr (+) (*) u (Fst fst) == fst + u
206-
assert $ bifoldr (+) (*) u (Snd snd) == snd * u
214+
testBifoldableIOrWith :: forall t e. (Bifoldable t, Eq (t Int Int)) =>
215+
(forall l r. IOr l r -> t l r) ->
216+
Int -> Int -> Int ->
217+
Eff (assert :: ASSERT | e) Unit
218+
testBifoldableIOrWith lift fst snd u = do
219+
assert $ bifoldr (+) (*) u (lift $ Both fst snd) == fst + (snd * u)
220+
assert $ bifoldr (+) (*) u (lift $ Fst fst) == fst + u
221+
assert $ bifoldr (+) (*) u (lift $ Snd snd) == snd * u
207222

208-
assert $ bifoldl (+) (*) u (Both fst snd) == (u + fst) * snd
209-
assert $ bifoldl (+) (*) u (Fst fst) == u + fst
210-
assert $ bifoldl (+) (*) u (Snd snd) == u * snd
223+
assert $ bifoldl (+) (*) u (lift $ Both fst snd) == (u + fst) * snd
224+
assert $ bifoldl (+) (*) u (lift $ Fst fst) == u + fst
225+
assert $ bifoldl (+) (*) u (lift $ Snd snd) == u * snd
211226

212-
assert $ bifoldMap Additive Additive (Both fst snd) == Additive (fst + snd)
213-
assert $ bifoldMap Additive Additive (Fst fst) == Additive fst
214-
assert $ bifoldMap Additive Additive (Snd snd) == Additive snd
227+
assert $ bifoldMap Additive Additive (lift $ Both fst snd) == Additive (fst + snd)
228+
assert $ bifoldMap Additive Additive (lift $ Fst fst) == Additive fst
229+
assert $ bifoldMap Additive Additive (lift $ Snd snd) == Additive snd
215230

216231
testBitraversableIOrWith :: forall t e. (Bitraversable t, Eq (t Boolean Boolean)) =>
217232
(forall l r. IOr l r -> t l r) ->
@@ -226,6 +241,39 @@ testBitraversableIOrWith lift = do
226241
assert $ bitraverse Just Just (lift (Snd false)) == just (Snd false :: IOr Boolean Boolean)
227242

228243

244+
-- structures for testing default `Bifoldable` implementations
245+
246+
newtype BifoldMapDefaultL l r = BFML (IOr l r)
247+
newtype BifoldMapDefaultR l r = BFMR (IOr l r)
248+
newtype BifoldlDefault l r = BFLD (IOr l r)
249+
newtype BifoldrDefault l r = BFRD (IOr l r)
250+
251+
instance eqBFML :: (Eq l, Eq r) => Eq (BifoldMapDefaultL l r) where eq (BFML l) (BFML r) = l == r
252+
instance eqBFMR :: (Eq l, Eq r) => Eq (BifoldMapDefaultR l r) where eq (BFMR l) (BFMR r) = l == r
253+
instance eqBFLD :: (Eq l, Eq r) => Eq (BifoldlDefault l r) where eq (BFLD l) (BFLD r) = l == r
254+
instance eqBFRD :: (Eq l, Eq r) => Eq (BifoldrDefault l r) where eq (BFRD l) (BFRD r) = l == r
255+
256+
instance bifoldableBFML :: Bifoldable BifoldMapDefaultL where
257+
bifoldMap f g m = bifoldMapDefaultL f g m
258+
bifoldr f g u (BFML m) = bifoldr f g u m
259+
bifoldl f g u (BFML m) = bifoldl f g u m
260+
261+
instance bifoldableBFMR :: Bifoldable BifoldMapDefaultR where
262+
bifoldMap f g m = bifoldMapDefaultR f g m
263+
bifoldr f g u (BFMR m) = bifoldr f g u m
264+
bifoldl f g u (BFMR m) = bifoldl f g u m
265+
266+
instance bifoldableBFLD :: Bifoldable BifoldlDefault where
267+
bifoldMap f g (BFLD m) = bifoldMap f g m
268+
bifoldr f g u (BFLD m) = bifoldr f g u m
269+
bifoldl f g u m = bifoldlDefault f g u m
270+
271+
instance bifoldableBFRD :: Bifoldable BifoldrDefault where
272+
bifoldMap f g (BFRD m) = bifoldMap f g m
273+
bifoldr f g u m = bifoldrDefault f g u m
274+
bifoldl f g u (BFRD m) = bifoldl f g u m
275+
276+
229277
-- structures for testing default `Bitraversable` implementations
230278

231279
newtype BitraverseDefault l r = BTD (IOr l r)

0 commit comments

Comments
 (0)