1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.internal.textservice;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.ArgumentMatchers.anyInt;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.support.test.filters.SmallTest;
29 import android.support.test.runner.AndroidJUnit4;
30 
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 import java.util.concurrent.atomic.AtomicReference;
35 import java.util.function.IntUnaryOperator;
36 
37 @SmallTest
38 @RunWith(AndroidJUnit4.class)
39 public class LazyIntToIntMapTest {
40     @Test
testLaziness()41     public void testLaziness() {
42         final IntUnaryOperator func = mock(IntUnaryOperator.class);
43         when(func.applyAsInt(eq(1))).thenReturn(11);
44         when(func.applyAsInt(eq(2))).thenReturn(22);
45 
46         final LazyIntToIntMap map = new LazyIntToIntMap(func);
47 
48         verify(func, never()).applyAsInt(anyInt());
49 
50         assertEquals(22, map.get(2));
51         verify(func, times(0)).applyAsInt(eq(1));
52         verify(func, times(1)).applyAsInt(eq(2));
53 
54         // Accessing to the same key does not evaluate the function again.
55         assertEquals(22, map.get(2));
56         verify(func, times(0)).applyAsInt(eq(1));
57         verify(func, times(1)).applyAsInt(eq(2));
58     }
59 
60     @Test
testDelete()61     public void testDelete() {
62         final IntUnaryOperator func1 = mock(IntUnaryOperator.class);
63         when(func1.applyAsInt(eq(1))).thenReturn(11);
64         when(func1.applyAsInt(eq(2))).thenReturn(22);
65 
66         final IntUnaryOperator func2 = mock(IntUnaryOperator.class);
67         when(func2.applyAsInt(eq(1))).thenReturn(111);
68         when(func2.applyAsInt(eq(2))).thenReturn(222);
69 
70         final AtomicReference<IntUnaryOperator> funcRef = new AtomicReference<>(func1);
71         final LazyIntToIntMap map = new LazyIntToIntMap(i -> funcRef.get().applyAsInt(i));
72 
73         verify(func1, never()).applyAsInt(anyInt());
74         verify(func2, never()).applyAsInt(anyInt());
75 
76         assertEquals(22, map.get(2));
77         verify(func1, times(1)).applyAsInt(eq(2));
78         verify(func2, times(0)).applyAsInt(eq(2));
79 
80         // Swap func1 with func2 then invalidate the key=2
81         funcRef.set(func2);
82         map.delete(2);
83 
84         // Calling get(2) again should re-evaluate the value.
85         assertEquals(222, map.get(2));
86         verify(func1, times(1)).applyAsInt(eq(2));
87         verify(func2, times(1)).applyAsInt(eq(2));
88 
89         // Trying to delete non-existing keys does nothing.
90         map.delete(1);
91     }
92 }
93