Tests for arithmetic promotion.
authorRobert Bradshaw <robertwb@math.washington.edu>
Sun, 3 Apr 2011 01:51:41 +0000 (18:51 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Sun, 3 Apr 2011 01:51:41 +0000 (18:51 -0700)
tests/run/arithmetic_analyse_types.pyx [new file with mode: 0644]
tests/run/arithmetic_analyse_types_helper.h [new file with mode: 0644]

diff --git a/tests/run/arithmetic_analyse_types.pyx b/tests/run/arithmetic_analyse_types.pyx
new file mode 100644 (file)
index 0000000..d37ef86
--- /dev/null
@@ -0,0 +1,62 @@
+# ticket: 676
+# tag: cpp
+
+from cython cimport typeof
+
+cdef extern from "arithmetic_analyse_types_helper.h":
+    cdef struct short_return:
+        char *msg
+    cdef struct int_return:
+        char *msg
+    cdef struct longlong_return:
+        char *msg
+    cdef short_return f(short)
+    cdef int_return f(int)
+    cdef longlong_return f(long long)
+
+def short_binop(short val):
+    """
+    Arithmetic in C is always done with at least int precision.
+    
+    >>> short_binop(3)
+    'int called'
+    """
+    assert typeof(val + val) == "int", typeof(val + val)
+    assert typeof(val - val) == "int", typeof(val - val)
+    assert typeof(val & val) == "int", typeof(val & val)
+    cdef int_return x = f(val + val)
+    return x.msg
+
+def short_unnop(short val):
+    """
+    Arithmetic in C is always done with at least int precision.
+    
+    >>> short_unnop(3)
+    'int called'
+    """
+    cdef int_return x = f(-val)
+    return x.msg
+
+def longlong_binop(long long val):
+    """
+    >>> longlong_binop(3)
+    'long long called'
+    """
+    cdef longlong_return x = f(val * val)
+    return x.msg
+
+def longlong_unnop(long long val):
+    """
+    >>> longlong_unnop(3)
+    'long long called'
+    """
+    cdef longlong_return x = f(~val)
+    return x.msg
+
+
+def test_bint(bint a):
+    """
+    >>> test_bint(True)
+    """
+    assert typeof(a + a) == "int", typeof(a + a)
+    assert typeof(a & a) == "bint", typeof(a & a)
diff --git a/tests/run/arithmetic_analyse_types_helper.h b/tests/run/arithmetic_analyse_types_helper.h
new file mode 100644 (file)
index 0000000..72145c9
--- /dev/null
@@ -0,0 +1,27 @@
+/* A set of mutually incompatable return types. */
+
+struct short_return { const char *msg; };
+struct int_return { const char *msg; };
+struct longlong_return { const char *msg; };
+
+/* A set of overloaded methods. */
+
+short_return f(short arg) {
+    short_return val;
+    val.msg = "short called";
+    return val;
+}
+
+int_return f(int arg) {
+    int_return val;
+    val.msg = "int called";
+    return val;
+}
+
+longlong_return f(long long arg) {
+    longlong_return val;
+    val.msg = "long long called";
+    return val;
+}