mirror of
https://github.com/harivansh-afk/veet-code.git
synced 2026-04-15 12:03:54 +00:00
problems
This commit is contained in:
parent
76062021e9
commit
c548626d7f
13 changed files with 570 additions and 1 deletions
86
problems/hard/lru-cache/tests.py
Normal file
86
problems/hard/lru-cache/tests.py
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
"""Tests for lru-cache."""
|
||||
import pytest
|
||||
from solution import LRUCache
|
||||
|
||||
|
||||
class TestBasicCases:
|
||||
"""Test basic functionality with typical inputs."""
|
||||
|
||||
def test_basic_put_get(self):
|
||||
"""Test basic put and get operations."""
|
||||
cache = LRUCache(2)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
assert cache.get("a") == 1
|
||||
assert cache.get("b") == 2
|
||||
|
||||
def test_eviction_lru(self):
|
||||
"""Test that least recently used is evicted."""
|
||||
cache = LRUCache(2)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
cache.get("a") # a is now most recent
|
||||
cache.put("c", 3) # b should be evicted
|
||||
assert cache.get("b") == -1
|
||||
assert cache.get("a") == 1
|
||||
assert cache.get("c") == 3
|
||||
|
||||
def test_update_existing_key(self):
|
||||
"""Test updating an existing key."""
|
||||
cache = LRUCache(2)
|
||||
cache.put("a", 1)
|
||||
cache.put("a", 10)
|
||||
assert cache.get("a") == 10
|
||||
|
||||
|
||||
class TestEdgeCases:
|
||||
"""Test edge cases and boundary conditions."""
|
||||
|
||||
def test_capacity_one(self):
|
||||
"""Test with capacity of 1."""
|
||||
cache = LRUCache(1)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
assert cache.get("a") == -1
|
||||
assert cache.get("b") == 2
|
||||
|
||||
def test_get_nonexistent(self):
|
||||
"""Test getting a key that doesn't exist."""
|
||||
cache = LRUCache(2)
|
||||
assert cache.get("missing") == -1
|
||||
|
||||
def test_put_updates_recency(self):
|
||||
"""Test that put on existing key updates recency."""
|
||||
cache = LRUCache(2)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
cache.put("a", 10) # a is now most recent
|
||||
cache.put("c", 3) # b should be evicted
|
||||
assert cache.get("b") == -1
|
||||
assert cache.get("a") == 10
|
||||
|
||||
def test_many_operations(self):
|
||||
"""Test a sequence of many operations."""
|
||||
cache = LRUCache(3)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
cache.put("c", 3)
|
||||
cache.get("a")
|
||||
cache.put("d", 4) # evicts b
|
||||
cache.put("e", 5) # evicts c
|
||||
assert cache.get("a") == 1
|
||||
assert cache.get("b") == -1
|
||||
assert cache.get("c") == -1
|
||||
assert cache.get("d") == 4
|
||||
assert cache.get("e") == 5
|
||||
|
||||
def test_get_updates_recency(self):
|
||||
"""Test that get updates the access order."""
|
||||
cache = LRUCache(2)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
cache.get("a") # touch a
|
||||
cache.put("c", 3) # evicts b, not a
|
||||
assert cache.get("a") == 1
|
||||
assert cache.get("b") == -1
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue