def test_stats(self): for size in SIZES: l = TTLRU(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_get_and_del(self): l = TTLRU(2) l[1] = '1' self.assertEqual('1', l.get(1)) self.assertEqual('1', l.get(2, '1')) self.assertIsNone(l.get(2)) self.assertEqual('1', l[1]) self.assertRaises(KeyError, lambda: l['2']) with self.assertRaises(KeyError): del l['2']
def test_access_within_size(self): for size in SIZES: l = TTLRU(size) for i in range(size): l[i] = str(i) for i in range(size): self.assertEqual(l[i], str(i)) self.assertEqual(l.get(i, None), str(i))
def test_access(self): for size in SIZES: l = TTLRU(size) n = size * 2 for i in range(n): l[i] = str(i) self._check_kvi(range(n - 1, size - 1, -1), l) for i in range(size, n): self.assertEqual(l[i], str(i)) self.assertEqual(l.get(i, None), str(i))
def test_ref_count(self): l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) l.get(1) self.assertEqual(sys.getrefcount(x), 2) # ==================== l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) l.peek_first_item() self.assertEqual(sys.getrefcount(x), 2) # ==================== l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) l.peek_last_item() self.assertEqual(sys.getrefcount(x), 2) # ==================== l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) l.keys() self.assertEqual(sys.getrefcount(x), 2) # ==================== l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) l[1] = 2 self.assertEqual(sys.getrefcount(x), 2) # ==================== l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) 1 in l self.assertEqual(sys.getrefcount(x), 2) l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) time.sleep(0.02) self.assertEqual(sys.getrefcount(x), 3) try: l[1] except: pass self.assertEqual(sys.getrefcount(x), 2) # ==================== l = TTLRU(2, ttl=int(20e6)) x = {1: 2} self.assertEqual(sys.getrefcount(x), 2) l[1] = x self.assertEqual(sys.getrefcount(x), 3) l[1] = 2 self.assertEqual(sys.getrefcount(x), 2)