diff options
Diffstat (limited to 'tests/run/fused_types.pyx')
-rw-r--r-- | tests/run/fused_types.pyx | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/tests/run/fused_types.pyx b/tests/run/fused_types.pyx index de63fbdd0..5e6e72540 100644 --- a/tests/run/fused_types.pyx +++ b/tests/run/fused_types.pyx @@ -21,6 +21,7 @@ ctypedef double *p_double ctypedef int *p_int fused_type3 = cython.fused_type(int, double) fused_composite = cython.fused_type(fused_type2, fused_type3) +just_float = cython.fused_type(float) def test_pure(): """ @@ -323,7 +324,17 @@ def test_fused_memslice_dtype(cython.floating[:] array): double[:] double[:] 5.0 6.0 >>> test_fused_memslice_dtype[cython.float](get_array(4, 'f')) float[:] float[:] 5.0 6.0 + + # None should evaluate to *something* (currently the first + # in the list, but this shouldn't be a hard requirement) + >>> test_fused_memslice_dtype(None) + float[:] + >>> test_fused_memslice_dtype[cython.double](None) + double[:] """ + if array is None: + print(cython.typeof(array)) + return cdef cython.floating[:] otherarray = array[0:100:1] print cython.typeof(array), cython.typeof(otherarray), \ array[5], otherarray[6] @@ -457,6 +468,36 @@ def test_cdef_func_with_const_fused_arg(): cdef_func_const_fused_arg(arg0, &arg1, &arg2) +cdef in_check_1(just_float x): + return just_float in floating + +cdef in_check_2(just_float x, floating y): + # the "floating" on the right-hand side of the in statement should not be specialized + # - the test should still work. + return just_float in floating + +cdef in_check_3(floating x): + # the floating on the left-hand side of the in statement should be specialized + # but the one of the right-hand side should not (so that the test can still work). + return floating in floating + +def test_fused_in_check(): + """ + It should be possible to use fused types on in "x in ...fused_type" statements + even if that type is specialized in the function. + + >>> test_fused_in_check() + True + True + True + True + """ + print(in_check_1(1.0)) + print(in_check_2(1.0, 2.0)) + print(in_check_2[float, double](1.0, 2.0)) + print(in_check_3[float](1.0)) + + ### see GH3642 - presence of cdef inside "unrelated" caused a type to be incorrectly inferred cdef unrelated(cython.floating x): cdef cython.floating t = 1 @@ -479,3 +520,48 @@ def convert_to_ptr(cython.floating x): return handle_float(&x) elif cython.floating is double: return handle_double(&x) + +cdef double get_double(): + return 1.0 +cdef float get_float(): + return 0.0 + +cdef call_func_pointer(cython.floating (*f)()): + return f() + +def test_fused_func_pointer(): + """ + >>> test_fused_func_pointer() + 1.0 + 0.0 + """ + print(call_func_pointer(get_double)) + print(call_func_pointer(get_float)) + +cdef double get_double_from_int(int i): + return i + +cdef call_func_pointer_with_1(cython.floating (*f)(cython.integral)): + return f(1) + +def test_fused_func_pointer2(): + """ + >>> test_fused_func_pointer2() + 1.0 + """ + print(call_func_pointer_with_1(get_double_from_int)) + +cdef call_function_that_calls_fused_pointer(object (*f)(cython.floating (*)(cython.integral))): + if cython.floating is double and cython.integral is int: + return 5*f(get_double_from_int) + else: + return None # practically it's hard to make this kind of function useful... + +def test_fused_func_pointer_multilevel(): + """ + >>> test_fused_func_pointer_multilevel() + 5.0 + None + """ + print(call_function_that_calls_fused_pointer(call_func_pointer_with_1[double, int])) + print(call_function_that_calls_fused_pointer(call_func_pointer_with_1[float, int])) |