2 * SPDX-FileCopyrightText: The memoization.java Authors
3 * SPDX-License-Identifier: 0BSD
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
{
19 void shouldAcceptCacheAndKeyFunctionAndOperator() {
21 final ConcurrentMap
<String
, Integer
> cache
= new ConcurrentHashMap
<>();
22 final IntBinaryFunction
<String
> keyFunction
= (first
, second
) -> first
+ " " + second
;
23 final IntBinaryOperator operator
= Integer
::sum
;
26 final var memoizer
= new ConcurrentMapBasedIntBinaryOperatorMemoizer
<>(cache
, keyFunction
, operator
);
29 Assertions
.assertNotNull(memoizer
);
33 void shouldRequireNonNullCache() {
35 final ConcurrentMap
<String
, Integer
> cache
= null;
36 final IntBinaryFunction
<String
> keyFunction
= (first
, second
) -> first
+ " " + second
;
37 final IntBinaryOperator operator
= Integer
::sum
;
41 Assertions
.assertThrows(NullPointerException
.class,
42 () -> new ConcurrentMapBasedIntBinaryOperatorMemoizer
<>(cache
, keyFunction
, operator
));
46 void shouldRequireNonNullKeyFunction() {
48 final ConcurrentMap
<String
, Integer
> cache
= new ConcurrentHashMap
<>();
49 final IntBinaryFunction
<String
> keyFunction
= null;
50 final IntBinaryOperator operator
= Integer
::sum
;
54 Assertions
.assertThrows(NullPointerException
.class,
55 () -> new ConcurrentMapBasedIntBinaryOperatorMemoizer
<>(cache
, keyFunction
, operator
));
59 void shouldRequireNonNullOperator() {
61 final ConcurrentMap
<String
, Integer
> cache
= new ConcurrentHashMap
<>();
62 final IntBinaryFunction
<String
> keyFunction
= (first
, second
) -> first
+ " " + second
;
63 final IntBinaryOperator operator
= null;
67 Assertions
.assertThrows(NullPointerException
.class,
68 () -> new ConcurrentMapBasedIntBinaryOperatorMemoizer
<>(cache
, keyFunction
, operator
));
72 void shouldMemoizeOperator() {
74 final ConcurrentMap
<String
, Integer
> cache
= new ConcurrentHashMap
<>();
75 final IntBinaryFunction
<String
> keyFunction
= (first
, second
) -> first
+ " " + second
;
76 final IntBinaryOperator operator
= Integer
::sum
;
79 final var memoizer
= new ConcurrentMapBasedIntBinaryOperatorMemoizer
<>(cache
, keyFunction
, operator
);
82 memoizer
.applyAsInt(123, 789);
86 void shouldUseSetCacheKeyAndValue() {
88 final ConcurrentMap
<String
, Integer
> cache
= new ConcurrentHashMap
<>();
89 final IntBinaryFunction
<String
> keyFunction
= (first
, second
) -> first
+ " " + second
;
90 final IntBinaryOperator operator
= Integer
::sum
;
93 final var memoizer
= new ConcurrentMapBasedIntBinaryOperatorMemoizer
<>(cache
, keyFunction
, operator
);
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());
103 void shouldUseCallWrappedOperator() {
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);
110 final var memoizer
= new ConcurrentMapBasedIntBinaryOperatorMemoizer
<>(cache
, keyFunction
, operator
);
113 memoizer
.applyAsInt(123, 789);
114 Mockito
.verify(operator
).applyAsInt(123, 789);
118 void shouldUseReturnOperatorResult() {
120 final ConcurrentMap
<String
, Integer
> cache
= new ConcurrentHashMap
<>();
121 final IntBinaryFunction
<String
> keyFunction
= (first
, second
) -> first
+ " " + second
;
122 final IntBinaryOperator operator
= Integer
::sum
;
125 final var memoizer
= new ConcurrentMapBasedIntBinaryOperatorMemoizer
<>(cache
, keyFunction
, operator
);
128 Assertions
.assertEquals(912, memoizer
.applyAsInt(123, 789));