File size: 3,080 Bytes
2c3c408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from __future__ import annotations
from __future__ import unicode_literals

import pytest

from textual._cache import LRUCache


def test_lru_cache():
    cache = LRUCache(3)

    # insert some values
    cache["foo"] = 1
    cache["bar"] = 2
    cache["baz"] = 3
    assert "foo" in cache
    assert "bar" in cache
    assert "baz" in cache

    #  Cache size is 3, so the following should kick oldest one out
    cache["egg"] = 4
    assert "foo" not in cache
    assert "egg" in cache

    # cache is now full
    # look up two keys
    cache["bar"]
    cache["baz"]

    # Insert a new value
    cache["eggegg"] = 5
    assert len(cache) == 3
    # Check it kicked out the 'oldest' key
    assert "egg" not in cache
    assert "eggegg" in cache


def test_lru_cache_get():
    cache = LRUCache(3)

    # insert some values
    cache["foo"] = 1
    cache["bar"] = 2
    cache["baz"] = 3
    assert "foo" in cache

    #  Cache size is 3, so the following should kick oldest one out
    cache["egg"] = 4
    # assert len(cache) == 3
    assert cache.get("foo") is None
    assert "egg" in cache

    # cache is now full
    # look up two keys
    cache.get("bar")
    cache.get("baz")

    # Insert a new value
    cache["eggegg"] = 5
    # Check it kicked out the 'oldest' key
    assert "egg" not in cache
    assert "eggegg" in cache


def test_lru_cache_mapping():
    """Test cache values can be set and read back."""
    cache = LRUCache(3)
    cache["foo"] = 1
    cache.set("bar", 2)
    cache.set("baz", 3)
    assert cache["foo"] == 1
    assert cache["bar"] == 2
    assert cache.get("baz") == 3


def test_lru_cache_clear():
    cache = LRUCache(3)
    assert len(cache) == 0
    cache["foo"] = 1
    assert "foo" in cache
    assert len(cache) == 1
    cache.clear()
    assert "foo" not in cache
    assert len(cache) == 0


def test_lru_cache_bool():
    cache = LRUCache(3)
    assert not cache
    cache["foo"] = "bar"
    assert cache


@pytest.mark.parametrize(
    "keys,expected",
    [
        ((), ()),
        (("foo",), ("foo",)),
        (("foo", "bar"), ("foo", "bar")),
        (("foo", "bar", "baz"), ("foo", "bar", "baz")),
        (("foo", "bar", "baz", "egg"), ("bar", "baz", "egg")),
        (("foo", "bar", "baz", "egg", "bob"), ("baz", "egg", "bob")),
    ],
)
def test_lru_cache_evicts(keys: list[str], expected: list[str]):
    """Test adding adding additional values evicts oldest key"""
    cache = LRUCache(3)
    for value, key in enumerate(keys):
        cache[key] = value
    assert tuple(cache.keys()) == expected


@pytest.mark.parametrize(
    "keys,expected_len",
    [
        ((), 0),
        (("foo",), 1),
        (("foo", "bar"), 2),
        (("foo", "bar", "baz"), 3),
        (("foo", "bar", "baz", "egg"), 3),
        (("foo", "bar", "baz", "egg", "bob"), 3),
    ],
)
def test_lru_cache_len(keys: list[str], expected_len: int):
    """Test adding adding additional values evicts oldest key"""
    cache = LRUCache(3)
    for value, key in enumerate(keys):
        cache[key] = value
    assert len(cache) == expected_len