Exemple #1
0
    def test_cache_lookup(self):
        cache = LRUCache(4)
        for x in ['a', 'b', 'c', 'd']:
            cache[x] = x

        for x in ['a', 'b', 'c', 'd']:
            self.assertEqual(x, cache[x])
Exemple #2
0
    def test_cache_overflow(self):
        cache = LRUCache(3)
        for x in ['a', 'b', 'c', 'd']:
            cache[x] = x

        for x in ['b', 'c', 'd']:
            self.assertEqual(x, cache[x])

        with self.assertRaises(KeyError):
            _ = cache['a']

        _ = cache['b']
        _ = cache['d']
        # 'c' should be LRU now
        cache['e'] = 'e'

        with self.assertRaises(KeyError):
            _ = cache['c']

        for x in ['b', 'd', 'e']:
            self.assertEqual(x, cache[x])
Exemple #3
0
 def test_cache_no_entry(self):
     cache = LRUCache(3)
     with self.assertRaises(KeyError):
         _ = cache['a']