/* * 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 cache = newCreatingCache(); String created = cache.get("aa"); assertEquals("created-aa", created); } public void testNoCreateOnCacheHit() { BasicLruCache cache = newCreatingCache(); cache.put("aa", "put-aa"); assertEquals("put-aa", cache.get("aa")); } public void testConstructorDoesNotAllowZeroCacheSize() { try { new BasicLruCache(0); fail(); } catch (IllegalArgumentException expected) { } } public void testCannotPutNullKey() { BasicLruCache cache = new BasicLruCache(3); try { cache.put(null, "A"); fail(); } catch (NullPointerException expected) { } } public void testCannotPutNullValue() { BasicLruCache cache = new BasicLruCache(3); try { cache.put("a", null); fail(); } catch (NullPointerException expected) { } } public void testEvictionWithSingletonCache() { BasicLruCache cache = new BasicLruCache(1); cache.put("a", "A"); cache.put("b", "B"); assertSnapshot(cache, "b", "B"); } public void testEntryEvictedWhenFull() { List expectedEvictionLog = new ArrayList(); final List evictionLog = new ArrayList(); BasicLruCache cache = new BasicLruCache(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 evictionLog = new ArrayList(); List expectedEvictionLog = new ArrayList(); BasicLruCache cache = new BasicLruCache(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 evictionLog = new ArrayList(); BasicLruCache cache = new BasicLruCache(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 newCreatingCache() { return new BasicLruCache(3) { @Override protected String create(String key) { return (key.length() > 1) ? ("created-" + key) : null; } }; } private void assertSnapshot(BasicLruCache cache, T... keysAndValues) { List actualKeysAndValues = new ArrayList(); for (Map.Entry 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); } }