rice-jdguyot 1.4.0.p1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +23 -0
- data/Doxyfile +1253 -0
- data/Makefile.am +26 -0
- data/Makefile.in +743 -0
- data/README +1119 -0
- data/README.mingw +8 -0
- data/Rakefile +33 -0
- data/aclocal.m4 +874 -0
- data/bootstrap +8 -0
- data/config.guess +1500 -0
- data/config.sub +1616 -0
- data/configure +7461 -0
- data/configure.ac +52 -0
- data/depcomp +584 -0
- data/doxygen.ac +314 -0
- data/doxygen.am +186 -0
- data/extconf.rb +34 -0
- data/install-sh +507 -0
- data/missing +367 -0
- data/post-autoconf.rb +22 -0
- data/post-automake.rb +28 -0
- data/rice/Address_Registration_Guard.cpp +22 -0
- data/rice/Address_Registration_Guard.hpp +7 -0
- data/rice/Address_Registration_Guard.ipp +37 -0
- data/rice/Address_Registration_Guard_defn.hpp +75 -0
- data/rice/Allocation_Strategies.hpp +37 -0
- data/rice/Arg.hpp +8 -0
- data/rice/Arg_impl.hpp +127 -0
- data/rice/Arg_operators.cpp +21 -0
- data/rice/Arg_operators.hpp +19 -0
- data/rice/Array.hpp +220 -0
- data/rice/Array.ipp +263 -0
- data/rice/Builtin_Object.hpp +8 -0
- data/rice/Builtin_Object.ipp +50 -0
- data/rice/Builtin_Object_defn.hpp +51 -0
- data/rice/Class.cpp +57 -0
- data/rice/Class.hpp +8 -0
- data/rice/Class.ipp +6 -0
- data/rice/Class_defn.hpp +83 -0
- data/rice/Constructor.hpp +367 -0
- data/rice/Critical_Guard.hpp +40 -0
- data/rice/Critical_Guard.ipp +26 -0
- data/rice/Data_Object.hpp +8 -0
- data/rice/Data_Object.ipp +133 -0
- data/rice/Data_Object_defn.hpp +132 -0
- data/rice/Data_Type.cpp +54 -0
- data/rice/Data_Type.hpp +8 -0
- data/rice/Data_Type.ipp +365 -0
- data/rice/Data_Type_defn.hpp +261 -0
- data/rice/Data_Type_fwd.hpp +12 -0
- data/rice/Director.cpp +13 -0
- data/rice/Director.hpp +39 -0
- data/rice/Enum.hpp +117 -0
- data/rice/Enum.ipp +246 -0
- data/rice/Exception.cpp +59 -0
- data/rice/Exception.hpp +9 -0
- data/rice/Exception_Base.hpp +8 -0
- data/rice/Exception_Base.ipp +13 -0
- data/rice/Exception_Base_defn.hpp +27 -0
- data/rice/Exception_defn.hpp +69 -0
- data/rice/Hash.hpp +210 -0
- data/rice/Hash.ipp +338 -0
- data/rice/Identifier.cpp +8 -0
- data/rice/Identifier.hpp +50 -0
- data/rice/Identifier.ipp +33 -0
- data/rice/Jump_Tag.hpp +24 -0
- data/rice/Makefile.am +130 -0
- data/rice/Makefile.in +716 -0
- data/rice/Module.cpp +84 -0
- data/rice/Module.hpp +8 -0
- data/rice/Module.ipp +6 -0
- data/rice/Module_defn.hpp +88 -0
- data/rice/Module_impl.hpp +281 -0
- data/rice/Module_impl.ipp +348 -0
- data/rice/Object.cpp +160 -0
- data/rice/Object.hpp +8 -0
- data/rice/Object.ipp +19 -0
- data/rice/Object_defn.hpp +191 -0
- data/rice/Require_Guard.hpp +21 -0
- data/rice/String.cpp +94 -0
- data/rice/String.hpp +89 -0
- data/rice/Struct.cpp +117 -0
- data/rice/Struct.hpp +162 -0
- data/rice/Struct.ipp +26 -0
- data/rice/Symbol.cpp +25 -0
- data/rice/Symbol.hpp +66 -0
- data/rice/Symbol.ipp +44 -0
- data/rice/VM.cpp +92 -0
- data/rice/VM.hpp +32 -0
- data/rice/config.hpp +41 -0
- data/rice/config.hpp.in +40 -0
- data/rice/detail/Arguments.hpp +118 -0
- data/rice/detail/Auto_Function_Wrapper.hpp +829 -0
- data/rice/detail/Auto_Function_Wrapper.ipp +3391 -0
- data/rice/detail/Auto_Member_Function_Wrapper.hpp +828 -0
- data/rice/detail/Auto_Member_Function_Wrapper.ipp +2503 -0
- data/rice/detail/Caster.hpp +103 -0
- data/rice/detail/Exception_Handler.hpp +8 -0
- data/rice/detail/Exception_Handler.ipp +68 -0
- data/rice/detail/Exception_Handler_defn.hpp +96 -0
- data/rice/detail/Iterator.hpp +93 -0
- data/rice/detail/Not_Copyable.hpp +25 -0
- data/rice/detail/Wrapped_Function.hpp +33 -0
- data/rice/detail/cfp.hpp +24 -0
- data/rice/detail/cfp.ipp +51 -0
- data/rice/detail/check_ruby_type.cpp +27 -0
- data/rice/detail/check_ruby_type.hpp +23 -0
- data/rice/detail/creation_funcs.hpp +37 -0
- data/rice/detail/creation_funcs.ipp +36 -0
- data/rice/detail/default_allocation_func.hpp +23 -0
- data/rice/detail/default_allocation_func.ipp +11 -0
- data/rice/detail/define_method_and_auto_wrap.hpp +31 -0
- data/rice/detail/define_method_and_auto_wrap.ipp +30 -0
- data/rice/detail/demangle.cpp +56 -0
- data/rice/detail/demangle.hpp +19 -0
- data/rice/detail/env.hpp +19 -0
- data/rice/detail/from_ruby.hpp +43 -0
- data/rice/detail/from_ruby.ipp +60 -0
- data/rice/detail/method_data.cpp +159 -0
- data/rice/detail/method_data.hpp +21 -0
- data/rice/detail/mininode.cpp +1220 -0
- data/rice/detail/mininode.hpp +320 -0
- data/rice/detail/node.hpp +13 -0
- data/rice/detail/object_call.hpp +68 -0
- data/rice/detail/object_call.ipp +131 -0
- data/rice/detail/protect.cpp +29 -0
- data/rice/detail/protect.hpp +34 -0
- data/rice/detail/ruby.hpp +93 -0
- data/rice/detail/ruby_version_code.hpp +6 -0
- data/rice/detail/ruby_version_code.hpp.in +6 -0
- data/rice/detail/rubysig.hpp +19 -0
- data/rice/detail/st.hpp +60 -0
- data/rice/detail/to_ruby.hpp +22 -0
- data/rice/detail/to_ruby.ipp +36 -0
- data/rice/detail/traits.hpp +43 -0
- data/rice/detail/win32.hpp +16 -0
- data/rice/detail/wrap_function.hpp +341 -0
- data/rice/detail/wrap_function.ipp +514 -0
- data/rice/global_function.hpp +33 -0
- data/rice/global_function.ipp +22 -0
- data/rice/protect.hpp +91 -0
- data/rice/protect.ipp +1133 -0
- data/rice/ruby_mark.hpp +13 -0
- data/rice/ruby_try_catch.hpp +86 -0
- data/rice/rubypp.rb +97 -0
- data/rice/to_from_ruby.hpp +8 -0
- data/rice/to_from_ruby.ipp +294 -0
- data/rice/to_from_ruby_defn.hpp +71 -0
- data/ruby/Makefile.am +1 -0
- data/ruby/Makefile.in +497 -0
- data/ruby/lib/Makefile.am +3 -0
- data/ruby/lib/Makefile.in +373 -0
- data/ruby/lib/mkmf-rice.rb.in +216 -0
- data/ruby/lib/version.rb +3 -0
- data/ruby.ac +136 -0
- data/sample/Makefile.am +47 -0
- data/sample/Makefile.in +379 -0
- data/sample/enum/extconf.rb +3 -0
- data/sample/enum/sample_enum.cpp +54 -0
- data/sample/enum/test.rb +8 -0
- data/sample/inheritance/animals.cpp +98 -0
- data/sample/inheritance/extconf.rb +3 -0
- data/sample/inheritance/test.rb +7 -0
- data/sample/map/extconf.rb +3 -0
- data/sample/map/map.cpp +81 -0
- data/sample/map/test.rb +7 -0
- data/test/Makefile.am +78 -0
- data/test/Makefile.in +728 -0
- data/test/ext/Makefile.am +43 -0
- data/test/ext/Makefile.in +375 -0
- data/test/ext/t1/Foo.hpp +10 -0
- data/test/ext/t1/extconf.rb +2 -0
- data/test/ext/t1/t1.cpp +15 -0
- data/test/ext/t2/extconf.rb +2 -0
- data/test/ext/t2/t2.cpp +11 -0
- data/test/test_Address_Registration_Guard.cpp +43 -0
- data/test/test_Allocation_Strategies.cpp +77 -0
- data/test/test_Array.cpp +241 -0
- data/test/test_Builtin_Object.cpp +72 -0
- data/test/test_Class.cpp +498 -0
- data/test/test_Constructor.cpp +128 -0
- data/test/test_Critical_Guard.cpp +51 -0
- data/test/test_Data_Object.cpp +275 -0
- data/test/test_Data_Type.cpp +348 -0
- data/test/test_Director.cpp +301 -0
- data/test/test_Enum.cpp +195 -0
- data/test/test_Exception.cpp +46 -0
- data/test/test_Hash.cpp +195 -0
- data/test/test_Identifier.cpp +70 -0
- data/test/test_Jump_Tag.cpp +17 -0
- data/test/test_Memory_Management.cpp +50 -0
- data/test/test_Module.cpp +481 -0
- data/test/test_Object.cpp +148 -0
- data/test/test_String.cpp +94 -0
- data/test/test_Struct.cpp +192 -0
- data/test/test_Symbol.cpp +63 -0
- data/test/test_To_From_Ruby.cpp +277 -0
- data/test/test_VM.cpp +26 -0
- data/test/test_global_functions.cpp +114 -0
- data/test/test_rice.rb +43 -0
- data/test/unittest.cpp +136 -0
- data/test/unittest.hpp +292 -0
- metadata +289 -0
@@ -0,0 +1,348 @@
|
|
1
|
+
#include "unittest.hpp"
|
2
|
+
#include "rice/Data_Type.hpp"
|
3
|
+
#include "rice/Exception.hpp"
|
4
|
+
#include "rice/Constructor.hpp"
|
5
|
+
#include "rice/global_function.hpp"
|
6
|
+
|
7
|
+
using namespace Rice;
|
8
|
+
|
9
|
+
TESTSUITE(Data_Type);
|
10
|
+
|
11
|
+
/**
|
12
|
+
* The tests here are for the feature of taking an instance
|
13
|
+
* of a Ruby-subclass of a Rice wrapped class and passing
|
14
|
+
* that instance back into the Rice wrapper. While that
|
15
|
+
* might be confusing, the test code is pretty straight foward
|
16
|
+
* to see what we're talking about.
|
17
|
+
*/
|
18
|
+
|
19
|
+
namespace {
|
20
|
+
|
21
|
+
/**
|
22
|
+
* The class we will subclass in Ruby
|
23
|
+
*/
|
24
|
+
class Listener {
|
25
|
+
public:
|
26
|
+
Listener() { }
|
27
|
+
|
28
|
+
virtual ~Listener() { }
|
29
|
+
|
30
|
+
virtual int getValue() { return 4; }
|
31
|
+
};
|
32
|
+
|
33
|
+
/**
|
34
|
+
* This class will recieve a new Listener instance
|
35
|
+
* from Ruby
|
36
|
+
*/
|
37
|
+
class ListenerHandler {
|
38
|
+
|
39
|
+
public:
|
40
|
+
|
41
|
+
ListenerHandler() { }
|
42
|
+
|
43
|
+
void addListener(Listener* newList) {
|
44
|
+
mListeners.push_back(newList);
|
45
|
+
}
|
46
|
+
|
47
|
+
int process() {
|
48
|
+
std::vector<Listener*>::iterator i = mListeners.begin();
|
49
|
+
int accum = 0;
|
50
|
+
for(; i != mListeners.end(); i++) {
|
51
|
+
accum += (*i)->getValue();
|
52
|
+
}
|
53
|
+
|
54
|
+
return accum;
|
55
|
+
}
|
56
|
+
|
57
|
+
int listenerCount() { return mListeners.size(); }
|
58
|
+
|
59
|
+
private:
|
60
|
+
std::vector<Listener*> mListeners;
|
61
|
+
};
|
62
|
+
}
|
63
|
+
|
64
|
+
SETUP(Data_Type)
|
65
|
+
{
|
66
|
+
ruby_init();
|
67
|
+
|
68
|
+
define_class<Listener>("Listener")
|
69
|
+
.define_constructor(Constructor<Listener>())
|
70
|
+
.define_method("get_value", &Listener::getValue);
|
71
|
+
|
72
|
+
define_class<ListenerHandler>("ListenerHandler")
|
73
|
+
.define_constructor(Constructor<ListenerHandler>())
|
74
|
+
.define_method("add_listener", &ListenerHandler::addListener)
|
75
|
+
.define_method("process", &ListenerHandler::process)
|
76
|
+
.define_method("listener_count", &ListenerHandler::listenerCount);
|
77
|
+
|
78
|
+
}
|
79
|
+
|
80
|
+
TESTCASE(can_send_ruby_instance_back_into_rice)
|
81
|
+
{
|
82
|
+
Module m = define_module("TestingModule");
|
83
|
+
Object handler = m.instance_eval("@handler = ListenerHandler.new");
|
84
|
+
|
85
|
+
ASSERT_EQUAL(INT2NUM(0), handler.call("listener_count").value());
|
86
|
+
|
87
|
+
m.instance_eval("class MyListener < Listener; end;");
|
88
|
+
m.instance_eval("@handler.add_listener(MyListener.new)");
|
89
|
+
|
90
|
+
ASSERT_EQUAL(INT2NUM(1), handler.call("listener_count").value());
|
91
|
+
ASSERT_EQUAL(INT2NUM(4), handler.call("process").value());
|
92
|
+
|
93
|
+
m.instance_eval("@handler.add_listener(Listener.new)");
|
94
|
+
|
95
|
+
ASSERT_EQUAL(INT2NUM(2), handler.call("listener_count").value());
|
96
|
+
ASSERT_EQUAL(INT2NUM(8), handler.call("process").value());
|
97
|
+
}
|
98
|
+
|
99
|
+
/**
|
100
|
+
* The following test SEGFAULTs right now
|
101
|
+
*/
|
102
|
+
/*
|
103
|
+
TESTCASE(no_super_in_constructor_still_works)
|
104
|
+
{
|
105
|
+
Module m = define_module("TestingModule");
|
106
|
+
Object handler = m.instance_eval("@handler = ListenerHandler.new");
|
107
|
+
|
108
|
+
ASSERT_EQUAL(INT2NUM(0), handler.call("listener_count").value());
|
109
|
+
|
110
|
+
// Because of this, there's a constructor but no super call
|
111
|
+
m.instance_eval("class MyListener < Listener; def initialize; @val = 10; end; end;");
|
112
|
+
m.instance_eval("@handler.add_listener(MyListener.new)");
|
113
|
+
|
114
|
+
ASSERT_EQUAL(INT2NUM(1), handler.call("listener_count").value());
|
115
|
+
ASSERT_EQUAL(INT2NUM(4), handler.call("process").value());
|
116
|
+
|
117
|
+
m.instance_eval("@handler.add_listener(MyListener.new)");
|
118
|
+
|
119
|
+
ASSERT_EQUAL(INT2NUM(2), handler.call("listener_count").value());
|
120
|
+
ASSERT_EQUAL(INT2NUM(8), handler.call("process").value());
|
121
|
+
}
|
122
|
+
*/
|
123
|
+
|
124
|
+
/**
|
125
|
+
* Implicit Casting across unrelated types
|
126
|
+
*
|
127
|
+
* Two ways of defining if types are implicitly castable
|
128
|
+
*
|
129
|
+
* 1) operator
|
130
|
+
* 2) constructor
|
131
|
+
*/
|
132
|
+
|
133
|
+
/**
|
134
|
+
* Examples here taken from Ogre's Math library.
|
135
|
+
* This uses the constructor method of casting types.
|
136
|
+
*/
|
137
|
+
namespace
|
138
|
+
{
|
139
|
+
const int degree2Radians = (3.14 / 180.0);
|
140
|
+
const int radian2Degrees = (180.0 / 3.14);
|
141
|
+
|
142
|
+
class Radian;
|
143
|
+
|
144
|
+
class Degree
|
145
|
+
{
|
146
|
+
public:
|
147
|
+
explicit Degree(float d) : val_(d) {}
|
148
|
+
Degree(const Radian& r);
|
149
|
+
|
150
|
+
float valueDegrees() const { return val_; }
|
151
|
+
float valueRadians() const { return val_ * degree2Radians; }
|
152
|
+
|
153
|
+
private:
|
154
|
+
float val_;
|
155
|
+
};
|
156
|
+
|
157
|
+
class Radian
|
158
|
+
{
|
159
|
+
public:
|
160
|
+
explicit Radian(float r) : val_(r) {}
|
161
|
+
Radian(const Degree& d) : val_(d.valueRadians()) {}
|
162
|
+
|
163
|
+
float valueRadians() const { return val_; }
|
164
|
+
float valueDegrees() const { return val_ * radian2Degrees; }
|
165
|
+
|
166
|
+
private:
|
167
|
+
float val_;
|
168
|
+
};
|
169
|
+
|
170
|
+
// Due to circular dependencies, need to define some
|
171
|
+
// methods down here
|
172
|
+
Degree::Degree(const Radian& r)
|
173
|
+
{
|
174
|
+
val_ = r.valueDegrees();
|
175
|
+
}
|
176
|
+
|
177
|
+
/**
|
178
|
+
* And now some methods that work w/ the above two classes
|
179
|
+
*/
|
180
|
+
bool isAcute(Degree degree) {
|
181
|
+
return degree.valueDegrees() < 90;
|
182
|
+
}
|
183
|
+
|
184
|
+
bool isObtuse(Radian radian) {
|
185
|
+
return radian.valueDegrees() > 90 && radian.valueDegrees() <= 180;
|
186
|
+
}
|
187
|
+
|
188
|
+
bool isRight(Degree* degree) {
|
189
|
+
return degree->valueDegrees() == 90;
|
190
|
+
}
|
191
|
+
}
|
192
|
+
|
193
|
+
TESTCASE(can_define_implicit_type_conversions_across_wrapped_types)
|
194
|
+
{
|
195
|
+
define_class<Degree>("Degree")
|
196
|
+
.define_constructor(Constructor<Degree, float>());
|
197
|
+
|
198
|
+
define_class<Radian>("Radian")
|
199
|
+
.define_constructor(Constructor<Radian, float>());
|
200
|
+
|
201
|
+
define_implicit_cast<Degree, Radian>();
|
202
|
+
define_implicit_cast<Radian, Degree>();
|
203
|
+
|
204
|
+
define_global_function("is_acute", &isAcute);
|
205
|
+
define_global_function("is_obtuse", &isObtuse);
|
206
|
+
define_global_function("is_right", &isRight);
|
207
|
+
|
208
|
+
Module m = define_module("TestingModule");
|
209
|
+
Object result;
|
210
|
+
|
211
|
+
// ACUTE
|
212
|
+
result = m.instance_eval("is_acute(Degree.new(75))");
|
213
|
+
ASSERT(from_ruby<bool>(result.value()));
|
214
|
+
|
215
|
+
result = m.instance_eval("is_acute(Radian.new(2.0))");
|
216
|
+
ASSERT(!from_ruby<bool>(result.value()));
|
217
|
+
|
218
|
+
// OBTUSE
|
219
|
+
result = m.instance_eval("is_obtuse(Degree.new(75))");
|
220
|
+
ASSERT(!from_ruby<bool>(result.value()));
|
221
|
+
|
222
|
+
result = m.instance_eval("is_obtuse(Radian.new(2.0))");
|
223
|
+
ASSERT(from_ruby<bool>(result.value()));
|
224
|
+
|
225
|
+
// RIGHT
|
226
|
+
result = m.instance_eval("is_right(Degree.new(90))");
|
227
|
+
ASSERT(from_ruby<bool>(result.value()));
|
228
|
+
|
229
|
+
result = m.instance_eval("is_right(Radian.new(2.0))");
|
230
|
+
ASSERT(!from_ruby<bool>(result.value()));
|
231
|
+
}
|
232
|
+
|
233
|
+
namespace {
|
234
|
+
class Explicit
|
235
|
+
{
|
236
|
+
public:
|
237
|
+
Explicit(float v) {
|
238
|
+
value = v;
|
239
|
+
}
|
240
|
+
|
241
|
+
Explicit(const Degree &d) {
|
242
|
+
value = d.valueDegrees();
|
243
|
+
}
|
244
|
+
|
245
|
+
float getValue() { return value; }
|
246
|
+
|
247
|
+
private:
|
248
|
+
float value;
|
249
|
+
};
|
250
|
+
|
251
|
+
float getExplicitValue(Explicit* v) {
|
252
|
+
return v->getValue();
|
253
|
+
}
|
254
|
+
}
|
255
|
+
|
256
|
+
TESTCASE(supports_multiple_implicit_conversions_for_a_type)
|
257
|
+
{
|
258
|
+
define_class<Degree>("Degree")
|
259
|
+
.define_constructor(Constructor<Degree, float>());
|
260
|
+
|
261
|
+
define_class<Radian>("Radian")
|
262
|
+
.define_constructor(Constructor<Radian, float>());
|
263
|
+
|
264
|
+
define_class<Explicit>("Explicit")
|
265
|
+
.define_constructor(Constructor<Explicit, float>());
|
266
|
+
|
267
|
+
define_global_function("is_obtuse", &isObtuse);
|
268
|
+
define_global_function("explicit_value", &getExplicitValue);
|
269
|
+
|
270
|
+
define_implicit_cast<Radian, Degree>();
|
271
|
+
define_implicit_cast<Degree, Radian>();
|
272
|
+
define_implicit_cast<Degree, Explicit>();
|
273
|
+
|
274
|
+
Module m = define_module("TestingModule");
|
275
|
+
Object result;
|
276
|
+
|
277
|
+
result = m.instance_eval("is_obtuse(Degree.new(75))");
|
278
|
+
ASSERT(!from_ruby<bool>(result.value()));
|
279
|
+
|
280
|
+
result = m.instance_eval("explicit_value(Degree.new(75))");
|
281
|
+
ASSERT_EQUAL(75.0, from_ruby<float>(result.value()));
|
282
|
+
}
|
283
|
+
|
284
|
+
/**
|
285
|
+
* Sample taken and modified from boost::python::implicit:
|
286
|
+
* http://www.boost.org/doc/libs/1_41_0/libs/python/doc/v2/implicit.html
|
287
|
+
*
|
288
|
+
* This is the operator version of casting and shows that this works for
|
289
|
+
* base types as well as defined types
|
290
|
+
*/
|
291
|
+
/*
|
292
|
+
namespace {
|
293
|
+
struct Real
|
294
|
+
{
|
295
|
+
Real(int x)
|
296
|
+
: v(x)
|
297
|
+
{}
|
298
|
+
|
299
|
+
operator int() const
|
300
|
+
{
|
301
|
+
return v;
|
302
|
+
}
|
303
|
+
|
304
|
+
int v;
|
305
|
+
};
|
306
|
+
|
307
|
+
int realValue(Real const& x)
|
308
|
+
{
|
309
|
+
return x.v;
|
310
|
+
}
|
311
|
+
|
312
|
+
Real makeReal(int n)
|
313
|
+
{
|
314
|
+
return Real(n);
|
315
|
+
}
|
316
|
+
}
|
317
|
+
|
318
|
+
TESTCASE(can_define_implicit_type_conversions_to_base_types)
|
319
|
+
{
|
320
|
+
define_class<Real>("Real")
|
321
|
+
.define_constructor(Constructor<Real, int>());
|
322
|
+
|
323
|
+
// Define the conversion rules
|
324
|
+
define_implicit_cast<Real, int>();
|
325
|
+
define_implicit_cast<int, Real>();
|
326
|
+
|
327
|
+
define_global_function("real_value", &realValue);
|
328
|
+
define_global_function("make_real", &makeReal);
|
329
|
+
|
330
|
+
Module m = define_module("TestingModule");
|
331
|
+
|
332
|
+
// As Real object
|
333
|
+
Object result = m.instance_eval("real_value( Real.new(4) )");
|
334
|
+
ASSERT_EQUAL(4, from_ruby<int>(result.value()));
|
335
|
+
|
336
|
+
// As fixnum (int)
|
337
|
+
result = m.instance_eval("real_value(4)");
|
338
|
+
ASSERT_EQUAL(4, from_ruby<int>(result.value()));
|
339
|
+
|
340
|
+
// As Real object
|
341
|
+
result = m.instance_eval("r = make_real( Real.new(6) ); real_value(r)");
|
342
|
+
ASSERT_EQUAL(6, from_ruby<int>(result.value()));
|
343
|
+
|
344
|
+
// As fixnum (int)
|
345
|
+
result = m.instance_eval("r = make_real(6); real_value(r)");
|
346
|
+
ASSERT_EQUAL(6, from_ruby<int>(result.value()));
|
347
|
+
}
|
348
|
+
*/
|
@@ -0,0 +1,301 @@
|
|
1
|
+
#include "unittest.hpp"
|
2
|
+
#include "rice/Director.hpp"
|
3
|
+
#include "rice/Constructor.hpp"
|
4
|
+
#include "rice/Data_Type.hpp"
|
5
|
+
|
6
|
+
#include <vector>
|
7
|
+
#include <iostream>
|
8
|
+
|
9
|
+
using namespace std;
|
10
|
+
using namespace Rice;
|
11
|
+
|
12
|
+
TESTSUITE(Director);
|
13
|
+
|
14
|
+
namespace {
|
15
|
+
/**
|
16
|
+
* Abstract base class
|
17
|
+
*/
|
18
|
+
class Worker {
|
19
|
+
public:
|
20
|
+
virtual ~Worker() { }
|
21
|
+
|
22
|
+
int getNumber() { return 12; }
|
23
|
+
|
24
|
+
virtual int doSomething(int num) { return num * 4; }
|
25
|
+
|
26
|
+
virtual int process(int num) = 0;
|
27
|
+
};
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Subclass that implements pure virtual
|
31
|
+
*/
|
32
|
+
class MultiplyWorker : public Worker {
|
33
|
+
public:
|
34
|
+
virtual ~MultiplyWorker() { }
|
35
|
+
|
36
|
+
virtual int process(int num) { return num * 2; }
|
37
|
+
};
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Class to handle workers
|
41
|
+
*/
|
42
|
+
class Handler {
|
43
|
+
std::vector<Worker*> mWorkers;
|
44
|
+
|
45
|
+
public:
|
46
|
+
|
47
|
+
void addWorker(Worker* worker) { mWorkers.push_back(worker); }
|
48
|
+
|
49
|
+
int processWorkers(int start) {
|
50
|
+
std::vector<Worker*>::iterator i = mWorkers.begin();
|
51
|
+
int results = start;
|
52
|
+
|
53
|
+
for(; i != mWorkers.end(); i++) {
|
54
|
+
results = (*i)->process(results);
|
55
|
+
}
|
56
|
+
|
57
|
+
return results;
|
58
|
+
}
|
59
|
+
};
|
60
|
+
|
61
|
+
/**
|
62
|
+
* Our Director wrapper of Worker
|
63
|
+
*/
|
64
|
+
class WorkerDirector : public Worker, public Rice::Director {
|
65
|
+
public:
|
66
|
+
WorkerDirector(Object self) : Director(self) { }
|
67
|
+
|
68
|
+
virtual int process(int num) {
|
69
|
+
return from_ruby<int>( getSelf().call("process", num) );
|
70
|
+
}
|
71
|
+
|
72
|
+
int default_process(int num) {
|
73
|
+
raisePureVirtual();
|
74
|
+
}
|
75
|
+
|
76
|
+
virtual int doSomething(int num) {
|
77
|
+
return from_ruby<int>( getSelf().call("do_something", num) );
|
78
|
+
}
|
79
|
+
|
80
|
+
int default_doSomething(int num) {
|
81
|
+
return Worker::doSomething(num);
|
82
|
+
}
|
83
|
+
};
|
84
|
+
};
|
85
|
+
|
86
|
+
SETUP(Director)
|
87
|
+
{
|
88
|
+
ruby_init();
|
89
|
+
}
|
90
|
+
|
91
|
+
TESTCASE(exposes_worker_as_instantiatable_class)
|
92
|
+
{
|
93
|
+
define_class<Worker>("Worker")
|
94
|
+
.define_director<WorkerDirector>()
|
95
|
+
.define_constructor(Constructor<WorkerDirector, Object>())
|
96
|
+
.define_method("get_number", &Worker::getNumber);
|
97
|
+
|
98
|
+
Module m = define_module("Testing");
|
99
|
+
Object result = m.instance_eval("worker = Worker.new; worker.get_number");
|
100
|
+
|
101
|
+
ASSERT_EQUAL(12, from_ruby<int>(result.value()));
|
102
|
+
}
|
103
|
+
|
104
|
+
TESTCASE(can_call_virtual_methods_on_base_class)
|
105
|
+
{
|
106
|
+
define_class<Worker>("Worker")
|
107
|
+
.define_director<WorkerDirector>()
|
108
|
+
.define_constructor(Constructor<WorkerDirector, Object>())
|
109
|
+
.define_method("get_number", &Worker::getNumber)
|
110
|
+
.define_method("do_something", &WorkerDirector::default_doSomething);
|
111
|
+
|
112
|
+
Module m = define_module("Testing");
|
113
|
+
|
114
|
+
Object result = m.instance_eval("worker = Worker.new; worker.do_something(4)");
|
115
|
+
|
116
|
+
ASSERT_EQUAL(16, from_ruby<int>(result.value()));
|
117
|
+
}
|
118
|
+
|
119
|
+
TESTCASE(super_calls_pass_execution_up_the_inheritance_chain)
|
120
|
+
{
|
121
|
+
define_class<Worker>("Worker")
|
122
|
+
.define_director<WorkerDirector>()
|
123
|
+
.define_constructor(Constructor<WorkerDirector, Object>())
|
124
|
+
.define_method("do_something", &WorkerDirector::default_doSomething);
|
125
|
+
|
126
|
+
Module m = define_module("Testing");
|
127
|
+
m.instance_eval("class RubyWorker < Worker; def do_something(num); super * num; end; end");
|
128
|
+
|
129
|
+
Object result = m.instance_eval("worker = RubyWorker.new; worker.do_something(10)");
|
130
|
+
|
131
|
+
ASSERT_EQUAL(400, from_ruby<int>(result.value()));
|
132
|
+
}
|
133
|
+
|
134
|
+
TESTCASE(super_calls_on_pure_virtual_raise_error)
|
135
|
+
{
|
136
|
+
define_class<Worker>("Worker")
|
137
|
+
.define_director<WorkerDirector>()
|
138
|
+
.define_constructor(Constructor<WorkerDirector, Object>())
|
139
|
+
.define_method("process", &WorkerDirector::default_process);
|
140
|
+
|
141
|
+
Module m = define_module("Testing");
|
142
|
+
m.instance_eval("class RubyWorker < Worker; def process(num); super; end; end");
|
143
|
+
|
144
|
+
ASSERT_EXCEPTION_CHECK(
|
145
|
+
Exception,
|
146
|
+
m.instance_eval("worker = RubyWorker.new; worker.process(10)"),
|
147
|
+
ASSERT_EQUAL(
|
148
|
+
Object(rb_eNotImpError),
|
149
|
+
Object(CLASS_OF(ex.value()))
|
150
|
+
)
|
151
|
+
);
|
152
|
+
}
|
153
|
+
|
154
|
+
TESTCASE(polymorphic_calls_head_down_the_call_chain)
|
155
|
+
{
|
156
|
+
define_class<Handler>("Handler")
|
157
|
+
.define_constructor(Constructor<Handler>())
|
158
|
+
.define_method("add_worker", &Handler::addWorker)
|
159
|
+
.define_method("process_workers", &Handler::processWorkers);
|
160
|
+
|
161
|
+
define_class<Worker>("Worker")
|
162
|
+
.define_director<WorkerDirector>()
|
163
|
+
.define_constructor(Constructor<WorkerDirector, Object>())
|
164
|
+
.define_method("process", &WorkerDirector::default_process);
|
165
|
+
|
166
|
+
Module m = define_module("Testing");
|
167
|
+
|
168
|
+
m.instance_eval(
|
169
|
+
"class EchoWorker < Worker; def process(num); num + 2; end; end;"
|
170
|
+
"class DoubleWorker < Worker; def process(num); num * 2; end; end;"
|
171
|
+
"$handler = Handler.new;"
|
172
|
+
"$handler.add_worker(EchoWorker.new);"
|
173
|
+
"$handler.add_worker(DoubleWorker.new);"
|
174
|
+
);
|
175
|
+
|
176
|
+
Object result = m.instance_eval("$handler.process_workers(5)");
|
177
|
+
|
178
|
+
// Hit's EchoWorker, so 5 + 2, then passes that to DoubleWorker, so 7 * 2 = 14
|
179
|
+
ASSERT_EQUAL(14, from_ruby<int>(result.value()));
|
180
|
+
}
|
181
|
+
|
182
|
+
namespace {
|
183
|
+
|
184
|
+
class CallsSelf {
|
185
|
+
public:
|
186
|
+
int doIt(int in) {
|
187
|
+
return doItImpl(in);
|
188
|
+
}
|
189
|
+
|
190
|
+
virtual int doItImpl(int in) = 0;
|
191
|
+
};
|
192
|
+
|
193
|
+
class CallsSelfDirector : public CallsSelf, public Director {
|
194
|
+
|
195
|
+
public:
|
196
|
+
CallsSelfDirector(Object self) : Director(self) { }
|
197
|
+
|
198
|
+
virtual int doItImpl(int in) {
|
199
|
+
return from_ruby<int>( getSelf().call("do_it_impl", in) );
|
200
|
+
}
|
201
|
+
|
202
|
+
int default_doItImpl(int in) {
|
203
|
+
raisePureVirtual();
|
204
|
+
}
|
205
|
+
};
|
206
|
+
|
207
|
+
struct MyCallsSelf : CallsSelf {
|
208
|
+
MyCallsSelf() { }
|
209
|
+
|
210
|
+
int doItImpl(int in) { return in * 12; }
|
211
|
+
};
|
212
|
+
|
213
|
+
// Abstract type return types handled properly
|
214
|
+
CallsSelf* getCallsSelf() {
|
215
|
+
return new MyCallsSelf();
|
216
|
+
}
|
217
|
+
|
218
|
+
// Abstract type Argument types handled properly
|
219
|
+
int doItOnPointer(CallsSelf* obj, int in) {
|
220
|
+
return obj->doIt(in);
|
221
|
+
}
|
222
|
+
|
223
|
+
int doItOnReference(CallsSelf& obj, int in) {
|
224
|
+
return obj.doIt(in);
|
225
|
+
}
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
TESTCASE(mix_of_polymorphic_calls_and_inheritance_dont_cause_infinite_loops)
|
230
|
+
{
|
231
|
+
define_class<CallsSelf>("CallsSelf")
|
232
|
+
.define_director<CallsSelfDirector>()
|
233
|
+
.define_constructor(Constructor<CallsSelfDirector, Rice::Object>())
|
234
|
+
.define_method("do_it_impl", &CallsSelfDirector::default_doItImpl)
|
235
|
+
.define_method("do_it", &CallsSelf::doIt);
|
236
|
+
|
237
|
+
Module m = define_module("Testing");
|
238
|
+
|
239
|
+
Object result = m.instance_eval(
|
240
|
+
"class MySelf < CallsSelf; def do_it_impl(num); num * 10; end; end;"
|
241
|
+
"c = MySelf.new; c.do_it(10)"
|
242
|
+
);
|
243
|
+
|
244
|
+
ASSERT_EQUAL(100, from_ruby<int>(result.value()));
|
245
|
+
}
|
246
|
+
|
247
|
+
TESTCASE(director_class_super_classes_get_type_bound)
|
248
|
+
{
|
249
|
+
Module m = define_module("Testing");
|
250
|
+
m.define_module_function("get_calls_self", &getCallsSelf);
|
251
|
+
|
252
|
+
define_class<CallsSelf>("CallsSelf")
|
253
|
+
.define_director<CallsSelfDirector>()
|
254
|
+
.define_constructor(Constructor<CallsSelfDirector, Rice::Object>())
|
255
|
+
.define_method("do_it_impl", &CallsSelfDirector::default_doItImpl)
|
256
|
+
.define_method("do_it", &CallsSelf::doIt);
|
257
|
+
|
258
|
+
Object result = m.instance_eval("cs = Testing::get_calls_self; cs.do_it(3);");
|
259
|
+
ASSERT_EQUAL(36, from_ruby<int>(result.value()));
|
260
|
+
}
|
261
|
+
|
262
|
+
TESTCASE(director_allows_abstract_types_used_as_parameters_pointers)
|
263
|
+
{
|
264
|
+
Module m = define_module("Testing");
|
265
|
+
m.define_module_function("do_it_on_pointer", &doItOnPointer);
|
266
|
+
|
267
|
+
define_class<CallsSelf>("CallsSelf")
|
268
|
+
.define_director<CallsSelfDirector>()
|
269
|
+
.define_constructor(Constructor<CallsSelfDirector, Rice::Object>())
|
270
|
+
.define_method("do_it_impl", &CallsSelfDirector::default_doItImpl)
|
271
|
+
.define_method("do_it", &CallsSelf::doIt);
|
272
|
+
|
273
|
+
Object result = m.instance_eval(
|
274
|
+
"class MySelf < CallsSelf; def do_it_impl(num); num * 10; end; end;"
|
275
|
+
"c = MySelf.new;"
|
276
|
+
"Testing::do_it_on_pointer(c, 5)"
|
277
|
+
);
|
278
|
+
|
279
|
+
ASSERT_EQUAL(50, from_ruby<int>(result.value()));
|
280
|
+
}
|
281
|
+
/*
|
282
|
+
TESTCASE(director_allows_abstract_types_used_as_parameters_reference)
|
283
|
+
{
|
284
|
+
Module m = define_module("Testing");
|
285
|
+
m.define_module_function("do_it_on_ref", &doItOnReference);
|
286
|
+
|
287
|
+
define_class<CallsSelf>("CallsSelf")
|
288
|
+
.define_director<CallsSelfDirector>()
|
289
|
+
.define_constructor(Constructor<CallsSelfDirector, Rice::Object>())
|
290
|
+
.define_method("do_it_impl", &CallsSelfDirector::default_doItImpl)
|
291
|
+
.define_method("do_it", &CallsSelf::doIt);
|
292
|
+
|
293
|
+
Object result = m.instance_eval(
|
294
|
+
"class MySelf < CallsSelf; def do_it_impl(num); num * 10; end; end;"
|
295
|
+
"c = MySelf.new;"
|
296
|
+
"Testing::do_it_on_ref(c, 3)"
|
297
|
+
);
|
298
|
+
|
299
|
+
ASSERT_EQUAL(30, from_ruby<int>(result.value()));
|
300
|
+
}
|
301
|
+
*/
|