def test_get(self): obj = LRU('a',1) obj.put('a') obj.put('b') # obj.put('c') # print(obj.l) self.assertEqual(obj.get(),False)
def main(a): if a == 1: least_recently_used = LRU(3) least_recently_used.put("A", 0) least_recently_used.put("B", 1) least_recently_used.put("C", 2) least_recently_used.print_cache_dictionary() least_recently_used.print_cache_doubly() print() least_recently_used.get("A") least_recently_used.print_cache_dictionary() least_recently_used.print_cache_doubly() print() else: word_list = [] with open("WORDS") as file: line = file.readline().split() while line: word_list.extend(line) line = file.readline().split() most_frequent_elements(word_list)
def test_get_cache(self): obj = LRU('a',1) obj.put('a') obj.put('b') obj.put('c') obj.put('d') self.assertEqual(obj.get_cache(),['b','c','d'])
def main(): l = [2, 9, 1, 7, 3, 5, 7, 6, 9, 2, 1] cache = LRU() for each in l: cache.put(each) print(cache.get_cache()) assert cache.get_cache() == [1, 2, 9, 6, 7, 5, 3]
def testClear(self): obj = LRU(2) obj.put(1, 2) obj.put(3, 4) self.assertEqual(len(obj), 2) obj.clear() self.assertEqual(len(obj), 0)
def testChangeNodeToMostRecentlyUsed(self): obj = LRU(3) obj.put(1, 1) obj.put(2, 1) obj.put(3, 1) node = obj.mapping[1] # points to 1 obj.changeNodeToMostRecentlyUsed(node) self.assertEqual(obj.head, node.next)
def testGetPut(self): obj = LRU(1) obj.put(1, 2) self.assertEqual(obj.get(1), 2) obj.put(1, 4) # checkk if value is updated self.assertEqual(obj.get(1), 4) # check if default value is returned if key isn't there self.assertEqual(obj.get(2, "not found"), "not found")
class LRUTest: def __init__(self): self.lru = LRU() def put(self, key): return self.lru.put(key) def get(self): return self.lru.get() def get_cache(self): return self.lru.get_cache()
def main(): lru_obj = LRU(3) #tc1 assert lru_obj.get('a')==-1 #tc2 lru_obj.put('a',1) lru_obj.put('b',2) lru_obj.put('c',3) assert lru_obj.get('a')==1, 'Wrong Value' #tc3 assert lru_obj.get_cache()== {'a':1, 'b':2, 'c':3}, 'Cache is not updated properly' #tc4 lru_obj.put('d',4) #tc5 assert len(lru_obj.get_cache())==3, 'capacity crossed' #tc6 assert lru_obj.get('b')==-1, 'Least Recently Used is not being removed when capacity is crossed' print("All test cases passed!!")
def testLRUItemIsActuallyRemoved(self): obj = LRU(4) obj.put(1, 1) obj.put(2, 1) obj.put(3, 1) obj.put(4, 1) obj.put(1, 1) # 2 becomes lru obj.put(5, 1) # now get(2) should return none self.assertEqual(obj.get(2), None)
from LRU import LRU import random random_cache_size = random.randrange(10) print("Initiaizing cache of size", random_cache_size) cache_obj = LRU(random_cache_size) # returns None as cache is empty print("1 ->", cache_obj.get(1)) # returns "not found" as it is the default argument print("1 ->", cache_obj.get(1, "not found")) cache_obj.put(1, 2) # prints 2 print("1 ->", cache_obj.get(1))
def test_put(self): obj = LRU('a',1) self.assertEqual(obj.put('a'),'done')