-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cache.py
58 lines (44 loc) · 1.44 KB
/
test_cache.py
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
'''
Created on 15-Mar-2012
@author: dushyant
'''
from cache import Cache
SIZE = 10
TEST_SIZE = 15
class TestCache():
def __init__(self):
self.cache = Cache(SIZE)
def test_read_write(self):
#basic write and read
self.cache.write("test_key", "test_value")
assert 'test_value' == self.cache.beginRead("test_key")
#beginRead: read key not present
try:
self.cache.beginRead("key_not_present")
assert False
except:
assert True
#endRead: read key not present
try:
self.cache.endRead("key_not_present")
assert False
except:
assert True
def test_eviction(self):
""" To check an entry is never evicted once beginRead is called """
self.cache.write("test_key", "test_value")
value = self.cache.beginRead("test_key")
for i in range(TEST_SIZE):
self.cache.write(i, i)
try:
#if evicted, endRead should throw and exception
self.cache.endRead("test_key")
assert True
except:
assert False
def test_size(self):
""" size of hash_db should never exceed specified size of cache """
#size
for i in range(TEST_SIZE):
self.cache.write(i, i)
assert len(self.cache.hash_db) <= SIZE