From b2084b6df9ea5dfde0d3aa75631a902966fe1da1 Mon Sep 17 00:00:00 2001 From: Marco Cetica Date: Wed, 18 Sep 2024 10:47:29 +0200 Subject: [PATCH] Added unit tests for functor laws --- .../ceticamarco/lambdatonic/LeftTests.java | 26 +++++++++++++++++++ .../ceticamarco/lambdatonic/RightTests.java | 26 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/test/java/com/ceticamarco/lambdatonic/LeftTests.java b/src/test/java/com/ceticamarco/lambdatonic/LeftTests.java index dd9e21c..ac15d70 100644 --- a/src/test/java/com/ceticamarco/lambdatonic/LeftTests.java +++ b/src/test/java/com/ceticamarco/lambdatonic/LeftTests.java @@ -3,6 +3,8 @@ package com.ceticamarco.lambdatonic; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach; +import java.util.function.Function; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -48,4 +50,28 @@ public class LeftTests { _ -> "Undefined variable" ), "Undefined variable"); } + + @Test + public void testLeftFunctorIdentityMorphism() { + // Applying the map function with the identity function, + // should not change data type structure + Either result = this.numEither.map(Function.identity()); + + assertEquals(this.numEither, result); + } + + @Test + public void testLeftFunctorCompositionMorphism() { + // map ( f . g ) == map f . map g + Function f = x -> x + 1; + Function g = x -> x * 2; + + // Evaluate map x . map g + Either gMapped = this.numEither.map(g); + Either FGMapped = gMapped.map(f); + // Evaluate map ( f . g ) + Either composition = this.numEither.map(f.compose(g)); + + assertEquals(composition, FGMapped); + } } diff --git a/src/test/java/com/ceticamarco/lambdatonic/RightTests.java b/src/test/java/com/ceticamarco/lambdatonic/RightTests.java index 140e6f9..f23f9ab 100644 --- a/src/test/java/com/ceticamarco/lambdatonic/RightTests.java +++ b/src/test/java/com/ceticamarco/lambdatonic/RightTests.java @@ -3,6 +3,8 @@ package com.ceticamarco.lambdatonic; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach; +import java.util.function.Function; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -48,4 +50,28 @@ public class RightTests { x -> x ), 16); } + + @Test + public void testRightFunctorIdentityMorphism() { + // Applying the map function with the identity function, + // should not change data type structure + Either result = this.numEither.map(Function.identity()); + + assertEquals(this.numEither, result); + } + + @Test + public void testRightFunctorCompositionMorphism() { + // map ( f . g ) == map f . map g + Function f = x -> x + 1; + Function g = x -> x * 2; + + // Evaluate map x . map g + Either gMapped = this.numEither.map(g); + Either FGMapped = gMapped.map(f); + // Evaluate map ( f . g ) + Either composition = this.numEither.map(f.compose(g)); + + assertEquals(composition, FGMapped); + } }