Running C++ operator tests.
authorRobert Bradshaw <robertwb@math.washington.edu>
Fri, 15 Jan 2010 01:22:18 +0000 (17:22 -0800)
committerRobert Bradshaw <robertwb@math.washington.edu>
Fri, 15 Jan 2010 01:22:18 +0000 (17:22 -0800)
tests/run/cpp_operators.pyx [new file with mode: 0644]
tests/run/cpp_operators_helper.h [new file with mode: 0644]

diff --git a/tests/run/cpp_operators.pyx b/tests/run/cpp_operators.pyx
new file mode 100644 (file)
index 0000000..338b355
--- /dev/null
@@ -0,0 +1,68 @@
+cimport cython
+from cython cimport dereference as deref
+
+cdef extern from "cpp_operators_helper.h":
+    cdef cppclass TestOps:
+
+        char* operator+()
+        char* operator-()
+        char* operator*()
+        char* operator~()
+
+        char* operator++()
+        char* operator--()
+        char* operator++(int)
+        char* operator--(int)
+
+        char* operator+(int)
+        char* operator-(int)
+        char* operator*(int)
+        char* operator/(int)
+        char* operator%(int)
+
+def test_unops():
+    """
+    >>> test_unops()
+    unary+
+    unary-
+    unary~
+    unary*
+    """
+    cdef TestOps* t = new TestOps()
+    print +t[0]
+    print -t[0]
+    print ~t[0]
+    print deref(t[0])
+    del t
+
+def test_incdec():
+    """
+    >>> test_incdec()
+    unary++
+    unary--
+    post++
+    post--
+    """
+    cdef TestOps* t = new TestOps()
+    print cython.preincrement(t[0])
+    print cython.predecrement(t[0])
+    print cython.postincrement(t[0])
+    print cython.postdecrement(t[0])
+    del t
+
+def test_binop():
+    """
+    >>> test_binop()
+    binary+
+    binary-
+    binary*
+    binary/
+    binary%
+    """
+    cdef TestOps* t = new TestOps()
+    print t[0] + 1
+    print t[0] - 1
+    print t[0] * 1
+    print t[0] / 1
+    print t[0] % 1
+    del t
diff --git a/tests/run/cpp_operators_helper.h b/tests/run/cpp_operators_helper.h
new file mode 100644 (file)
index 0000000..545266a
--- /dev/null
@@ -0,0 +1,27 @@
+#define UN_OP(op) const char* operator op () { return "unary"#op; }
+#define POST_UN_OP(op) const char* operator op (int x) { return "post"#op; }
+#define BIN_OP(op) const char* operator op (int x) { return "binary"#op; }
+
+class TestOps {
+
+public:
+
+    UN_OP(-);
+    UN_OP(+);
+    UN_OP(*);
+    UN_OP(~);
+    UN_OP(!);
+    UN_OP(&);
+    
+    UN_OP(++);
+    UN_OP(--);
+    POST_UN_OP(++);
+    POST_UN_OP(--);
+    
+    BIN_OP(+);
+    BIN_OP(-);
+    BIN_OP(*);
+    BIN_OP(/);
+    BIN_OP(%);
+    
+};