def test_bool_return(): def bool_return_False(): return False def bool_return_True(): return True f_false = compile_function(bool_return_False, []) assert f_false() == bool_return_False() f_true = compile_function(bool_return_True , []) assert f_true() == bool_return_True()
def test_slice(self): def one_slice(s): return s[1:] def two_slice(s): return s[2:] fn = compile_function(one_slice, [str]) assert fn("dupa") == "upa" fn = compile_function(two_slice, [str]) assert fn("kupa") == "pa"
def test_slice(self): def one_slice(s): return s[1:] def two_slice(s): return s[2:] fn = compile_function(one_slice, [str]) assert fn("dupa") == "upa" fn = compile_function(two_slice, [str]) assert fn("kupa") == "pa"
def test_primitive_is_true(): def var_is_true(v): return bool(v) f = compile_function(var_is_true, [int]) assert f(256) assert not f(0) f = compile_function(var_is_true, [r_uint]) assert f(r_uint(256)) assert not f(r_uint(0)) f = compile_function(var_is_true, [float]) assert f(256.0) assert not f(0.0)
def test_primitive_is_true(): def var_is_true(v): return bool(v) f = compile_function(var_is_true, [int]) assert f(256) assert not f(0) f = compile_function(var_is_true, [r_uint]) assert f(r_uint(256)) assert not f(r_uint(0)) f = compile_function(var_is_true, [float]) assert f(256.0) assert not f(0.0)
def test_is(): def testfn(): l1 = [] return l1 is l1 fn = compile_function(testfn, []) result = fn() assert result == True def testfn(): l1 = [] return l1 is None fn = compile_function(testfn, []) result = fn() assert result == False
def test_while_loop(): def factorial(i): r = 1 while i > 1: r *= i i -= 1 return r f = compile_function(factorial, [int]) assert factorial(4) == 24 assert factorial(5) == 120 f = compile_function(factorial, [float]) assert factorial(4.) == 24. assert factorial(5.) == 120.
def DONTtest_shift_with_overflow( ): #issue with Javascript Number() having a larger range def shiftleft(x, y): return x << y def shiftright(x, y): return x >> y shl = compile_function(shiftleft, [int, int]) shr = compile_function(shiftright, [int, int]) for i in [1, 2, 3, 100000, 2000000, sys.maxint - 1]: for j in [1, 2, 3, 100000, 2000000, sys.maxint - 1]: assert shl(i, j) == i << j assert shr(i, j) == i >> j
def test_while_loop(): def factorial(i): r = 1 while i > 1: r *= i i -= 1 return r f = compile_function(factorial, [int]) assert factorial(4) == 24 assert factorial(5) == 120 f = compile_function(factorial, [float]) assert factorial(4.0) == 24.0 assert factorial(5.0) == 120.0
def test_ackermann(self): f = compile_function(llvmsnippet.ackermann, [int, int]) for i in range(4): # (otherwise too much recursion) max 4 in Safari, max 7 in Firefox, IE allows more recursion assert f(0, i) == i + 1 assert f(1, i) == i + 2 assert f(2, i) == 2 * i + 3 assert f(3, i) == 2 ** (i + 3) - 3
def test_get_set_del_slice(): def get_set_del_nonneg_slice(): # no neg slices for now! l = [ord('a'), ord('b'), ord('c'), ord('d'), ord('e'), ord('f'), ord('g'), ord('h'), ord('i'), ord('j')] del l[:1] bound = len(l)-1 if bound >= 0: del l[bound:] del l[2:4] #l[:1] = [3] #bound = len(l)-1 #assert bound >= 0 #l[bound:] = [9] no setting slice into lists for now #l[2:4] = [8,11] l[0], l[-1], l[2], l[3] = 3, 9, 8, 11 list_3_c = l[:2] list_9 = l[5:] list_11_h = l[3:5] return list((len(l), l[0], l[1], l[2], l[3], l[4], l[5], len(list_3_c), list_3_c[0], list_3_c[1], len(list_9), list_9[0], len(list_11_h), list_11_h[0], list_11_h[1])) def wrapper(): res = get_set_del_nonneg_slice() expected = [6, 3, ord('c'), 8, 11, ord('h'), 9, 2, 3, ord('c'), 1, 9, 2, 11, ord('h')] return res == expected fn = compile_function(wrapper, []) result = fn() assert result
def test_global_instance(self): #issue we restart every test with a fresh set of globals f = compile_function(llvmsnippet.global_instance, [int]) assert f(-1) == llvmsnippet.global_instance(-1) for i in range(20): x = f(i) y = llvmsnippet.global_instance(i) assert x == y
def test_circular_class(): def circular_class(): b = A(a) return b.b fn = compile_function(circular_class, []) assert fn() == 3
def test_string_getitem2(): def string_test(i): l = "Hello, World" return l[i] f = compile_function(string_test, [int]) assert f(0) == "H"
def test_tuple_getitem(): def tuple_getitem(i): l = (4, 5, i) return l[1] f = compile_function(tuple_getitem, [int]) assert f(1) == tuple_getitem(1)
def test_upperlower(self): def upperlower(): s = "aBaF" return s.upper() + s.lower() fn = compile_function(upperlower, []) assert fn() == "ABAFabaf"
def test_nested_tuple(): def nested_tuple(i): l = (1, (1, 2, i), i) return l[1][2] f = compile_function(nested_tuple, [int]) assert f(4) == 4
def test_cast_str(): def cast_str(x): return str(x) + str(x) + "px" f = compile_function(cast_str, [int]) assert f(1) == cast_str(1) assert f(10) == cast_str(10)
def test_pbc_function1(self): #py.test.skip("Method mapping not implemented") f = compile_function(llvmsnippet.pbc_function1, [int]) assert f(0) == 2 assert f(1) == 4 assert f(2) == 6 assert f(3) == 8
def test_normal_list(self): def normal_list(): l = [1,2,3] return l[1] fn = compile_function(normal_list, []) assert fn() == 2
def test_string_simple(): # py.test.skip("ord semantics") def string_simple(i): return ord(str(i)) f = compile_function(string_simple, [int]) assert f(3) == string_simple(3)
def test_upperlower(self): def upperlower(): s = "aBaF" return s.upper() + s.lower() fn = compile_function(upperlower, []) assert fn() == "ABAFabaf"
def test_init_0(self): def l_init(): l = [0] * 100 return l[38] fn = compile_function(l_init, []) assert fn() == 0
def test_init_0(self): def l_init(): l = [0] * 100 return l[38] fn = compile_function(l_init, []) assert fn() == 0
def test_normal_list(self): def normal_list(): l = [1, 2, 3] return l[1] fn = compile_function(normal_list, []) assert fn() == 2
def test_simple3(): def raise_(i): if i == 0: raise TestException() elif i == 1: raise MyException(42) else: return 3 def fn(i): try: a = raise_(i) + 11 b = raise_(i) + 12 c = raise_(i) + 13 return a + b + c except TestException: return 7 except MyException: return 123 except: return 22 return 66 f = compile_function(fn, [int]) assert f(0) == fn(0) assert f(1) == fn(1) assert f(2) == fn(2)
def test_string_getitem2(): def string_test(i): l = "Hello, World" return l[i] f = compile_function(string_test, [int]) assert f(0) == "H"
def test_simple3(): def raise_(i): if i == 0: raise TestException() elif i == 1: raise MyException(42) else: return 3 def fn(i): try: a = raise_(i) + 11 b = raise_(i) + 12 c = raise_(i) + 13 return a+b+c except TestException: return 7 except MyException: return 123 except: return 22 return 66 f = compile_function(fn, [int]) assert f(0) == fn(0) assert f(1) == fn(1) assert f(2) == fn(2)
def test_float_abs(): def float_abs_(n): return abs(n) f = compile_function(float_abs_, [float]) for i in (-100.1 - 50.2, -0.0, 0.0, 25.3, 50.4): assert f(i) == float_abs_(i)
def test_cast_to_int(): def casting(v): return int(ord(chr(v))) f = compile_function(casting, [int]) for ii in range(255): assert f(ii) == ii
def test_int_abs(): def int_abs_(n): return abs(n) f = compile_function(int_abs_, [int]) for i in (-25, 0, 75): assert f(i) == int_abs_(i)
def test_raise_outside_testfn(): def raiser(n): if n < 0: raise ValueError("hello") else: raise MyException("world") def intermediate(n): raiser(n) def testfn(n): try: intermediate(n) except ValueError: return 1 except Exception: return 2 return 0 saved = no_magic() try: f = compile_function(testfn, [int]) assert f(1) == testfn(1) assert f(-1) == testfn(-1) finally: restore_magic(saved)
def test_time_waster(self): #py.test.skip("Loop error in loop detection software") f = compile_function(test.time_waster, [int]) assert f(1) == 1 assert f(2) == 2 assert f(3) == 6 assert f(4) == 12
def test_int_invert(): def fn(i): return ~i f = compile_function(fn, [int]) for i in range(-15, 15): assert f(i) == fn(i)
def test_is(): def testfn(): l1 = [] return l1 is l1 fn = compile_function(testfn, []) result = fn() assert result == True def testfn(): l1 = [] return l1 is None fn = compile_function(testfn, []) result = fn() assert result == False
def test_int_abs(): def int_abs_(n): return abs(n) f = compile_function(int_abs_, [int]) for i in (-25, 0, 75): assert f(i) == int_abs_(i)
def test_float_abs(): def float_abs_(n): return abs(n) f = compile_function(float_abs_, [float]) for i in (-100.1 - 50.2, -0.0, 0.0, 25.3, 50.4): assert f(i) == float_abs_(i)
def test_circular_class(): def circular_class(): b = A(a) return b.b fn = compile_function(circular_class, []) assert fn() == 3
def test_int_invert(): def fn(i): return ~i f = compile_function(fn, [int]) for i in range(-15, 15): assert f(i) == fn(i)
def test_raise_outside_testfn(): def raiser(n): if n < 0: raise ValueError("hello") else: raise MyException("world") def intermediate(n): raiser(n) def testfn(n): try: intermediate(n) except ValueError: return 1 except Exception: return 2 return 0 saved = no_magic() try: f = compile_function(testfn, [int]) assert f(1) == testfn(1) assert f(-1) == testfn(-1) finally: restore_magic(saved)
def test_time_waster(self): #py.test.skip("Loop error in loop detection software") f = compile_function(test.time_waster, [int]) assert f(1) == 1 assert f(2) == 2 assert f(3) == 6 assert f(4) == 12
def test_tuple_getitem(): def tuple_getitem(i): l = (4, 5, i) return l[1] f = compile_function(tuple_getitem, [int]) assert f(1) == tuple_getitem(1)
def test_string_simple(): #py.test.skip("ord semantics") def string_simple(i): return ord(str(i)) f = compile_function(string_simple, [int]) assert f(3) == string_simple(3)
def test_nested_tuple(): def nested_tuple(i): l = (1, (1, 2, i), i) return l[1][2] f = compile_function(nested_tuple, [int]) assert f(4) == 4
def test_cast_to_int(): def casting(v): return int(ord(chr(v))) f = compile_function(casting, [int]) for ii in range(255): assert f(ii) == ii
def test_cast_str(): def cast_str(x): return str(x) + str(x) + 'px' f = compile_function(cast_str, [int]) assert f(1) == cast_str(1) assert f(10) == cast_str(10)
def test_global_instance( self): #issue we restart every test with a fresh set of globals f = compile_function(llvmsnippet.global_instance, [int]) assert f(-1) == llvmsnippet.global_instance(-1) for i in range(20): x = f(i) y = llvmsnippet.global_instance(i) assert x == y
def test_basicexternal_element(): def be_fun(): b = B() b.a = a b.a.some_code("aa") fun = compile_function(be_fun, []) assert check_source_contains(fun, "\.some_code")
def test_string_getitem1(): l = "Hello, World" def string_getitem1(i): return l[i] f = compile_function(string_getitem1, [int]) assert f(0) == "H"
def test_list_list_getitem(): #py.test.skip("List support") def list_list_getitem(): l = [[1]] return l[0][0] f = compile_function(list_list_getitem, []) assert f() == 1
def test_nones(): a = [None] * 4 def nones(): a.append(None) return len(a) fn = compile_function(nones, []) result = fn() assert result == 4
def test_me(): def some_test(): a = A() a.some_fun = some_fun return a.some_fun() fn = compile_function(some_test, []) assert fn() == some_test()
def test_me(): def some_test(): a = A() a.some_fun = some_fun return a.some_fun() fn = compile_function(some_test, []) assert fn() == some_test()
def test_simple_builtin(): from pypy.translator.js.modules.dom import document def test_document_call(): return document.getElementById("some_id") fn = compile_function(test_document_call, []) assert check_source_contains(fn, "= document") assert check_source_contains(fn, "\.getElementById")
def test_basicexternal_element(): def be_fun(): b = B() b.a = a b.a.some_code("aa") fun = compile_function(be_fun, []) assert check_source_contains(fun, "\.some_code")
def test_pbc_function2(self): #py.test.skip("issue 'null' for Ptr's? or recurse into Ptr.TO?) see: opwriter.py") #py.test.skip("Method mapping not implemented") f = compile_function(llvmsnippet.pbc_function2, [int]) assert f(0) == 13 assert f(1) == 15 assert f(2) == 17 assert f(3) == 19
def test_init_list(): def init_list(i): if i == 8: b.a = [1, 1, 1] return b.a[2] fn = compile_function(init_list, [int]) assert fn(3) == INIT_VAL assert fn(8) == 1