%extend and default args tests

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6327 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2004-10-05 22:01:23 +00:00
commit 2b742879a0
2 changed files with 332 additions and 0 deletions

View file

@ -0,0 +1,107 @@
// Tests %extend with default arguments as well as %extend with default arguments with overloading
%module extend_default
// %extend before the class definition
%extend Before {
Before(int i = -1, double d = -1.0) {
Before *self = new Before();
self->i = i;
self->d = d;
return self;
}
static double AddedStaticMethod(int i = -1, double d = -1) { return i+d; }
double AddedMethod(int i = -1, double d = -1.0) { return i+d; }
}
%inline %{
struct Before {
double d;
int i;
};
%}
// %extend after the class definition
%inline %{
struct After {
double d;
int i;
};
%}
%extend After {
After(int i = -1, double d = -1.0) {
After *self = new After();
self->i = i;
self->d = d;
return self;
}
static double AddedStaticMethod(int i = -1, double d = -1) { return i+d; }
double AddedMethod(int i = -1, double d = -1.0) { return i+d; }
}
// %extend before the class definition - with overloading and default args
%extend OverBefore {
OverBefore(int i = -1, double d = -1.0) {
OverBefore *self = new OverBefore("boo");
self->i = i;
self->d = d;
return self;
}
static double AddedStaticMethod(int i = -1, double d = -1) { return i+d; }
double AddedMethod(int i = -1, double d = -1.0) { return i+d; }
}
%inline %{
struct OverBefore {
OverBefore(const char *str, int i = -2, double d = -2.0) : d(d), i(i) { str=0; }
static double AddedStaticMethod(const char*, int i = -1, double d = -1) { return i+d; }
double AddedMethod(const char*, int i = -1, double d = -1.0) { return i+d; }
double d;
int i;
};
%}
// %extend after the class definition - with overloading and default args
%extend OverAfter {
OverAfter(int i = -1, double d = -1.0) {
OverAfter *self = new OverAfter("boo");
self->i = i;
self->d = d;
return self;
}
static double AddedStaticMethod(int i = -1, double d = -1) { return i+d; }
double AddedMethod(int i = -1, double d = -1.0) { return i+d; }
}
%inline %{
struct OverAfter {
OverAfter(const char *str, int i = -2, double d = -2.0) : d(d), i(i) { str=0; }
static double AddedStaticMethod(const char*, int i = -1, double d = -1) { return i+d; }
double AddedMethod(const char*, int i = -1, double d = -1.0) { return i+d; }
double d;
int i;
};
%}
#pragma SWIG nowarn=-302
//%warnfilter(302) over; // why doesn't this work?
// %extend overrides the class definition
%extend Override {
int over(int a) { return a*a; } // SWIG should give a warning then choose this one over the real one
int overload(int a) { return a*a; } // Similarly, but this one generated uncompileable code in SWIG-1.3.22
}
%inline %{
struct Override {
int over(int a = -1) { return a; }
int ride(int a = -1) { return a; }
int overload(int a) { return a; }
int overload() { return -10; }
};
%}
%extend Override {
int ride(int a) { return a+a; } // SWIG should give a warning then ignore this one
}

View file

@ -0,0 +1,225 @@
import extend_default.*;
public class extend_default_runme {
static {
try {
System.loadLibrary("extend_default");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
// %extend before the class definition
{
Before ex = new Before();
if (ex.getI() != -1.0 && ex.getD() != -1.0)
throw new RuntimeException("Before constructor 1 failed");
ex = new Before(10);
if (ex.getI() != 10.0 && ex.getD() != -1.0)
throw new RuntimeException("Before constructor 2 failed");
ex = new Before(20, 30.0);
if (ex.getI() != 20 && ex.getD() != 30.0)
throw new RuntimeException("Before constructor 3 failed");
}
{
Before ex = new Before();
if (ex.AddedMethod() != -2.0)
throw new RuntimeException("Before AddedMethod 1 failed");
if (ex.AddedMethod(-2) != -3.0)
throw new RuntimeException("Before AddedMethod 2 failed");
if (ex.AddedMethod(-10, -10.0) != -20)
throw new RuntimeException("Before AddedMethod 3 failed");
}
{
if (Before.AddedStaticMethod() != -2.0)
throw new RuntimeException("Before AddedStaticMethod 1 failed");
if (Before.AddedStaticMethod(-2) != -3.0)
throw new RuntimeException("Before AddedStaticMethod 2 failed");
if (Before.AddedStaticMethod(-10, -10.0) != -20)
throw new RuntimeException("Before AddedStaticMethod 3 failed");
}
// %extend after the class definition
{
After ex = new After();
if (ex.getI() != -1.0 && ex.getD() != -1.0)
throw new RuntimeException("After constructor 1 failed");
ex = new After(10);
if (ex.getI() != 10.0 && ex.getD() != -1.0)
throw new RuntimeException("After constructor 2 failed");
ex = new After(20, 30.0);
if (ex.getI() != 20 && ex.getD() != 30.0)
throw new RuntimeException("After constructor 3 failed");
}
{
After ex = new After();
if (ex.AddedMethod() != -2.0)
throw new RuntimeException("After AddedMethod 1 failed");
if (ex.AddedMethod(-2) != -3.0)
throw new RuntimeException("After AddedMethod 2 failed");
if (ex.AddedMethod(-10, -10.0) != -20)
throw new RuntimeException("After AddedMethod 3 failed");
}
{
if (After.AddedStaticMethod() != -2.0)
throw new RuntimeException("After AddedStaticMethod 1 failed");
if (After.AddedStaticMethod(-2) != -3.0)
throw new RuntimeException("After AddedStaticMethod 2 failed");
if (After.AddedStaticMethod(-10, -10.0) != -20)
throw new RuntimeException("After AddedStaticMethod 3 failed");
}
// %extend before the class definition - with overloading and default args
{
OverBefore ex = new OverBefore();
if (ex.getI() != -1.0 && ex.getD() != -1.0)
throw new RuntimeException("OverBefore constructor 1 failed");
ex = new OverBefore(10);
if (ex.getI() != 10.0 && ex.getD() != -1.0)
throw new RuntimeException("OverBefore constructor 2 failed");
ex = new OverBefore(20, 30.0);
if (ex.getI() != 20 && ex.getD() != 30.0)
throw new RuntimeException("OverBefore constructor 3 failed");
}
{
OverBefore ex = new OverBefore();
if (ex.AddedMethod() != -2.0)
throw new RuntimeException("OverBefore AddedMethod 1 failed");
if (ex.AddedMethod(-2) != -3.0)
throw new RuntimeException("OverBefore AddedMethod 2 failed");
if (ex.AddedMethod(-10, -10.0) != -20)
throw new RuntimeException("OverBefore AddedMethod 3 failed");
}
{
if (OverBefore.AddedStaticMethod() != -2.0)
throw new RuntimeException("OverBefore AddedStaticMethod 1 failed");
if (OverBefore.AddedStaticMethod(-2) != -3.0)
throw new RuntimeException("OverBefore AddedStaticMethod 2 failed");
if (OverBefore.AddedStaticMethod(-10, -10.0) != -20)
throw new RuntimeException("OverBefore AddedStaticMethod 3 failed");
}
{
OverBefore ex = new OverBefore("hello");
if (ex.getI() != -2.0 && ex.getD() != -2.0)
throw new RuntimeException("OverBefore overload constructor 1 failed");
ex = new OverBefore("hello", 10);
if (ex.getI() != 10.0 && ex.getD() != -1.0)
throw new RuntimeException("OverBefore overload constructor 2 failed");
ex = new OverBefore("hello", 20, 30.0);
if (ex.getI() != 20 && ex.getD() != 30.0)
throw new RuntimeException("OverBefore overload constructor 3 failed");
}
{
OverBefore ex = new OverBefore("hello");
if (ex.AddedMethod("hello") != -2.0)
throw new RuntimeException("OverBefore overload AddedMethod 1 failed");
if (ex.AddedMethod("hello", -2) != -3.0)
throw new RuntimeException("OverBefore overload AddedMethod 2 failed");
if (ex.AddedMethod("hello", -10, -10.0) != -20)
throw new RuntimeException("OverBefore overload AddedMethod 3 failed");
}
{
if (OverBefore.AddedStaticMethod("hello") != -2.0)
throw new RuntimeException("OverBefore overload AddedStaticMethod 1 failed");
if (OverBefore.AddedStaticMethod("hello", -2) != -3.0)
throw new RuntimeException("OverBefore overload AddedStaticMethod 2 failed");
if (OverBefore.AddedStaticMethod("hello", -10, -10.0) != -20)
throw new RuntimeException("OverBefore overload AddedStaticMethod 3 failed");
}
// %extend after the class definition - with overloading and default args
{
OverAfter ex = new OverAfter();
if (ex.getI() != -1.0 && ex.getD() != -1.0)
throw new RuntimeException("OverAfter constructor 1 failed");
ex = new OverAfter(10);
if (ex.getI() != 10.0 && ex.getD() != -1.0)
throw new RuntimeException("OverAfter constructor 2 failed");
ex = new OverAfter(20, 30.0);
if (ex.getI() != 20 && ex.getD() != 30.0)
throw new RuntimeException("OverAfter constructor 3 failed");
}
{
OverAfter ex = new OverAfter();
if (ex.AddedMethod() != -2.0)
throw new RuntimeException("OverAfter AddedMethod 1 failed");
if (ex.AddedMethod(-2) != -3.0)
throw new RuntimeException("OverAfter AddedMethod 2 failed");
if (ex.AddedMethod(-10, -10.0) != -20)
throw new RuntimeException("OverAfter AddedMethod 3 failed");
}
{
if (OverAfter.AddedStaticMethod() != -2.0)
throw new RuntimeException("OverAfter AddedStaticMethod 1 failed");
if (OverAfter.AddedStaticMethod(-2) != -3.0)
throw new RuntimeException("OverAfter AddedStaticMethod 2 failed");
if (OverAfter.AddedStaticMethod(-10, -10.0) != -20)
throw new RuntimeException("OverAfter AddedStaticMethod 3 failed");
}
{
OverAfter ex = new OverAfter("hello");
if (ex.getI() != -2.0 && ex.getD() != -2.0)
throw new RuntimeException("OverAfter overload constructor 1 failed");
ex = new OverAfter("hello", 10);
if (ex.getI() != 10.0 && ex.getD() != -1.0)
throw new RuntimeException("OverAfter overload constructor 2 failed");
ex = new OverAfter("hello", 20, 30.0);
if (ex.getI() != 20 && ex.getD() != 30.0)
throw new RuntimeException("OverAfter overload constructor 3 failed");
}
{
OverAfter ex = new OverAfter("hello");
if (ex.AddedMethod("hello") != -2.0)
throw new RuntimeException("OverAfter overload AddedMethod 1 failed");
if (ex.AddedMethod("hello", -2) != -3.0)
throw new RuntimeException("OverAfter overload AddedMethod 2 failed");
if (ex.AddedMethod("hello", -10, -10.0) != -20)
throw new RuntimeException("OverAfter overload AddedMethod 3 failed");
}
{
if (OverAfter.AddedStaticMethod("hello") != -2.0)
throw new RuntimeException("OverAfter overload AddedStaticMethod 1 failed");
if (OverAfter.AddedStaticMethod("hello", -2) != -3.0)
throw new RuntimeException("OverAfter overload AddedStaticMethod 2 failed");
if (OverAfter.AddedStaticMethod("hello", -10, -10.0) != -20)
throw new RuntimeException("OverAfter overload AddedStaticMethod 3 failed");
}
// Override
{
Override o = new Override();
if (o.over() != -1)
throw new RuntimeException("override test 1 failed");
if (o.over(10) != 10*10)
throw new RuntimeException("override test 2 failed");
if (o.ride() != -1)
throw new RuntimeException("override test 3 failed");
if (o.ride(10) != 10)
throw new RuntimeException("override test 4 failed");
if (o.overload() != -10)
throw new RuntimeException("override test 5 failed");
if (o.overload(10) != 10*10)
throw new RuntimeException("override test 6 failed");
}
}
}