From 1fcaf85398e4a3ee9d4d77fc0647fdf9984b1d5b Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Tue, 21 May 2013 09:36:25 -0700 Subject: [PATCH] ptrdif_t test --- Cython/Compiler/PyrexTypes.py | 2 +- tests/run/ptrdiff_t.pyx | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/run/ptrdiff_t.pyx diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index 3996c84..6059e36 100755 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -3456,7 +3456,7 @@ c_py_hash_t_type = CPyHashTType(RANK_LONG+0.5, SIGNED) c_py_ssize_t_type = CPySSizeTType(RANK_LONG+0.5, SIGNED) c_ssize_t_type = CSSizeTType(RANK_LONG+0.5, SIGNED) c_size_t_type = CSizeTType(RANK_LONG+0.5, UNSIGNED) -c_ptrdiff_t_type = CPtrdiffTType(RANK_LONG+0.25, SIGNED) +c_ptrdiff_t_type = CPtrdiffTType(RANK_LONG+0.75, SIGNED) c_null_ptr_type = CNullPtrType(c_void_type) c_void_ptr_type = CPtrType(c_void_type) diff --git a/tests/run/ptrdiff_t.pyx b/tests/run/ptrdiff_t.pyx new file mode 100644 index 0000000..a5362f1 --- /dev/null +++ b/tests/run/ptrdiff_t.pyx @@ -0,0 +1,55 @@ +from cython cimport typeof + +def test(ptrdiff_t i): + """ + >>> test(0) + 0 + >>> test(1) + 1 + >>> test(2) + 2 + >>> test(-1) + -1 + >>> test(-2) + -2 + >>> str(test((1<<31)-1)) + '2147483647' + """ + return i + +cdef class A: + """ + >>> try: test(1<<200) + ... except (OverflowError, TypeError): print("ERROR") + ERROR + + >>> a = A(1,2) + >>> a.a == 1 + True + >>> a.b == 2 + True + >>> a.foo(5) + 5 + >>> try: a.foo(1<<200) + ... except (OverflowError, TypeError): print("ERROR") + ERROR + """ + cdef public ptrdiff_t a + cdef readonly ptrdiff_t b + + def __init__(self, ptrdiff_t a, object b): + self.a = a + self.b = b + + cpdef ptrdiff_t foo(self, ptrdiff_t x): + cdef object o = x + return o + +def test_types(): + """ + >>> test_types() + """ + cdef int a = 1, b = 2 + assert typeof(&a - &b) == "ptrdiff_t", typeof(&a - &b) + assert typeof((&a - &b) + 1) == "ptrdiff_t", typeof((&a - &b) + 1) + assert typeof(&a + (&b - &a)) == "int *", typeof(&a + (&b - &a)) -- 2.7.4