diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-06-08 08:19:24 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-06-08 08:19:24 (GMT) |
commit | e7070f09bcbc4aa960216d099beff4431744a7e2 (patch) | |
tree | 0959f5c789d72922945ae6e250845585d64e840c /Lib/test/test_functools.py | |
parent | 77cb197aaa289d1815b9b476f48ebe86d9b75a72 (diff) | |
download | cpython-e7070f09bcbc4aa960216d099beff4431744a7e2.zip cpython-e7070f09bcbc4aa960216d099beff4431744a7e2.tar.gz cpython-e7070f09bcbc4aa960216d099beff4431744a7e2.tar.bz2 |
Issue #14373: C implementation of functools.lru_cache() now can be used with
methods.
Diffstat (limited to 'Lib/test/test_functools.py')
-rw-r--r-- | Lib/test/test_functools.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 632b2d3..569bdcf 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -1203,6 +1203,37 @@ class TestLRU: def f(): pass + def test_lru_method(self): + class X(int): + f_cnt = 0 + @self.module.lru_cache(2) + def f(self, x): + self.f_cnt += 1 + return x*10+self + a = X(5) + b = X(5) + c = X(7) + self.assertEqual(X.f.cache_info(), (0, 0, 2, 0)) + + for x in 1, 2, 2, 3, 1, 1, 1, 2, 3, 3: + self.assertEqual(a.f(x), x*10 + 5) + self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 0, 0)) + self.assertEqual(X.f.cache_info(), (4, 6, 2, 2)) + + for x in 1, 2, 1, 1, 1, 1, 3, 2, 2, 2: + self.assertEqual(b.f(x), x*10 + 5) + self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 4, 0)) + self.assertEqual(X.f.cache_info(), (10, 10, 2, 2)) + + for x in 2, 1, 1, 1, 1, 2, 1, 3, 2, 1: + self.assertEqual(c.f(x), x*10 + 7) + self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 4, 5)) + self.assertEqual(X.f.cache_info(), (15, 15, 2, 2)) + + self.assertEqual(a.f.cache_info(), X.f.cache_info()) + self.assertEqual(b.f.cache_info(), X.f.cache_info()) + self.assertEqual(c.f.cache_info(), X.f.cache_info()) + class TestLRUC(TestLRU, unittest.TestCase): module = c_functools |