| 1 |
|
| 2 |
from pylru import * |
| 3 |
import random |
| 4 |
|
| 5 |
# This tests PyLRU by fuzzing it with random operations, then checking the |
| 6 |
# results against another, simpler, LRU cache implementation. |
| 7 |
|
| 8 |
class simplelrucache: |
| 9 |
|
| 10 |
def __init__(self, size): |
| 11 |
|
| 12 |
# Initialize the cache as empty. |
| 13 |
self.cache = [] |
| 14 |
self.size = size |
| 15 |
|
| 16 |
def __contains__(self, key): |
| 17 |
|
| 18 |
for x in self.cache: |
| 19 |
if x[0] == key: |
| 20 |
return True |
| 21 |
|
| 22 |
return False |
| 23 |
|
| 24 |
|
| 25 |
def __getitem__(self, key): |
| 26 |
|
| 27 |
for i in range(len(self.cache)): |
| 28 |
x = self.cache[i] |
| 29 |
if x[0] == key: |
| 30 |
del self.cache[i] |
| 31 |
self.cache.append(x) |
| 32 |
return x[1] |
| 33 |
|
| 34 |
raise KeyError |
| 35 |
|
| 36 |
|
| 37 |
def __setitem__(self, key, value): |
| 38 |
|
| 39 |
for i in range(len(self.cache)): |
| 40 |
x = self.cache[i] |
| 41 |
if x[0] == key: |
| 42 |
x[1] = value |
| 43 |
del self.cache[i] |
| 44 |
self.cache.append(x) |
| 45 |
return |
| 46 |
|
| 47 |
if len(self.cache) == self.size: |
| 48 |
self.cache = self.cache[1:] |
| 49 |
|
| 50 |
self.cache.append([key, value]) |
| 51 |
|
| 52 |
|
| 53 |
def __delitem__(self, key): |
| 54 |
|
| 55 |
for i in range(len(self.cache)): |
| 56 |
if self.cache[i][0] == key: |
| 57 |
del self.cache[i] |
| 58 |
return |
| 59 |
|
| 60 |
raise KeyError |
| 61 |
|
| 62 |
def resize(self, x=None): |
| 63 |
assert x > 0 |
| 64 |
self.size = x |
| 65 |
if x < len(self.cache): |
| 66 |
del self.cache[:len(self.cache) - x] |
| 67 |
|
| 68 |
|
| 69 |
def test(a, b, c, d, verify): |
| 70 |
|
| 71 |
for i in range(1000): |
| 72 |
x = random.randint(0, 512) |
| 73 |
y = random.randint(0, 512) |
| 74 |
|
| 75 |
a[x] = y |
| 76 |
b[x] = y |
| 77 |
verify(c, d) |
| 78 |
|
| 79 |
for i in range(1000): |
| 80 |
x = random.randint(0, 512) |
| 81 |
if x in a: |
| 82 |
assert x in b |
| 83 |
z = a[x] |
| 84 |
z += b[x] |
| 85 |
else: |
| 86 |
assert x not in b |
| 87 |
verify(c, d) |
| 88 |
|
| 89 |
for i in range(256): |
| 90 |
x = random.randint(0, 512) |
| 91 |
if x in a: |
| 92 |
assert x in b |
| 93 |
del a[x] |
| 94 |
del b[x] |
| 95 |
else: |
| 96 |
assert x not in b |
| 97 |
verify(c, d) |
| 98 |
|
| 99 |
|
| 100 |
def testcache(): |
| 101 |
def verify(a, b): |
| 102 |
q = [] |
| 103 |
z = a.head |
| 104 |
for j in range(len(a.table)): |
| 105 |
q.append([z.key, z.value]) |
| 106 |
z = z.next |
| 107 |
|
| 108 |
assert q == b.cache[::-1] |
| 109 |
|
| 110 |
q2 = [] |
| 111 |
for x, y in q: |
| 112 |
q2.append((x, y)) |
| 113 |
|
| 114 |
assert list(a.items()) == q2 |
| 115 |
assert list(zip(a.keys(), a.values())) == q2 |
| 116 |
assert list(a.keys()) == list(a) |
| 117 |
|
| 118 |
|
| 119 |
a = lrucache(128) |
| 120 |
b = simplelrucache(128) |
| 121 |
verify(a, b) |
| 122 |
test(a, b, a, b, verify) |
| 123 |
|
| 124 |
a.size(71) |
| 125 |
b.resize(71) |
| 126 |
verify(a, b) |
| 127 |
test(a, b, a, b, verify) |
| 128 |
|
| 129 |
a.size(341) |
| 130 |
b.resize(341) |
| 131 |
verify(a, b) |
| 132 |
test(a, b, a, b, verify) |
| 133 |
|
| 134 |
a.size(127) |
| 135 |
b.resize(127) |
| 136 |
verify(a, b) |
| 137 |
test(a, b, a, b, verify) |
| 138 |
|
| 139 |
|
| 140 |
def wraptest(): |
| 141 |
|
| 142 |
def verify(p, x): |
| 143 |
assert p == x.store |
| 144 |
for key, value in x.cache.items(): |
| 145 |
assert x.store[key] == value |
| 146 |
|
| 147 |
tmp = list(x.items()) |
| 148 |
tmp.sort() |
| 149 |
|
| 150 |
tmp2 = list(p.items()) |
| 151 |
tmp2.sort() |
| 152 |
|
| 153 |
assert tmp == tmp2 |
| 154 |
|
| 155 |
p = dict() |
| 156 |
q = dict() |
| 157 |
x = lruwrap(q, 128) |
| 158 |
|
| 159 |
test(p, x, p, x, verify) |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
def wraptest2(): |
| 164 |
|
| 165 |
def verify(p, x): |
| 166 |
for key, value in x.store.items(): |
| 167 |
if key not in x.dirty: |
| 168 |
assert p[key] == value |
| 169 |
|
| 170 |
for key in x.dirty: |
| 171 |
assert x.cache.peek(key) == p[key] |
| 172 |
|
| 173 |
for key, value in x.cache.items(): |
| 174 |
if key not in x.dirty: |
| 175 |
assert x.store[key] == p[key] == value |
| 176 |
|
| 177 |
tmp = list(x.items()) |
| 178 |
tmp.sort() |
| 179 |
|
| 180 |
tmp2 = list(p.items()) |
| 181 |
tmp2.sort() |
| 182 |
|
| 183 |
assert tmp == tmp2 |
| 184 |
|
| 185 |
p = dict() |
| 186 |
q = dict() |
| 187 |
x = lruwrap(q, 128, True) |
| 188 |
|
| 189 |
test(p, x, p, x, verify) |
| 190 |
|
| 191 |
x.sync() |
| 192 |
assert p == q |
| 193 |
|
| 194 |
def wraptest3(): |
| 195 |
|
| 196 |
def verify(p, x): |
| 197 |
for key, value in x.store.items(): |
| 198 |
if key not in x.dirty: |
| 199 |
assert p[key] == value |
| 200 |
|
| 201 |
for key in x.dirty: |
| 202 |
assert x.cache.peek(key) == p[key] |
| 203 |
|
| 204 |
for key, value in x.cache.items(): |
| 205 |
if key not in x.dirty: |
| 206 |
assert x.store[key] == p[key] == value |
| 207 |
|
| 208 |
p = dict() |
| 209 |
q = dict() |
| 210 |
with lruwrap(q, 128, True) as x: |
| 211 |
test(p, x, p, x, verify) |
| 212 |
|
| 213 |
assert p == q |
| 214 |
|
| 215 |
|
| 216 |
@lrudecorator(100) |
| 217 |
def square(x): |
| 218 |
return x*x |
| 219 |
|
| 220 |
def testDecorator(): |
| 221 |
for i in range(1000): |
| 222 |
x = random.randint(0, 200) |
| 223 |
assert square(x) == x*x |
| 224 |
|
| 225 |
|
| 226 |
if __name__ == '__main__': |
| 227 |
|
| 228 |
random.seed() |
| 229 |
|
| 230 |
|
| 231 |
for i in range(20): |
| 232 |
testcache() |
| 233 |
wraptest() |
| 234 |
wraptest2() |
| 235 |
wraptest3() |
| 236 |
testDecorator() |
| 237 |
|
| 238 |
|