def test_cache_one(self): cache = lru.LRUCache(max_size=1) self.assertIsNone(cache.query(1)) cache.add(1, 1) self.assertEqual(cache.query(1), 1) cache.add(2, 2) self.assertIsNone(cache.query(1)) self.assertEqual(cache.query(2), 2) self.assertEqual(len(cache._cache_by_key), 1) self.assertEqual(cache._cache_by_use.size, 1)
def test_cache_two(self): cache = lru.LRUCache(max_size=2) cache.add(1, 1) cache.add(2, 2) self.assertEqual(cache.query(2), 2) cache.add(3, 3) self.assertEqual(cache.query(2), 2) cache.add(5, 5) self.assertIsNone(cache.query(3)) self.assertEqual(len(cache._cache_by_key), 2) self.assertEqual(cache._cache_by_use.size, 2)
def test_cache_cyclical(self): cache = lru.LRUCache(max_size=3) cache.add(1, 1) cache.add(2, 2) cache.add(3, 3) cache.add(4, 4) cache.add(5, 5) cache.add(6, 6) self.assertIsNone(cache.query(1)) self.assertIsNone(cache.query(2)) self.assertIsNone(cache.query(3)) self.assertEqual(cache.query(4), 4) self.assertEqual(cache.query(5), 5) self.assertEqual(cache.query(6), 6) self.assertEqual(len(cache._cache_by_key), 3) self.assertEqual(cache._cache_by_use.size, 3)
def test_cache_three(self): cache = lru.LRUCache(max_size=3) self.assertIsNone(cache.query(1)) cache.add(1, 1) cache.add(2, 2) cache.add(3, 3) self.assertEqual(cache.query(1), 1) self.assertEqual(cache.query(3), 3) self.assertEqual(cache.query(2), 2) cache.add(4, 4) self.assertIsNone(cache.query(1)) self.assertEqual(cache.query(4), 4) self.assertEqual(cache.query(3), 3) self.assertEqual(cache.query(2), 2) cache.add(5, 5) self.assertIsNone(cache.query(4)) self.assertEqual(len(cache._cache_by_key), 3) self.assertEqual(cache._cache_by_use.size, 3)
def test_cache_empty(self): cache = lru.LRUCache() self.assertIsNone(cache.query(123)) self.assertEqual(len(cache._cache_by_key), 0) self.assertEqual(cache._cache_by_use.size, 0)