def testExecutionContextSetRetainsInsertedElements(self): cache = function_cache.FunctionCache() ctx_1 = function_cache.ExecutionContext(1, 1, 1, 1, 1, 1) self.assertFalse(cache.has_call_context(ctx_1)) cache.add_call_context(ctx_1) self.assertTrue(cache.has_call_context(ctx_1)) ctx_2 = function_cache.ExecutionContext(1, 1, 1, 1, 1, 1) self.assertTrue(cache.has_call_context(ctx_2)) ctx_3 = function_cache.ExecutionContext(1, 1, 1, 1, 1, None) self.assertFalse(cache.has_call_context(ctx_3)) cache.add_call_context(ctx_3) self.assertTrue(cache.has_call_context(ctx_3))
def testFunctionCacheKeyRespectsSupertype(self): ctx = function_cache.ExecutionContext(1, 1, 1, 1, 1, 1) key_a = function_cache.FunctionCacheKey(MockSupertypes2With3(1), ctx) key_b = function_cache.FunctionCacheKey(MockSupertypes2With3(2), ctx) self.assertEqual( key_b.most_specific_common_subtype([key_a]), function_cache.FunctionCacheKey(MockSupertypes2With3(3), ctx)) self.assertIsNone(key_a.most_specific_common_subtype([key_b]))
def testFunctionCacheKeyRespectsSubtype(self): ctx = function_cache.ExecutionContext(1, 1, 1, 1, 1, 1) key_a = function_cache.FunctionCacheKey(MockSubtypeOf2(1), ctx) key_b = function_cache.FunctionCacheKey(MockSubtypeOf2(2), ctx) key_c = function_cache.FunctionCacheKey(MockSubtypeOf2(1), ctx) self.assertTrue(key_b.is_subtype_of(key_a)) self.assertFalse(key_a.is_subtype_of(key_b)) self.assertFalse(key_c.is_subtype_of(key_a))
def testFunctionCacheKeyRespectsEquality(self): ctx = function_cache.ExecutionContext(1, 1, 1, 1, 1, 1) generic = function_trace_type.GenericType key_a = function_cache.FunctionCacheKey(generic(1), ctx) key_b = function_cache.FunctionCacheKey(generic(2), ctx) key_c = function_cache.FunctionCacheKey(generic(1), ctx) self.assertNotEqual(key_a, key_b) self.assertEqual(key_a, key_c) self.assertEqual(hash(key_a), hash(key_c))
def testFirstMostSpecificFunctionCacheKeyIsLookedUp(self): ctx = function_cache.ExecutionContext(1, 1, 1, 1, 1, 1) cache = function_cache.FunctionCache() cache.add(function_cache.FunctionCacheKey(MockShape(1, 2, None), ctx), trace_type.WeakrefDeletionObserver(), "a") cache.add(function_cache.FunctionCacheKey(MockShape(1, None, 3), ctx), trace_type.WeakrefDeletionObserver(), "b") self.assertEqual( cache.lookup( function_cache.FunctionCacheKey(MockShape(1, 2, 3), ctx), True), "a")
def testMostSpecificFunctionCacheKeyIsOrderAgnostic(self): ctx = function_cache.ExecutionContext(1, 1, 1, 1, 1, 1) keys = [(function_cache.FunctionCacheKey(MockShape(1, 1, 1), ctx), "a"), (function_cache.FunctionCacheKey(MockShape(1, None, 1), ctx), "b"), (function_cache.FunctionCacheKey(MockShape(None, None, 1), ctx), "c"), (function_cache.FunctionCacheKey(MockShape(None, None, None), ctx), "d")] for permutation in itertools.permutations(keys): cache = function_cache.FunctionCache() cache.add(permutation[0][0], trace_type.WeakrefDeletionObserver(), permutation[0][1]) cache.add(permutation[1][0], trace_type.WeakrefDeletionObserver(), permutation[1][1]) cache.add(permutation[2][0], trace_type.WeakrefDeletionObserver(), permutation[2][1]) cache.add(permutation[3][0], trace_type.WeakrefDeletionObserver(), permutation[3][1]) self.assertEqual( cache.lookup( function_cache.FunctionCacheKey(MockShape(1, 1, 1), ctx), True), "a") self.assertEqual( cache.lookup( function_cache.FunctionCacheKey(MockShape(1, 2, 1), ctx), True), "b") self.assertEqual( cache.lookup( function_cache.FunctionCacheKey(MockShape(2, 2, 1), ctx), True), "c") self.assertEqual( cache.lookup( function_cache.FunctionCacheKey(MockShape(2, 2, 2), ctx), True), "d")