Added unit tests for functor laws
This commit is contained in:
parent
7ebc811d0e
commit
b2084b6df9
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user