summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.txt4
-rw-r--r--test.py112
2 files changed, 53 insertions, 63 deletions
diff --git a/README.txt b/README.txt
index 6358c2c..384102b 100644
--- a/README.txt
+++ b/README.txt
@@ -47,7 +47,7 @@ The lrucache takes an optional callback function as a second argument. Since the
# the fixed cache size. But, not before the callback is called to let you
# know.
-Often a cache is used to speed up access to some other low latency object. If that object has a dictionary interface a convieniance wrapper class provided by PyLRU can be used. This class takes as an argument the object you want to wrap and the cache size. It then creates an LRU cache for the object and automatically manages it. For example, imagine you have an object with a dictionary interface that reads/writes its values to and from a remote server. Let us call this object slowDict::
+Often a cache is used to speed up access to some other low latency object. If that object has a dictionary interface a convenience wrapper class provided by PyLRU can be used. This class takes as an argument the object you want to wrap and the cache size. It then creates an LRU cache for the object and automatically manages it. For example, imagine you have an object with a dictionary interface that reads/writes its values to and from a remote server. Let us call this object slowDict::
import pylru
@@ -74,7 +74,7 @@ The programmer is responsible for one thing though. They MUST call sync() when t
# DON'T forget to call sync() when finished
cacheDict.sync()
- To help the programmer with this the lruwrap can be used in a with statement::
+To help the programmer with this the lruwrap can be used in a with statement::
with pylru.lruwrap(slowDict, size, True) as cacheDict
diff --git a/test.py b/test.py
index 27c0c0d..6a7167d 100644
--- a/test.py
+++ b/test.py
@@ -59,17 +59,15 @@ class simplelrucache:
return
-def basictest():
- a = lrucache(128)
- b = simplelrucache(128)
-
- for i in range(500):
+def test(a, b, c, d, verify):
+
+ for i in range(1000):
x = random.randint(0, 512)
y = random.randint(0, 512)
a[x] = y
b[x] = y
- verify(a, b)
+ verify(c, d)
for i in range(1000):
x = random.randint(0, 512)
@@ -79,7 +77,7 @@ def basictest():
z += b[x]
else:
assert x not in b
- verify(a, b)
+ verify(c, d)
for i in range(256):
x = random.randint(0, 512)
@@ -89,92 +87,84 @@ def basictest():
del b[x]
else:
assert x not in b
- verify(a, b)
+ verify(c, d)
+def testcache():
+ def verify(a, b):
+ q = []
+ z = a.head
+ for j in range(len(a.table)):
+ q.append([z.key, z.obj])
+ z = z.next
+
+ assert q == b.cache[::-1]
-
-def verify2(x, q, n):
- for i in range(n):
- tmp1 = None
- tmp2 = None
- try:
- tmp1 = x[i]
- except KeyError:
- tmp1 = None
-
- try:
- tmp2 = q[i]
- except KeyError:
- tmp2 = None
-
- assert tmp1 == tmp2
+ a = lrucache(128)
+ b = simplelrucache(128)
+
+ test(a, b, a, b, verify)
+
def wraptest():
- q = dict()
- x = lruwrap(q, 32)
- for i in range(256):
- a = random.randint(0, 128)
- b = random.randint(0, 256)
- x[a] = b
+ def verify(p, q):
+ assert p == q
- verify2(x, q, 128)
+ p = dict()
+ q = dict()
+ x = lruwrap(q, 128)
+
+ test(p, x, p, q, verify)
+
+
def wraptest2():
+ def verify(x, y):
+ pass
+
+ p = dict()
q = dict()
- x = lruwrap(q, 32, True)
- for i in range(256):
- a = random.randint(0, 128)
- b = random.randint(0, 256)
+ x = lruwrap(q, 128, True)
+
+ test(p, x, None, None, verify)
- x[a] = b
-
x.sync()
- verify2(x, q, 128)
-
+ assert p == q
def wraptest3():
+ def verify(x, y):
+ pass
+
+ p = dict()
q = dict()
- with lruwrap(q, 32, True) as x:
- for i in range(256):
- a = random.randint(0, 128)
- b = random.randint(0, 256)
-
- x[a] = b
-
- verify2(x, q, 128)
-
-
+ with lruwrap(q, 128, True) as x:
+ test(p, x, None, None, verify)
+
+ assert p == q
+
+
@lrudecorator(25)
def square(x):
return x*x
def testDecorator():
- for i in range(500):
- x = random.randint(0, 100)
+ for i in range(1000):
+ x = random.randint(0, 1493)
assert square(x) == x*x
-def verify(a, b):
- q = []
- z = a.head
- for j in range(len(a.table)):
- q.append([z.key, z.obj])
- z = z.next
-
- if q != b.cache[::-1]:
- assert False
+
if __name__ == '__main__':
random.seed()
- basictest()
+ testcache()
wraptest()
wraptest2()
wraptest3()