LRUCacheTest.java
1package org.udger.parser;
2
3import org.junit.Test;
4
5import static org.junit.Assert.*;
6
7public class LRUCacheTest {
8
9 private LRUCache<Integer, Integer> cache = new LRUCache<>(3);
10
11 @Test
12 public void testLRUCacheRespectCapacity() throws Exception {
13 for(int i = 0; i < cache.getCapacity() + 1; i++){
14 cache.put(i,i);
15 }
16 assertNull("Expected the first value put into cache to be absent after adding one more than capacity allows.", cache.get(0));
17 }
18
19 @Test
20 public void testLRUCacheUpdatesLRUOrder() throws Exception {
21 for(int i = 0; i < cache.getCapacity(); i++){
22 cache.put(i,i);
23 }
24 assertNotNull("Expected to first put value to be in the cache.", cache.get(0));
25 cache.put(cache.getCapacity()+1, cache.getCapacity()+1);
26 assertNull("Expected the first value to have gone to the tail and second value to be removed for the next.", cache.get(1));
27 }
28}