Tests for operators and templates
authorDanilo Freitas <dsurviver@gmail.com>
Mon, 24 Aug 2009 20:31:31 +0000 (17:31 -0300)
committerDanilo Freitas <dsurviver@gmail.com>
Mon, 24 Aug 2009 20:31:31 +0000 (17:31 -0300)
Cython/Compiler/PyrexTypes.py
tests/compile/operators.h [new file with mode: 0644]
tests/compile/operators.pyx [new file with mode: 0644]
tests/compile/templates.h [new file with mode: 0644]
tests/compile/templates.pyx [new file with mode: 0644]

index acadd2b9adceccac66e41815baafe69661b0a2cd..04eaee098ab1805499418ecc12cd8b5824051ce3 100755 (executable)
@@ -1422,8 +1422,8 @@ class CppClassType(CType):
             return PyrexTypes.error_type
         if len(self.templates) != len(template_values):
             error(pos, "%s templated type receives %d arguments, got %d" % 
-                  (base_type, len(self.templates), len(template_values)))
-            return PyrexTypes.error_type
+                  (self.name, len(self.templates), len(template_values)))
+            return error_type
         return self.specialize(dict(zip(self.templates, template_values)))
     
     def specialize(self, values):
diff --git a/tests/compile/operators.h b/tests/compile/operators.h
new file mode 100644 (file)
index 0000000..f719a3d
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef _OPERATORS_H_
+#define _OPERATORS_H_
+
+class Operators
+{
+public:
+    int value;
+    Operators() { }
+    Operators(int value) { this->value = value; }
+    virtual ~Operators() { }
+    Operators operator+(Operators f) { return Operators(this->value + f.value); }
+    Operators operator-(Operators f) { return Operators(this->value - f.value); }
+    Operators operator*(Operators f) { return Operators(this->value * f.value); }
+    Operators operator/(Operators f) { return Operators(this->value / f.value); }
+    bool operator<(Operators f) { return this->value < f.value; }
+    bool operator<=(Operators f) { return this->value <= f.value; }
+    bool operator==(Operators f) { return this->value == f.value; }
+    bool operator!=(Operators f) { return this->value != f.value; }
+    bool operator>(Operators f) { return this->value > f.value; }
+    bool operator>=(Operators f) { return this->value >= f.value; }
+    Operators operator>>(int v) { return Operators(this->value >> v); }
+    Operators operator<<(int v) { return Operators(this->value << v); }
+    Operators operator%(int v) { return Operators(this->value % v); }
+};
+
+#endif
diff --git a/tests/compile/operators.pyx b/tests/compile/operators.pyx
new file mode 100644 (file)
index 0000000..f1f20ab
--- /dev/null
@@ -0,0 +1,36 @@
+cdef extern from "operators.h":
+    cdef cppclass Operators:
+        __init__(int)
+        Operators __add__(Operators, Operators)
+        Operators __sub__(Operators, Operators)
+        Operators __mul__(Operators, Operators)
+        Operators __div__(Operators, Operators)
+        bool __lt__(Operators, Operators)
+        bool __le__(Operators, Operators)
+        bool __eq__(Operators, Operators)
+        bool __ne__(Operators, Operators)
+        bool __gt__(Operators, Operators)
+        bool __ge__(Operators, Operators)
+        Operators __rshift__(Operators, int)
+        Operators __lshift__(Operators, int)
+        Operators __mod__(Operators, int)
+
+cdef int v = 10
+cdef Operators a
+cdef Operators b
+cdef Operators c
+
+c = a + b
+c = a - b
+c = a * b
+c = a / b
+c = a << 2
+c = a >> 1
+c = b % 2
+a < b
+a <= b
+a == b
+a != b
+a > b
+a >= b
+
diff --git a/tests/compile/templates.h b/tests/compile/templates.h
new file mode 100644 (file)
index 0000000..7a13911
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef _TEMPLATES_H_
+#define _TEMPLATES_H_
+
+template<class T>
+class TemplateTest1
+{
+public:
+    T value;
+    int t;
+    TemplateTest1() { }
+    T getValue() { return value; }
+};
+
+template<class T, class U>
+class TemplateTest2
+{
+public:
+    T value1;
+    U value2;
+    TemplateTest2() { }
+    T getValue1() { return value1; }
+    U getValue2() { return value2; }
+};
+
+#endif
diff --git a/tests/compile/templates.pyx b/tests/compile/templates.pyx
new file mode 100644 (file)
index 0000000..fb1b2cc
--- /dev/null
@@ -0,0 +1,28 @@
+cdef extern from "templates.h":
+    cdef cppclass TemplateTest1[T]:
+        __init__()
+        T value
+        int t
+        T getValue()
+
+    cdef cppclass TemplateTest2[T, U]:
+        __init__()
+        T value1
+        U value2
+        T getValue1()
+        U getValue2()
+
+cdef TemplateTest1[int] a
+cdef TemplateTest1[int]* b = new TemplateTest1[int]()
+
+cdef int c = a.getValue()
+c = b.getValue()
+
+cdef TemplateTest2[int, char] d
+cdef TemplateTest2[int, char]* e = new TemplateTest2[int, char]()
+
+c = d.getValue1()
+c = e.getValue2()
+
+cdef char f = d.getValue2()
+f = e.getValue2()