*** empty log message ***
[luabind.git] / test / main.cpp
blob58235793c22b749e15266fe73fec8b1ee600e7c4
1 #include "test.h"
3 extern "C"
5 #include "lauxlib.h"
6 #include "lualib.h"
7 #include "lua.h"
10 #include <iostream>
11 #include <algorithm>
13 #include <luabind/luabind.hpp>
14 #include <luabind/scope.hpp>
16 bool dostring(lua_State* L, const char* str)
18 if (luaL_loadbuffer(L, str, std::strlen(str), str) || lua_pcall(L, 0, 0, 0))
20 const char* a = lua_tostring(L, -1);
21 std::cout << a << "\n";
22 lua_pop(L, 1);
23 return true;
25 return false;
28 int dostring2(lua_State* L, const char* str)
30 if (luaL_loadbuffer(L, str, std::strlen(str), str) || lua_pcall(L, 0, 0, 0))
32 return 1;
34 return 0;
37 bool report_success(bool result, const char* name)
39 std::cout << name;
40 #ifdef BOOST_MSVC
41 if (result) std::cout << ": passed\n";
42 else std::cout << ": failed\n";
43 #else
44 if (result) std::cout << ": \033[32mpassed\033[0m\n";
45 else std::cout << ": \033[31mfailed\033[0m\n";
46 #endif
47 return result;
50 #if 0
52 #include <luabind/runtime_converters.hpp>
54 namespace
56 using namespace luabind::rt_converter;
58 struct my_type
60 my_type()
61 { std::cout << "constructed\n"; }
63 my_type(const my_type&)
64 { std::cout << "copied\n"; }
66 ~my_type()
67 { std::cout << "destructed\n"; }
70 void int_from_lua(lua_State* L, int index, rvalue_data* data)
72 data->result = new (data->data) int(lua_tonumber(L, index));
75 void float_from_lua(lua_State* L, int index, rvalue_data* data)
77 data->result = new (data->data) float(lua_tonumber(L, index));
80 void string_from_lua(lua_State* L, int index, rvalue_data* data)
82 data->result = new (data->data) std::string(lua_tostring(L, index));
85 void my_type_from_lua(lua_State* L, int index, rvalue_data* data)
87 data->result = new (data->data) my_type;
90 void f(int a, float b, const std::vector<float>& s)
92 int c = a + b;
95 template<class Source, class Target>
96 struct implicit_conversion
98 typedef typename boost::add_reference<
99 typename boost::add_const<Target>::type
100 >::type target_t;
102 typedef typename boost::add_reference<Source>::type source_t;
104 implicit_conversion()
106 insert_converter(&convert, typeid(target_t));
109 static void convert(lua_State* L, int index, rvalue_data* data)
111 typename boost::python::detail::referent_storage<source_t>::type storage;
113 rvalue_data tmp;
114 tmp.data = storage.bytes;
116 rvalue_convert(L, index, &tmp, registered<Source>::converters);
118 new (data->data) Target(void_ptr_to_reference(tmp.result, (source_t(*)())0));
119 data->result = data->data;
121 if (tmp.result == tmp.data)
122 boost::python::detail::destroy_referent(tmp.result, (source_t(*)())0);
126 struct A
127 { int n; virtual void f() {} };
129 struct B : A
130 { float e; virtual void f() {} };
132 void test_runtime()
134 lua_State* L = lua_open();
136 using namespace luabind;
137 open(L);
139 insert_converter(&int_from_lua, typeid(const int&));
140 insert_converter(&float_from_lua, typeid(const float&));
141 // insert_converter(&string_from_lua, typeid(const std::string&));
142 insert_converter(&my_type_from_lua, typeid(const my_type&));
144 implicit_conversion<int, std::vector<float> >();
146 module(L)
148 def("f", &f, runtime(_1) + runtime(_2) + runtime(_3))
151 lua_close(L);
155 #endif
158 int main()
160 bool passed = true;
162 // test_runtime();
164 passed &= report_success(test_construction(), "construction");
165 passed &= report_success(test_attributes(), "attributes");
166 passed &= report_success(test_operators(), "operators");
167 passed &= report_success(test_implicit_cast(), "implicit cast");
168 passed &= report_success(test_const(), "const");
169 #ifndef LUABIND_NO_EXCEPTIONS
170 passed &= report_success(test_exceptions(), "exceptions");
171 #else
172 std::cout << "exceptions: skipped \n";
173 #endif
174 passed &= report_success(test_null_pointer(), "null pointer");
175 passed &= report_success(test_policies(), "policies");
176 passed &= report_success(test_lua_classes(), "lua classes");
177 passed &= report_success(test_free_functions(), "free functions");
178 passed &= report_success(test_object(), "object");
179 passed &= report_success(test_held_type(), "held type");
180 passed &= report_success(test_iterator(), "iterator");
181 passed &= report_success(test_scope(), "scopes");
182 passed &= report_success(test_yield(), "yield");
184 if (passed) std::cout << "\n\nall tests passed\n";
185 else std::cout << "\n\nsome tests failed\n";
187 return 0;