def test_lfu(self): c = cache.PerfectLfuCache(3) self.assertEquals(len(c), 0) c.put(1) self.assertEquals(len(c), 1) c.put(2) self.assertEquals(len(c), 2) c.put(3) self.assertEquals(len(c), 3) self.assertEquals(len(c.dump()), 3) for v in (1, 2, 3): self.assertTrue(c.has(v)) c.get(1) c.get(1) c.get(1) c.get(1) c.get(1) c.get(2) c.get(2) c.get(2) c.get(2) c.get(3) c.get(3) c.get(3) c.get(5) c.put(5) # This does not removes 3 self.assertEquals(c.dump(), [1, 2, 3]) c.get(5) c.get(5) c.get(5) c.get(5) c.get(5) # Now 5 has been requested frequently enough to be included in cache # and replace 3 c.put(5) self.assertEquals(c.dump(), [5, 1, 2]) # Now 5 has been requested 2 times, but 3 was requested 3 times. If I # reinsert 3, 3 is kept and 5 discarded c.get(3) c.get(3) c.get(3) c.get(3) c.get(3) # Now 3 has been requested enough times to be inserted and evict 2 c.put(3) self.assertEquals(c.dump(), [3, 5, 1]) c.clear() self.assertEquals(len(c), 0) self.assertEquals(c.dump(), [])
def test_lfu(self): c = cache.PerfectLfuCache(3) assert len(c) == 0 c.put(1) assert len(c) == 1 c.put(2) assert len(c) == 2 c.put(3) assert len(c) == 3 assert len(c.dump()) == 3 for v in (1, 2, 3): assert c.has(v) c.get(1) c.get(1) c.get(1) c.get(1) c.get(1) c.get(2) c.get(2) c.get(2) c.get(2) c.get(3) c.get(3) c.get(3) c.get(5) c.put(5) # This does not removes 3 assert c.dump() == [1, 2, 3] c.get(5) c.get(5) c.get(5) c.get(5) c.get(5) # Now 5 has been requested frequently enough to be included in cache # and replace 3 c.put(5) assert c.dump() == [5, 1, 2] # Now 5 has been requested 2 times, but 3 was requested 3 times. If I # reinsert 3, 3 is kept and 5 discarded c.get(3) c.get(3) c.get(3) c.get(3) c.get(3) # Now 3 has been requested enough times to be inserted and evict 2 c.put(3) assert c.dump() == [3, 5, 1] c.clear() assert len(c) == 0 assert c.dump() == []