]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/cppbind_test.cxx
CMake: Create list of libs for testing only once
[simgear.git] / simgear / nasal / cppbind / cppbind_test.cxx
1 #include <simgear/math/SGMath.hxx>
2
3 #include "Ghost.hxx"
4 #include "NasalHash.hxx"
5 #include "NasalString.hxx"
6
7 #include <boost/shared_ptr.hpp>
8
9 #include <cstring>
10 #include <iostream>
11
12 #define VERIFY(a) \
13   if( !(a) ) \
14   { \
15     std::cerr << "failed: line " << __LINE__ << ": " << #a << std::endl; \
16     return 1; \
17   }
18
19 struct Base
20 {
21   naRef member(const nasal::CallContext&) { return naNil(); }
22   virtual ~Base(){};
23
24   std::string getString() const { return ""; }
25   void setString(const std::string&) {}
26   void constVoidFunc() const {}
27   size_t test1Arg(const std::string& str) const { return str.length(); }
28   bool test2Args(const std::string& s, bool c) { return c && s.empty(); }
29
30   std::string var;
31   const std::string& getVar() const { return var; }
32   void setVar(const std::string v) { var = v; }
33 };
34
35 void baseVoidFunc(Base& b) {}
36 void baseConstVoidFunc(const Base& b) {}
37 size_t baseFunc2Args(Base& b, int x, const std::string& s) { return x + s.size(); }
38 std::string testPtr(Base& b) { return b.getString(); }
39 void baseFuncCallContext(const Base&, const nasal::CallContext&) {}
40
41 struct Derived:
42   public Base
43 {
44   int _x;
45   int getX() const { return _x; }
46   void setX(int x) { _x = x; }
47 };
48 struct DoubleDerived:
49   public Derived
50 {
51
52 };
53
54 typedef boost::shared_ptr<Base> BasePtr;
55
56 struct DoubleDerived2:
57   public Derived
58 {
59   const BasePtr& getBase() const{return _base;}
60   BasePtr _base;
61 };
62
63 typedef boost::shared_ptr<Derived> DerivedPtr;
64 typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
65 typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
66
67 naRef derivedFreeMember(Derived&, const nasal::CallContext&) { return naNil(); }
68 naRef f_derivedGetX(naContext c, const Derived& d)
69 {
70   return nasal::to_nasal(c, d.getX());
71 }
72 naRef f_freeFunction(nasal::CallContext) { return naNil(); }
73
74 int main(int argc, char* argv[])
75 {
76   naContext c = naNewContext();
77   naRef r;
78
79   using namespace nasal;
80
81   r = to_nasal(c, "Test");
82   VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
83   VERIFY( from_nasal<std::string>(c, r) == "Test" );
84
85   r = to_nasal(c, std::string("Test"));
86   VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
87   VERIFY( from_nasal<std::string>(c, r) == "Test" );
88
89   r = to_nasal(c, 42);
90   VERIFY( naNumValue(r).num == 42 );
91   VERIFY( from_nasal<int>(c, r) == 42 );
92
93   r = to_nasal(c, 4.2f);
94   VERIFY( naNumValue(r).num == 4.2f );
95   VERIFY( from_nasal<float>(c, r) == 4.2f );
96
97   float test_data[3] = {0, 4, 2};
98   r = to_nasal(c, test_data);
99
100   SGVec2f vec(0,2);
101   r = to_nasal(c, vec);
102   VERIFY( from_nasal<SGVec2f>(c, r) == vec );
103
104   std::vector<int> std_vec;
105   r = to_nasal(c, std_vec);
106
107   r = to_nasal(c, "string");
108   try
109   {
110     from_nasal<int>(c, r);
111
112     std::cerr << "failed: Expected bad_nasal_cast to be thrown" << std::endl;
113     return 1;
114   }
115   catch(nasal::bad_nasal_cast&)
116   {}
117
118   Hash hash(c);
119   hash.set("vec", r);
120   hash.set("vec2", vec);
121   hash.set("name", "my-name");
122   hash.set("string", std::string("blub"));
123   hash.set("func", &f_freeFunction);
124
125   r = to_nasal(c, hash);
126   VERIFY( naIsHash(r) );
127
128   VERIFY( hash.get<std::string>("name") == "my-name" );
129   VERIFY( naIsString(hash.get("name")) );
130
131   Hash mod = hash.createHash("mod");
132   mod.set("parent", hash);
133
134   //----------------------------------------------------------------------------
135   // Test exposing classes to Nasal
136   //----------------------------------------------------------------------------
137
138   Ghost<BasePtr>::init("BasePtr")
139     .method("member", &Base::member)
140     .method("strlen", &Base::test1Arg)
141     .member("str", &Base::getString, &Base::setString)
142     .method("str_m", &Base::getString)
143     .method("void", &Base::constVoidFunc)
144     .member("var_r", &Base::getVar)
145     .member("var_w", &Base::setVar)
146     .member("var", &Base::getVar, &Base::setVar)
147     .method("void", &baseVoidFunc)
148     .method("void_c", &baseConstVoidFunc)
149     .method("int2args", &baseFunc2Args)
150     .method("bool2args", &Base::test2Args)
151     .method("str_ptr", &testPtr);
152   Ghost<DerivedPtr>::init("DerivedPtr")
153     .bases<BasePtr>()
154     .member("x", &Derived::getX, &Derived::setX)
155     .member("x_alternate", &f_derivedGetX)
156     .method("free_fn", &derivedFreeMember)
157     .method("free_member", &derivedFreeMember)
158     .method("baseDoIt", &baseFuncCallContext);
159   Ghost<DoubleDerivedPtr>::init("DoubleDerivedPtr")
160     .bases<DerivedPtr>();
161   Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
162     .bases< Ghost<DerivedPtr> >()
163     .member("base", &DoubleDerived2::getBase);
164
165   nasal::to_nasal(c, DoubleDerived2Ptr());
166
167   BasePtr d( new Derived );
168   naRef derived = Ghost<BasePtr>::create(c, d);
169   VERIFY( naIsGhost(derived) );
170   VERIFY( std::string("DerivedPtr") == naGhost_type(derived)->name );
171
172   BasePtr d2( new DoubleDerived );
173   derived = Ghost<BasePtr>::create(c, d2);
174   VERIFY( naIsGhost(derived) );
175   VERIFY( std::string("DoubleDerivedPtr") ==  naGhost_type(derived)->name );
176
177   BasePtr d3( new DoubleDerived2 );
178   derived = Ghost<BasePtr>::create(c, d3);
179   VERIFY( naIsGhost(derived) );
180   VERIFY( std::string("DoubleDerived2Ptr") ==  naGhost_type(derived)->name );
181
182   VERIFY( Ghost<BasePtr>::isBaseOf(derived) );
183   VERIFY( Ghost<DerivedPtr>::isBaseOf(derived) );
184   VERIFY( Ghost<DoubleDerived2Ptr>::isBaseOf(derived) );
185
186   VERIFY( Ghost<BasePtr>::fromNasal(c, derived) == d3 );
187   VERIFY( Ghost<BasePtr>::fromNasal(c, derived) != d2 );
188   VERIFY(    Ghost<DerivedPtr>::fromNasal(c, derived)
189           == boost::dynamic_pointer_cast<Derived>(d3) );
190   VERIFY(    Ghost<DoubleDerived2Ptr>::fromNasal(c, derived)
191           == boost::dynamic_pointer_cast<DoubleDerived2>(d3) );
192   VERIFY( !Ghost<DoubleDerivedPtr>::fromNasal(c, derived) );
193
194   std::map<std::string, BasePtr> instances;
195   VERIFY( naIsHash(to_nasal(c, instances)) );
196
197   std::map<std::string, DerivedPtr> instances_d;
198   VERIFY( naIsHash(to_nasal(c, instances_d)) );
199
200   std::map<std::string, int> int_map;
201   VERIFY( naIsHash(to_nasal(c, int_map)) );
202
203   std::map<std::string, std::vector<int> > int_vector_map;
204   VERIFY( naIsHash(to_nasal(c, int_vector_map)) );
205
206   // Check converting to Ghost if using Nasal hashes with actual ghost inside
207   // the hashes parents vector
208   std::vector<naRef> parents;
209   parents.push_back(hash.get_naRef());
210   parents.push_back(derived);
211
212   Hash obj(c);
213   obj.set("parents", parents);
214   VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
215
216   // Check recursive parents (aka parent-of-parent)
217   std::vector<naRef> parents2;
218   parents2.push_back(obj.get_naRef());
219   Hash derived_obj(c);
220   derived_obj.set("parents", parents2);
221   VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
222
223   // TODO actually do something with the ghosts...
224
225   //----------------------------------------------------------------------------
226   // Test nasal::CallContext
227   //----------------------------------------------------------------------------
228
229
230   int int_vec[] = {1,2,3};
231   std::map<std::string, std::string> map;
232   naRef args[] = {
233     to_nasal(c, std::string("test-arg")),
234     to_nasal(c, 4),
235     to_nasal(c, int_vec),
236     to_nasal(c, map)
237   };
238   CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
239   VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
240   VERIFY( cc.getArg<std::string>(0) == "test-arg" );
241   VERIFY( cc.getArg<std::string>(10) == "" );
242   VERIFY( cc.isString(0) );
243   VERIFY( !cc.isNumeric(0) );
244   VERIFY( !cc.isVector(0) );
245   VERIFY( !cc.isHash(0) );
246   VERIFY( !cc.isGhost(0) );
247   VERIFY( cc.isNumeric(1) );
248   VERIFY( cc.isVector(2) );
249   VERIFY( cc.isHash(3) );
250
251   naRef args_vec = nasal::to_nasal(c, args);
252   VERIFY( naIsVector(args_vec) );
253
254   //----------------------------------------------------------------------------
255   // Test nasal::String
256   //----------------------------------------------------------------------------
257
258   String string( to_nasal(c, "Test") );
259   VERIFY( from_nasal<std::string>(c, string.get_naRef()) == "Test" );
260   VERIFY( string.c_str() == std::string("Test") );
261   VERIFY( string.starts_with(string) );
262   VERIFY( string.starts_with(String(c, "T")) );
263   VERIFY( string.starts_with(String(c, "Te")) );
264   VERIFY( string.starts_with(String(c, "Tes")) );
265   VERIFY( string.starts_with(String(c, "Test")) );
266   VERIFY( !string.starts_with(String(c, "Test1")) );
267   VERIFY( !string.starts_with(String(c, "bb")) );
268   VERIFY( !string.starts_with(String(c, "bbasdasdafasd")) );
269   VERIFY( string.find('e') == 1 );
270   VERIFY( string.find('9') == String::npos );
271   VERIFY( string.find_first_of(String(c, "st")) == 2 );
272   VERIFY( string.find_first_of(String(c, "st"), 3) == 3 );
273   VERIFY( string.find_first_of(String(c, "xyz")) == String::npos );
274   VERIFY( string.find_first_not_of(String(c, "Tst")) == 1 );
275   VERIFY( string.find_first_not_of(String(c, "Tse"), 2) == 3 );
276   VERIFY( string.find_first_not_of(String(c, "abc")) == 0 );
277   VERIFY( string.find_first_not_of(String(c, "abc"), 20) == String::npos );
278
279   naFreeContext(c);
280
281   return 0;
282 }