From a828368232fe08e0065c26e07c38f4dafc78aadf Mon Sep 17 00:00:00 2001 From: Andy Cedilnik Date: Fri, 30 Apr 2004 12:17:06 -0400 Subject: [PATCH] ENH: Here is test for swig module --- Tests/SwigTest/CMakeLists.txt | 45 ++++++++++++++++++++++ Tests/SwigTest/example.cxx | 28 ++++++++++++++ Tests/SwigTest/example.h | 39 +++++++++++++++++++ Tests/SwigTest/example.i | 10 +++++ Tests/SwigTest/runme.php4 | 58 +++++++++++++++++++++++++++++ Tests/SwigTest/runme.pike | 53 ++++++++++++++++++++++++++ Tests/SwigTest/runme.pl | 57 ++++++++++++++++++++++++++++ Tests/SwigTest/runme.py | 51 +++++++++++++++++++++++++ Tests/SwigTest/runme.rb | 49 ++++++++++++++++++++++++ Tests/SwigTest/runme.tcl | 50 +++++++++++++++++++++++++ Tests/SwigTest/runme2.tcl | 70 +++++++++++++++++++++++++++++++++++ 11 files changed, 510 insertions(+) create mode 100644 Tests/SwigTest/CMakeLists.txt create mode 100644 Tests/SwigTest/example.cxx create mode 100644 Tests/SwigTest/example.h create mode 100644 Tests/SwigTest/example.i create mode 100644 Tests/SwigTest/runme.php4 create mode 100755 Tests/SwigTest/runme.pike create mode 100644 Tests/SwigTest/runme.pl create mode 100644 Tests/SwigTest/runme.py create mode 100644 Tests/SwigTest/runme.rb create mode 100644 Tests/SwigTest/runme.tcl create mode 100644 Tests/SwigTest/runme2.tcl diff --git a/Tests/SwigTest/CMakeLists.txt b/Tests/SwigTest/CMakeLists.txt new file mode 100644 index 000000000..9fbb1f485 --- /dev/null +++ b/Tests/SwigTest/CMakeLists.txt @@ -0,0 +1,45 @@ +SET(language "python") + + +PROJECT(example_${language}_class) + +FIND_PACKAGE(SWIG) +IF(SWIG_FOUND) + INCLUDE(${SWIG_USE_FILE}) + + IF(${language} MATCHES python) + FIND_PACKAGE(PythonLibs) + INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH}) + ENDIF(${language} MATCHES python) + IF(${language} MATCHES perl) + FIND_PACKAGE(PerlLibs) + INCLUDE_DIRECTORIES(${PERL_INCLUDE_PATH}) + ADD_DEFINITIONS(${PERL_EXTRA_C_FLAGS}) + ENDIF(${language} MATCHES perl) + IF(${language} MATCHES tcl) + FIND_PACKAGE(TCL) + INCLUDE_DIRECTORIES(${TCL_INCLUDE_PATH}) + ENDIF(${language} MATCHES tcl) + IF(${language} MATCHES ruby) + FIND_PACKAGE(Ruby) + INCLUDE_DIRECTORIES(${RUBY_INCLUDE_PATH}) + ENDIF(${language} MATCHES ruby) + IF(${language} MATCHES php4) + FIND_PACKAGE(PHP4) + INCLUDE_DIRECTORIES(${PHP4_INCLUDE_PATH}) + ENDIF(${language} MATCHES php4) + IF(${language} MATCHES pike) + FIND_PACKAGE(Pike) + INCLUDE_DIRECTORIES(${PIKE_INCLUDE_PATH}) + ENDIF(${language} MATCHES pike) + + INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) + + SET(CMAKE_SWIG_FLAGS "") + + SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES CPLUSPLUS ON) + SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES SWIG_FLAGS "-includeall") + SWIG_ADD_MODULE(example "${language}" + example.i example.cxx) + SWIG_LINK_LIBRARIES(example ${PYTHON_LIBRARY}) +ENDIF(SWIG_FOUND) diff --git a/Tests/SwigTest/example.cxx b/Tests/SwigTest/example.cxx new file mode 100644 index 000000000..1e8e203dd --- /dev/null +++ b/Tests/SwigTest/example.cxx @@ -0,0 +1,28 @@ +/* File : example.c */ + +#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 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/Tests/SwigTest/example.h b/Tests/SwigTest/example.h new file mode 100644 index 000000000..46d901361 --- /dev/null +++ b/Tests/SwigTest/example.h @@ -0,0 +1,39 @@ +/* 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/Tests/SwigTest/example.i b/Tests/SwigTest/example.i new file mode 100644 index 000000000..75700b305 --- /dev/null +++ b/Tests/SwigTest/example.i @@ -0,0 +1,10 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + diff --git a/Tests/SwigTest/runme.php4 b/Tests/SwigTest/runme.php4 new file mode 100644 index 000000000..653ced256 --- /dev/null +++ b/Tests/SwigTest/runme.php4 @@ -0,0 +1,58 @@ + diff --git a/Tests/SwigTest/runme.pike b/Tests/SwigTest/runme.pike new file mode 100755 index 000000000..a6377600e --- /dev/null +++ b/Tests/SwigTest/runme.pike @@ -0,0 +1,53 @@ +import .example; + +int main() +{ + // ----- Object creation ----- + + write("Creating some objects:\n"); + Circle c = Circle(10.0); + write(" Created circle.\n"); + Square s = Square(10.0); + write(" Created square.\n"); + + // ----- Access a static member ----- + + write("\nA total of " + Shape_nshapes_get() + " shapes were created\n"); + + // ----- Member data access ----- + + // Set the location of the object + + c->x_set(20.0); + c->y_set(30.0); + + s->x_set(-10.0); + s->y_set(5.0); + + write("\nHere is their current position:\n"); + write(" Circle = (%f, %f)\n", c->x_get(), c->y_get()); + write(" Square = (%f, %f)\n", s->x_get(), s->y_get()); + + // ----- Call some methods ----- + + write("\nHere are some properties of the shapes:\n"); + write(" The circle:\n"); + write(" area = %f.\n", c->area()); + write(" perimeter = %f.\n", c->perimeter()); + write(" The square:\n"); + write(" area = %f.\n", s->area()); + write(" perimeter = %f.\n", s->perimeter()); + + write("\nGuess I'll clean up now\n"); + + /* See if we can force 's' to be garbage-collected */ + s = 0; + + /* Now we should be down to only 1 shape */ + write("%d shapes remain\n", Shape_nshapes_get()); + + /* Done */ + write("Goodbye\n"); + + return 0; +} diff --git a/Tests/SwigTest/runme.pl b/Tests/SwigTest/runme.pl new file mode 100644 index 000000000..5bfb3d83a --- /dev/null +++ b/Tests/SwigTest/runme.pl @@ -0,0 +1,57 @@ +# file: runme.pl + +# This file illustrates the low-level C++ interface +# created by SWIG. In this case, all of our C++ classes +# get converted into function calls. + +use example; + +# ----- Object creation ----- + +print "Creating some objects:\n"; +$c = examplec::new_Circle(10); +print " Created circle $c\n"; +$s = examplec::new_Square(10); +print " Created square $s\n"; + +# ----- Access a static member ----- + +print "\nA total of $examplec::Shape_nshapes shapes were created\n"; + +# ----- Member data access ----- + +# Set the location of the object. +# Note: methods in the base class Shape are used since +# x and y are defined there. + +examplec::Shape_x_set($c, 20); +examplec::Shape_y_set($c, 30); +examplec::Shape_x_set($s,-10); +examplec::Shape_y_set($s,5); + +print "\nHere is their current position:\n"; +print " Circle = (",examplec::Shape_x_get($c),",", examplec::Shape_y_get($c),")\n"; +print " Square = (",examplec::Shape_x_get($s),",", examplec::Shape_y_get($s),")\n"; + +# ----- Call some methods ----- + +print "\nHere are some properties of the shapes:\n"; +foreach $o ($c,$s) { + print " $o\n"; + print " area = ", examplec::Shape_area($o), "\n"; + print " perimeter = ", examplec::Shape_perimeter($o), "\n"; + } +# Notice how the Shape_area() and Shape_perimeter() functions really +# invoke the appropriate virtual method on each object. + +# ----- Delete everything ----- + +print "\nGuess I'll clean up now\n"; + +# Note: this invokes the virtual destructor +examplec::delete_Shape($c); +examplec::delete_Shape($s); + +print $examplec::Shape_nshapes," shapes remain\n"; +print "Goodbye\n"; + diff --git a/Tests/SwigTest/runme.py b/Tests/SwigTest/runme.py new file mode 100644 index 000000000..42a5aa363 --- /dev/null +++ b/Tests/SwigTest/runme.py @@ -0,0 +1,51 @@ +# file: runme.py + +# This file illustrates the shadow-class C++ interface generated +# by SWIG. + +import example + +# ----- Object creation ----- + +print "Creating some objects:" +c = example.Circle(10) +print " Created circle", c +s = example.Square(10) +print " Created square", s + +# ----- Access a static member ----- + +print "\nA total of", example.cvar.Shape_nshapes,"shapes were created" + +# ----- Member data access ----- + +# Set the location of the object + +c.x = 20 +c.y = 30 + +s.x = -10 +s.y = 5 + +print "\nHere is their current position:" +print " Circle = (%f, %f)" % (c.x,c.y) +print " Square = (%f, %f)" % (s.x,s.y) + +# ----- Call some methods ----- + +print "\nHere are some properties of the shapes:" +for o in [c,s]: + print " ", o + print " area = ", o.area() + print " perimeter = ", o.perimeter() + +print "\nGuess I'll clean up now" + +# Note: this invokes the virtual destructor +del c +del s + +s = 3 +print example.cvar.Shape_nshapes,"shapes remain" +print "Goodbye" + diff --git a/Tests/SwigTest/runme.rb b/Tests/SwigTest/runme.rb new file mode 100644 index 000000000..de73bcd46 --- /dev/null +++ b/Tests/SwigTest/runme.rb @@ -0,0 +1,49 @@ +# file: runme.rb + +# This file illustrates the C++ interface created by SWIG. +# All of our C++ classes get converted into Ruby classes. + +require 'example' + +# ----- Object creation ----- + +print "Creating some objects:\n" +c = Example::Circle.new(10) +print " Created circle #{c}\n" +s = Example::Square.new(10) +print " Created square #{s}\n" + +# ----- Access a static member ----- + +print "\nA total of #{Example::Shape.nshapes} shapes were created\n" + +# ----- Member data access ----- + +# Set the location of the object + +# Notice how we can do this using functions specific to +# the 'Circle' class. +c.x = 20 +c.y = 30 + +# Now use the same functions in the base class +s.x = -10 +s.y = 5 + +print "\nHere is their current position:\n" +print " Circle = (", c.x, ",", c.y, ")\n" +print " Square = (", s.x, ",", s.y, ")\n" + +# ----- Call some methods ----- + +print "\nHere are some properties of the shapes:\n" +for o in [c, s] + print " #{o}\n" + print " area = ", o.area, "\n" + print " perimeter = ", o.perimeter, "\n" +end +# Notice how the Shape#area() and Shape#perimeter() functions really +# invoke the appropriate virtual method on each object. + +print "\n", Example::Shape.nshapes," shapes remain\n" +print "Goodbye\n" diff --git a/Tests/SwigTest/runme.tcl b/Tests/SwigTest/runme.tcl new file mode 100644 index 000000000..c7f472560 --- /dev/null +++ b/Tests/SwigTest/runme.tcl @@ -0,0 +1,50 @@ +# file: runme.tcl + +# This file illustrates the high level C++ interface. +# In this case C++ classes work kind of like Tk widgets + +catch { load ./example[info sharedlibextension] example} + +# ----- Object creation ----- + +puts "Creating some objects:" +Circle c 10 +puts " Created circle [c cget -this]" +Square s 10 +puts " Created square [s cget -this]" + +# ----- Access a static member ----- + +puts "\nA total of $Shape_nshapes shapes were created" + +# ----- Member data access ----- + +# Set the location of the object + +c configure -x 20 -y 30 +s configure -x -10 -y 5 + +puts "\nHere is their current position:" +puts " Circle = ([c cget -x], [c cget -y])" +puts " Square = ([s cget -x], [s cget -y])" + +# ----- Call some methods ----- + +puts "\nHere are some properties of the shapes:" +foreach o "c s" { + puts " [$o cget -this]" + puts " area = [$o area]" + puts " perimeter = [$o perimeter]" +} + +# ----- Delete everything ----- + +puts "\nGuess I'll clean up now" + +# Note: this invokes the virtual destructor +rename c "" +rename s "" + +puts "$Shape_nshapes shapes remain" +puts "Goodbye" + diff --git a/Tests/SwigTest/runme2.tcl b/Tests/SwigTest/runme2.tcl new file mode 100644 index 000000000..88ec2f678 --- /dev/null +++ b/Tests/SwigTest/runme2.tcl @@ -0,0 +1,70 @@ +# file: runme2.tcl + +# This file illustrates the low-level C++ interface +# created by SWIG. In this case, all of our C++ classes +# get converted into function calls. + +catch { load ./example[info sharedlibextension] example} + +# ----- Object creation ----- + +puts "Creating some objects:" +set c [new_Circle 10] +puts " Created circle $c" +set s [new_Square 10] +puts " Created square $s" + +# ----- Access a static member ----- + +puts "\nA total of $Shape_nshapes shapes were created" + +# ----- Member data access ----- + +# Set the location of the object +# Note: the base class must be used since that's where x and y +# were declared. + +Shape_x_set $c 20 +Shape_y_set $c 30 +Shape_x_set $s -10 +Shape_y_set $s 5 + +puts "\nHere is their current position:" +puts " Circle = ([Shape_x_get $c], [Shape_y_get $c])" +puts " Square = ([Shape_x_get $s], [Shape_y_get $s])" + +# ----- Call some methods ----- + +puts "\nHere are some properties of the shapes:" +foreach o "$c $s" { + puts " $o" + puts " area = [Shape_area $o]" + puts " perimeter = [Shape_perimeter $o]" +} +# Notice how the Shape_area() and Shape_perimeter() functions really +# invoke the appropriate virtual method on each object. + +# ----- Try to cause a type error ----- + +puts "\nI'm going to try and break the type system" + +if { [catch { + # Bad script! + Square_area $c # Try to invoke Square method on a Circle + puts " Bad bad SWIG!" + +}]} { + puts " Well, it didn't work. Good SWIG." +} + +# ----- Delete everything ----- + +puts "\nGuess I'll clean up now" + +# Note: this invokes the virtual destructor +delete_Shape $c +delete_Shape $s + +puts "$Shape_nshapes shapes remain" +puts "Goodbye" +