New test objects test suite.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@7502 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
d8ce6dcff7
commit
13b0e65ae6
2 changed files with 175 additions and 0 deletions
80
SWIG/Examples/test-suite/ruby/track_objects_runme.rb
Normal file
80
SWIG/Examples/test-suite/ruby/track_objects_runme.rb
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
require 'track_objects'
|
||||
|
||||
def test_same_ruby_object(foo1, foo2)
|
||||
if not foo1.equal?(foo2)
|
||||
raise "Ruby objects should be the same."
|
||||
end
|
||||
end
|
||||
|
||||
def test_same_cpp_object(foo1, foo2)
|
||||
if not foo1.cpp_equal(foo2)
|
||||
raise "C++ objects should be the same"
|
||||
end
|
||||
end
|
||||
|
||||
bar = Track_objects::Bar.new
|
||||
foo1 = Track_objects::Foo.new()
|
||||
bar.set_unowned_foo(foo1)
|
||||
|
||||
# test_simple_identity
|
||||
foo2 = Track_objects::Foo.new()
|
||||
foo3 = foo2
|
||||
|
||||
test_same_ruby_object(foo2, foo3)
|
||||
test_same_cpp_object(foo2, foo3)
|
||||
|
||||
# test_unowned_foo_identity
|
||||
foo4 = bar.get_unowned_foo()
|
||||
|
||||
test_same_ruby_object(foo1, foo4)
|
||||
test_same_cpp_object(foo1, foo4)
|
||||
|
||||
# test_owned_foo_identity
|
||||
foo5 = bar.get_owned_foo()
|
||||
foo6 = bar.get_owned_foo()
|
||||
|
||||
test_same_ruby_object(foo5, foo6)
|
||||
test_same_cpp_object(foo5, foo6)
|
||||
|
||||
# test_new_foo_identity
|
||||
foo7 = Track_objects::Bar.get_new_foo()
|
||||
foo8 = Track_objects::Bar.get_new_foo()
|
||||
|
||||
if foo7.equal?(foo8)
|
||||
raise "Ruby objects should be different."
|
||||
end
|
||||
|
||||
if foo7.cpp_equal(foo8)
|
||||
raise "C++ objects should be different."
|
||||
end
|
||||
|
||||
# test_set_owned_identity
|
||||
foo9 = Track_objects::Foo.new
|
||||
bar.set_owned_foo(foo9)
|
||||
foo10 = bar.get_owned_foo()
|
||||
|
||||
test_same_ruby_object(foo9, foo10)
|
||||
test_same_cpp_object(foo9, foo10)
|
||||
|
||||
# test_set_owned_identity2
|
||||
begin
|
||||
foo11 = Track_objects::Foo.new
|
||||
bar.set_owned_foo(foo11)
|
||||
foo11 = nil
|
||||
end
|
||||
|
||||
GC.start
|
||||
|
||||
foo12 = bar.get_owned_foo()
|
||||
|
||||
if not (foo12.say_hello == "Hello")
|
||||
raise "Invalid C++ object returned."
|
||||
end
|
||||
|
||||
# test_set_owned_identity3
|
||||
foo13 = bar.get_owned_foo_by_argument()
|
||||
foo14 = bar.get_owned_foo_by_argument()
|
||||
|
||||
test_same_ruby_object(foo13, foo14)
|
||||
test_same_cpp_object(foo13, foo14)
|
||||
|
||||
95
SWIG/Examples/test-suite/track_objects.i
Normal file
95
SWIG/Examples/test-suite/track_objects.i
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
%module track_objects
|
||||
|
||||
%include typemaps.i
|
||||
|
||||
%trackobjects Foo;
|
||||
|
||||
%newobject Bar::get_new_foo;
|
||||
|
||||
%typemap(in, numinputs=0) Foo** foo (Foo *temp) {
|
||||
/* %typemap(in, numinputs=0) Foo** foo */
|
||||
$1 = &temp;
|
||||
}
|
||||
|
||||
%typemap(argout) Foo** foo {
|
||||
/* %typemap(argout) Foo** foo */
|
||||
$result = SWIG_NewPointerObj((void *) *$1, $*1_descriptor, 0);
|
||||
}
|
||||
|
||||
%apply SWIGTYPE *DISOWN {Foo* ownedFoo};
|
||||
|
||||
%inline %{
|
||||
|
||||
class Foo
|
||||
{
|
||||
public:
|
||||
Foo() {}
|
||||
~Foo() {}
|
||||
|
||||
/* Helper method that can be called from Ruby that checks
|
||||
that two Ruby objects are pointing to the same underlying
|
||||
C++ object */
|
||||
bool cpp_equal(const Foo* other)
|
||||
{
|
||||
return (this == other);
|
||||
}
|
||||
|
||||
/* Just a simple method to call on Foo*/
|
||||
char* say_hello()
|
||||
{
|
||||
return "Hello";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class Bar
|
||||
{
|
||||
private:
|
||||
Foo* owned_;
|
||||
Foo* unowned_;
|
||||
public:
|
||||
Bar(): owned_(new Foo), unowned_(0)
|
||||
{
|
||||
}
|
||||
|
||||
~Bar()
|
||||
{
|
||||
delete owned_;
|
||||
}
|
||||
|
||||
/* Test that track objects works with %newobject */
|
||||
static Foo* get_new_foo()
|
||||
{
|
||||
return new Foo;
|
||||
}
|
||||
|
||||
/* Test the same foo Ruby object is created each time */
|
||||
Foo* get_owned_foo()
|
||||
{
|
||||
return owned_;
|
||||
}
|
||||
|
||||
/* Test that track objects works with argout parameters.*/
|
||||
void get_owned_foo_by_argument(Foo** foo)
|
||||
{
|
||||
*foo = owned_;
|
||||
}
|
||||
|
||||
/* Test that track objects works with the DISOWN typemap.*/
|
||||
void set_owned_foo(Foo* ownedFoo)
|
||||
{
|
||||
delete owned_;
|
||||
owned_ = ownedFoo;
|
||||
}
|
||||
|
||||
Foo* get_unowned_foo()
|
||||
{
|
||||
return unowned_;
|
||||
}
|
||||
|
||||
void set_unowned_foo(Foo* foo)
|
||||
{
|
||||
unowned_ = foo;
|
||||
}
|
||||
};
|
||||
%}
|
||||
Loading…
Add table
Add a link
Reference in a new issue