mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Support cache sizes.
This commit is contained in:
parent
a893927491
commit
0f56e90f05
2 changed files with 49 additions and 1 deletions
|
@ -482,6 +482,30 @@ class TestLRU(unittest.TestCase):
|
|||
self.assertEqual(f.hits, 0)
|
||||
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)
|
||||
|
||||
# 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)
|
||||
self.assertEqual(f_cnt, 1)
|
||||
|
||||
def test_lfu(self):
|
||||
def orig(x, y):
|
||||
return 3*x+y
|
||||
|
@ -503,6 +527,30 @@ class TestLRU(unittest.TestCase):
|
|||
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():
|
||||
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
|
||||
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)
|
||||
|
||||
def test_main(verbose=None):
|
||||
test_classes = (
|
||||
TestPartial,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue