Upstream version 1.3.40
[profile/ivi/swig.git] / Examples / test-suite / ruby / ruby_track_objects_runme.rb
1 #!/usr/bin/env ruby
2 #
3 # Put description here
4 #
5
6
7
8 #
9
10 require 'swig_assert'
11
12 require 'ruby_track_objects'
13
14 def test_same_ruby_object(foo1, foo2)
15   if not foo1.equal?(foo2)
16     raise "Ruby objects should be the same."
17   end
18 end
19
20 def test_same_cpp_object(foo1, foo2)
21   if not foo1.cpp_equal(foo2)
22     raise "C++ objects should be the same"
23   end
24 end
25
26 bar = Ruby_track_objects::Bar.new
27 foo1 = Ruby_track_objects::Foo.new()
28 bar.set_unowned_foo(foo1)
29   
30 # test_simple_identity
31 foo2 = Ruby_track_objects::Foo.new()
32 foo3 = foo2
33
34 test_same_ruby_object(foo2, foo3)
35 test_same_cpp_object(foo2, foo3)
36
37 # test_unowned_foo_identity
38 foo4 = bar.get_unowned_foo()
39
40 test_same_ruby_object(foo1, foo4)
41 test_same_cpp_object(foo1, foo4)
42
43 # test_owned_foo_identity
44 foo5 = bar.get_owned_foo()
45 foo6 = bar.get_owned_foo()
46
47 test_same_ruby_object(foo5, foo6)
48 test_same_cpp_object(foo5, foo6)
49   
50 # test_new_foo_identity
51 foo7 = Ruby_track_objects::Bar.get_new_foo()
52 foo8 = Ruby_track_objects::Bar.get_new_foo()
53
54 if foo7.equal?(foo8)
55   raise "Ruby objects should be different."
56 end
57
58 if foo7.cpp_equal(foo8)
59   raise "C++ objects should be different."
60 end
61     
62 # test_set_owned_identity
63 foo9 = Ruby_track_objects::Foo.new
64 bar.set_owned_foo(foo9)
65 foo10 = bar.get_owned_foo()
66     
67 test_same_ruby_object(foo9, foo10)
68 test_same_cpp_object(foo9, foo10)
69
70 # test_set_owned_identity2
71 begin
72   foo11 = Ruby_track_objects::Foo.new
73   bar.set_owned_foo(foo11)
74   foo11 = nil
75 end
76    
77 GC.start
78
79 foo12 = bar.get_owned_foo()
80
81 if not (foo12.say_hello == "Hello")
82   raise "Invalid C++ object returned."
83 end
84
85 # test_set_owned_identity3
86 foo13 = bar.get_owned_foo_by_argument()
87 foo14 = bar.get_owned_foo_by_argument()
88
89 test_same_ruby_object(foo13, foo14)
90 test_same_cpp_object(foo13, foo14)
91
92 # Now create the factory
93 factory = Ruby_track_objects::Factory.new
94
95 # Create itemA which is really an itemB 
96 itemA = factory.createItem
97
98 # Check class
99 if itemA.class != Ruby_track_objects::ItemA
100   raise RuntimeError, 'Item should have an ItemA class'
101 end
102
103 # Now downcast
104 itemB = Ruby_track_objects.downcast(itemA)
105
106 if itemB.class != Ruby_track_objects::ItemB
107   raise RuntimeError, 'Item should have an ItemB class'
108 end
109
110 if itemA.eql?(itemB)
111   raise RuntimeError, 'Items should be different'
112 end
113
114
115
116
117