Fix LFUCache (#3847)

This commit is contained in:
YuLuo
2023-01-15 17:28:16 +08:00
committed by GitHub
parent b6c1d250f4
commit b14f55096d
2 changed files with 19 additions and 1 deletions

View File

@ -101,7 +101,7 @@ public class LFUCache<K, V> {
node.next = temp; node.next = temp;
node.previous = temp.previous; node.previous = temp.previous;
temp.previous.next = node; temp.previous.next = node;
node.previous = temp.previous; temp.previous = node;
break; break;
} }
} else { } else {

View File

@ -61,4 +61,22 @@ class LFUCacheTest {
assertEquals(null, lfuCache.get(2)); assertEquals(null, lfuCache.get(2));
assertEquals("Zeta", lfuCache.get(7)); assertEquals("Zeta", lfuCache.get(7));
} }
/**
* test addNodeWithUpdatedFrequency method
* @author yuluo
*/
@Test
void testAddNodeWithUpdatedFrequency() {
LFUCache<Integer, String> lfuCache = new LFUCache<>(3);
lfuCache.put(1, "beijing");
lfuCache.put(2, "shanghai");
lfuCache.put(3, "gansu");
assertEquals("beijing", lfuCache.get(1));
lfuCache.put(1, "shanxi");
assertEquals("shanxi", lfuCache.get(1));
}
} }