fix #68
[memoization.java.git] / memoization-guava / src / test / java / de / xn__ho_hia / memoization / guava / GuavaMemoizeCustomKeyTest.java
blob0ed56d1843f803ebfd566d7294b99060b0746e2b
1 /*
2 * This file is part of memoization.java. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of memoization.java,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
5 * in the LICENSE file.
6 */
7 package de.xn__ho_hia.memoization.guava;
9 import java.util.function.BiConsumer;
10 import java.util.function.BiFunction;
11 import java.util.function.BiPredicate;
12 import java.util.function.BooleanSupplier;
13 import java.util.function.Consumer;
14 import java.util.function.DoubleBinaryOperator;
15 import java.util.function.DoubleConsumer;
16 import java.util.function.DoubleFunction;
17 import java.util.function.DoublePredicate;
18 import java.util.function.DoubleSupplier;
19 import java.util.function.DoubleToIntFunction;
20 import java.util.function.DoubleToLongFunction;
21 import java.util.function.DoubleUnaryOperator;
22 import java.util.function.Function;
23 import java.util.function.IntBinaryOperator;
24 import java.util.function.IntConsumer;
25 import java.util.function.IntFunction;
26 import java.util.function.IntPredicate;
27 import java.util.function.IntSupplier;
28 import java.util.function.IntToDoubleFunction;
29 import java.util.function.LongBinaryOperator;
30 import java.util.function.LongConsumer;
31 import java.util.function.LongFunction;
32 import java.util.function.LongPredicate;
33 import java.util.function.LongSupplier;
34 import java.util.function.Predicate;
35 import java.util.function.Supplier;
37 import org.junit.Assert;
38 import org.junit.Test;
40 import de.xn__ho_hia.memoization.shared.DoubleBinaryFunction;
41 import de.xn__ho_hia.memoization.shared.IntBinaryFunction;
42 import de.xn__ho_hia.memoization.shared.LongBinaryFunction;
43 import de.xn__ho_hia.quality.suppression.CompilerWarnings;
45 /**
49 @SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.STATIC_METHOD })
50 public class GuavaMemoizeCustomKeyTest {
52 /**
55 @Test
56 public void shouldMemoizeBiConsumerWithKeyBiFunction() {
57 // given
58 final BiConsumer<String, String> biConsumer = (a, b) -> System.out.println(a + b);
59 final BiFunction<String, String, String> keyFunction = (a, b) -> "key";
61 // when
62 final BiConsumer<String, String> memoize = GuavaMemoize.biConsumer(biConsumer, keyFunction);
64 // then
65 Assert.assertNotNull("Memoized BiConsumer is NULL", memoize);
68 /**
71 @Test
72 public void shouldMemoizeBiFunctionWithKeyBiFunction() {
73 // given
74 final BiFunction<String, String, String> biFunction = (a, b) -> "test";
75 final BiFunction<String, String, String> keyFunction = (a, b) -> "key";
77 // when
78 final BiFunction<String, String, String> memoize = GuavaMemoize.biFunction(biFunction, keyFunction);
80 // then
81 Assert.assertNotNull("Memoized BiFunction is NULL", memoize);
84 /**
87 @Test
88 public void shouldMemoizeBiPredicateWithKeyBiFunction() {
89 // given
90 final BiPredicate<String, String> biPredicate = (a, b) -> true;
91 final BiFunction<String, String, String> keyFunction = (a, b) -> "key";
93 // when
94 final BiPredicate<String, String> memoize = GuavaMemoize.biPredicate(biPredicate, keyFunction);
96 // then
97 Assert.assertNotNull("Memoized BiPredicate is NULL", memoize);
103 @Test
104 public void shouldMemoizeBooleanSupplierWithKeySupplier() {
105 // given
106 final BooleanSupplier supplier = () -> true;
107 final Supplier<String> keySupplier = () -> "key";
109 // when
110 final BooleanSupplier memoize = GuavaMemoize.booleanSupplier(supplier, keySupplier);
112 // then
113 Assert.assertNotNull("Memoized BooleanSupplier is NULL", memoize);
119 @Test
120 public void shouldMemoizeConsumerWithKeyFunction() {
121 // given
122 final Consumer<String> consumer = System.out::println;
123 final Function<String, String> keyFunction = a -> "key";
125 // when
126 final Consumer<String> memoize = GuavaMemoize.consumer(consumer, keyFunction);
128 // then
129 Assert.assertNotNull("Memoized Consumer is NULL", memoize);
135 @Test
136 public void shouldMemoizeDoubleBinaryOperatorWithKeyFunction() {
137 // given
138 final DoubleBinaryOperator operator = (a, b) -> 123.456D;
139 final DoubleBinaryFunction<String> keyFunction = (a, b) -> "key";
141 // when
142 final DoubleBinaryOperator memoize = GuavaMemoize.doubleBinaryOperator(operator, keyFunction);
144 // then
145 Assert.assertNotNull("Memoized DoubleBinaryOperator is NULL", memoize);
151 @Test
152 public void shouldMemoizeDoubleConsumerWithKeyFunction() {
153 // given
154 final DoubleConsumer consumer = System.out::println;
155 final DoubleFunction<String> keyFunction = a -> "key";
157 // when
158 final DoubleConsumer memoize = GuavaMemoize.doubleConsumer(consumer, keyFunction);
160 // then
161 Assert.assertNotNull("Memoized DoubleConsumer is NULL", memoize);
167 @Test
168 public void shouldMemoizeDoubleFunctionWithKeyFunction() {
169 // given
170 final DoubleFunction<String> function = a -> "test";
171 final DoubleFunction<String> keyFunction = a -> "key";
173 // when
174 final DoubleFunction<String> memoize = GuavaMemoize.doubleFunction(function, keyFunction);
176 // then
177 Assert.assertNotNull("Memoized DoubleFunction is NULL", memoize);
183 @Test
184 public void shouldMemoizeDoublePredicateWithKeyFunction() {
185 // given
186 final DoublePredicate predicate = a -> true;
187 final DoubleFunction<String> keyFunction = a -> "key";
189 // when
190 final DoublePredicate memoize = GuavaMemoize.doublePredicate(predicate, keyFunction);
192 // then
193 Assert.assertNotNull("Memoized DoublePredicate is NULL", memoize);
199 @Test
200 public void shouldMemoizeDoubleSupplierWithKeySupplier() {
201 // given
202 final DoubleSupplier supplier = () -> 123.456D;
203 final Supplier<String> keySupplier = () -> "key";
205 // when
206 final DoubleSupplier memoize = GuavaMemoize.doubleSupplier(supplier, keySupplier);
208 // then
209 Assert.assertNotNull("Memoized DoubleSupplier is NULL", memoize);
215 @Test
216 public void shouldMemoizeDoubleToIntFunctionWithKeyFunction() {
217 // given
218 final DoubleToIntFunction function = a -> 123;
219 final DoubleFunction<Double> keyFunction = Double::valueOf;
221 // when
222 final DoubleToIntFunction memoize = GuavaMemoize.doubleToIntFunction(function, keyFunction);
224 // then
225 Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize);
231 @Test
232 public void shouldMemoizeDoubleToLongFunctionWithKeyFunction() {
233 // given
234 final DoubleToLongFunction function = a -> 123;
235 final DoubleFunction<Double> keyFunction = Double::valueOf;
237 // when
238 final DoubleToLongFunction memoize = GuavaMemoize.doubleToLongFunction(function, keyFunction);
240 // then
241 Assert.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize);
247 @Test
248 public void shouldMemoizeDoubleUnaryOperatorWithKeyFunction() {
249 // given
250 final DoubleUnaryOperator function = a -> 123.456D;
251 final DoubleFunction<Double> keyFunction = Double::valueOf;
253 // when
254 final DoubleUnaryOperator memoize = GuavaMemoize.doubleUnaryOperator(function, keyFunction);
256 // then
257 Assert.assertNotNull("Memoized DoubleUnaryOperator is NULL", memoize);
263 @Test
264 public void shouldMemoizeFunctionWithKeyFunction() {
265 // given
266 final Function<String, String> function = a -> "test";
267 final Function<String, String> keyFunction = a -> "key";
269 // when
270 final Function<String, String> memoize = GuavaMemoize.function(function, keyFunction);
272 // then
273 Assert.assertNotNull("Memoized Function is NULL", memoize);
279 @Test
280 public void shouldMemoizeIntBinaryOperatorWithKeyFunction() {
281 // given
282 final IntBinaryOperator function = (a, b) -> 123;
283 final IntBinaryFunction<String> keyFunction = (a, b) -> "key";
285 // when
286 final IntBinaryOperator memoize = GuavaMemoize.intBinaryOperator(function, keyFunction);
288 // then
289 Assert.assertNotNull("Memoized IntBinaryOperator is NULL", memoize);
295 @Test
296 public void shouldMemoizeIntConsumerWithKeyFunction() {
297 // given
298 final IntConsumer consumer = System.out::println;
299 final IntFunction<String> keyFunction = a -> "key";
301 // when
302 final IntConsumer memoize = GuavaMemoize.intConsumer(consumer, keyFunction);
304 // then
305 Assert.assertNotNull("Memoized IntConsumer is NULL", memoize);
311 @Test
312 public void shouldMemoizeIntFunctionWithKeyFunction() {
313 // given
314 final IntFunction<String> function = a -> "test";
315 final IntFunction<String> keyFunction = a -> "key";
317 // when
318 final IntFunction<String> memoize = GuavaMemoize.intFunction(function, keyFunction);
320 // then
321 Assert.assertNotNull("Memoized IntFunction is NULL", memoize);
327 @Test
328 public void shouldMemoizeIntPredicateWithKeyFunction() {
329 // given
330 final IntPredicate predicate = a -> true;
331 final IntFunction<String> keyFunction = a -> "key";
333 // when
334 final IntPredicate memoize = GuavaMemoize.intPredicate(predicate, keyFunction);
336 // then
337 Assert.assertNotNull("Memoized IntPredicate is NULL", memoize);
343 @Test
344 public void shouldMemoizeIntSupplierWithKeySupplier() {
345 // given
346 final IntSupplier supplier = () -> 123;
347 final Supplier<String> keySupplier = () -> "key";
349 // when
350 final IntSupplier memoize = GuavaMemoize.intSupplier(supplier, keySupplier);
352 // then
353 Assert.assertNotNull("Memoized IntSupplier is NULL", memoize);
359 @Test
360 public void shouldMemoizeIntToDoubleFunctionWithKeyFunction() {
361 // given
362 final IntToDoubleFunction function = a -> 123D;
363 final IntFunction<Integer> keyFunction = Integer::valueOf;
365 // when
366 final IntToDoubleFunction memoize = GuavaMemoize.intToDoubleFunction(function, keyFunction);
368 // then
369 Assert.assertNotNull("Memoized IntToDoubleFunction is NULL", memoize);
375 @Test
376 public void shouldMemoizeLongBinaryOperatorWithKeyFunction() {
377 // given
378 final LongBinaryOperator operator = (a, b) -> 123;
379 final LongBinaryFunction<String> keyFunction = (a, b) -> "key";
381 // when
382 final LongBinaryOperator memoize = GuavaMemoize.longBinaryOperator(operator, keyFunction);
384 // then
385 Assert.assertNotNull("Memoized LongBinaryOperator is NULL", memoize);
391 @Test
392 public void shouldMemoizeLongConsumerWithKeyFunction() {
393 // given
394 final LongConsumer consumer = System.out::println;
395 final LongFunction<String> keyFunction = a -> "key";
397 // when
398 final LongConsumer memoize = GuavaMemoize.longConsumer(consumer, keyFunction);
400 // then
401 Assert.assertNotNull("Memoized LongConsumer is NULL", memoize);
407 @Test
408 public void shouldMemoizeLongFunctionWithKeyFunction() {
409 // given
410 final LongFunction<String> function = a -> "test";
411 final LongFunction<String> keyFunction = a -> "key";
413 // when
414 final LongFunction<String> memoize = GuavaMemoize.longFunction(function, keyFunction);
416 // then
417 Assert.assertNotNull("Memoized LongFunction is NULL", memoize);
423 @Test
424 public void shouldMemoizeLongPredicateWithLambda() {
425 // given
426 final LongPredicate predicate = a -> true;
427 final LongFunction<String> keyFunction = a -> "key";
429 // when
430 final LongPredicate memoize = GuavaMemoize.longPredicate(predicate, keyFunction);
432 // then
433 Assert.assertNotNull("Memoized LongPredicate is NULL", memoize);
439 @Test
440 public void shouldMemoizeLongSupplierWithKeySupplier() {
441 // given
442 final LongSupplier supplier = () -> 123;
443 final Supplier<String> keySupplier = () -> "key";
445 // when
446 final LongSupplier memoize = GuavaMemoize.longSupplier(supplier, keySupplier);
448 // then
449 Assert.assertNotNull("Memoized LongSupplier is NULL", memoize);
455 @Test
456 public void shouldMemoizePredicateWithKeyFunction() {
457 // given
458 final Predicate<String> predicate = a -> true;
459 final Function<String, String> keyFunction = a -> "key";
461 // when
462 final Predicate<String> memoize = GuavaMemoize.predicate(predicate, keyFunction);
464 // then
465 Assert.assertNotNull("Memoized Predicate is NULL", memoize);
471 @Test
472 public void shouldMemoizeSupplierWithKeySupplier() {
473 // given
474 final Supplier<String> supplier = () -> "test";
475 final Supplier<String> keySupplier = () -> "key";
477 // when
478 final Supplier<String> memoize = GuavaMemoize.supplier(supplier, keySupplier);
480 // then
481 Assert.assertNotNull("Memoized Supplier is NULL", memoize);