summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_functools.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-08-15 03:30:45 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-08-15 03:30:45 (GMT)
commitf309828175e8db1b858d31dda23bbd984940a08a (patch)
tree582fef3dcf80cba6db31d1a239d93ea496c7baa2 /Lib/test/test_functools.py
parent0f56e90f0582a4171a9e3e1e2ffde2781be4bffd (diff)
downloadcpython-f309828175e8db1b858d31dda23bbd984940a08a.zip
cpython-f309828175e8db1b858d31dda23bbd984940a08a.tar.gz
cpython-f309828175e8db1b858d31dda23bbd984940a08a.tar.bz2
Remove the lfu_cache. Add more tests.
Diffstat (limited to 'Lib/test/test_functools.py')
-rw-r--r--Lib/test/test_functools.py65
1 files changed, 15 insertions, 50 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 4dd6fab..f9e8a97 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -483,73 +483,38 @@ class TestLRU(unittest.TestCase):
self.assertEqual(f.misses, 1)
# test size zero (which means "never-cache")
- f_cnt = 0
@functools.lru_cache(0)
def f():
nonlocal f_cnt
f_cnt += 1
return 20
- self.assertEqual(f(), 20)
- self.assertEqual(f(), 20)
- self.assertEqual(f(), 20)
- self.assertEqual(f_cnt, 3)
+ f_cnt = 0
+ for i in range(5):
+ self.assertEqual(f(), 20)
+ self.assertEqual(f_cnt, 5)
# test size one
- f_cnt = 0
@functools.lru_cache(1)
def f():
nonlocal f_cnt
f_cnt += 1
return 20
- self.assertEqual(f(), 20)
- self.assertEqual(f(), 20)
- self.assertEqual(f(), 20)
+ f_cnt = 0
+ for i in range(5):
+ self.assertEqual(f(), 20)
self.assertEqual(f_cnt, 1)
- def test_lfu(self):
- def orig(x, y):
- return 3*x+y
- f = functools.lfu_cache(maxsize=20)(orig)
-
- domain = range(5)
- for i in range(1000):
- x, y = choice(domain), choice(domain)
- actual = f(x, y)
- expected = orig(x, y)
- self.assertEquals(actual, expected)
- self.assert_(f.hits > f.misses)
- self.assertEquals(f.hits + f.misses, 1000)
-
- f.clear() # test clearing
- self.assertEqual(f.hits, 0)
- self.assertEqual(f.misses, 0)
- f(x, y)
- self.assertEqual(f.hits, 0)
- self.assertEqual(f.misses, 1)
-
- # test size zero (which means "never-cache")
- f_cnt = 0
- @functools.lfu_cache(0)
- def f():
+ # test size two
+ @functools.lru_cache(2)
+ def f(x):
nonlocal f_cnt
f_cnt += 1
- return 20
- self.assertEqual(f(), 20)
- self.assertEqual(f(), 20)
- self.assertEqual(f(), 20)
- self.assertEqual(f_cnt, 3)
-
- # test size one
+ return x*10
f_cnt = 0
- @functools.lfu_cache(1)
- def f():
- nonlocal f_cnt
- f_cnt += 1
- return 20
- self.assertEqual(f(), 20)
- self.assertEqual(f(), 20)
- self.assertEqual(f(), 20)
- self.assertEqual(f_cnt, 1)
+ for x in 7, 9, 7, 9, 7, 9, 8, 8, 8, 9, 9, 9, 8, 8, 8, 7:
+ # * * * *
+ self.assertEqual(f(x), x*10)
+ self.assertEqual(f_cnt, 4)
def test_main(verbose=None):
test_classes = (