BAEL-1049

* BAEL-1049: Update to latest version, and added tests.

* Update FunctionalJavaUnitTest.java

* Update FunctionalJavaUnitTest.java

* Add blank lines before asserts.
This commit is contained in:
Paul van Gool 2018-12-05 18:11:21 -08:00 committed by Emily Cheyne
parent df7b723cba
commit 8b8fda7d0b
2 changed files with 123 additions and 80 deletions

View File

@ -942,7 +942,7 @@
<yarg.version>2.0.4</yarg.version> <yarg.version>2.0.4</yarg.version>
<mbassador.version>1.3.1</mbassador.version> <mbassador.version>1.3.1</mbassador.version>
<jdeferred.version>1.2.6</jdeferred.version> <jdeferred.version>1.2.6</jdeferred.version>
<functionaljava.version>4.7</functionaljava.version> <functionaljava.version>4.8.1</functionaljava.version>
<jgrapht.version>1.0.1</jgrapht.version> <jgrapht.version>1.0.1</jgrapht.version>
<docx4j.version>3.3.5</docx4j.version> <docx4j.version>3.3.5</docx4j.version>
<jaxb-api.version>2.1</jaxb-api.version> <jaxb-api.version>2.1</jaxb-api.version>

View File

@ -1,79 +1,122 @@
package com.baeldung.fj; package com.baeldung.fj;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.junit.Test; import org.junit.Test;
import fj.F; import fj.F;
import fj.data.Array; import fj.Show;
import fj.data.List; import fj.data.Array;
import fj.data.Option; import fj.data.List;
import fj.function.Characters; import fj.data.Option;
import fj.function.Integers; import fj.function.Characters;
import fj.function.Integers;
public class FunctionalJavaUnitTest {
public class FunctionalJavaUnitTest {
public static final F<Integer, Boolean> isEven = i -> i % 2 == 0;
public static final F<Integer, Boolean> isEven = i -> i % 2 == 0;
@Test
public void calculateEvenNumbers_givenIntList_returnTrue() { public static final Integer timesTwoRegular(Integer i) {
List<Integer> fList = List.list(3, 4, 5, 6); return i * 2;
List<Boolean> evenList = fList.map(isEven); }
List<Boolean> evenListTrueResult = List.list(false, true, false, true);
List<Boolean> evenListFalseResult = List.list(true, false, false, true); public static final F<Integer, Integer> timesTwo = i -> i * 2;
assertEquals(evenList.equals(evenListTrueResult), true);
assertEquals(evenList.equals(evenListFalseResult), false); public static final F<Integer, Integer> plusOne = i -> i + 1;
}
@Test
@Test public void multiplyNumbers_givenIntList_returnTrue() {
public void mapList_givenIntList_returnResult() { List<Integer> fList = List.list(1, 2, 3, 4);
List<Integer> fList = List.list(3, 4, 5, 6); List<Integer> fList1 = fList.map(timesTwo);
fList = fList.map(i -> i + 100); List<Integer> fList2 = fList.map(i -> i * 2);
List<Integer> resultList = List.list(103, 104, 105, 106);
List<Integer> falseResultList = List.list(15, 504, 105, 106); assertEquals(fList1.equals(fList2), true);
assertEquals(fList.equals(resultList), true); }
assertEquals(fList.equals(falseResultList), false);
} @Test
public void applyMultipleFunctions_givenIntList_returnFalse() {
@Test List<Integer> fList = List.list(1, 2, 3, 4);
public void filterList_givenIntList_returnResult() { List<Integer> fList1 = fList.map(timesTwo).map(plusOne);
Array<Integer> array = Array.array(3, 4, 5, 6); Show.listShow(Show.intShow).println(fList1);
Array<Integer> filteredArray = array.filter(Integers.even); List<Integer> fList2 = fList.map(plusOne).map(timesTwo);
Array<Integer> result = Array.array(4, 6); Show.listShow(Show.intShow).println(fList2);
Array<Integer> wrongResult = Array.array(3, 5);
assertEquals(filteredArray.equals(result), true); assertEquals(fList1.equals(fList2), false);
assertEquals(filteredArray.equals(wrongResult), false); }
}
@Test
@Test public void calculateEvenNumbers_givenIntList_returnTrue() {
public void checkForLowerCase_givenStringArray_returnResult() { List<Integer> fList = List.list(3, 4, 5, 6);
Array<String> array = Array.array("Welcome", "To", "baeldung"); List<Boolean> evenList = fList.map(isEven);
Array<String> array2 = Array.array("Welcome", "To", "Baeldung"); List<Boolean> evenListTrueResult = List.list(false, true, false, true);
Boolean isExist = array.exists(s -> List.fromString(s).forall(Characters.isLowerCase)); List<Boolean> evenListFalseResult = List.list(true, false, false, true);
Boolean isExist2 = array2.exists(s -> List.fromString(s).forall(Characters.isLowerCase));
assertEquals(isExist, true); assertEquals(evenList.equals(evenListTrueResult), true);
assertEquals(isExist2, false); assertEquals(evenList.equals(evenListFalseResult), false);
} }
@Test @Test
public void checkOptions_givenOptions_returnResult() { public void mapList_givenIntList_returnResult() {
Option<Integer> n1 = Option.some(1); List<Integer> fList = List.list(3, 4, 5, 6);
Option<Integer> n2 = Option.some(2); fList = fList.map(i -> i + 100);
List<Integer> resultList = List.list(103, 104, 105, 106);
F<Integer, Option<Integer>> f1 = i -> i % 2 == 0 ? Option.some(i + 100) : Option.none(); List<Integer> falseResultList = List.list(15, 504, 105, 106);
Option<Integer> result1 = n1.bind(f1); assertEquals(fList.equals(resultList), true);
Option<Integer> result2 = n2.bind(f1); assertEquals(fList.equals(falseResultList), false);
}
assertEquals(result1, Option.none());
assertEquals(result2, Option.some(102)); @Test
} public void filterList_givenIntList_returnResult() {
Array<Integer> array = Array.array(3, 4, 5, 6);
@Test Array<Integer> filteredArray = array.filter(isEven);
public void foldLeft_givenArray_returnResult() { Array<Integer> result = Array.array(4, 6);
Array<Integer> intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27); Array<Integer> wrongResult = Array.array(3, 5);
int sum = intArray.foldLeft(Integers.add, 0);
assertEquals(sum, 260); assertEquals(filteredArray.equals(result), true);
} assertEquals(filteredArray.equals(wrongResult), false);
}
}
@Test
public void checkForLowerCase_givenStringArray_returnResult() {
Array<String> array = Array.array("Welcome", "To", "baeldung");
Array<String> array2 = Array.array("Welcome", "To", "Baeldung");
Boolean isExist = array.exists(s -> List.fromString(s).forall(Characters.isLowerCase));
Boolean isExist2 = array2.exists(s -> List.fromString(s).forall(Characters.isLowerCase));
Boolean isAll = array.forall(s -> List.fromString(s).forall(Characters.isLowerCase));
assertEquals(isExist, true);
assertEquals(isExist2, false);
assertEquals(isAll, false);
}
@Test
public void checkOptions_givenOptions_returnResult() {
Option<Integer> n1 = Option.some(1);
Option<Integer> n2 = Option.some(2);
Option<Integer> n3 = Option.none();
F<Integer, Option<Integer>> function = i -> i % 2 == 0 ? Option.some(i + 100) : Option.none();
Option<Integer> result1 = n1.bind(function);
Option<Integer> result2 = n2.bind(function);
Option<Integer> result3 = n3.bind(function);
assertEquals(result1, Option.none());
assertEquals(result2, Option.some(102));
assertEquals(result3, Option.none());
}
@Test
public void foldLeft_givenArray_returnResult() {
Array<Integer> intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27);
int sumAll = intArray.foldLeft(Integers.add, 0);
assertEquals(sumAll, 260);
int sumEven = intArray.filter(isEven).foldLeft(Integers.add, 0);
assertEquals(sumEven, 148);
}
}