Ejemplo n.º 1
0
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()    
Ejemplo n.º 2
0
 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"
Ejemplo n.º 3
0
    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"
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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
Ejemplo n.º 7
0
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.
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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
Ejemplo n.º 10
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
Ejemplo n.º 11
0
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 
Ejemplo n.º 12
0
 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
Ejemplo n.º 13
0
def test_circular_class():
    def circular_class():
        b = A(a)
        return b.b
    
    fn = compile_function(circular_class, [])
    assert fn() == 3
Ejemplo n.º 14
0
def test_string_getitem2():
    def string_test(i):
        l = "Hello, World"
        return l[i]

    f = compile_function(string_test, [int])
    assert f(0) == "H"
Ejemplo n.º 15
0
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)
Ejemplo n.º 16
0
    def test_upperlower(self):
        def upperlower():
            s = "aBaF"
            return s.upper() + s.lower()

        fn = compile_function(upperlower, [])
        assert fn() == "ABAFabaf"
Ejemplo n.º 17
0
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
Ejemplo n.º 18
0
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)
Ejemplo n.º 19
0
 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
Ejemplo n.º 20
0
 def test_normal_list(self):
     def normal_list():
         l = [1,2,3]
         return l[1]
     
     fn = compile_function(normal_list, [])
     assert fn() == 2
Ejemplo n.º 21
0
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)
Ejemplo n.º 22
0
 def test_upperlower(self):
     def upperlower():
         s = "aBaF"
         return s.upper() + s.lower()
     
     fn = compile_function(upperlower, [])
     assert fn() == "ABAFabaf"
Ejemplo n.º 23
0
    def test_init_0(self):
        def l_init():
            l = [0] * 100
            return l[38]

        fn = compile_function(l_init, [])
        assert fn() == 0
Ejemplo n.º 24
0
 def test_init_0(self):
     def l_init():
         l = [0] * 100
         return l[38]
     
     fn = compile_function(l_init, [])
     assert fn() == 0
Ejemplo n.º 25
0
    def test_normal_list(self):
        def normal_list():
            l = [1, 2, 3]
            return l[1]

        fn = compile_function(normal_list, [])
        assert fn() == 2
Ejemplo n.º 26
0
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)
Ejemplo n.º 27
0
def test_string_getitem2():
    def string_test(i):
        l = "Hello, World"
        return l[i]

    f = compile_function(string_test, [int])
    assert f(0) == "H"
Ejemplo n.º 28
0
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)
Ejemplo n.º 29
0
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)
Ejemplo n.º 30
0
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
Ejemplo n.º 31
0
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)
Ejemplo n.º 32
0
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)
Ejemplo n.º 33
0
 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
Ejemplo n.º 34
0
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)
Ejemplo n.º 35
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
Ejemplo n.º 36
0
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)
Ejemplo n.º 37
0
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)
Ejemplo n.º 38
0
def test_circular_class():
    def circular_class():
        b = A(a)
        return b.b

    fn = compile_function(circular_class, [])
    assert fn() == 3
Ejemplo n.º 39
0
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)
Ejemplo n.º 40
0
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)
Ejemplo n.º 41
0
 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
Ejemplo n.º 42
0
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)
Ejemplo n.º 43
0
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)
Ejemplo n.º 44
0
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
Ejemplo n.º 45
0
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
Ejemplo n.º 46
0
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)
Ejemplo n.º 47
0
 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
Ejemplo n.º 48
0
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")
Ejemplo n.º 49
0
def test_string_getitem1():
    l = "Hello, World"

    def string_getitem1(i):
        return l[i]

    f = compile_function(string_getitem1, [int])
    assert f(0) == "H"
Ejemplo n.º 50
0
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
Ejemplo n.º 51
0
def test_nones():
    a = [None] * 4
    def nones():        
        a.append(None)
        return len(a)
    fn = compile_function(nones, [])
    result = fn()
    assert result == 4
Ejemplo n.º 52
0
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()
Ejemplo n.º 53
0
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()
Ejemplo n.º 54
0
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")
Ejemplo n.º 55
0
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")
Ejemplo n.º 56
0
 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
Ejemplo n.º 57
0
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