File: test_lfu_cache.py

package info (click to toggle)
hishel 0.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,548 kB
  • sloc: python: 6,387; sh: 13; makefile: 4
file content (91 lines) | stat: -rw-r--r-- 1,620 bytes parent folder | download | duplicates (2)
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
import pytest

from hishel import LFUCache


def test_lfu_cache():
    cache: LFUCache[int, int] = LFUCache(2)

    cache.put(1, 2)
    a = cache.get(1)

    assert a == 2


def test_lfu_cache_delete():
    cache: LFUCache[int, int] = LFUCache(2)

    cache.put(1, 2)
    cache.put(3, 4)
    cache.put(5, 6)
    cache.get(3)
    cache.get(3)

    with pytest.raises(KeyError):
        cache.get(1)


def test_lfu_cache_invalid_capacity():
    with pytest.raises(ValueError, match="Capacity must be positive"):
        LFUCache(0)


def test_lfu_cache_delete_least_frequently():
    cache: LFUCache[int, int] = LFUCache(2)

    cache.put(1, 10)
    cache.put(2, 10)

    cache.get(1)
    cache.put(3, 10)

    with pytest.raises(KeyError):
        cache.get(2)

    cache: LFUCache[int, int] = LFUCache(2)  # type: ignore[no-redef]

    cache.put(1, 10)
    cache.put(2, 10)

    cache.get(2)
    cache.put(3, 10)

    with pytest.raises(KeyError):
        cache.get(1)


def test_lfu_cache_remove_key():
    cache: LFUCache[int, int] = LFUCache(2)

    cache.put(1, 10)
    cache.put(2, 10)

    cache.remove_key(1)

    with pytest.raises(KeyError):
        cache.get(1)

    cache.put(1, 10)
    cache.get(1)

    assert cache.min_freq == 1
    cache.remove_key(2)
    assert cache.min_freq == 2


def test_lfu_cache_put_existing():
    cache: LFUCache[int, int] = LFUCache(2)

    cache.put(1, 10)
    cache.put(2, 10)

    cache.put(1, 20)

    assert cache.get(1) == 20

    cache: LFUCache[int, int] = LFUCache(2)  # type: ignore[no-redef]

    cache.put(1, 10)
    cache.put(1, 20)

    assert cache.get(1) == 20