Cpp exception tests.
authorRobert Bradshaw <robertwb@math.washington.edu>
Thu, 22 Oct 2009 10:15:34 +0000 (03:15 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Thu, 22 Oct 2009 10:15:34 +0000 (03:15 -0700)
tests/run/cpp_exceptions.pyx [new file with mode: 0644]
tests/run/cpp_exceptions_helper.cpp [new file with mode: 0644]

diff --git a/tests/run/cpp_exceptions.pyx b/tests/run/cpp_exceptions.pyx
new file mode 100644 (file)
index 0000000..6fa26a9
--- /dev/null
@@ -0,0 +1,71 @@
+cdef int raise_py_error() except *:
+    raise TypeError("custom")
+
+cdef extern from "cpp_exceptions_helper.cpp":
+    cdef int raise_int_raw "raise_int"(bint fire) except +
+    cdef int raise_int_value "raise_int"(bint fire) except +ValueError
+    cdef int raise_int_custom "raise_int"(bint fire) except +raise_py_error
+    
+    cdef int raise_index_raw "raise_index"(bint fire) except +
+    cdef int raise_index_value "raise_index"(bint fire) except +ValueError
+    cdef int raise_index_custom "raise_index"(bint fire) except +raise_py_error
+
+def test_int_raw(bint fire):
+    """
+    >>> test_int_raw(False)
+    >>> test_int_raw(True)
+    Traceback (most recent call last):
+    ...
+    RuntimeError: Unknown exception
+    """
+    raise_int_raw(fire)
+
+def test_int_value(bint fire):
+    """
+    >>> test_int_value(False)
+    >>> test_int_value(True)
+    Traceback (most recent call last):
+    ...
+    ValueError
+    """
+    raise_int_value(fire)
+
+def test_int_custom(bint fire):
+    """
+    >>> test_int_custom(False)
+    >>> test_int_custom(True)
+    Traceback (most recent call last):
+    ...
+    TypeError: custom
+    """
+    raise_int_custom(fire)
+
+def test_index_raw(bint fire):
+    """
+    >>> test_index_raw(False)
+    >>> test_index_raw(True)
+    Traceback (most recent call last):
+    ...
+    IndexError: c++ error
+    """
+    raise_index_raw(fire)
+
+def test_index_value(bint fire):
+    """
+    >>> test_index_value(False)
+    >>> test_index_value(True)
+    Traceback (most recent call last):
+    ...
+    ValueError: c++ error
+    """
+    raise_index_value(fire)
+
+def test_index_custom(bint fire):
+    """
+    >>> test_index_custom(False)
+    >>> test_index_custom(True)
+    Traceback (most recent call last):
+    ...
+    TypeError: custom
+    """
+    raise_index_custom(fire)
diff --git a/tests/run/cpp_exceptions_helper.cpp b/tests/run/cpp_exceptions_helper.cpp
new file mode 100644 (file)
index 0000000..7640f53
--- /dev/null
@@ -0,0 +1,15 @@
+#include <stdexcept>
+
+int raise_int(int fire) {
+    if (fire) {
+        throw 1;
+    }
+    return 0;
+}
+
+int raise_index(int fire) {
+    if (fire) {
+        throw std::out_of_range("c++ error");
+    }
+    return 0;
+}