From 8b8fda7d0b989174b3f0db1e20cadbcdf837257c Mon Sep 17 00:00:00 2001 From: Paul van Gool Date: Wed, 5 Dec 2018 18:11:21 -0800 Subject: [PATCH] BAEL-1049 * BAEL-1049: Update to latest version, and added tests. * Update FunctionalJavaUnitTest.java * Update FunctionalJavaUnitTest.java * Add blank lines before asserts. --- libraries/pom.xml | 2 +- .../baeldung/fj/FunctionalJavaUnitTest.java | 201 +++++++++++------- 2 files changed, 123 insertions(+), 80 deletions(-) diff --git a/libraries/pom.xml b/libraries/pom.xml index 936e86873d..fb1f1cd1b6 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -942,7 +942,7 @@ 2.0.4 1.3.1 1.2.6 - 4.7 + 4.8.1 1.0.1 3.3.5 2.1 diff --git a/libraries/src/test/java/com/baeldung/fj/FunctionalJavaUnitTest.java b/libraries/src/test/java/com/baeldung/fj/FunctionalJavaUnitTest.java index d62a115abd..aa06ba4eb9 100644 --- a/libraries/src/test/java/com/baeldung/fj/FunctionalJavaUnitTest.java +++ b/libraries/src/test/java/com/baeldung/fj/FunctionalJavaUnitTest.java @@ -1,79 +1,122 @@ -package com.baeldung.fj; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -import fj.F; -import fj.data.Array; -import fj.data.List; -import fj.data.Option; -import fj.function.Characters; -import fj.function.Integers; - -public class FunctionalJavaUnitTest { - - public static final F isEven = i -> i % 2 == 0; - - @Test - public void calculateEvenNumbers_givenIntList_returnTrue() { - List fList = List.list(3, 4, 5, 6); - List evenList = fList.map(isEven); - List evenListTrueResult = List.list(false, true, false, true); - List evenListFalseResult = List.list(true, false, false, true); - assertEquals(evenList.equals(evenListTrueResult), true); - assertEquals(evenList.equals(evenListFalseResult), false); - } - - @Test - public void mapList_givenIntList_returnResult() { - List fList = List.list(3, 4, 5, 6); - fList = fList.map(i -> i + 100); - List resultList = List.list(103, 104, 105, 106); - List falseResultList = List.list(15, 504, 105, 106); - assertEquals(fList.equals(resultList), true); - assertEquals(fList.equals(falseResultList), false); - } - - @Test - public void filterList_givenIntList_returnResult() { - Array array = Array.array(3, 4, 5, 6); - Array filteredArray = array.filter(Integers.even); - Array result = Array.array(4, 6); - Array wrongResult = Array.array(3, 5); - assertEquals(filteredArray.equals(result), true); - assertEquals(filteredArray.equals(wrongResult), false); - } - - @Test - public void checkForLowerCase_givenStringArray_returnResult() { - Array array = Array.array("Welcome", "To", "baeldung"); - Array 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)); - assertEquals(isExist, true); - assertEquals(isExist2, false); - } - - @Test - public void checkOptions_givenOptions_returnResult() { - Option n1 = Option.some(1); - Option n2 = Option.some(2); - - F> f1 = i -> i % 2 == 0 ? Option.some(i + 100) : Option.none(); - - Option result1 = n1.bind(f1); - Option result2 = n2.bind(f1); - - assertEquals(result1, Option.none()); - assertEquals(result2, Option.some(102)); - } - - @Test - public void foldLeft_givenArray_returnResult() { - Array intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27); - int sum = intArray.foldLeft(Integers.add, 0); - assertEquals(sum, 260); - } - -} +package com.baeldung.fj; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import fj.F; +import fj.Show; +import fj.data.Array; +import fj.data.List; +import fj.data.Option; +import fj.function.Characters; +import fj.function.Integers; + +public class FunctionalJavaUnitTest { + + public static final F isEven = i -> i % 2 == 0; + + public static final Integer timesTwoRegular(Integer i) { + return i * 2; + } + + public static final F timesTwo = i -> i * 2; + + public static final F plusOne = i -> i + 1; + + @Test + public void multiplyNumbers_givenIntList_returnTrue() { + List fList = List.list(1, 2, 3, 4); + List fList1 = fList.map(timesTwo); + List fList2 = fList.map(i -> i * 2); + + assertEquals(fList1.equals(fList2), true); + } + + @Test + public void applyMultipleFunctions_givenIntList_returnFalse() { + List fList = List.list(1, 2, 3, 4); + List fList1 = fList.map(timesTwo).map(plusOne); + Show.listShow(Show.intShow).println(fList1); + List fList2 = fList.map(plusOne).map(timesTwo); + Show.listShow(Show.intShow).println(fList2); + + assertEquals(fList1.equals(fList2), false); + } + + @Test + public void calculateEvenNumbers_givenIntList_returnTrue() { + List fList = List.list(3, 4, 5, 6); + List evenList = fList.map(isEven); + List evenListTrueResult = List.list(false, true, false, true); + List evenListFalseResult = List.list(true, false, false, true); + + assertEquals(evenList.equals(evenListTrueResult), true); + assertEquals(evenList.equals(evenListFalseResult), false); + } + + @Test + public void mapList_givenIntList_returnResult() { + List fList = List.list(3, 4, 5, 6); + fList = fList.map(i -> i + 100); + List resultList = List.list(103, 104, 105, 106); + List falseResultList = List.list(15, 504, 105, 106); + + assertEquals(fList.equals(resultList), true); + assertEquals(fList.equals(falseResultList), false); + } + + @Test + public void filterList_givenIntList_returnResult() { + Array array = Array.array(3, 4, 5, 6); + Array filteredArray = array.filter(isEven); + Array result = Array.array(4, 6); + Array wrongResult = Array.array(3, 5); + + assertEquals(filteredArray.equals(result), true); + assertEquals(filteredArray.equals(wrongResult), false); + } + + @Test + public void checkForLowerCase_givenStringArray_returnResult() { + Array array = Array.array("Welcome", "To", "baeldung"); + Array 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 n1 = Option.some(1); + Option n2 = Option.some(2); + Option n3 = Option.none(); + + F> function = i -> i % 2 == 0 ? Option.some(i + 100) : Option.none(); + + Option result1 = n1.bind(function); + Option result2 = n2.bind(function); + Option result3 = n3.bind(function); + + assertEquals(result1, Option.none()); + assertEquals(result2, Option.some(102)); + assertEquals(result3, Option.none()); + } + + @Test + public void foldLeft_givenArray_returnResult() { + Array 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); + } + +}