From: Robert Bradshaw Date: Thu, 14 May 2009 07:58:14 +0000 (-0700) Subject: Complex numbers tests X-Git-Tag: 0.11.2.rc1~10^2~18 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5329d55db74ce4e54b3a0476f49eede750c1997c;p=cython.git Complex numbers tests --- diff --git a/tests/run/complex_numbers_T305.pyx b/tests/run/complex_numbers_T305.pyx new file mode 100644 index 00000000..8cdd6087 --- /dev/null +++ b/tests/run/complex_numbers_T305.pyx @@ -0,0 +1,84 @@ +__doc__ = u""" + >>> test_object_conversion(2) + (2+0j) + >>> test_object_conversion(2j - 0.5) + (-0.5+2j) + + >>> test_arithmetic(2j, 4j) + (6j, -2j, (-8+0j), (0.5+0j)) + >>> test_arithmetic(6+12j, 3j) + ((6+15j), (6+9j), (-36+18j), (4-2j)) + >>> test_arithmetic(5-10j, 3+4j) + ((8-6j), (2-14j), (55-10j), (-1-2j)) + + >>> test_div_by_zero(4j) + -0.25j + >>> test_div_by_zero(0) + Traceback (most recent call last): + ... + ZeroDivisionError: float division + + >>> test_coercion(1, 1.5, 2.5, 4+1j, 10j) + (1+0j) + (1.5+0j) + (2.5+0j) + (4+1j) + 10j + (9+21j) + + >>> test_compare(3, 3) + (True, False) + >>> test_compare(3j, 3j) + (True, False) + >>> test_compare(3j, 4j) + (False, True) + >>> test_compare(3, 4) + (False, True) + + >>> test_compare_coerce(3, 4) + (False, True) + >>> test_compare_coerce(4+1j, 4) + (False, True) + >>> test_compare_coerce(4, 4) + (True, False) + + >>> test_literal() + (5j, (1-2.5j)) +""" + +#cdef extern from "complex.h": +# pass + +cimport cython + +def test_object_conversion(o): + cdef float complex a = o + cdef double complex z = o + return z + +def test_arithmetic(double complex z, double complex w): + return z+w, z-w, z*w, z/w + +@cython.cdivision(False) +def test_div_by_zero(double complex z): + return 1/z + +def test_coercion(int a, float b, double c, float complex d, double complex e): + cdef double complex z + z = a; print z + z = b; print z + z = c; print z + z = d; print z + z = e; print z + return z + a + b + c + d + e + +def test_compare(double complex a, double complex b): + return a == b, a != b + +def test_compare_coerce(double complex a, int b): + return a == b, a != b + +def test_literal(): + return 5j, 1-2.5j + +