CoolFace
Apppublic

cactus183/patchbench-dev

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
test_code.py65 linesDownload Raw Back to hard_02
1from solution import LRUCache2 3 4def test_get_returns_value():5    c = LRUCache(2)6    c.put("a", 1)7    assert c.get("a") == 18 9 10def test_get_missing_returns_none():11    c = LRUCache(2)12    assert c.get("missing") is None13 14 15def test_put_stores():16    c = LRUCache(3)17    c.put("x", 10)18    c.put("y", 20)19    assert c.get("x") == 1020    assert c.get("y") == 2021 22 23def test_put_updates_existing():24    c = LRUCache(2)25    c.put("a", 1)26    c.put("a", 2)27    assert c.get("a") == 228 29 30def test_capacity_not_exceeded():31    c = LRUCache(2)32    c.put("a", 1)33    c.put("b", 2)34    c.put("c", 3)35    # One of the first two should have been evicted36    assert len(c.cache) == 237 38 39def test_lru_eviction_basic():40    c = LRUCache(2)41    c.put("a", 1)42    c.put("b", 2)43    # Access "a" to make it recently used44    c.get("a")45    # Insert "c" — should evict "b" (least recently used), not "a"46    c.put("c", 3)47    assert c.get("a") == 1, "a should still be in cache (was recently accessed)"48    assert c.get("b") is None, "b should have been evicted"49    assert c.get("c") == 350 51 52def test_get_marks_as_recent():53    c = LRUCache(3)54    c.put("a", 1)55    c.put("b", 2)56    c.put("c", 3)57    # Access "a" — it should now be most recent58    c.get("a")59    # Insert "d" — should evict "b" (oldest not-recently-accessed)60    c.put("d", 4)61    assert c.get("a") == 1, "a should survive (was accessed recently)"62    assert c.get("b") is None, "b should be evicted (least recently used)"63    assert c.get("c") == 364    assert c.get("d") == 465