def test_least_recent_changed_then_evicted(): cache = LRUCache(3) cache.set(2, 12) cache.set(4, 14) cache.set(5, 15) # key=2 is LRU assert cache.get(2) == 12 # key=4 is LRU cache.set(7, 17) # key=4 is evicted assert cache.get(2) == 12 assert cache.get(4) == -1
def test_least_recent_evicted(): cache = LRUCache(3) cache.set(2, 12) cache.set(4, 14) cache.set(5, 15) # key=2 is LRU assert cache.get(4) == 14 # key=2 is LRU cache.set(7, 17) # key=2 is evicted assert cache.get(2) == -1 assert cache.get(4) == 14
def test_lru_cache(capacity, input, expected): sut = LRUCache(capacity) get_expectations = deque(expected) for args in input: if len(args) > 1: sut.put(*args) else: assert sut.get(*args) == get_expectations.popleft() assert len(get_expectations) == 0
def test_add_capacity_limit(): cache = LRUCache(3) assert cache.capacity == 3 cache.set(3, 5) cache.set(2, 6) cache.set(1, 7) cache.set(0, 8) cache.set(11, 12) cache.set(13, 14) cache.set(15, 16) assert cache.capacity == 3
def test_add_capacity_override(): cache = LRUCache(4) assert cache.capacity == 4 cache.set(3, 3) cache.set(3, 3) cache.set(3, 3) cache.set(3, 3) cache.set(3, 3) cache.set(3, 3) cache.set(3, 3) cache.set(3, 3) assert cache.capacity == 4
def test_longer_sequence2(): limit = pow(2, 10) cache = LRUCache(limit) for i in range(1, limit + 1): cache.set(i, i + 100) assert cache.capacity == limit assert cache.get(100000) == -1 cache.set(10100, -201) cache.set(10200, -202) assert cache.get(10100) == -201 assert cache.get(1) == -1 assert cache.get(2) == -1 assert cache.get(3) == 103
def test_longer_sequence(): cache = LRUCache(100) for i in range(1, 101): cache.set(i, i + 100) assert cache.capacity == 100 assert cache.get(100) == 200 cache.set(101, 201) cache.set(102, 202) assert cache.get(1) == -1 assert cache.get(2) == -1 assert cache.get(3) == 103
def test_capacity_max_int_op(): limit = pow(2, 31) - 1 cache = LRUCache(limit) for i in range(1000, 2000): cache.set(i, i / 2 + 3) for j in range(1500, 2500): cache.get(j) assert cache.capacity == pow(2, 31) - 1 assert cache.get(1010) == 508
def test_capacity_empty_get3(): cache = LRUCache(0) assert cache.capacity == 0 for i in range(100): cache.set(i, i + 1) for i in range(100): cache.get(i) assert cache.capacity == 0 assert cache.get(3) == -1
def test_many_set_op_util(cap): capacity = cap cache = LRUCache(capacity) cache_on = LRUCacheOn(capacity) cache_time = np.array(get_exec_time(cache, 5, capacity)) cache_on_time = np.array(get_exec_time(cache_on, 5, capacity)) print("") print("using cache, capacity=", capacity, "average exec=", np.mean(cache_time), "standad dev=", np.std(cache_time)) print("using cache, capacity=", capacity, "average exec=", np.mean(cache_on_time), "standad dev=", np.std(cache_on_time))
def test_capacity_neg(): with pytest.raises(ValueError): # detect a bug assert LRUCache(-1)
def test_capacity_max_int(): limit = pow(2, 31) - 1 cache = LRUCache(limit) assert cache.capacity == pow(2, 31) - 1
def test_capacity_empty(): cache = LRUCache(0) assert cache.capacity == 0
def test_capacity_empty_get2(): cache = LRUCache(0) assert cache.capacity == 0 cache.set(3, 33) assert cache.get(3) == -1
def test_long_sequence(): cache = LRUCache(3) cache.set(2, 22) cache.set(1, 11) cache.set(5, 55) assert cache.get(3) == -1 cache.set(7, 77) assert cache.get(2) == -1 assert cache.get(7) == 77 cache.set(8, 88) assert cache.get(1) == -1 assert cache.get(5) == 55 cache.set(8, -88) assert cache.get(5) == 55 assert cache.get(8) == -88
def test_capacity_empty_set2(): cache = LRUCache(0) assert cache.capacity == 0 for i in range(100): cache.set(i, i + 1) assert cache.capacity == 0
def test_capacity_empty_set(): cache = LRUCache(0) assert cache.capacity == 0 cache.set(3, 5) # detect a bug assert cache.capacity == 0
def test_add_capacity_changes_empty(): cache = LRUCache(0) assert cache.capacity == 0
def test_add_capacity_changes_n(): cache = LRUCache(5) assert cache.capacity == 5 cache.set(3, 5) assert cache.capacity == 5
def test_capacity_neg2(): with pytest.raises(ValueError): assert LRUCache(-3)
def test_capacity_empty_get(): cache = LRUCache(0) assert cache.capacity == 0 assert cache.get(3) == -1
def test_capacity_neg3(): cache_size = -pow(2, 31) with pytest.raises(ValueError): assert LRUCache(cache_size)
def test_get_key_not_exist(): cache = LRUCache(3) cache.set(3, 335) assert cache.get(4) == -1