Added unit tests for functor laws

This commit is contained in:
Marco Cetica 2024-09-18 10:47:29 +02:00
parent 7ebc811d0e
commit b2084b6df9
Signed by: marco
GPG Key ID: 45060A949E90D0FD
2 changed files with 52 additions and 0 deletions

View File

@ -3,6 +3,8 @@ package com.ceticamarco.lambdatonic;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@ -48,4 +50,28 @@ public class LeftTests {
_ -> "Undefined variable" _ -> "Undefined variable"
), "Undefined variable"); ), "Undefined variable");
} }
@Test
public void testLeftFunctorIdentityMorphism() {
// Applying the map function with the identity function,
// should not change data type structure
Either<Error, Integer> result = this.numEither.map(Function.identity());
assertEquals(this.numEither, result);
}
@Test
public void testLeftFunctorCompositionMorphism() {
// map ( f . g ) == map f . map g
Function<Integer, Integer> f = x -> x + 1;
Function<Integer, Integer> g = x -> x * 2;
// Evaluate map x . map g
Either<Error, Integer> gMapped = this.numEither.map(g);
Either<Error, Integer> FGMapped = gMapped.map(f);
// Evaluate map ( f . g )
Either<Error, Integer> composition = this.numEither.map(f.compose(g));
assertEquals(composition, FGMapped);
}
} }

View File

@ -3,6 +3,8 @@ package com.ceticamarco.lambdatonic;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@ -48,4 +50,28 @@ public class RightTests {
x -> x x -> x
), 16); ), 16);
} }
@Test
public void testRightFunctorIdentityMorphism() {
// Applying the map function with the identity function,
// should not change data type structure
Either<Error, Integer> result = this.numEither.map(Function.identity());
assertEquals(this.numEither, result);
}
@Test
public void testRightFunctorCompositionMorphism() {
// map ( f . g ) == map f . map g
Function<Integer, Integer> f = x -> x + 1;
Function<Integer, Integer> g = x -> x * 2;
// Evaluate map x . map g
Either<Error, Integer> gMapped = this.numEither.map(g);
Either<Error, Integer> FGMapped = gMapped.map(f);
// Evaluate map ( f . g )
Either<Error, Integer> composition = this.numEither.map(f.compose(g));
assertEquals(composition, FGMapped);
}
} }