def test_state_type(self): """ State must be a list. """ cache = lru_cache(state=(1)) self.assertRaises(TypeError, cache, self.func) cache = lru_cache(state=-1) self.assertRaises(TypeError, cache, self.func)
def set_cache(): cls = FuturesStore funcs = ['open_table'] for func_name in funcs: func_cls = getattr(cls, func_name) if not hasattr(func_cls, "__wrapped__"): setattr(cls, func_name, lru_cache(None)(getattr(cls, func_name)))
def set_cache(): cls = BcolzDayStore funcs = ['open_bcolz_carray', 'open_bcolz_table'] for func_name in funcs: func_cls = getattr(cls, func_name) if not hasattr(func_cls, "__wrapped__"): setattr(cls, func_name, lru_cache(None)(getattr(cls, func_name)))
def test_memory_leaks(self): """ Longer running test to check for memory leaks. """ def tfunc(a, b, c): return (a - 1, 2 * c) + (10 * b - 1, a * b, a * b + c) cfunc = lru_cache(maxsize=2000)(tfunc) for i, j in self.arg_gen(max=1500, repeat=5): self.assertEqual(cfunc(i, j, c=i - j), tfunc(i, j, c=i - j))
def test_memory_leaks(self): """ Longer running test to check for memory leaks. """ def tfunc(a, b, c): return (a-1, 2*c) + (10*b-1, a*b, a*b+c) cfunc = lru_cache(maxsize=2000)(tfunc) for i, j in self.arg_gen(max=1500, repeat=5): self.assertEqual(cfunc(i, j, c=i-j), tfunc(i, j, c=i-j))
def test_function_attributes(self): """ Simple tests for attribute preservation. """ def tfunc(a, b): """test function docstring.""" return a + b cfunc = lru_cache()(tfunc) self.assertEqual(cfunc.__doc__, tfunc.__doc__) self.assertTrue(hasattr(cfunc, 'cache_info')) self.assertTrue(hasattr(cfunc, 'cache_clear')) self.assertTrue(hasattr(cfunc, '__wrapped__'))
def test_function_cache(self): """ Test that cache returns appropriate values. """ cat_tuples = [True] def tfunc(a, b, c=None): if (cat_tuples[0] == True): return (a, b, c) + (c, a) else: return 2*a-10*b cfunc = lru_cache(maxsize=100,state=cat_tuples)(tfunc) for i, j in self.arg_gen(max=75, repeat=5): self.assertEqual(cfunc(i, j), tfunc(i, j)) # change extra state cat_tuples[0] = False for i, j in self.arg_gen(max=75, repeat=5): self.assertEqual(cfunc(i, j), tfunc(i, j)) # test dict state d = {} cfunc = lru_cache(maxsize=100, state=d)(tfunc) cfunc(1, 2) self.assertEqual(cfunc.cache_info().misses, 1) d['a'] = 42 cfunc(1, 2) self.assertEqual(cfunc.cache_info().misses, 2) cfunc(1, 2) self.assertEqual(cfunc.cache_info().misses, 2) self.assertEqual(cfunc.cache_info().hits, 1) d.clear() cfunc(1, 2) self.assertEqual(cfunc.cache_info().misses, 2) self.assertEqual(cfunc.cache_info().hits, 2) d['a'] = 44 cfunc(1, 2) self.assertEqual(cfunc.cache_info().misses, 3)
def test_typed_True(self): """ Verify typed==True. """ cfunc = lru_cache(typed=True)(self.func) if hasattr(self, 'assertIsNot'): myAssert = self.assertIsNot else: myAssert = self.assertEqual myAssert(cfunc(1, 2), cfunc(1.0, 2)) myAssert(cfunc(1, 2), cfunc(1, 2.0)) # test keywords myAssert(cfunc(1,b=2), cfunc(1.0,b=2)) myAssert(cfunc(1,b=2), cfunc(1,b=2.0))
def test_typed_True(self): """ Verify typed==True. """ cfunc = lru_cache(typed=True)(self.func) if hasattr(self, 'assertIsNot'): myAssert = self.assertIsNot else: myAssert = self.assertEqual myAssert(cfunc(1, 2), cfunc(1.0, 2)) myAssert(cfunc(1, 2), cfunc(1, 2.0)) # test keywords myAssert(cfunc(1, b=2), cfunc(1.0, b=2)) myAssert(cfunc(1, b=2), cfunc(1, b=2.0))
def test_typed_False(self): """ Verify typed==False. """ cfunc = lru_cache(typed=False)(self.func) # initialize cache with integer args cfunc(1,2) if hasattr(self, 'assertIs'): myAssert = self.assertIs else: myAssert = self.assertEqual myAssert(cfunc(1, 2), cfunc(1.0, 2)) myAssert(cfunc(1, 2), cfunc(1, 2.0)) # test keywords cfunc(1,b=2) myAssert(cfunc(1,b=2), cfunc(1.0,b=2)) myAssert(cfunc(1,b=2), cfunc(1,b=2.0))
def test_typed_False(self): """ Verify typed==False. """ cfunc = lru_cache(typed=False)(self.func) # initialize cache with integer args cfunc(1, 2) if hasattr(self, 'assertIs'): myAssert = self.assertIs else: myAssert = self.assertEqual myAssert(cfunc(1, 2), cfunc(1.0, 2)) myAssert(cfunc(1, 2), cfunc(1, 2.0)) # test keywords cfunc(1, b=2) myAssert(cfunc(1, b=2), cfunc(1.0, b=2)) myAssert(cfunc(1, b=2), cfunc(1, b=2.0))
def test_function_cache(self): """ Test that cache returns appropriate values. """ cat_tuples = [True] def tfunc(a, b, c=None): if (cat_tuples[0] == True): return (a, b, c) + (c, a) else: return 2 * a - 10 * b cfunc = lru_cache(maxsize=100, state=cat_tuples)(tfunc) for i, j in self.arg_gen(max=75, repeat=5): self.assertEqual(cfunc(i, j), tfunc(i, j)) # change extra state cat_tuples[0] = False for i, j in self.arg_gen(max=75, repeat=5): self.assertEqual(cfunc(i, j), tfunc(i, j))
def test_function_cache(self): """ Test that cache returns appropriate values. """ cat_tuples = [True] def tfunc(a, b, c=None): if (cat_tuples[0] == True): return (a, b, c) + (c, a) else: return 2*a-10*b cfunc = lru_cache(maxsize=100,state=cat_tuples)(tfunc) for i, j in self.arg_gen(max=75, repeat=5): self.assertEqual(cfunc(i, j), tfunc(i, j)) # change extra state cat_tuples[0] = False for i, j in self.arg_gen(max=75, repeat=5): self.assertEqual(cfunc(i, j), tfunc(i, j))
def test_dynamic_attribute(self): cfunc = lru_cache()(self.func) cfunc.new_attr = 5 self.assertEqual(cfunc.new_attr, 5)