|
| 1 | +package com.baeldung.fj; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import fj.F; |
| 8 | +import fj.data.Array; |
| 9 | +import fj.data.List; |
| 10 | +import fj.data.Option; |
| 11 | +import fj.function.Characters; |
| 12 | +import fj.function.Integers; |
| 13 | + |
| 14 | +public class FunctionalJavaTest { |
| 15 | + |
| 16 | + public static final F<Integer, Boolean> isEven = i -> i % 2 == 0; |
| 17 | + |
| 18 | + @Test |
| 19 | + public void calculateEvenNumbers_givenIntList_returnTrue() { |
| 20 | + List<Integer> fList = List.list(3, 4, 5, 6); |
| 21 | + List<Boolean> evenList = fList.map(isEven); |
| 22 | + List<Boolean> evenListTrueResult = List.list(false, true, false, true); |
| 23 | + List<Boolean> evenListFalseResult = List.list(true, false, false, true); |
| 24 | + assertEquals(evenList.equals(evenListTrueResult), true); |
| 25 | + assertEquals(evenList.equals(evenListFalseResult), false); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void mapList_givenIntList_returnResult() { |
| 30 | + List<Integer> fList = List.list(3, 4, 5, 6); |
| 31 | + fList = fList.map(i -> i + 100); |
| 32 | + List<Integer> resultList = List.list(103, 104, 105, 106); |
| 33 | + List<Integer> falseResultList = List.list(15, 504, 105, 106); |
| 34 | + assertEquals(fList.equals(resultList), true); |
| 35 | + assertEquals(fList.equals(falseResultList), false); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void filterList_givenIntList_returnResult() { |
| 40 | + Array<Integer> array = Array.array(3, 4, 5, 6); |
| 41 | + Array<Integer> filteredArray = array.filter(Integers.even); |
| 42 | + Array<Integer> result = Array.array(4, 6); |
| 43 | + Array<Integer> wrongResult = Array.array(3, 5); |
| 44 | + assertEquals(filteredArray.equals(result), true); |
| 45 | + assertEquals(filteredArray.equals(wrongResult), false); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void checkForLowerCase_givenStringArray_returnResult() { |
| 50 | + Array<String> array = Array.array("Welcome", "To", "baeldung"); |
| 51 | + Array<String> array2 = Array.array("Welcome", "To", "Baeldung"); |
| 52 | + Boolean isExist = array.exists(s -> List.fromString(s).forall(Characters.isLowerCase)); |
| 53 | + Boolean isExist2 = array2.exists(s -> List.fromString(s).forall(Characters.isLowerCase)); |
| 54 | + assertEquals(isExist, true); |
| 55 | + assertEquals(isExist2, false); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void checkOptions_givenOptions_returnResult() { |
| 60 | + Option<Integer> n1 = Option.some(1); |
| 61 | + Option<Integer> n2 = Option.some(2); |
| 62 | + |
| 63 | + F<Integer, Option<Integer>> f1 = i -> i % 2 == 0 ? Option.some(i + 100) : Option.none(); |
| 64 | + |
| 65 | + Option<Integer> result1 = n1.bind(f1); |
| 66 | + Option<Integer> result2 = n2.bind(f1); |
| 67 | + |
| 68 | + assertEquals(result1, Option.none()); |
| 69 | + assertEquals(result2, Option.some(102)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void foldLeft_givenArray_returnResult() { |
| 74 | + Array<Integer> intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27); |
| 75 | + int sum = intArray.foldLeft(Integers.add, 0); |
| 76 | + assertEquals(sum, 260); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments