def test_ww(): code = Code() code.ww(""" if {name}: return {name} """, name="x") src = code.build() assert src == ("if x:\n" " return x")
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 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