Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions tests/test_cache_clear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# SPDX-FileCopyrightText: 2026 Alec Delaney
# SPDX-FileCopyrightText: Python Software Foundation
#
# SPDX-License-Identifier: MIT
# SPDX-License-Identifier: PSF-2.0

"""Tests for cache_clear method."""

from circuitpython_functools import cache, lru_cache

# Factorial example adapted from CPython documentation

TOTAL_CALLS = 0


def test_cache_cache_clear():
"""Tests the automatically attached cache_clear method works."""
global TOTAL_CALLS # noqa: PLW0603

@cache
def factorial(n):
global TOTAL_CALLS # noqa: PLW0603
TOTAL_CALLS += 1
return n * factorial(n=n - 1) if n else 1

assert TOTAL_CALLS == 0 # noqa: PLR2004

_ = factorial(n=10)
assert TOTAL_CALLS == 11 # noqa: PLR2004

factorial.cache_clear()

_ = factorial(n=10)
assert TOTAL_CALLS == 22 # noqa: PLR2004

factorial.cache_clear()

TOTAL_CALLS = 0


def test_cache_cache_clear_empty():
"""Tests the automatically attached cache_clear method works."""
global TOTAL_CALLS # noqa: PLW0603

@cache
def factorial(n):
global TOTAL_CALLS # noqa: PLW0603
TOTAL_CALLS += 1
return n * factorial(n=n - 1) if n else 1

assert TOTAL_CALLS == 0 # noqa: PLR2004

factorial.cache_clear()

assert TOTAL_CALLS == 0

TOTAL_CALLS = 0


def test_lru_cache_cache_clear():
"""Tests the automatically attached cache_clear method works."""
global TOTAL_CALLS # noqa: PLW0603

@lru_cache
def factorial(n):
global TOTAL_CALLS # noqa: PLW0603
TOTAL_CALLS += 1
return n * factorial(n=n - 1) if n else 1

assert TOTAL_CALLS == 0 # noqa: PLR2004

_ = factorial(n=10)
assert TOTAL_CALLS == 11 # noqa: PLR2004

factorial.cache_clear()

_ = factorial(n=10)
assert TOTAL_CALLS == 22 # noqa: PLR2004

factorial.cache_clear()

TOTAL_CALLS = 0


def test_lru_cache_cache_clear_empty():
"""Tests the automatically attached cache_clear method works."""
global TOTAL_CALLS # noqa: PLW0603

@lru_cache
def factorial(n):
global TOTAL_CALLS # noqa: PLW0603
TOTAL_CALLS += 1
return n * factorial(n=n - 1) if n else 1

assert TOTAL_CALLS == 0 # noqa: PLR2004

factorial.cache_clear()

assert TOTAL_CALLS == 0

TOTAL_CALLS = 0
25 changes: 1 addition & 24 deletions tests/test_lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import pytest

from circuitpython_functools import _lru_cache_records, lru_cache
from circuitpython_functools import lru_cache

# Factorial example adapted from CPython documentation

Expand Down Expand Up @@ -115,29 +115,6 @@ def factorial(*, n):
TOTAL_CALLS = 0


def test_lru_cache_cache_clear():
"""Tests the automatically attached cache_clear method works."""
global TOTAL_CALLS # noqa: PLW0603

@lru_cache
def factorial(n):
global TOTAL_CALLS # noqa: PLW0603
TOTAL_CALLS += 1
return n * factorial(n=n - 1) if n else 1

assert TOTAL_CALLS == 0 # noqa: PLR2004

_ = factorial(n=10)
assert TOTAL_CALLS == 11 # noqa: PLR2004

factorial.cache_clear()

_ = factorial(n=10)
assert TOTAL_CALLS == 22 # noqa: PLR2004

TOTAL_CALLS = 0


def test_lru_cache_typed_error_args():
"""Tests that lru_cache raises an error if "typed" given as arg."""
with pytest.raises(NotImplementedError):
Expand Down