def test_past_max_capacity(self): our_cache = LRU_Cache(5) our_cache.set(1, 1) our_cache.set(2, 2) our_cache.set(3, 3) our_cache.set(4, 4) our_cache.get(1) self.assertEqual(our_cache.get(2), 2) self.assertEqual(our_cache.get(1), -1)
def test_get_should_return_neg1_when_falsey_key_given(self): """ LRU_Cache::get() should return -1 when a falsey key is given. """ # Set up. cache = LRU_Cache(5) self.assertEqual(-1, cache.get('')) self.assertEqual(-1, cache.get(False)) self.assertEqual(-1, cache.get(None)) self.assertEqual(-1, cache.get([])) self.assertEqual(-1, cache.get({}))
def test_search_cache_nonexistent_data(self): our_cache = LRU_Cache(5) our_cache.set(1, 1) self.assertEqual(-1, our_cache.get( 9)) # returns -1 because 9 is not present in the cache
def test_get_should_return_neg1_when_0_capacity(self): """ LRU_Cache::get() should return -1 when the cache's capacity is 0. """ cache = LRU_Cache(0) cache.set(1, 1) self.assertEqual(-1, cache.get(1))
def test_get_should_return_value_and_move_node_to_front(self): """ LRU_Cache::get() should return the value and move the node to the front (MRU) position of the linked list. """ # Set up. cache = LRU_Cache(5) test_data = { 'udacity': 1, 'python': 2, 100: 'hello world', 200: 'algorithms' } for key, value in test_data.items(): cache.set(key, value) # Test -1 is returned if the key does not exist. self.assertEqual(-1, cache.get('does_not_exist')) # Test getting the value and that the node was moved to the front (MRU) position. self.assertEqual('hello world', cache.get(100)) node = cache.head.next self.assertEqual(cache.hashtable[100], node) self.assertEqual('hello world', node.value) self.assertEqual(100, node.key) self.assertEqual(2, cache.get('python')) node = cache.head.next self.assertEqual(cache.hashtable['python'], node) self.assertEqual(2, node.value) self.assertEqual('python', node.key) self.assertEqual(1, cache.get('udacity')) node = cache.head.next self.assertEqual(cache.hashtable['udacity'], node) self.assertEqual(1, node.value) self.assertEqual('udacity', node.key) self.assertEqual('algorithms', cache.get(200)) node = cache.head.next self.assertEqual(cache.hashtable[200], node) self.assertEqual('algorithms', node.value) self.assertEqual(200, node.key) # Clean up. cache.clear()
def test_get_shouild_return_neg1_when_not_cached(self): """ LRU_Cache::get() should return -1 when the key is not cached. """ # Set up. cache = LRU_Cache(5) self.assertEqual(-1, cache.get('does_not_exist'))
def test_cache_before_limit(self): our_cache = LRU_Cache(3) our_cache.set(1, 1) our_cache.set(2, 2) our_cache.set(3, 3) self.assertEqual(our_cache.curr_oper_capacity, 3) self.assertEqual(our_cache.get(1), 1)
def test_insert_data_in_cache(self): our_cache = LRU_Cache(5) our_cache.set(1, 1) our_cache.set(2, 2) our_cache.set(3, 3) our_cache.set(4, 4) self.assertEqual(1, our_cache.get(1)) # returns 1 self.assertEqual(2, our_cache.get(2)) # returns 2
def test_insert_data_cache_capacity_exceeded(self): our_cache = LRU_Cache(5) our_cache.set(1, 1) our_cache.set(2, 2) our_cache.set(3, 3) our_cache.set(4, 4) #Move items in the cache, so that they are positioned closer to the top if the recently used list our_cache.get(1) our_cache.get(2) our_cache.set(5, 5) our_cache.set(6, 6) self.assertEqual( our_cache.get(3), -1 ) # returns -1 because the cache reached it's capacity and 3 was the least recently used entry self.assertEqual(list(our_cache.cacheStorageMap.keys()), [1, 2, 4, 5, 6]) # [1, 2, 4, 5, 6]
def test_set_should_remove_lru_node_when_overcapacity(self): """ LRU_Cache::set() should remove the LRU node when overcapacity. """ # Set up. cache = LRU_Cache(4) test_data = { 'udacity': 1, 'python': 2, 100: 'hello world', 200: 'algorithms' } for key, value in test_data.items(): cache.set(key, value) # Check the before states. self.assertEqual(cache.hashtable['udacity'], cache.tail.prev) self.assertEqual(len(cache.hashtable), cache.capacity) cache.set(300, 'overcapacity') # Test that the previous last node has been removed. self.assertEqual(len(cache.hashtable), cache.capacity) self.assertFalse('udacity' in cache.hashtable) self.assertEqual(-1, cache.get('udacity')) self.assertEqual(cache.hashtable['python'], cache.tail.prev) # Retest each node in the linked list. node = cache.head.next self.assertEqual(cache.hashtable[300], node) self.assertEqual(cache.hashtable[200], node.next) self.assertEqual(cache.hashtable[100], node.next.next) self.assertEqual(cache.hashtable['python'], node.next.next.next) # Clean up. cache.clear()
def test_empty_cache(self): our_cache = LRU_Cache(5) result = our_cache.get(1) self.assertEqual(result, -1)
def test_invalid_element(self): our_cache = LRU_Cache(2) our_cache.set(1, 1) self.assertEqual(our_cache.get(9), -1)