moar tests

This commit is contained in:
Will McGugan
2022-12-28 15:44:56 +00:00
parent 331a0ce65d
commit 8113ff8705
2 changed files with 36 additions and 2 deletions

View File

@@ -14,7 +14,7 @@ where the overhead of the cache is a small fraction of the total processing time
from __future__ import annotations
from typing import cast, Dict, Generic, KeysView, TypeVar, overload
from typing import Dict, Generic, KeysView, TypeVar, overload
CacheKey = TypeVar("CacheKey")
CacheValue = TypeVar("CacheValue")
@@ -71,7 +71,7 @@ class LRUCache(Generic[CacheKey, CacheValue]):
def __repr__(self) -> str:
return (
f"<LRUCache maxsize={self._maxsize} hits={self.hits} misses={self.misses}"
f"<LRUCache maxsize={self._maxsize} hits={self.hits} misses={self.misses}>"
)
def grow(self, maxsize: int) -> None:

View File

@@ -9,6 +9,8 @@ from textual._cache import FIFOCache, LRUCache
def test_lru_cache():
cache = LRUCache(3)
assert str(cache) == "<LRUCache maxsize=3 hits=0 misses=0>"
# insert some values
cache["foo"] = 1
cache["bar"] = 2
@@ -35,6 +37,38 @@ def test_lru_cache():
assert "eggegg" in cache
def test_lru_cache_hits():
cache = LRUCache(4)
assert cache.hits == 0
assert cache.misses == 0
try:
cache["foo"]
except KeyError:
assert cache.hits == 0
assert cache.misses == 1
cache["foo"] = 1
assert cache.hits == 0
assert cache.misses == 1
cache["foo"]
cache["foo"]
assert cache.hits == 2
assert cache.misses == 1
cache.get("bar")
assert cache.hits == 2
assert cache.misses == 2
cache.get("foo")
assert cache.hits == 3
assert cache.misses == 2
assert str(cache) == "<LRUCache maxsize=4 hits=3 misses=2>"
def test_lru_cache_get():
cache = LRUCache(3)