def test_get(self): c = LRUCache(3) c.put(4, 2) c.put(1, 1) c.put(2, 3) # get the first key self.assertEqual(c.get(2), 3) self.assertEqual(c.cache(), {2: 3, 1: 1, 4: 2}) self.assertEqual(c.size(), 3) # get the second key self.assertEqual(c.get(1), 1) self.assertEqual(c.cache(), {1: 1, 2: 3, 4: 2}) self.assertEqual(c.size(), 3) # get the last key self.assertEqual(c.get(4), 2) self.assertEqual(c.cache(), {4: 2, 2: 3, 1: 1}) self.assertEqual(c.size(), 3) # get a non-existing key self.assertEqual(c.get(40), -1) self.assertEqual(c.cache(), {4: 2, 2: 3, 1: 1}) self.assertEqual(c.size(), 3)
def test_instanciation_with_timeout_5sec(self): cache = LRUCache(5, 5) cache.put('first', 'first') cache.put('second', 'second') self.assertEqual(len(cache.keys()), 2) time.sleep(7) self.assertEqual(len(cache.keys()), 0) cache.stop_timer()
def test_reset(self): c = LRUCache(3) c.put(4, 2) c.put(1, 1) c.put(2, 3) # after reset, nothing existing in the cache c.reset() self.assertEqual(c.size(), 0) self.assertEqual(c.cache(), {})
def test_insert_over_capacity(): cache = LRUCache(3) cache.put('key1', 'val1') cache.put('key2', 'val2') cache.put('key3', 'val3') cache.put('key4', 'val4') with pytest.raises(CacheMissException) as ex: cache.get('key1') assert 'key1' in str(ex) assert cache.get('key2') == 'val2' assert cache.get('key3') == 'val3' assert cache.get('key4') == 'val4'
def test_delete(self): c = LRUCache(3) c.put(4, 2) c.put(1, 1) c.put(2, 3) # delete a key that's not in the map, # nothing happens c.delete(20) self.assertEqual(c.size(), 3) self.assertEqual(c.cache(), {2: 3, 1: 1, 4: 2}) # delete an existing key c.delete(1) self.assertEqual(c.size(), 2) self.assertEqual(c.cache(), {2: 3, 4: 2})
def test_put(self): c = LRUCache(3) c.put(4, 2) c.put(1, 1) c.put(2, 3) self.assertEqual(c.size(), 3) self.assertEqual(c.cache(), {2: 3, 1: 1, 4: 2}) # put a key that's in the map, not at the top; # this will bring the key and its value to the top of the cache, # since it's been interacted c.put(1, 10) self.assertEqual(c.cache(), {1: 10, 2: 3, 4: 2}) # put an existing key that's at the top c.put(1, 12) self.assertEqual(c.cache(), {1: 12, 2: 3, 4: 2}) # put a key that's not in the map; # the LRU cache should be removed, # and the new one should be added to the top c.put(5, 6) self.assertEqual(c.size(), 3) self.assertEqual(c.cache(), {5: 6, 1: 12, 2: 3})
def test_instanciation_without_timeout(self): cache = LRUCache(5) cache.put('1', '2') cache.put('2', '3') cache.put('3', '4') cache.put('4', '5') cache.put('first', 'value') self.assertEqual(cache.get('1'), '2') self.assertEqual(cache.get('2'), '3') self.assertEqual(cache.get('3'), '4') self.assertEqual(cache.get('4'), '5') self.assertEqual(cache.get('5'), -1)
def main(): testLRU = LRUCache(3) testLRU.put(1, "Hyd") testLRU.put(2, "Mumbai") testLRU.put(3, "Delhi") assert testLRU.cache == {1: "Hyd", 2: "Mumbai", 3: "Delhi"} print("Testcase1 passed") assert testLRU.get(2) == "Mumbai" print("Testcase2 passed") assert testLRU.cache == {1: "Hyd", 3: "Delhi", 2: "Mumbai"} print("Testcase3 passed") assert testLRU.get(5) == -1 print("Testcase4 passed") testLRU.put(4, "Chennai") assert testLRU.get_cache() == {3: "Delhi", 2: "Mumbai", 4: "Chennai"} print("Testcase5 passed") print("All test cases passed")
def test_all(self): c = LRUCache(5) c.put(1, 'one') c.put(2, 'two') c.put(3, 'three') c.delete(3) self.assertEqual(c.size(), 2) self.assertEqual(c.cache(), {2: 'two', 1: 'one'}) c.put(4, 'four') c.put(6, 'six') self.assertEqual(c.get(1), 'one') self.assertEqual(c.size(), 4) self.assertEqual(c.cache(), {1: 'one', 6: 'six', 4: 'four', 2: 'two'}) self.assertEqual(c.get(3), -1) c.put(1, 'five') self.assertEqual(c.cache(), {1: 'five', 6: 'six', 4: 'four', 2: 'two'}) c.put(12, 'twelve') self.assertEqual(c.cache(), { 12: 'twelve', 1: 'five', 6: 'six', 4: 'four', 2: 'two' }) c.put(5, 'five') self.assertEqual(c.cache(), { 5: 'five', 12: 'twelve', 1: 'five', 6: 'six', 4: 'four' }) self.assertEqual(c.get(2), -1) c.reset() self.assertEqual(c.size(), 0) self.assertEqual(c.cache(), {})
def test_insert_one(): cache = LRUCache(3) cache.put('key', 'val') assert cache.get('key') == 'val'
def test_insert_many(): cache = LRUCache(3) cache.put('key', 'val') cache.put('key2', 'val2') cache.put('key3', 'val3') assert cache.get('key3') == 'val3'
def test_update_one(): cache = LRUCache(3) cache.put('key', 'val') cache.put('key', 'new val') assert cache.get('key') == 'new val'