reformat & fix javadocs
[memoization.java.git] / memoization-map / src / test / java / wtf / metio / memoization / map / ConcurrentMapBasedIntBinaryOperatorMemoizerTest.java
blobca223b797bfe7332b135ce0b0051abf809dc53ba
1 /*
2 * SPDX-FileCopyrightText: The memoization.java Authors
3 * SPDX-License-Identifier: 0BSD
4 */
5 package wtf.metio.memoization.map;
7 import org.junit.jupiter.api.Assertions;
8 import org.junit.jupiter.api.Test;
9 import org.mockito.Mockito;
10 import wtf.metio.memoization.core.IntBinaryFunction;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14 import java.util.function.IntBinaryOperator;
16 class ConcurrentMapBasedIntBinaryOperatorMemoizerTest {
18 @Test
19 void shouldAcceptCacheAndKeyFunctionAndOperator() {
20 // given
21 final ConcurrentMap<String, Integer> cache = new ConcurrentHashMap<>();
22 final IntBinaryFunction<String> keyFunction = (first, second) -> first + " " + second;
23 final IntBinaryOperator operator = Integer::sum;
25 // when
26 final var memoizer = new ConcurrentMapBasedIntBinaryOperatorMemoizer<>(cache, keyFunction, operator);
28 // then
29 Assertions.assertNotNull(memoizer);
32 @Test
33 void shouldRequireNonNullCache() {
34 // given
35 final ConcurrentMap<String, Integer> cache = null;
36 final IntBinaryFunction<String> keyFunction = (first, second) -> first + " " + second;
37 final IntBinaryOperator operator = Integer::sum;
39 // when
40 // then
41 Assertions.assertThrows(NullPointerException.class,
42 () -> new ConcurrentMapBasedIntBinaryOperatorMemoizer<>(cache, keyFunction, operator));
45 @Test
46 void shouldRequireNonNullKeyFunction() {
47 // given
48 final ConcurrentMap<String, Integer> cache = new ConcurrentHashMap<>();
49 final IntBinaryFunction<String> keyFunction = null;
50 final IntBinaryOperator operator = Integer::sum;
52 // when
53 // then
54 Assertions.assertThrows(NullPointerException.class,
55 () -> new ConcurrentMapBasedIntBinaryOperatorMemoizer<>(cache, keyFunction, operator));
58 @Test
59 void shouldRequireNonNullOperator() {
60 // given
61 final ConcurrentMap<String, Integer> cache = new ConcurrentHashMap<>();
62 final IntBinaryFunction<String> keyFunction = (first, second) -> first + " " + second;
63 final IntBinaryOperator operator = null;
65 // when
66 // then
67 Assertions.assertThrows(NullPointerException.class,
68 () -> new ConcurrentMapBasedIntBinaryOperatorMemoizer<>(cache, keyFunction, operator));
71 @Test
72 void shouldMemoizeOperator() {
73 // given
74 final ConcurrentMap<String, Integer> cache = new ConcurrentHashMap<>();
75 final IntBinaryFunction<String> keyFunction = (first, second) -> first + " " + second;
76 final IntBinaryOperator operator = Integer::sum;
78 // when
79 final var memoizer = new ConcurrentMapBasedIntBinaryOperatorMemoizer<>(cache, keyFunction, operator);
81 // then
82 memoizer.applyAsInt(123, 789);
85 @Test
86 void shouldUseSetCacheKeyAndValue() {
87 // given
88 final ConcurrentMap<String, Integer> cache = new ConcurrentHashMap<>();
89 final IntBinaryFunction<String> keyFunction = (first, second) -> first + " " + second;
90 final IntBinaryOperator operator = Integer::sum;
92 // when
93 final var memoizer = new ConcurrentMapBasedIntBinaryOperatorMemoizer<>(cache, keyFunction, operator);
95 // then
96 memoizer.applyAsInt(123, 789);
97 Assertions.assertFalse(memoizer.viewCacheForTest().isEmpty());
98 Assertions.assertEquals("123 789", memoizer.viewCacheForTest().keySet().iterator().next());
99 Assertions.assertEquals(912, memoizer.viewCacheForTest().values().iterator().next());
102 @Test
103 void shouldUseCallWrappedOperator() {
104 // given
105 final ConcurrentMap<String, Integer> cache = new ConcurrentHashMap<>();
106 final IntBinaryFunction<String> keyFunction = (first, second) -> first + " " + second;
107 final IntBinaryOperator operator = Mockito.mock(IntBinaryOperator.class);
109 // when
110 final var memoizer = new ConcurrentMapBasedIntBinaryOperatorMemoizer<>(cache, keyFunction, operator);
112 // then
113 memoizer.applyAsInt(123, 789);
114 Mockito.verify(operator).applyAsInt(123, 789);
117 @Test
118 void shouldUseReturnOperatorResult() {
119 // given
120 final ConcurrentMap<String, Integer> cache = new ConcurrentHashMap<>();
121 final IntBinaryFunction<String> keyFunction = (first, second) -> first + " " + second;
122 final IntBinaryOperator operator = Integer::sum;
124 // when
125 final var memoizer = new ConcurrentMapBasedIntBinaryOperatorMemoizer<>(cache, keyFunction, operator);
127 // then
128 Assertions.assertEquals(912, memoizer.applyAsInt(123, 789));