summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/util/BasicLruCacheTest.java
blob: bce084790bfbeb940b2ad43efca3a1d927d263bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;

public final class BasicLruCacheTest extends TestCase {

    public void testCreateOnCacheMiss() {
        BasicLruCache<String, String> cache = newCreatingCache();
        String created = cache.get("aa");
        assertEquals("created-aa", created);
    }

    public void testNoCreateOnCacheHit() {
        BasicLruCache<String, String> cache = newCreatingCache();
        cache.put("aa", "put-aa");
        assertEquals("put-aa", cache.get("aa"));
    }

    public void testConstructorDoesNotAllowZeroCacheSize() {
        try {
            new BasicLruCache<String, String>(0);
            fail();
        } catch (IllegalArgumentException expected) {
        }
    }

    public void testCannotPutNullKey() {
        BasicLruCache<String, String> cache = new BasicLruCache<String, String>(3);
        try {
            cache.put(null, "A");
            fail();
        } catch (NullPointerException expected) {
        }
    }

    public void testCannotPutNullValue() {
        BasicLruCache<String, String> cache = new BasicLruCache<String, String>(3);
        try {
            cache.put("a", null);
            fail();
        } catch (NullPointerException expected) {
        }
    }

    public void testEvictionWithSingletonCache() {
        BasicLruCache<String, String> cache = new BasicLruCache<String, String>(1);
        cache.put("a", "A");
        cache.put("b", "B");
        assertSnapshot(cache, "b", "B");
    }

    public void testEntryEvictedWhenFull() {
        List<String> expectedEvictionLog = new ArrayList<String>();
        final List<String> evictionLog = new ArrayList<String>();
        BasicLruCache<String, String> cache = new BasicLruCache<String, String>(3) {
            @Override protected void entryEvicted(String key, String value) {
                evictionLog.add(key + "=" + value);
            }
        };

        cache.put("a", "A");
        cache.put("b", "B");
        cache.put("c", "C");
        assertEquals(expectedEvictionLog, evictionLog);

        cache.put("d", "D");
        expectedEvictionLog.add("a=A");
        assertEquals(expectedEvictionLog, evictionLog);
    }

    /**
     * Replacing the value for a key doesn't cause an eviction but it does bring
     * the replaced entry to the front of the queue.
     */
    public void testPutDoesNotCauseEviction() {
        final List<String> evictionLog = new ArrayList<String>();
        List<String> expectedEvictionLog = new ArrayList<String>();
        BasicLruCache<String, String> cache = new BasicLruCache<String, String>(3) {
            @Override protected void entryEvicted(String key, String value) {
                evictionLog.add(key + "=" + value);
            }
        };

        cache.put("a", "A");
        cache.put("b", "B");
        cache.put("c", "C");
        cache.put("b", "B2");
        assertEquals(expectedEvictionLog, evictionLog);
        assertSnapshot(cache, "a", "A", "c", "C", "b", "B2");
    }

    public void testEvictAll() {
        final List<String> evictionLog = new ArrayList<String>();
        BasicLruCache<String, String> cache = new BasicLruCache<String, String>(10) {
            @Override protected void entryEvicted(String key, String value) {
                evictionLog.add(key + "=" + value);
            }
        };

        cache.put("a", "A");
        cache.put("b", "B");
        cache.put("c", "C");
        cache.evictAll();
        assertSnapshot(cache);
        assertEquals(Arrays.asList("a=A", "b=B", "c=C"), evictionLog);
    }

    private BasicLruCache<String, String> newCreatingCache() {
        return new BasicLruCache<String, String>(3) {
            @Override protected String create(String key) {
                return (key.length() > 1) ? ("created-" + key) : null;
            }
        };
    }

    private <T> void assertSnapshot(BasicLruCache<T, T> cache, T... keysAndValues) {
        List<T> actualKeysAndValues = new ArrayList<T>();
        for (Map.Entry<T, T> entry : cache.snapshot().entrySet()) {
            actualKeysAndValues.add(entry.getKey());
            actualKeysAndValues.add(entry.getValue());
        }

        // assert using lists because order is important for LRUs
        assertEquals(Arrays.asList(keysAndValues), actualKeysAndValues);
    }
}