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