def test_deletion(self): # Zero capacity cache is not allowed with AssertRaises(ValueError): c = LRUCache(0) # Capacity = 1 c = LRUCache(1) c[0] = '0' c[1] = '1' assert_eq(1, len(c)) assert_eq('1', c[1]) assert_is(miss, c.get(2)) del c[1] assert_eq(0, len(c)) # Capacity = 2 c = LRUCache(2) c[0] = '0' c[1] = '1' c[2] = '2' del c[1] assert_eq(1, len(c)) assert_is(miss, c.get(1)) assert_eq('2', c[2]) c = LRUCache(2) c[0] = '0' c[1] = '1' c[2] = '2' del c[2] assert_eq(1, len(c)) assert_eq('1', c[1]) assert_is(miss, c.get(2)) # Capacity = 3 c = LRUCache(3) c[0] = '0' c[1] = '1' c[2] = '2' c[3] = '3' del c[2] assert_eq(2, len(c)) assert_is(miss, c.get(2)) assert_eq('1', c[1]) assert_eq('3', c[3]) # Deletion of invalid item with AssertRaises(KeyError): del c[15]
def test_get(self): c = LRUCache(3) c[0] = 'a' c[1] = 'b' c[2] = 'c' # Getting a value should make it MRU assert_in(1, c) assert_eq('b', c.get(1)) self._check_order([(0, 'a'), (2, 'c'), (1, 'b')], c) # Missing value should have no effect assert_not_in(100, c) assert_eq(miss, c.get(100)) self._check_order([(0, 'a'), (2, 'c'), (1, 'b')], c)
def test_get(self): c = LRUCache(3) c[0] = "a" c[1] = "b" c[2] = "c" # Getting a value should make it MRU assert_in(1, c) assert_eq("b", c.get(1)) self._check_order([(0, "a"), (2, "c"), (1, "b")], c) # Missing value should have no effect assert_not_in(100, c) assert_eq(miss, c.get(100)) self._check_order([(0, "a"), (2, "c"), (1, "b")], c)