def test_def_(): code = Code() with code.def_('foo', ['x', 'y']): code.w('return x+y') # code.compile() foo = code['foo'] assert foo(1, 2) == 3
def test_def__default_args(): code = Code() with code.def_('foo', ['x', ('y', 3)]): code.w('return x+y') # code.compile() foo = code['foo'] assert foo(1) == 4 assert foo(1, 10) == 11
def test_simple(self, schema, benchmark): code = Code() code.global_scope.N = self.N code.ww(""" def new_and_sum(Point): res = 0 for i in range({N}): obj = Point(x=1, y=2, z=3) res += obj.z return res """) code.compile() new_and_sum = code['new_and_sum'] res = benchmark(new_and_sum, schema.Point) assert res == self.N * 3
def test_list(self, schema, benchmark): code = Code() code.global_scope.N = self.N code.ww(""" def new_and_sum(MyStructContainer, items): res = 0 for i in range({N}): obj = MyStructContainer(items) res += 1 return res """) code.compile() new_and_sum = code['new_and_sum'] items = [get_obj(schema)] * 10 res = benchmark(new_and_sum, schema.MyStructContainer, items) assert res == self.N
def test_list_of_ints(self, schema, benchmark): code = Code() code.global_scope.N = self.N code.ww(""" def new_and_sum(MyInt64List, items): res = 0 for i in range({N}): obj = MyInt64List(items) res += 1 return res """) code.compile() new_and_sum = code['new_and_sum'] items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] res = benchmark(new_and_sum, schema.MyInt64List, items) assert res == self.N
def test_struct(self, schema, benchmark): code = Code() code.global_scope.N = self.N code.ww(""" def new_and_sum(Rectangle, a, b): res = 0 for i in range({N}): obj = Rectangle(a=a, b=b) res += 1 return res """) code.compile() new_and_sum = code['new_and_sum'] a = schema.Point(1, 2, 3) b = schema.Point(4, 5, 6) res = benchmark(new_and_sum, schema.Rectangle, a, b) assert res == self.N
def test_text(self, schema, benchmark): code = Code() code.global_scope.N = self.N code.ww(""" def new_and_sum(StrPoint): res = 0 for i in range({N}): obj = StrPoint(x='hello', y='this is a longer string', z='bar') res += len(obj.z) return res """) code.compile() new_and_sum = code['new_and_sum'] res = benchmark(new_and_sum, schema.StrPoint) assert res == self.N * 3
def make_wrappers(): from pypytools.codegen import Code code = Code() names = [ 'all', 'any', 'argmax', 'argmin', 'argsort', 'choose', 'clip', 'copy', 'cumprod', 'cumsum', 'diagonal', 'max', 'min', 'nonzero', 'prod', 'ptp', 'put', 'ravel', 'repeat', 'reshape', 'round', 'squeeze', 'sum', 'swapaxes', 'transpose' ] # for name in names: ns = code.new_scope(name=name) with code.def_(name, ['myarray'], '*args', '**kwargs'): ns.w('return myarray.{name}(*args, **kwargs)') # code.compile() gbl = globals() for name in names: gbl[name] = code[name]
def test_numeric(self, schema, numeric_type, benchmark, obj=None): # the extra_info is used to generate the charts benchmark.extra_info['attribute_type'] = numeric_type code = Code() code.global_scope.N = self.N code.global_scope.numeric_type = numeric_type code.ww(""" def sum_attr(obj): myobjs = (obj, obj) res = 0 for i in range({N}): obj = myobjs[i%2] res += obj.{numeric_type} return res """) code.compile() sum_attr = code['sum_attr'] obj = get_obj(schema) res = benchmark(sum_attr, obj) assert res == 100 * self.N
def test_compile(): code = Code() code.w('x = 42') code.compile() assert code['x'] == 42