Back to index

python3.2  3.2.2
Public Member Functions
test.test_functools.TestLRU Class Reference

List of all members.

Public Member Functions

def test_lru
def test_lru_with_maxsize_none

Detailed Description

Definition at line 553 of file test_functools.py.


Member Function Documentation

Definition at line 555 of file test_functools.py.

00555 
00556     def test_lru(self):
00557         def orig(x, y):
00558             return 3*x+y
00559         f = functools.lru_cache(maxsize=20)(orig)
00560         hits, misses, maxsize, currsize = f.cache_info()
00561         self.assertEqual(maxsize, 20)
00562         self.assertEqual(currsize, 0)
00563         self.assertEqual(hits, 0)
00564         self.assertEqual(misses, 0)
00565 
00566         domain = range(5)
00567         for i in range(1000):
00568             x, y = choice(domain), choice(domain)
00569             actual = f(x, y)
00570             expected = orig(x, y)
00571             self.assertEqual(actual, expected)
00572         hits, misses, maxsize, currsize = f.cache_info()
00573         self.assertTrue(hits > misses)
00574         self.assertEqual(hits + misses, 1000)
00575         self.assertEqual(currsize, 20)
00576 
00577         f.cache_clear()   # test clearing
00578         hits, misses, maxsize, currsize = f.cache_info()
00579         self.assertEqual(hits, 0)
00580         self.assertEqual(misses, 0)
00581         self.assertEqual(currsize, 0)
00582         f(x, y)
00583         hits, misses, maxsize, currsize = f.cache_info()
00584         self.assertEqual(hits, 0)
00585         self.assertEqual(misses, 1)
00586         self.assertEqual(currsize, 1)
00587 
00588         # Test bypassing the cache
00589         self.assertIs(f.__wrapped__, orig)
00590         f.__wrapped__(x, y)
00591         hits, misses, maxsize, currsize = f.cache_info()
00592         self.assertEqual(hits, 0)
00593         self.assertEqual(misses, 1)
00594         self.assertEqual(currsize, 1)
00595 
00596         # test size zero (which means "never-cache")
00597         @functools.lru_cache(0)
00598         def f():
00599             nonlocal f_cnt
00600             f_cnt += 1
00601             return 20
00602         self.assertEqual(f.cache_info().maxsize, 0)
00603         f_cnt = 0
00604         for i in range(5):
00605             self.assertEqual(f(), 20)
00606         self.assertEqual(f_cnt, 5)
00607         hits, misses, maxsize, currsize = f.cache_info()
00608         self.assertEqual(hits, 0)
00609         self.assertEqual(misses, 5)
00610         self.assertEqual(currsize, 0)
00611 
00612         # test size one
00613         @functools.lru_cache(1)
00614         def f():
00615             nonlocal f_cnt
00616             f_cnt += 1
00617             return 20
00618         self.assertEqual(f.cache_info().maxsize, 1)
00619         f_cnt = 0
00620         for i in range(5):
00621             self.assertEqual(f(), 20)
00622         self.assertEqual(f_cnt, 1)
00623         hits, misses, maxsize, currsize = f.cache_info()
00624         self.assertEqual(hits, 4)
00625         self.assertEqual(misses, 1)
00626         self.assertEqual(currsize, 1)
00627 
00628         # test size two
00629         @functools.lru_cache(2)
00630         def f(x):
00631             nonlocal f_cnt
00632             f_cnt += 1
00633             return x*10
00634         self.assertEqual(f.cache_info().maxsize, 2)
00635         f_cnt = 0
00636         for x in 7, 9, 7, 9, 7, 9, 8, 8, 8, 9, 9, 9, 8, 8, 8, 7:
00637             #    *  *              *                          *
00638             self.assertEqual(f(x), x*10)
00639         self.assertEqual(f_cnt, 4)
00640         hits, misses, maxsize, currsize = f.cache_info()
00641         self.assertEqual(hits, 12)
00642         self.assertEqual(misses, 4)
00643         self.assertEqual(currsize, 2)

Here is the call graph for this function:

Definition at line 644 of file test_functools.py.

00644 
00645     def test_lru_with_maxsize_none(self):
00646         @functools.lru_cache(maxsize=None)
00647         def fib(n):
00648             if n < 2:
00649                 return n
00650             return fib(n-1) + fib(n-2)
00651         self.assertEqual([fib(n) for n in range(16)],
00652             [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610])
00653         self.assertEqual(fib.cache_info(),
00654             functools._CacheInfo(hits=28, misses=16, maxsize=None, currsize=16))
00655         fib.cache_clear()
00656         self.assertEqual(fib.cache_info(),
00657             functools._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0))

Here is the call graph for this function:


The documentation for this class was generated from the following file: