|
5 | 5 | import org.junit.runners.JUnit4;
|
6 | 6 |
|
7 | 7 | import java.math.BigDecimal;
|
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Queue; |
| 11 | +import java.util.concurrent.ConcurrentLinkedQueue; |
8 | 12 | import java.util.function.*;
|
9 | 13 |
|
10 | 14 | import static org.junit.Assert.*;
|
@@ -126,4 +130,69 @@ public void testNMultiplyFunctionSupplier() {
|
126 | 130 |
|
127 | 131 | assertEquals(55, result);
|
128 | 132 | }
|
| 133 | + |
| 134 | + @Test |
| 135 | + public void testRunningThreadSupplier() throws InterruptedException { |
| 136 | + Queue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>(); |
| 137 | + Supplier<Thread> runningThreadSupplier = CrazyLambdas.runningThreadSupplier(() -> concurrentLinkedQueue.add(25)); |
| 138 | + |
| 139 | + // supplier does not create and start a thread before you call get() |
| 140 | + assertEquals(0, concurrentLinkedQueue.size()); |
| 141 | + |
| 142 | + Thread runningThread = runningThreadSupplier.get(); // new thread has been started |
| 143 | + runningThread.join(); |
| 144 | + |
| 145 | + assertEquals(1, concurrentLinkedQueue.size()); |
| 146 | + assertEquals(25, concurrentLinkedQueue.element().intValue()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testNewThreadRunnableConsumer() throws InterruptedException { |
| 151 | + Consumer<Runnable> newThreadRunnableConsumer = CrazyLambdas.newThreadRunnableConsumer(); |
| 152 | + |
| 153 | + Queue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>(); |
| 154 | + newThreadRunnableConsumer.accept(() -> concurrentLinkedQueue.add(50)); |
| 155 | + |
| 156 | + Thread.sleep(500); // don't do that in real code |
| 157 | + |
| 158 | + assertEquals(1, concurrentLinkedQueue.size()); |
| 159 | + assertEquals(50, concurrentLinkedQueue.element().intValue()); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testRunnableToThreadSupplierFunction() throws InterruptedException { |
| 164 | + Function<Runnable, Supplier<Thread>> runnableSupplierFunction = CrazyLambdas.runnableToThreadSupplierFunction(); |
| 165 | + Queue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>(); |
| 166 | + |
| 167 | + Supplier<Thread> threadSupplier = runnableSupplierFunction.apply(() -> concurrentLinkedQueue.add(200)); |
| 168 | + |
| 169 | + assertEquals(0, concurrentLinkedQueue.size()); // supplier does not create and start a thread before you call get() |
| 170 | + |
| 171 | + Thread thread = threadSupplier.get();// new thread has been started |
| 172 | + thread.join(); |
| 173 | + |
| 174 | + assertEquals(1, concurrentLinkedQueue.size()); |
| 175 | + assertEquals(200, concurrentLinkedQueue.element().intValue()); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void testFunctionToConditionalFunction() { |
| 180 | + BiFunction<IntUnaryOperator, IntPredicate, IntUnaryOperator> intFunctionToConditionalIntFunction |
| 181 | + = CrazyLambdas.functionToConditionalFunction(); |
| 182 | + |
| 183 | + IntUnaryOperator abs = intFunctionToConditionalIntFunction.apply(a -> -a, a -> a < 0); |
| 184 | + |
| 185 | + assertEquals(5, abs.applyAsInt(-5)); |
| 186 | + assertEquals(0, abs.applyAsInt(0)); |
| 187 | + assertEquals(5, abs.applyAsInt(5)); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testTrickyWellDoneSupplier() { |
| 192 | + Supplier<Supplier<Supplier<String>>> wellDoneSupplier = CrazyLambdas.trickyWellDoneSupplier(); |
| 193 | + |
| 194 | + String wellDoneStr = wellDoneSupplier.get().get().get(); |
| 195 | + |
| 196 | + assertEquals("WELL DONE!", wellDoneStr); |
| 197 | + } |
129 | 198 | }
|
0 commit comments