def test_setdefault(self): l = LRU(2) l[1] = '1' val = l.setdefault(1) self.assertEqual('1', val) self.assertEqual((1, 0), l.get_stats()) val = l.setdefault(2, '2') self.assertEqual('2', val) self.assertEqual((1, 1), l.get_stats()) self.assertEqual(val, l[2])
def test_setdefault(self): l = LRU(2) l[1] = '1' val = l.setdefault(1) self.assertEqual('1', val) self.assertEqual((1, 0), l.get_stats()) val = l.setdefault(2, '2') self.assertEqual('2', val) self.assertEqual((1, 1), l.get_stats()) self.assertEqual(val, l[2]) l.clear() val = 'long string' * 512 l.setdefault(1, val) l[2] = '2' l[3] = '3' self.assertTrue(val)
class LRUCache(DictBasedCache): def __init__(self, *args, max_size=128, **kwargs): super().__init__(*args, **kwargs) self._cache = LRU(max_size) @property def stats(self): return self._cache.get_stats()
def test_stats(self): for size in SIZES: l = LRU(size) for i in range(size): l[i] = str(i) self.assertTrue(l.get_stats() == (0, 0)) val = l[0] self.assertTrue(l.get_stats() == (1, 0)) val = l.get(0, None) self.assertTrue(l.get_stats() == (2, 0)) val = l.get(-1, None) self.assertTrue(l.get_stats() == (2, 1)) try: val = l[-1] except: pass self.assertTrue(l.get_stats() == (2, 2)) l.clear() self.assertTrue(len(l) == 0) self.assertTrue(l.get_stats() == (0, 0))
def test_stats(self): for size in SIZES: l = LRU(size) for i in range(size): l[i] = str(i) self.assertTrue(l.get_stats() == (0, 0)) val = l[0] self.assertTrue(l.get_stats() == (1, 0)) val = l.get(0, None) self.assertTrue(l.get_stats() == (2, 0)) val = l.get(-1, None) self.assertTrue(l.get_stats() == (2, 1)) try: val = l[-1] except: pass self.assertTrue(l.get_stats() == (2, 2)) l.clear() self.assertTrue(len(l) == 0) self.assertTrue(l.get_stats() == (0, 0))
def test_bench_with_original(benchmark, data, collector): m = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss c1 = LRU(2000) benchmark.pedantic(run_cache, args=(c1, data), iterations=1, rounds=100) hits, misses = c1.get_stats() items = len(c1.keys()) del c1 gc.collect() collector( dict(hits=hits, misses=misses, items=items, memory=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss - m))
def test_popitem(self): l = LRU(3) l[1] = '1' l[2] = '2' l[3] = '3' k, v = l.popitem() self.assertEqual((1, '1'), (k, v)) k, v = l.popitem(least_recent=False) self.assertEqual((3, '3'), (k, v)) self.assertEqual((2, '2'), l.popitem(True)) with self.assertRaises(KeyError) as ke: l.popitem() self.assertEqual('popitem(): LRU dict is empty', ke.args[0]) self.assertEqual((0, 0), l.get_stats())
def test_pop(self): l = LRU(2) v = '2' * 4096 l[1] = '1' l[2] = v val = l.pop(1) self.assertEqual('1', val) self.assertEqual((1, 0), l.get_stats()) val = l.pop(2, 'not used') self.assertEqual(v, val) del val self.assertTrue(v) self.assertEqual((2, 0), l.get_stats()) val = l.pop(3, '3' * 4096) self.assertEqual('3' * 4096, val) self.assertEqual((2, 1), l.get_stats()) self.assertEqual(0, len(l)) with self.assertRaises(KeyError) as ke: l.pop(4) self.assertEqual(4, ke.args[0]) self.assertEqual((2, 2), l.get_stats()) self.assertEqual(0, len(l)) with self.assertRaises(TypeError): l.pop()
# Would print [(3, '3'), (5, '5'), (2, '2'), (1, '1')] print(l.get_size()) # Would print 5 l.set_size(3) print(l.items()) # Would print [(3, '3'), (5, '5'), (2, '2')] print(l.get_size()) # Would print 3 print(l.has_key(5)) # Would print True print(2 in l) # Would print True l.get_stats() # Would print (1, 0) l.update(5='0') # Update an item print l.items() # Would print [(5, '0'), (3, '3'), (2, '2')] l.clear() print l.items() # Would print [] def evicted(key, value): print "removing: %s, %s" % (key, value)