def test_list_extend(self): pyfunc = list_extend cr = compile_isolated(pyfunc, (types.Dummy('list'), types.Dummy('list'))) cfunc = cr.entry_point l1 = range(10) l2 = range(10) self.assertEqual(cfunc(l1, l2), pyfunc(l1, l2))
def test_cache_trimming(self): # Test that the cache doesn't grow in size when types are # created and disposed of. gc.collect() cache_len = len(types._typecache) a = types.Dummy('xyzzyx') b = types.Dummy('foox') self.assertEqual(len(types._typecache), cache_len + 2) del a, b gc.collect() self.assertEqual(len(types._typecache), cache_len)
def test_map(self, flags=enable_pyobj_flags): pyfunc = map_usecase cr = compile_isolated( pyfunc, (types.Dummy('list'), types.Dummy('function_ptr')), flags=flags) cfunc = cr.entry_point map_func = lambda x: x * 2 x = [0, 1, 2, 3, 4] self.assertSequenceEqual(list(cfunc(x, map_func)), list(pyfunc(x, map_func)))
def test_cache_trimming(self): # Test that the cache doesn't grow in size when types are # created and disposed of. gc.collect() # Keep strong references to existing types, to avoid spurious failures existing_types = [wr() for wr in types._typecache] cache_len = len(types._typecache) a = types.Dummy('xyzzyx') b = types.Dummy('foox') self.assertEqual(len(types._typecache), cache_len + 2) del a, b gc.collect() self.assertEqual(len(types._typecache), cache_len)
def test_list_count(self): pyfunc = list_count with self.assertTypingError(): cr = compile_isolated(pyfunc, (types.Dummy('list'), types.int32)) cfunc = cr.entry_point l = [1, 1, 2, 1] self.assertEqual(cfunc(l, 1), pyfunc(l, 1))
def test_list_sort(self): pyfunc = list_sort with self.assertTypingError(): cr = compile_isolated(pyfunc, (types.Dummy('list'), )) cfunc = cr.entry_point l = self.random.randint(10, size=10) self.assertEqual(cfunc(l), pyfunc(l))
def test_list_reverse(self): pyfunc = list_reverse with self.assertTypingError(): cr = compile_isolated(pyfunc, (types.Dummy('list'), )) cfunc = cr.entry_point l = range(10) self.assertEqual(cfunc(l), pyfunc(l))
def test_list_insert(self): pyfunc = list_insert cr = compile_isolated(pyfunc, (types.Dummy('list'), types.int32, types.int32)) cfunc = cr.entry_point l = range(10) self.assertEqual(cfunc(l, 0, 999), pyfunc(l, 0, 999))
def test_list_index(self): pyfunc = list_index with self.assertTypingError(): cr = compile_isolated(pyfunc, (types.Dummy('list'), types.int32)) cfunc = cr.entry_point l = range(10) self.assertEqual(cfunc(l, 1), pyfunc(l, 1))
def test_set_list_item(self): pyfunc = set_list_item with self.assertTypingError(): cr = compile_isolated( pyfunc, (types.Dummy('list'), types.int32, types.int32)) cfunc = cr.entry_point l = range(10) self.assertEqual(cfunc(l, 0, 999), pyfunc(l, 0, 999))
def test_set_list_slice(self): pyfunc = set_list_slice cr = compile_isolated(pyfunc, (types.Dummy('list'), types.int32, types.int32, types.int32, types.int32)) cfunc = cr.entry_point l = range(10) x = [999, 999, 999, 999, 999] self.assertEqual(cfunc(l, 0, 10, 2, x), pyfunc(l, 0, 10, 2, x))
def test_reduce(self, flags=enable_pyobj_flags): pyfunc = reduce_usecase cr = compile_isolated( pyfunc, (types.Dummy('function_ptr'), types.Dummy('list')), flags=flags) cfunc = cr.entry_point reduce_func = lambda x, y: x + y x = range(10) self.assertPreciseEqual(cfunc(reduce_func, x), pyfunc(reduce_func, x)) x = [x + x / 10.0 for x in range(10)] self.assertPreciseEqual(cfunc(reduce_func, x), pyfunc(reduce_func, x)) x = [complex(x, x) for x in range(10)] self.assertPreciseEqual(cfunc(reduce_func, x), pyfunc(reduce_func, x))
def test_get_list_slice(self): pyfunc = get_list_slice with self.assertTypingError(): cr = compile_isolated( pyfunc, (types.Dummy('list'), types.int32, types.int32, types.int32)) cfunc = cr.entry_point l = range(10) self.assertEqual(cfunc(l, 0, 10, 2), pyfunc(l, 0, 10, 2))
def test_interning(self): # Test interning and lifetime of dynamic types. a = types.Dummy('xyzzyx') code = a._code b = types.Dummy('xyzzyx') self.assertIs(b, a) wr = weakref.ref(a) del a gc.collect() c = types.Dummy('xyzzyx') self.assertIs(c, b) # The code is always the same self.assertEqual(c._code, code) del b, c gc.collect() self.assertIs(wr(), None) d = types.Dummy('xyzzyx') # The original code wasn't reused. self.assertNotEqual(d._code, code)
def test_unbox_runtime_error(self): # Dummy type has no unbox support def foo(x): pass cres = compile_isolated(foo, (types.Dummy("dummy_type"),)) with self.assertRaises(TypeError) as raises: # Can pass in whatever and the unbox logic will always raise # without checking the input value. cres.entry_point(None) self.assertEqual(str(raises.exception), "can't unbox dummy_type type")
def test_bool_nonnumber(self, flags=enable_pyobj_flags): pyfunc = bool_usecase cr = compile_isolated(pyfunc, (types.string, ), flags=flags) cfunc = cr.entry_point for x in ['x', '']: self.assertPreciseEqual(cfunc(x), pyfunc(x)) cr = compile_isolated(pyfunc, (types.Dummy('list'), ), flags=flags) cfunc = cr.entry_point for x in [[1], []]: self.assertPreciseEqual(cfunc(x), pyfunc(x))
def test_type_casting_rules(self): tm = TypeManager() tcr = TypeCastingRules(tm) i32 = types.int32 i64 = types.int64 f64 = types.float64 f32 = types.float32 made_up = types.Dummy("made_up") tcr.promote_unsafe(i32, i64) tcr.safe_unsafe(i32, f64) tcr.promote_unsafe(f32, f64) def base_test(): # As declared self.assertEqual(tm.check_compatible(i32, i64), Conversion.promote) self.assertEqual(tm.check_compatible(i32, f64), Conversion.safe) self.assertEqual(tm.check_compatible(f32, f64), Conversion.promote) self.assertEqual(tm.check_compatible(i64, i32), Conversion.unsafe) self.assertEqual(tm.check_compatible(f64, i32), Conversion.unsafe) self.assertEqual(tm.check_compatible(f64, f32), Conversion.unsafe) # Propagated self.assertEqual(tm.check_compatible(i64, f64), Conversion.unsafe) self.assertEqual(tm.check_compatible(f64, i64), Conversion.unsafe) self.assertEqual(tm.check_compatible(i64, f32), Conversion.unsafe) self.assertEqual(tm.check_compatible(i32, f32), Conversion.unsafe) self.assertEqual(tm.check_compatible(f32, i32), Conversion.unsafe) # Test base graph base_test() self.assertIsNone(tm.check_compatible(i64, made_up)) self.assertIsNone(tm.check_compatible(i32, made_up)) self.assertIsNone(tm.check_compatible(f32, made_up)) self.assertIsNone(tm.check_compatible(made_up, f64)) self.assertIsNone(tm.check_compatible(made_up, i64)) # Add new test tcr.promote(f64, made_up) tcr.unsafe(made_up, i32) # Ensure the graph did not change by adding the new type base_test() # To "made up" type self.assertEqual(tm.check_compatible(i64, made_up), Conversion.unsafe) self.assertEqual(tm.check_compatible(i32, made_up), Conversion.safe) self.assertEqual(tm.check_compatible(f32, made_up), Conversion.promote) self.assertEqual(tm.check_compatible(made_up, f64), Conversion.unsafe) self.assertEqual(tm.check_compatible(made_up, i64), Conversion.unsafe)
def test_sum(self, flags=enable_pyobj_flags): pyfunc = sum_usecase cr = compile_isolated(pyfunc, (types.Dummy('list'), ), flags=flags) cfunc = cr.entry_point x = range(10) self.assertPreciseEqual(cfunc(x), pyfunc(x)) x = [x + x / 10.0 for x in range(10)] self.assertPreciseEqual(cfunc(x), pyfunc(x)) x = [complex(x, x) for x in range(10)] self.assertPreciseEqual(cfunc(x), pyfunc(x))
#--------------------------------------------------------------------------- # Typing information FunctionContext = types.OpaqueType('class.impala_udf::FunctionContext') class ImpalaValue(types.Type): pass AnyVal = ImpalaValue('AnyVal') BooleanVal = ImpalaValue('BooleanVal') BooleanValType = types.Dummy('BooleanValType') class BooleanValCtor(ConcreteTemplate): key = BooleanValType cases = [signature(BooleanVal, types.int8)] class BooleanValValueAttr(AttributeTemplate): key = BooleanVal def resolve_is_null(self, val): """ BooleanVal::is_null """ return types.boolean
def check_min_max_invalid_types(self, pyfunc, flags=enable_pyobj_flags): cr = compile_isolated(pyfunc, (types.int32, types.Dummy('list')), flags=flags) cfunc = cr.entry_point cfunc(1, [1])
def test_list_remove(self): pyfunc = list_remove cr = compile_isolated(pyfunc, (types.Dummy('list'), types.int32)) cfunc = cr.entry_point l = range(10) self.assertEqual(cfunc(l, 1), pyfunc(l, 1))
def test_list_pop(self): pyfunc = list_pop cr = compile_isolated(pyfunc, (types.Dummy('list'), )) cfunc = cr.entry_point l = range(10) self.assertEqual(cfunc(l), pyfunc(l))