diff options
author | Jay Hutchinson <jlhutch@gmail.com> | 2010-08-24 20:51:21 -0500 |
---|---|---|
committer | Jay Hutchinson <jlhutch@gmail.com> | 2010-08-24 20:51:21 -0500 |
commit | 1ab7e96778db3daa0f343946d91136c74147d2a0 (patch) | |
tree | 0795d2f2fef5ee89cfaeb1e366da5383fc1b8c01 | |
parent | aa546823dffa0fcaabdbcf32318d484383a99763 (diff) | |
download | pylru-1ab7e96778db3daa0f343946d91136c74147d2a0.tar.gz |
Fixed a few bugs and added some testing code.
-rw-r--r-- | lru.py | 25 | ||||
-rw-r--r-- | test.py | 45 |
2 files changed, 54 insertions, 16 deletions
@@ -263,7 +263,6 @@ class lruwrap(object): self.store.clear() def __contains__(self, key): - if key in self.cache: return True if key in self.store: @@ -292,17 +291,17 @@ class lruwrap(object): class lrudecorator(object): - def __init__(self, func, size): - self.func = func + def __init__(self, size): self.cache = lrucache(size) - def __call__(self, *args, **kwargs): - try: - value = self.cache[(args, kwargs)] - except KeyError: - pass - - value = self.func(*args, **kwargs) - self.cache[(args, kwargs)] = value - return value - + def __call__(self, func): + def wrapped(*args): # XXX What about kwargs + try: + value = self.cache[args] + except KeyError: + pass + + value = func(*args) + self.cache[args] = value + return value + return wrapped @@ -1,6 +1,6 @@ -from lru import lrucache +from lru import * def _selftest(): @@ -94,11 +94,50 @@ def testb(): for i in range(len(vect)): a[vect[i]] = 0 - + + +def wraptest(): + import random + + q = dict() + x = lruwrap(q, 32) + for i in range(256): + a = random.randint(0, 256) + b = random.randint(0, 256) + + x[a] = b + + for i in range(512): + a = random.randint(0, 256) + tmp1 = None + tmp2 = None + try: + tmp1 = x[a] + except KeyError: + tmp1 = None + + try: + tmp2 = q[a] + except KeyError: + tmp2 = None + + assert tmp1 == tmp2 + +@lrudecorator(14) +def cube(x): + return x*x*x if __name__ == '__main__': - import random + + wraptest() + + for i in range(300): + x = random.randint(0, 25) + assert cube(x) == x**3 + + + a = lrucache(20) b = simplelrucache(20) |