Exemple #1
0
def test_multiple():
    max_size = 5
    cache = LRUCache(max_size)

    @given(key=st.integers(min_value=-3, max_value=3), value=st.text())
    @settings(max_examples=500)
    @example(key=-42, value=5)
    @example(key=(-42, 13), value=5)
    def test_add_one(key, value):
        initial_size = cache.size
        initial_misses = cache.misses
        initial_hits = cache.hits
        try:
            cache.get(key)
        except KeyError:
            expected_size = initial_size + 1 if initial_size < max_size else \
                max_size
            assert cache.misses == initial_misses + 1
            assert cache.hits == initial_hits
        else:
            expected_size = initial_size
            assert cache.hits == initial_hits + 1
            assert cache.misses == initial_misses
        cache.put(key, value)
        assert cache.size == expected_size
        assert cache.size >= initial_size

    test_add_one()
    assert cache.size > 0
    cache.clear()
    assert cache.size == 0
    assert cache.misses == 0
    assert cache.hits == 0
Exemple #2
0
 def test_put_get(self):
     """Put an item an get it again."""
     key = 1
     item = 'aaa'
     cache = LRUCache(5)
     cache.put(key, item)
     assert item == cache.get(key)
     assert 1 == cache.size
Exemple #3
0
def test_adding_i_s(key, value):
    cache = LRUCache(3)
    assert cache.size == 0
    assert cache.hits == 0
    cache.put(key, value)
    assert cache.size == 1
    assert cache.get(key) == value
    assert cache.misses == 0
    assert cache.hits == 1
Exemple #4
0
 def test_statistics(self):
     """Test hits and _misses."""
     max_size = 3
     cache = LRUCache(max_size)
     for i in range(max_size):
         cache.put(i, str(i))
     assert 0 == cache.hits
     assert 0 == cache.misses
     cache.get(0)
     assert 1 == cache.hits
     assert 0 == cache.misses
     with pytest.raises(KeyError):
         cache.get(42)
     assert 1 == cache.hits
     assert 1 == cache.misses
Exemple #5
0
 def test_clear_cache(self):
     """Test clearing the cache."""
     max_size = 3
     cache = LRUCache(3)
     for i in range(max_size):
         cache.put(i, str(i))
     assert max_size == cache.size
     cache.clear()
     assert 0 == cache.size
Exemple #6
0
 def test_lru_behavior(self):
     """Put more than max_size items and check if the least recently used
     was dropped."""
     max_size = 3
     cache = LRUCache(max_size)
     for i in range(max_size):
         cache.put(i, str(i))
     assert max_size == cache.size
     for i in range(max_size):
         cache.get(i)
     cache.put(max_size, str(max_size))
     assert max_size == cache.size
     cache.get(max_size)
     with pytest.raises(KeyError):
         cache.get(0)
Exemple #7
0
 def test_not_in_cache(self):
     """Try to get an item which is not in cache."""
     cache = LRUCache(5)
     cache.put(1, 'aaa')
     with pytest.raises(KeyError):
         cache.get(2)
Exemple #8
0
 def test_put(self):
     """Just put an item."""
     cache = LRUCache(5)
     assert 0 == cache.size
     cache.put(1, 'aaa')
     assert 1 == cache.size