mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
moar tests
This commit is contained in:
@@ -14,7 +14,7 @@ where the overhead of the cache is a small fraction of the total processing time
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import cast, Dict, Generic, KeysView, TypeVar, overload
|
from typing import Dict, Generic, KeysView, TypeVar, overload
|
||||||
|
|
||||||
CacheKey = TypeVar("CacheKey")
|
CacheKey = TypeVar("CacheKey")
|
||||||
CacheValue = TypeVar("CacheValue")
|
CacheValue = TypeVar("CacheValue")
|
||||||
@@ -71,7 +71,7 @@ class LRUCache(Generic[CacheKey, CacheValue]):
|
|||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return (
|
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:
|
def grow(self, maxsize: int) -> None:
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ from textual._cache import FIFOCache, LRUCache
|
|||||||
def test_lru_cache():
|
def test_lru_cache():
|
||||||
cache = LRUCache(3)
|
cache = LRUCache(3)
|
||||||
|
|
||||||
|
assert str(cache) == "<LRUCache maxsize=3 hits=0 misses=0>"
|
||||||
|
|
||||||
# insert some values
|
# insert some values
|
||||||
cache["foo"] = 1
|
cache["foo"] = 1
|
||||||
cache["bar"] = 2
|
cache["bar"] = 2
|
||||||
@@ -35,6 +37,38 @@ def test_lru_cache():
|
|||||||
assert "eggegg" in 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():
|
def test_lru_cache_get():
|
||||||
cache = LRUCache(3)
|
cache = LRUCache(3)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user