|
6 | 6 |
|
7 | 7 | import java.math.BigDecimal;
|
8 | 8 | import java.util.ArrayList;
|
| 9 | +import java.util.HashMap; |
9 | 10 | import java.util.List;
|
| 11 | +import java.util.Map; |
10 | 12 | import java.util.Queue;
|
11 | 13 | import java.util.concurrent.ConcurrentLinkedQueue;
|
12 | 14 | import java.util.function.*;
|
@@ -35,6 +37,17 @@ public void testIsEmptyPredicate() {
|
35 | 37 | assertTrue(emptyStringResult);
|
36 | 38 | }
|
37 | 39 |
|
| 40 | + @Test |
| 41 | + public void testStringMultiplier() { |
| 42 | + BiFunction<String, Integer, String> stringMultiplier = CrazyLambdas.stringMultiplier(); |
| 43 | + |
| 44 | + String threeTimesHi = stringMultiplier.apply("Hi", 3); |
| 45 | + String twoTimesHello = stringMultiplier.apply("Hello", 2); |
| 46 | + |
| 47 | + assertEquals("HiHiHi", threeTimesHi); |
| 48 | + assertEquals("HelloHello", twoTimesHello); |
| 49 | + } |
| 50 | + |
38 | 51 | @Test
|
39 | 52 | public void testToDollarStringFunction() {
|
40 | 53 | Function<BigDecimal, String> toDollarStringFunction = CrazyLambdas.toDollarStringFunction();
|
@@ -131,6 +144,19 @@ public void testNMultiplyFunctionSupplier() {
|
131 | 144 | assertEquals(55, result);
|
132 | 145 | }
|
133 | 146 |
|
| 147 | + @Test |
| 148 | + public void testComposeWithTrimFunction() { |
| 149 | + UnaryOperator<Function<String, String>> composeWithTrimFunction = CrazyLambdas.composeWithTrimFunction(); |
| 150 | + Function<String, String> toLowerWithTrim = composeWithTrimFunction.apply(String::toLowerCase); |
| 151 | + Function<String, String> threeTimesRepeatWithTrim = composeWithTrimFunction.apply(s -> s.repeat(3)); |
| 152 | + |
| 153 | + String hey = toLowerWithTrim.apply(" Hey "); |
| 154 | + String threeTimesHi = threeTimesRepeatWithTrim.apply(" Hi "); |
| 155 | + |
| 156 | + assertEquals("hey", hey); |
| 157 | + assertEquals("HiHiHi", threeTimesHi); |
| 158 | + } |
| 159 | + |
134 | 160 | @Test
|
135 | 161 | public void testRunningThreadSupplier() throws InterruptedException {
|
136 | 162 | Queue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
|
@@ -187,6 +213,23 @@ public void testFunctionToConditionalFunction() {
|
187 | 213 | assertEquals(5, abs.applyAsInt(5));
|
188 | 214 | }
|
189 | 215 |
|
| 216 | + @Test |
| 217 | + public void testFunctionLoader() { |
| 218 | + BiFunction<Map<String, IntUnaryOperator>, String, IntUnaryOperator> functionLoader = CrazyLambdas.functionLoader(); |
| 219 | + Map<String, IntUnaryOperator> functionMap = new HashMap<>(); |
| 220 | + functionMap.put("increment", x -> x + 1); |
| 221 | + functionMap.put("square", x -> x * x); |
| 222 | + |
| 223 | + IntUnaryOperator incrementFunction = functionLoader.apply(functionMap, "increment"); |
| 224 | + IntUnaryOperator squareFunction = functionLoader.apply(functionMap, "square"); |
| 225 | + IntUnaryOperator identityFunction = functionLoader.apply(functionMap, "none"); |
| 226 | + |
| 227 | + assertEquals(5, incrementFunction.applyAsInt(4)); |
| 228 | + assertEquals(9, squareFunction.applyAsInt(3)); |
| 229 | + assertEquals(10, identityFunction.applyAsInt(10)); |
| 230 | + } |
| 231 | + |
| 232 | + |
190 | 233 | @Test
|
191 | 234 | public void testTrickyWellDoneSupplier() {
|
192 | 235 | Supplier<Supplier<Supplier<String>>> wellDoneSupplier = CrazyLambdas.trickyWellDoneSupplier();
|
|
0 commit comments