Sub topic 13
Testing the Cache and Its Edge Cases
This chapter assumes you have the Node, DoublyLinkedList, and LRUCache classes from the earlier chapters, saved together in a file named lru.py. It also assumes you know basic unittest.
What to test
An LRU cache can return correct values and still have a broken internal structure. A leaked map entry or a bad pointer may not show up until much later. Good tests therefore check two things:
- Behavior: what
getreturns and which key is evicted. - Structure: whether the map and the list agree with each other.
Helpers that inspect the structure
The helper keys_by_recency walks the list from the front, so you can assert on the exact order. check_invariants verifies that the map and list stay consistent.
import unittest
from lru import LRUCache
def keys_by_recency(cache):
keys = []
node = cache.order.head.next
while node is not cache.order.tail:
keys.append(node.key)
node = node.next
return keys
def check_invariants(cache):
nodes = []
node = cache.order.head.next
while node is not cache.order.tail:
assert node.next.prev is node, "broken prev/next link"
nodes.append(node)
node = node.next
assert len(nodes) == len(cache.map), "map and list sizes differ"
assert len(nodes) <= cache.capacity, "cache over capacity"
for n in nodes:
assert cache.map[n.key] is n, "map points at the wrong node"The edge cases
class LRUCacheTests(unittest.TestCase):
def test_missing_key_returns_none(self):
cache = LRUCache(2)
self.assertIsNone(cache.get("absent"))
check_invariants(cache)
def test_get_promotes_entry(self):
cache = LRUCache(2)
cache.put(1, "a")
cache.put(2, "b")
cache.get(1)
self.assertEqual(keys_by_recency(cache), [1, 2])
cache.put(3, "c")
self.assertIsNone(cache.get(2))
check_invariants(cache)
def test_update_does_not_evict_or_grow(self):
cache = LRUCache(2)
cache.put(1, "a")
cache.put(2, "b")
cache.put(1, "z")
self.assertEqual(cache.get(1), "z")
self.assertEqual(cache.get(2), "b")
self.assertEqual(len(cache.map), 2)
check_invariants(cache)
def test_update_refreshes_recency(self):
cache = LRUCache(2)
cache.put(1, "a")
cache.put(2, "b")
cache.put(1, "z")
self.assertEqual(keys_by_recency(cache), [1, 2])
def test_capacity_one(self):
cache = LRUCache(1)
cache.put(1, "a")
cache.put(2, "b")
self.assertIsNone(cache.get(1))
self.assertEqual(cache.get(2), "b")
cache.put(2, "c")
self.assertEqual(cache.get(2), "c")
check_invariants(cache)
def test_invalid_capacity(self):
for bad in (0, -1):
with self.assertRaises(ValueError):
LRUCache(bad)
def test_many_operations_keep_invariants(self):
cache = LRUCache(3)
for i in range(100):
cache.put(i % 7, i)
cache.get((i * 3) % 7)
check_invariants(cache)
if __name__ == "__main__":
unittest.main()Run it with python test_lru.py. All seven tests should pass.
Why these cases
| Case | What it catches |
|---|---|
| Missing key | A `get` that raises or mutates the list |
| Get promotes | A `get` that forgets `move_to_front` |
| Update existing key | A `put` that evicts needlessly or adds a duplicate node |
| Update refreshes recency | A `put` that changes the value but not the order |
| Capacity 1 | Head and tail sentinels pointing at the same single node |
| Invalid capacity | A cache that could never hold an entry |
| Repeated mixed operations | Pointer errors that only appear after many moves |
Exercise
Write a test showing that a failed lookup does not change the recency order. What should it assert?
Show answer
Fill a capacity-3 cache with keys 1, 2, 3, then call get(99). Assert that keysbyrecency(cache) is still [3, 2, 1] and that check_invariants(cache) passes. A miss must leave both the order and the map untouched.