Adjust LruCacheTest to use the Map API

This commit is contained in:
Jesse Wilson 2011-11-21 06:26:30 +00:00
parent 4da08b0ec6
commit 0ff7d980c5

View File

@ -28,56 +28,56 @@ import junit.framework.TestCase;
public class LruCacheTest extends TestCase {
public void testCacheHitAndMiss() throws Exception {
Cache<String, Integer> cache = new LruCache<String, Integer>(3);
LruCache<String, Integer> cache = new LruCache<String, Integer>(3);
String key = "key1";
assertNull(cache.getElement(key));
cache.addElement(key, 1);
assertEquals(1, cache.getElement(key).intValue());
assertNull(cache.get(key));
cache.put(key, 1);
assertEquals(1, cache.get(key).intValue());
String key2 = "key2";
cache.addElement(key2, 2);
assertEquals(1, cache.getElement(key).intValue());
assertEquals(2, cache.getElement(key2).intValue());
cache.put(key2, 2);
assertEquals(1, cache.get(key).intValue());
assertEquals(2, cache.get(key2).intValue());
}
public void testCacheKeyOverwrite() throws Exception {
Cache<String, Integer> cache = new LruCache<String, Integer>(3);
LruCache<String, Integer> cache = new LruCache<String, Integer>(3);
String key = "key1";
assertNull(cache.getElement(key));
cache.addElement(key, 1);
assertEquals(1, cache.getElement(key).intValue());
assertNull(cache.get(key));
cache.put(key, 1);
assertEquals(1, cache.get(key).intValue());
cache.addElement(key, 5);
assertEquals(5, cache.getElement(key).intValue());
cache.put(key, 5);
assertEquals(5, cache.get(key).intValue());
}
public void testCacheEviction() throws Exception {
Cache<String, Integer> cache = new LruCache<String, Integer>(5);
LruCache<String, Integer> cache = new LruCache<String, Integer>(5);
cache.addElement("key1", 1);
cache.addElement("key2", 2);
cache.addElement("key3", 3);
cache.addElement("key4", 4);
cache.addElement("key5", 5);
assertEquals(1, cache.getElement("key1").intValue());
assertEquals(2, cache.getElement("key2").intValue());
assertEquals(3, cache.getElement("key3").intValue());
assertEquals(4, cache.getElement("key4").intValue());
assertEquals(5, cache.getElement("key5").intValue());
cache.put("key1", 1);
cache.put("key2", 2);
cache.put("key3", 3);
cache.put("key4", 4);
cache.put("key5", 5);
assertEquals(1, cache.get("key1").intValue());
assertEquals(2, cache.get("key2").intValue());
assertEquals(3, cache.get("key3").intValue());
assertEquals(4, cache.get("key4").intValue());
assertEquals(5, cache.get("key5").intValue());
// Access key1 to show key2 will be evicted (shows not a FIFO cache)
cache.getElement("key1");
cache.getElement("key3");
cache.addElement("key6", 6);
cache.addElement("key7", 7);
assertEquals(1, cache.getElement("key1").intValue());
assertNull(cache.getElement("key2"));
assertEquals(3, cache.getElement("key3").intValue());
assertNull(cache.getElement("key4"));
assertEquals(5, cache.getElement("key5").intValue());
assertEquals(6, cache.getElement("key6").intValue());
assertEquals(7, cache.getElement("key7").intValue());
cache.get("key1");
cache.get("key3");
cache.put("key6", 6);
cache.put("key7", 7);
assertEquals(1, cache.get("key1").intValue());
assertNull(cache.get("key2"));
assertEquals(3, cache.get("key3").intValue());
assertNull(cache.get("key4"));
assertEquals(5, cache.get("key5").intValue());
assertEquals(6, cache.get("key6").intValue());
assertEquals(7, cache.get("key7").intValue());
}
}