diff options
author | Jay Hutchinson <jlhutch@gmail.com> | 2010-09-10 15:31:24 -0500 |
---|---|---|
committer | Jay Hutchinson <jlhutch@gmail.com> | 2010-09-10 15:31:24 -0500 |
commit | 15b4d1a3ffd4076f96f241988b4fc81a24a9cfc1 (patch) | |
tree | dc3bd973fcfdd10f1f6459f97567d4b18917d5f2 /test.py | |
parent | f545ffd3f09bb609e90abbb4dd6fd7dbe56ba231 (diff) | |
download | pylru-15b4d1a3ffd4076f96f241988b4fc81a24a9cfc1.tar.gz |
Improved the test code: test.py
Diffstat (limited to 'test.py')
-rw-r--r-- | test.py | 112 |
1 files changed, 51 insertions, 61 deletions
@@ -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() |