new director test

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9213 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2006-07-12 20:18:08 +00:00
commit 55d4c5e59b
5 changed files with 359 additions and 0 deletions

View file

@ -119,6 +119,7 @@ CPP_TEST_CASES += \
director_abstract \
director_basic \
director_classes \
director_classic \
director_constructor \
director_detect \
director_default \

View file

@ -0,0 +1,116 @@
using System;
namespace director_classicNamespace {
public class runme
{
static void Main()
{
{
Person person = new Person();
check(person, "Person");
person.Dispose();
}
{
Person person = new Child();
check(person, "Child");
person.Dispose();
}
{
Person person = new GrandChild();
check(person, "GrandChild");
person.Dispose();
}
{
Person person = new TargetLangPerson();
check(person, "TargetLangPerson");
person.Dispose();
}
{
Person person = new TargetLangChild();
check(person, "TargetLangChild");
person.Dispose();
}
{
Person person = new TargetLangGrandChild();
check(person, "TargetLangGrandChild");
person.Dispose();
}
}
static void check(Person person, String expected) {
String ret;
// Normal target language polymorphic call
ret = person.id();
if (debug)
Console.WriteLine(ret);
if (ret != expected)
throw new Exception("Failed. Received: " + ret + " Expected: " + expected);
// Polymorphic call from C++
Caller caller = new Caller();
caller.setCallback(person);
ret = caller.call();
if (debug)
Console.WriteLine(ret);
if (ret != expected)
throw new Exception("Failed. Received: " + ret + " Expected: " + expected);
// Polymorphic call of object created in target language and passed to C++ and back again
Person baseclass = caller.baseClass();
ret = baseclass.id();
if (debug)
Console.WriteLine(ret);
if (ret != expected)
throw new Exception("Failed. Received: " + ret + " Expected: " + expected);
caller.resetCallback();
if (debug)
Console.WriteLine("----------------------------------------");
}
static bool debug = false;
}
public class TargetLangPerson : Person
{
public TargetLangPerson()
: base()
{
}
public override String id()
{
String identifier = "TargetLangPerson";
return identifier;
}
}
public class TargetLangChild : Child
{
public TargetLangChild()
: base()
{
}
public override String id()
{
String identifier = "TargetLangChild";
return identifier;
}
}
public class TargetLangGrandChild : GrandChild
{
public TargetLangGrandChild()
: base()
{
}
public override String id()
{
String identifier = "TargetLangGrandChild";
return identifier;
}
}
}

View file

@ -0,0 +1,40 @@
%module(directors="1") director_classic
%include "std_string.i"
%feature("director");
%inline %{
#include <cstdio>
#include <iostream>
#include <string>
struct Person {
virtual std::string id() { return "Person"; }
virtual ~Person() {}
};
struct Child : Person {
virtual std::string id() { return "Child"; }
};
struct GrandChild : Child {
virtual std::string id() { return "GrandChild"; }
};
class Caller {
private:
Person *_callback;
public:
Caller(): _callback(0) {}
~Caller() { delCallback(); }
void delCallback() { delete _callback; _callback = 0; }
void setCallback(Person *cb) { delCallback(); _callback = cb; }
void resetCallback() { _callback = 0; }
std::string call() { if (_callback) return _callback->id(); else return "oops"; }
Person* baseClass() { return _callback; }
};
%}

View file

@ -0,0 +1,121 @@
import director_classic.*;
public class director_classic_runme {
static {
try {
System.loadLibrary("director_classic");
} 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[])
{
{
Person person = new Person();
check(person, "Person");
person.delete();
}
{
Person person = new Child();
check(person, "Child");
person.delete();
}
{
Person person = new GrandChild();
check(person, "GrandChild");
person.delete();
}
{
Person person = new TargetLangPerson();
check(person, "TargetLangPerson");
person.delete();
}
{
Person person = new TargetLangChild();
check(person, "TargetLangChild");
person.delete();
}
{
Person person = new TargetLangGrandChild();
check(person, "TargetLangGrandChild");
person.delete();
}
}
static void check(Person person, String expected) {
String ret;
// Normal target language polymorphic call
ret = person.id();
if (debug)
System.out.println(ret);
if (!ret.equals(expected))
throw new RuntimeException("Failed. Received: " + ret + " Expected: " + expected);
// Polymorphic call from C++
Caller caller = new Caller();
caller.setCallback(person);
ret = caller.call();
if (debug)
System.out.println(ret);
if (!ret.equals(expected))
throw new RuntimeException("Failed. Received: " + ret + " Expected: " + expected);
// Polymorphic call of object created in target language and passed to C++ and back again
Person baseclass = caller.baseClass();
ret = baseclass.id();
if (debug)
System.out.println(ret);
if (!ret.equals(expected))
throw new RuntimeException("Failed. Received: " + ret + " Expected: " + expected);
caller.resetCallback();
if (debug)
System.out.println("----------------------------------------");
}
static boolean debug = false;
}
class TargetLangPerson extends Person
{
public TargetLangPerson()
{
super();
}
public String id()
{
String identifier = "TargetLangPerson";
return identifier;
}
}
class TargetLangChild extends Child
{
public TargetLangChild()
{
super();
}
public String id()
{
String identifier = "TargetLangChild";
return identifier;
}
}
class TargetLangGrandChild extends GrandChild
{
public TargetLangGrandChild()
{
super();
}
public String id()
{
String identifier = "TargetLangGrandChild";
return identifier;
}
}

View file

@ -0,0 +1,81 @@
from director_classic import *
class TargetLangPerson(Person):
def __init__(self):
Person.__init__(self)
def id(self):
identifier = "TargetLangPerson"
return identifier
class TargetLangChild(Child):
def __init__(self):
Child.__init__(self)
def id(self):
identifier = "TargetLangChild"
return identifier
class TargetLangGrandChild(GrandChild):
def __init__(self):
GrandChild.__init__(self)
def id(self):
identifier = "TargetLangGrandChild"
return identifier
def check(person, expected):
debug = 0
# Normal target language polymorphic call
ret = person.id()
if (debug):
print(ret)
if (ret != expected):
raise ("Failed. Received: " + ret + " Expected: " + expected)
# Polymorphic call from C++
caller = Caller()
caller.setCallback(person)
ret = caller.call()
if (debug):
print(ret)
if (ret != expected):
raise ("Failed. Received: " + ret + " Expected: " + expected)
# Polymorphic call of object created in target language and passed to C++ and back again
baseclass = caller.baseClass()
ret = baseclass.id()
if (debug):
print(ret)
if (ret != expected):
raise ("Failed. Received: " + ret + " Expected: " + expected)
caller.resetCallback()
if (debug):
print("----------------------------------------")
person = Person();
check(person, "Person");
del person
person = Child();
check(person, "Child");
del person
person = GrandChild();
check(person, "GrandChild");
del person
person = TargetLangPerson();
check(person, "TargetLangPerson");
del person
person = TargetLangChild();
check(person, "TargetLangChild");
del person
person = TargetLangGrandChild();
check(person, "TargetLangGrandChild");
del person