Exemplo n.º 1
0
def test_resize():
    cache = LRUCache(maxsize=2)
    assert cache.maxsize == 2
    cache['w'] = 0
    cache['x'] = 1
    cache['y'] = 2
    assert list(cache.items()) == [('x', 1), ('y', 2)]
    cache.maxsize = 10
    cache['z'] = 3
    assert list(cache.items()) == [('x', 1), ('y', 2), ('z', 3)]
    cache.maxsize = 1
    assert list(cache.items()) == [('z', 3)]

    with pytest.raises(ValueError):
        cache.maxsize = -1
Exemplo n.º 2
0
def test_simple():
    cache = LRUCache(maxsize=2)
    cache['x'] = 1
    cache['y'] = 2

    assert cache['x'] == 1
    assert cache['y'] == 2
    assert len(cache) == 2
    assert dict(cache) == {'x': 1, 'y': 2}
    assert list(cache.keys()) == ['x', 'y']
    assert list(cache.items()) == [('x', 1), ('y', 2)]

    cache['z'] = 3
    assert len(cache) == 2
    assert list(cache.items()) == [('y', 2), ('z', 3)]
Exemplo n.º 3
0
def test_resize():
    cache = LRUCache(maxsize=2)
    assert cache.maxsize == 2
    cache["w"] = 0
    cache["x"] = 1
    cache["y"] = 2
    assert list(cache.items()) == [("x", 1), ("y", 2)]
    cache.maxsize = 10
    cache["z"] = 3
    assert list(cache.items()) == [("x", 1), ("y", 2), ("z", 3)]
    cache.maxsize = 1
    assert list(cache.items()) == [("z", 3)]

    with pytest.raises(ValueError):
        cache.maxsize = -1
Exemplo n.º 4
0
def test_simple():
    cache = LRUCache(maxsize=2)
    cache["x"] = 1
    cache["y"] = 2

    assert cache["x"] == 1
    assert cache["y"] == 2
    assert len(cache) == 2
    assert dict(cache) == {"x": 1, "y": 2}
    assert list(cache.keys()) == ["x", "y"]
    assert list(cache.items()) == [("x", 1), ("y", 2)]

    cache["z"] = 3
    assert len(cache) == 2
    assert list(cache.items()) == [("y", 2), ("z", 3)]
Exemplo n.º 5
0
def test_update_priority():
    cache = LRUCache(maxsize=2)
    cache['x'] = 1
    cache['y'] = 2
    assert list(cache) == ['x', 'y']
    assert 'x' in cache  # contains
    assert list(cache) == ['y', 'x']
    assert cache['y'] == 2  # getitem
    assert list(cache) == ['x', 'y']
    cache['x'] = 3  # setitem
    assert list(cache.items()) == [('y', 2), ('x', 3)]
Exemplo n.º 6
0
def test_update_priority():
    cache = LRUCache(maxsize=2)
    cache["x"] = 1
    cache["y"] = 2
    assert list(cache) == ["x", "y"]
    assert "x" in cache  # contains
    assert list(cache) == ["y", "x"]
    assert cache["y"] == 2  # getitem
    assert list(cache) == ["x", "y"]
    cache["x"] = 3  # setitem
    assert list(cache.items()) == [("y", 2), ("x", 3)]