More cpp test, don't need cpp file.
authorRobert Bradshaw <robertwb@math.washington.edu>
Tue, 7 Jul 2009 09:13:38 +0000 (02:13 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Tue, 7 Jul 2009 09:13:38 +0000 (02:13 -0700)
tests/run/cpp_classes.pyx
tests/run/shapes.cpp [deleted file]
tests/run/shapes.h

index e0d7e1b26d60b82fcf643188a4dd783267f2e7d4..d56be4e4e154215df01b2b8ec552fa81d8c40c1a 100644 (file)
@@ -1,29 +1,33 @@
 __doc__ = u"""
     >>> test_new_del()
     >>> test_rect_area(3, 4)
-    12
+    12.0
     >>> test_square_area(15)
-    225
+    (225.0, 225.0)
 """
 
-cdef extern from "shapes.cpp" namespace shapes:
+cdef extern from "shapes.h" namespace shapes:
 
     cdef cppclass Shape:
         float area()
     
+    cdef cppclass Circle(Shape):
+        int radius
+        __init__(int)
+    
     cdef cppclass Rectangle(Shape):
         int width
         int height
         __init__(int, int)
     
-    cdef cppclass Square(Shape):
+    cdef cppclass Square(Rectangle):
         int side
-        __init__(int)
+        # __init__(int) # need function overloading
 
 def test_new_del():
     cdef Rectangle *rect = new Rectangle(10, 20)
-    cdef Square *sqr = new Square(15)
-    del rect, sqr
+    cdef Circle *circ = new Circle(15)
+    del rect, circ
 
 def test_rect_area(w, h):
     cdef Rectangle *rect = new Rectangle(w, h)
@@ -33,7 +37,7 @@ def test_rect_area(w, h):
         del rect
 
 def test_square_area(w):
-    cdef Square *sqr = new Square(w)
+    cdef Square *sqr = new Square(w, w)
     cdef Rectangle *rect = sqr
     try:
         return rect.area(), sqr.area()
diff --git a/tests/run/shapes.cpp b/tests/run/shapes.cpp
deleted file mode 100644 (file)
index 11f6e2f..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-#include "shapes.h"
-
-using namespace shapes;
-
-Rectangle::Rectangle(int width, int height)
-{
-    this->width = width;
-    this->height = height;
-}
-
-Square::Square(int side)
-{
-    this->side = side;
-}
-
index 54c31f08add9977bf331f935329503120c465153..19dd2fce4a232288cbba338253816a20a95385de 100644 (file)
@@ -2,30 +2,43 @@
 #define SHAPES_H
 
 namespace shapes {
-
+    
     class Shape
     {
     public:
         virtual float area() = 0;
         virtual ~Shape() { }
     };
-    
+
     class Rectangle : public Shape
     {
     public:
-        Rectangle(int width, int height);
+        Rectangle(int width, int height) 
+        {
+            this->width = width;
+            this->height = height;
+        }
         float area() { return width * height; }
         int width;
         int height;
     };
-    
-    class Square : public Shape
+
+    class Square : public Rectangle
     {
     public:
-        Square(int side);
-        float area() { return side * side; }
+        Square(int side) : Rectangle(side, side) { this->side = side; }
+        /* need until function overloading in Cython */
+        Square(int side, int ignored) : Rectangle(side, side) { this->side = side; }
         int side;
     };
 
+    class Circle : public Shape {
+    public:
+        Circle(int radius) { this->radius = radius; }
+        float area() { return 3.1415926535897931f * radius; }
+        int radius;
+    };
+
 }
+
 #endif