|
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 FunctionalJavaUnitTest { |
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 | | -} |
| 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.Show; |
| 9 | +import fj.data.Array; |
| 10 | +import fj.data.List; |
| 11 | +import fj.data.Option; |
| 12 | +import fj.function.Characters; |
| 13 | +import fj.function.Integers; |
| 14 | + |
| 15 | +public class FunctionalJavaUnitTest { |
| 16 | + |
| 17 | + public static final F<Integer, Boolean> isEven = i -> i % 2 == 0; |
| 18 | + |
| 19 | + public static final Integer timesTwoRegular(Integer i) { |
| 20 | + return i * 2; |
| 21 | + } |
| 22 | + |
| 23 | + public static final F<Integer, Integer> timesTwo = i -> i * 2; |
| 24 | + |
| 25 | + public static final F<Integer, Integer> plusOne = i -> i + 1; |
| 26 | + |
| 27 | + @Test |
| 28 | + public void multiplyNumbers_givenIntList_returnTrue() { |
| 29 | + List<Integer> fList = List.list(1, 2, 3, 4); |
| 30 | + List<Integer> fList1 = fList.map(timesTwo); |
| 31 | + List<Integer> fList2 = fList.map(i -> i * 2); |
| 32 | + |
| 33 | + assertEquals(fList1.equals(fList2), true); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void applyMultipleFunctions_givenIntList_returnFalse() { |
| 38 | + List<Integer> fList = List.list(1, 2, 3, 4); |
| 39 | + List<Integer> fList1 = fList.map(timesTwo).map(plusOne); |
| 40 | + Show.listShow(Show.intShow).println(fList1); |
| 41 | + List<Integer> fList2 = fList.map(plusOne).map(timesTwo); |
| 42 | + Show.listShow(Show.intShow).println(fList2); |
| 43 | + |
| 44 | + assertEquals(fList1.equals(fList2), false); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void calculateEvenNumbers_givenIntList_returnTrue() { |
| 49 | + List<Integer> fList = List.list(3, 4, 5, 6); |
| 50 | + List<Boolean> evenList = fList.map(isEven); |
| 51 | + List<Boolean> evenListTrueResult = List.list(false, true, false, true); |
| 52 | + List<Boolean> evenListFalseResult = List.list(true, false, false, true); |
| 53 | + |
| 54 | + assertEquals(evenList.equals(evenListTrueResult), true); |
| 55 | + assertEquals(evenList.equals(evenListFalseResult), false); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void mapList_givenIntList_returnResult() { |
| 60 | + List<Integer> fList = List.list(3, 4, 5, 6); |
| 61 | + fList = fList.map(i -> i + 100); |
| 62 | + List<Integer> resultList = List.list(103, 104, 105, 106); |
| 63 | + List<Integer> falseResultList = List.list(15, 504, 105, 106); |
| 64 | + |
| 65 | + assertEquals(fList.equals(resultList), true); |
| 66 | + assertEquals(fList.equals(falseResultList), false); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void filterList_givenIntList_returnResult() { |
| 71 | + Array<Integer> array = Array.array(3, 4, 5, 6); |
| 72 | + Array<Integer> filteredArray = array.filter(isEven); |
| 73 | + Array<Integer> result = Array.array(4, 6); |
| 74 | + Array<Integer> wrongResult = Array.array(3, 5); |
| 75 | + |
| 76 | + assertEquals(filteredArray.equals(result), true); |
| 77 | + assertEquals(filteredArray.equals(wrongResult), false); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void checkForLowerCase_givenStringArray_returnResult() { |
| 82 | + Array<String> array = Array.array("Welcome", "To", "baeldung"); |
| 83 | + Array<String> array2 = Array.array("Welcome", "To", "Baeldung"); |
| 84 | + Boolean isExist = array.exists(s -> List.fromString(s).forall(Characters.isLowerCase)); |
| 85 | + Boolean isExist2 = array2.exists(s -> List.fromString(s).forall(Characters.isLowerCase)); |
| 86 | + Boolean isAll = array.forall(s -> List.fromString(s).forall(Characters.isLowerCase)); |
| 87 | + |
| 88 | + assertEquals(isExist, true); |
| 89 | + assertEquals(isExist2, false); |
| 90 | + assertEquals(isAll, false); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void checkOptions_givenOptions_returnResult() { |
| 95 | + Option<Integer> n1 = Option.some(1); |
| 96 | + Option<Integer> n2 = Option.some(2); |
| 97 | + Option<Integer> n3 = Option.none(); |
| 98 | + |
| 99 | + F<Integer, Option<Integer>> function = i -> i % 2 == 0 ? Option.some(i + 100) : Option.none(); |
| 100 | + |
| 101 | + Option<Integer> result1 = n1.bind(function); |
| 102 | + Option<Integer> result2 = n2.bind(function); |
| 103 | + Option<Integer> result3 = n3.bind(function); |
| 104 | + |
| 105 | + assertEquals(result1, Option.none()); |
| 106 | + assertEquals(result2, Option.some(102)); |
| 107 | + assertEquals(result3, Option.none()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void foldLeft_givenArray_returnResult() { |
| 112 | + Array<Integer> intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27); |
| 113 | + int sumAll = intArray.foldLeft(Integers.add, 0); |
| 114 | + |
| 115 | + assertEquals(sumAll, 260); |
| 116 | + |
| 117 | + int sumEven = intArray.filter(isEven).foldLeft(Integers.add, 0); |
| 118 | + |
| 119 | + assertEquals(sumEven, 148); |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments