Skip to content

Commit becdd11

Browse files
committed
test: add edge case and complex value tests for LruCache
- Verify that updating existing keys correctly refreshes their MRU status - Test edge case for a cache with a limit of one - Add test for retrieving non-existent keys (returning None) - Confirm support for complex data types like lists and dictionaries
1 parent db31358 commit becdd11

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎Sprint-2/implement_lru_cache/lru_cache_test.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,40 @@ def test_eviction_order_after_gets(self):
5454
self.assertEqual(cache.get("a"), 1)
5555
self.assertEqual(cache.get("c"), 3)
5656

57+
def test_update_existing_key(self):
58+
cache = LruCache(limit=2)
59+
cache.set("a", 1)
60+
cache.set("b", 2)
61+
62+
# Update "a" - it should stay in the cache but change value
63+
cache.set("a", 100)
64+
self.assertEqual(cache.get("a"), 100)
65+
66+
# Adding "c" should now evict "b", because "a" was recently updated
67+
cache.set("c", 3)
68+
self.assertIsNone(cache.get("b"))
69+
self.assertEqual(cache.get("a"), 100)
70+
71+
def test_limit_of_one(self):
72+
cache = LruCache(limit=1)
73+
cache.set("a", 1)
74+
self.assertEqual(cache.get("a"), 1)
75+
76+
cache.set("b", 2) # This should immediately kick out "a"
77+
self.assertIsNone(cache.get("a"))
78+
self.assertEqual(cache.get("b"), 2)
79+
80+
def test_get_non_existent_key(self):
81+
cache = LruCache(limit=5)
82+
# Testing "None" return for keys never added
83+
self.assertIsNone(cache.get("missing_key"))
84+
85+
def test_complex_values(self):
86+
cache = LruCache(limit=2)
87+
# Testing that we can store lists or dictionaries, not just strings
88+
cache.set("list", [1, 2, 3])
89+
self.assertEqual(cache.get("list"), [1, 2, 3])
90+
5791

5892
if __name__ == "__main__":
5993
unittest.main()

0 commit comments

Comments
 (0)