fixed problem with holders reported by evan wies
[luabind.git] / test / test_object.cpp
blob96b925ad15a4e6e31a96fe4a98fbd3f0495368fd
1 #include "test.h"
2 #include <iostream>
4 namespace
6 using namespace luabind;
7 LUABIND_ANONYMOUS_FIX int feedback1 = 0;
8 LUABIND_ANONYMOUS_FIX int feedback2 = 0;
9 LUABIND_ANONYMOUS_FIX int feedback3 = 0;
10 LUABIND_ANONYMOUS_FIX int feedback4 = 0;
11 LUABIND_ANONYMOUS_FIX int feedback5 = 0;
13 void test_object_param(const object& table)
15 object current_object;
16 current_object = table;
18 if (table.type() == LUA_TTABLE)
20 feedback1 = 1;
22 feedback2 = object_cast<int>(table["oh"]);
24 feedback3 = 0;
25 for (object::array_iterator i = table.abegin(); i != table.aend(); ++i)
27 feedback3 += object_cast<int>(*i);
30 feedback4 = 0;
31 for (object::iterator j = table.begin(); j != table.end(); ++j)
33 feedback4 += object_cast<int>(*j);
36 feedback5 = 0;
37 for (object::raw_iterator j = table.raw_begin(); j != table.raw_end(); ++j)
39 feedback5 += object_cast<int>(*j);
42 table["blurp"] = 5;
45 else
47 feedback1 = 2;
49 if (table.type() != LUA_TNIL)
51 feedback2 = 1;
53 else
55 feedback2 = 2;
60 int test_fun()
62 feedback1 = 3;
63 return 42;
66 struct test_param
68 ~test_param() { feedback1 = 30; }
71 void test_match(const luabind::object& o)
73 feedback1 = 28;
76 void test_match(int i)
78 feedback1 = 27;
81 function_ multiret(object a, object b)
83 object c(a);
85 c = 30;
87 return b, a, c;
90 } // anonymous namespace
92 bool test_object()
94 using namespace luabind;
96 lua_State* L = lua_open();
97 int top = lua_gettop(L);
99 open(L);
102 object a(L);
103 object b(L);
104 object c;
106 a = 10;
107 b = 25;
109 std::cout << object_cast<int>(a) << ", " << object_cast<int>(b) << "\n";
111 (a, b, c) = multiret(a, b);
113 std::cout << object_cast<int>(a) << ", " << object_cast<int>(b) << ", " << object_cast<int>(c) << "\n";
116 module(L)
118 def("test_object_param", &test_object_param),
119 def("test_fun", &test_fun),
120 def("test_match", (void(*)(const luabind::object&))&test_match),
121 def("test_match", (void(*)(int))&test_match),
123 class_<test_param>("test_param")
124 .def(constructor<>())
127 dostring(L, "t = 2");
128 dostring(L, "test_object_param(t)");
129 if (feedback1 != 2) return false;
130 if (feedback2 != 1) return false;
132 dostring(L, "test_object_param(nil)");
133 if (feedback1 != 2) return false;
134 if (feedback2 != 2) return false;
136 dostring(L, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
137 dostring(L, "test_object_param(t)");
138 if (feedback1 != 1) return false;
139 if (feedback2 != 4) return false;
140 if (feedback3 != 28) return false;
141 if (feedback4 != 32) return false;
142 if (feedback5 != 32) return false;
144 object g = get_globals(L);
146 object t = g["t"];
147 if (t.type() != LUA_TTABLE) return false;
149 object blurp = t["blurp"];
150 if (object_cast<int>(blurp) != 5) return false;
152 object fun = g["test_fun"];
153 object ret = fun();
154 if (object_cast<int>(ret) != 42) return false;
155 if (feedback1 != 3) return false;
157 dostring(L, "function test_param_policies(x, y) end");
158 object test_param_policies = g["test_param_policies"];
159 int a = test_param_policies.type();
160 if (a != LUA_TFUNCTION) return false;
161 // call the function and tell lua to adopt the pointer passed as first argument
162 test_param_policies(5, new test_param())[adopt(_2)];
164 dostring(L, "test_match(7)");
165 if (feedback1 != 27) return false;
166 dostring(L, "test_match('oo')");
167 if (feedback1 != 28) return false;
169 dostring(L, "function test_object_policies(a) glob = a\nreturn 6\nend");
170 object test_object_policies = g["test_object_policies"];
171 object ret_val = test_object_policies("teststring")[detail::null_type()];
172 if (object_cast<int>(ret_val) != 6) return false;
173 if (object_cast<std::string>(g["glob"]) != "teststring") return false;
174 if (object_cast<std::string>(g.at("glob")) != "teststring") return false;
175 if (object_cast<std::string>(g.raw_at("glob")) != "teststring") return false;
178 if (top != lua_gettop(L)) return false;
180 lua_close(L);
182 // make sure lua adopted the pointer by checking that it has been deleted
183 if (feedback1 != 30) return false;
185 return true;