diff options
Diffstat (limited to 'Examples/contract/simple_cxx')
| -rw-r--r-- | Examples/contract/simple_cxx/example.cxx | 30 | ||||
| -rw-r--r-- | Examples/contract/simple_cxx/example.h | 34 | ||||
| -rw-r--r-- | Examples/contract/simple_cxx/example.i | 28 | ||||
| -rw-r--r-- | Examples/contract/simple_cxx/runme1.py | 33 | ||||
| -rw-r--r-- | Examples/contract/simple_cxx/runme2.py | 44 | ||||
| -rw-r--r-- | Examples/contract/simple_cxx/runme3.py | 57 |
6 files changed, 226 insertions, 0 deletions
diff --git a/Examples/contract/simple_cxx/example.cxx b/Examples/contract/simple_cxx/example.cxx new file mode 100644 index 0000000..e3dd2ca --- /dev/null +++ b/Examples/contract/simple_cxx/example.cxx @@ -0,0 +1,30 @@ +#include "example.h" + +#define M_PI 3.14159265358979323846 + +/* Move the shape to a new location */ +void Shape::move(double dx, double dy) { + x += dx; + y += dy; +} + +int Shape::nshapes = 0; + +double Circle::area(void) { + /* return -1 is to test post-assertion */ + if (radius == 1) + return -1; + return M_PI*radius*radius; +} + +double Circle::perimeter(void) { + return 2*M_PI*radius; +} + +double Square::area(void) { + return width*width; +} + +double Square::perimeter(void) { + return 4*width; +} diff --git a/Examples/contract/simple_cxx/example.h b/Examples/contract/simple_cxx/example.h new file mode 100644 index 0000000..64b7684 --- /dev/null +++ b/Examples/contract/simple_cxx/example.h @@ -0,0 +1,34 @@ +/* File : example.h */ + +class Shape { +public: + Shape() { + nshapes++; + } + virtual ~Shape() { + nshapes--; + }; + double x, y; + void move(double dx, double dy); + virtual double area(void) = 0; + virtual double perimeter(void) = 0; + static int nshapes; +}; + +class Circle : public Shape { +private: + double radius; +public: + Circle(double r) : radius(r) { }; + virtual double area(void); + virtual double perimeter(void); +}; + +class Square : public Shape { +private: + double width; +public: + Square(double w) : width(w) { }; + virtual double area(void); + virtual double perimeter(void); +}; diff --git a/Examples/contract/simple_cxx/example.i b/Examples/contract/simple_cxx/example.i new file mode 100644 index 0000000..9b47409 --- /dev/null +++ b/Examples/contract/simple_cxx/example.i @@ -0,0 +1,28 @@ +%module example + +%contract Circle::Circle(double radius) { +require: + radius > 0; +} + +%contract Circle::area(void) { +ensure: + area > 0; +} + +%contract Shape::move(double dx, double dy) { +require: + dx > 0; +} + +/* should be no effect, since there is no move() for class Circle */ +%contract Circle::move(double dx, double dy) { +require: + dy > 1; +} + +# include must be after contracts +%{ +#include "example.h" +%} +%include "example.h" diff --git a/Examples/contract/simple_cxx/runme1.py b/Examples/contract/simple_cxx/runme1.py new file mode 100644 index 0000000..9028d02 --- /dev/null +++ b/Examples/contract/simple_cxx/runme1.py @@ -0,0 +1,33 @@ +import example + +# Create the Circle object + +r = 2; +print " Creating circle (radium: %d) :" % r +c = example.Circle(r) + +# Set the location of the object + +c.x = 20 +c.y = 30 +print " Here is its current position:" +print " Circle = (%f, %f)" % (c.x,c.y) + +# ----- Call some methods ----- + +print "\n Here are some properties of the Circle:" +print " area = ", c.area() +print " perimeter = ", c.perimeter() +dx = 1; +dy = 1; +print " Moving with (%d, %d)..." % (dx, dy) +c.move(dx, dy) + +del c + +print "===================================" + +# test construction */ +r = -1; +print " Creating circle (radium: %d) :" % r +c = example.Circle(r) diff --git a/Examples/contract/simple_cxx/runme2.py b/Examples/contract/simple_cxx/runme2.py new file mode 100644 index 0000000..5f9c0df --- /dev/null +++ b/Examples/contract/simple_cxx/runme2.py @@ -0,0 +1,44 @@ +import example + +# Create the Circle object + +r = 2; +print " Creating circle (radium: %d) :" % r +c = example.Circle(r) + +# Set the location of the object + +c.x = 20 +c.y = 30 +print " Here is its current position:" +print " Circle = (%f, %f)" % (c.x,c.y) + +# ----- Call some methods ----- + +print "\n Here are some properties of the Circle:" +print " area = ", c.area() +print " perimeter = ", c.perimeter() +dx = 1; +dy = 1; +print " Moving with (%d, %d)..." % (dx, dy) +c.move(dx, dy) + +del c + +print "===================================" + +# test area function */ +r = 1; +print " Creating circle (radium: %d) :" % r +c = example.Circle(r) +# Set the location of the object + +c.x = 20 +c.y = 30 +print " Here is its current position:" +print " Circle = (%f, %f)" % (c.x,c.y) + +# ----- Call some methods ----- + +print "\n Here are some properties of the Circle:" +print " area = ", c.area() diff --git a/Examples/contract/simple_cxx/runme3.py b/Examples/contract/simple_cxx/runme3.py new file mode 100644 index 0000000..a663732 --- /dev/null +++ b/Examples/contract/simple_cxx/runme3.py @@ -0,0 +1,57 @@ +import example + +# Create the Circle object + +r = 2; +print " Creating circle (radium: %d) :" % r +c = example.Circle(r) + +# Set the location of the object + +c.x = 20 +c.y = 30 +print " Here is its current position:" +print " Circle = (%f, %f)" % (c.x,c.y) + +# ----- Call some methods ----- + +print "\n Here are some properties of the Circle:" +print " area = ", c.area() +print " perimeter = ", c.perimeter() +dx = 1; +dy = 1; +print " Moving with (%d, %d)..." % (dx, dy) +c.move(dx, dy) + +del c + +print "===================================" + +# test move function */ +r = 2; +print " Creating circle (radium: %d) :" % r +c = example.Circle(r) +# Set the location of the object + +c.x = 20 +c.y = 30 +print " Here is its current position:" +print " Circle = (%f, %f)" % (c.x,c.y) + +# ----- Call some methods ----- + +print "\n Here are some properties of the Circle:" +print " area = ", c.area() +print " perimeter = ", c.perimeter() + +# no error for Circle's pre-assertion +dx = 1; +dy = -1; +print " Moving with (%d, %d)..." % (dx, dy) +c.move(dx, dy) + +# error with Shape's pre-assertion +dx = -1; +dy = 1; +print " Moving with (%d, %d)..." % (dx, dy) +c.move(dx, dy) |
