1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
// Copyright (c) 2003 Daniel Wallin and Arvid Norberg
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
// ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
#include "test.hpp"
#include <luabind/luabind.hpp>
#include <luabind/adopt_policy.hpp>
#include <luabind/detail/debug.hpp>
#include <luabind/error.hpp>
#include <luabind/operator.hpp>
#include <boost/lexical_cast.hpp>
#include <utility>
using namespace luabind;
int test_object_param(const object& table)
{
LUABIND_CHECK_STACK(table.interpreter());
object current_object;
current_object = table;
lua_State* L = table.interpreter();
if (type(table) == LUA_TTABLE)
{
int sum1 = 0;
for (iterator i(table), e; i != e; ++i)
{
assert(type(*i) == LUA_TNUMBER);
sum1 += object_cast<int>(*i);
}
int sum2 = 0;
for (raw_iterator i(table), e; i != e; ++i)
{
assert(type(*i) == LUA_TNUMBER);
sum2 += object_cast<int>(*i);
}
// test iteration of empty table
object empty_table = newtable(L);
for (iterator i(empty_table), e; i != e; ++i)
{}
table["sum1"] = sum1;
table["sum2"] = sum2;
table["blurp"] = 5;
table["sum3"] = table["sum1"];
return 0;
}
else
{
if (type(table) != LUA_TNIL)
{
return 1;
}
else
{
return 2;
}
}
}
int test_fun()
{
return 42;
}
struct test_param : counted_type<test_param>
{
luabind::object obj;
luabind::object obj2;
bool operator==(test_param const& rhs) const
{ return obj == rhs.obj && obj2 == rhs.obj2; }
};
COUNTER_GUARD(test_param);
int test_match(const luabind::object& o)
{
return 0;
}
int test_match(int i)
{
return 1;
}
void test_match_object(
luabind::object p1
, luabind::object p2
, luabind::object p3)
{
p1["ret"] = 1;
p2["ret"] = 2;
p3["ret"] = 3;
}
void test_call(lua_State* L)
{
DOSTRING(L,
"function sum(...)\n"
" local result = 0\n"
" for _,x in pairs({...}) do\n"
" result = result + x\n"
" end\n"
" return result\n"
"end\n"
);
object sum = globals(L)["sum"];
TEST_CHECK(object_cast<int>(sum()) == 0);
TEST_CHECK(object_cast<int>(sum(1)) == 1);
TEST_CHECK(object_cast<int>(sum(1,2)) == 3);
TEST_CHECK(object_cast<int>(sum(1,2,3)) == 6);
TEST_CHECK(object_cast<int>(sum(1,2,3,4)) == 10);
TEST_CHECK(object_cast<int>(sum(1,2,3,4,5)) == 15);
}
void test_metatable(lua_State* L)
{
object metatable = newtable(L);
object G = globals(L);
DOSTRING(L,
"function index_function(table, key)\n"
" return key*2\n"
"end\n"
"index_table = {}"
);
metatable["__index"] = G["index_function"];
setmetatable(G["index_table"], metatable);
DOSTRING(L,
"assert(index_table[0] == 0)\n"
"assert(index_table[1] == 2)\n"
"assert(index_table[2] == 4)\n"
);
DOSTRING(L,
"setmetatable(index_table, { ['luabind.metatable'] = 1 })"
);
assert(getmetatable(G["index_table"])["luabind.metatable"] == 1);
assert(type(getmetatable(G["index_table"])["nonexistent"]) == LUA_TNIL);
}
int with_upvalues(lua_State *)
{
return 0;
}
void test_upvalues(lua_State* L)
{
lua_pushnumber(L, 3);
lua_pushnumber(L, 4);
lua_pushcclosure(L, &with_upvalues, 2);
object f(from_stack(L, -1));
lua_pop(L, 1);
assert(getupvalue(f, 1) == 3);
assert(getupvalue(f, 2) == 4);
setupvalue(f, 1, object(L, 4));
assert(getupvalue(f, 1) == 4);
assert(getupvalue(f, 2) == 4);
setupvalue(f, 2, object(L, 5));
assert(getupvalue(f, 1) == 4);
assert(getupvalue(f, 2) == 5);
}
void test_explicit_conversions(lua_State* L)
{
lua_pushcclosure(L, &with_upvalues, 0);
object f(from_stack(L, -1));
lua_pop(L, 1);
assert(tocfunction(f) == &with_upvalues);
int* p = static_cast<int*>(lua_newuserdata(L, sizeof(int)));
*p = 1234;
object x(from_stack(L, -1));
lua_pop(L, 1);
assert(*touserdata<int>(x) == 1234);
}
int with_argument(argument const& arg)
{
return object_cast<int>(arg) * 2;
}
int with_table_argument(argument const& arg, argument const& key)
{
return object_cast<int>(arg[key]);
}
void test_argument(lua_State* L)
{
module(L) [
def("with_argument", &with_argument),
def("with_table_argument", &with_table_argument)
];
DOSTRING(L,
"assert(with_argument(1) == 2)\n"
"assert(with_argument(2) == 4)\n"
);
DOSTRING(L,
"x = { foo = 1, bar = 2 }\n"
"assert(with_table_argument(x, 'foo') == 1)\n"
"assert(with_table_argument(x, 'bar') == 2)\n"
);
}
void test_bool_convertible(lua_State* L)
{
DOSTRING(L,
"x1 = nil\n"
"x2 = false\n"
"x3 = 0\n"
"x4 = 1\n"
);
DOSTRING(L,
"assert(not x1)\n"
"assert(not x2)\n"
"assert(x3)\n"
"assert(x4)\n"
);
object G = globals(L);
object x1 = G["x1"];
object x2 = G["x2"];
object x3 = G["x3"];
object x4 = G["x4"];
assert(!x1);
assert(!x2);
assert(x3);
assert(x4);
assert(!G["x1"]);
assert(!G["x2"]);
assert(G["x3"]);
assert(G["x4"]);
}
void test_main(lua_State* L)
{
using namespace luabind;
module(L)
[
def("test_object_param", &test_object_param),
def("test_fun", &test_fun),
def("test_match", (int(*)(const luabind::object&))&test_match),
def("test_match", (int(*)(int))&test_match),
def("test_match_object", &test_match_object),
class_<test_param>("test_param")
.def(constructor<>())
.def_readwrite("obj", &test_param::obj)
.def_readonly("obj2", &test_param::obj2)
.def(const_self == const_self)
];
object uninitialized;
TEST_CHECK(!uninitialized);
TEST_CHECK(!uninitialized.is_valid());
test_param temp_object;
globals(L)["temp"] = temp_object;
TEST_CHECK(object_cast<test_param>(globals(L)["temp"]) == temp_object);
globals(L)["temp"] = &temp_object;
TEST_CHECK(object_cast<test_param const*>(globals(L)["temp"]) == &temp_object);
TEST_CHECK(globals(L)["temp"] == temp_object);
// test the registry
object reg = registry(L);
reg["__a"] = "foobar";
TEST_CHECK(object_cast<std::string>(registry(L)["__a"]) == "foobar");
DOSTRING(L,
"t = 2\n"
"assert(test_object_param(t) == 1)");
DOSTRING(L, "assert(test_object_param(nil) == 2)");
DOSTRING(L, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
DOSTRING(L, "assert(test_object_param(t) == 0)");
DOSTRING(L, "assert(t.sum1 == 4 + 3 + 5 + 7 + 13)");
DOSTRING(L, "assert(t.sum2 == 4 + 3 + 5 + 7 + 13)");
DOSTRING(L, "assert(t.blurp == 5)");
object g = globals(L);
object ret = g["test_fun"]();
TEST_CHECK(object_cast<int>(ret) == 42);
DOSTRING(L, "function test_param_policies(x, y) end");
object test_param_policies = g["test_param_policies"];
int a = type(test_param_policies);
TEST_CHECK(a == LUA_TFUNCTION);
luabind::object obj;
obj = luabind::object();
// call the function and tell lua to adopt the pointer passed as first argument
test_param_policies(5, new test_param())[adopt(_2)];
DOSTRING(L, "assert(test_match(7) == 1)");
DOSTRING(L, "assert(test_match('oo') == 0)");
DOSTRING(L,
"t = test_param()\n"
"t.obj = 'foo'\n"
"assert(t.obj == 'foo')\n"
"assert(t.obj2 == nil)");
DOSTRING(L,
"function test_object_policies(a) glob = a\n"
"return 6\n"
"end");
object test_object_policies = g["test_object_policies"];
object ret_val = test_object_policies("teststring")[detail::null_type()];
TEST_CHECK(object_cast<int>(ret_val) == 6);
TEST_CHECK(ret_val == 6);
TEST_CHECK(6 == ret_val);
g["temp_val"] = 6;
TEST_CHECK(ret_val == g["temp_val"]);
object temp_val = g["temp_val"];
TEST_CHECK(ret_val == temp_val);
g["temp"] = "test string";
TEST_CHECK(boost::lexical_cast<std::string>(g["temp"]) == "test string");
g["temp"] = 6;
TEST_CHECK(boost::lexical_cast<std::string>(g["temp"]) == "6");
TEST_CHECK(object_cast<std::string>(g["glob"]) == "teststring");
TEST_CHECK(object_cast<std::string>(gettable(g, "glob")) == "teststring");
TEST_CHECK(object_cast<std::string>(rawget(g, "glob")) == "teststring");
object t = newtable(L);
TEST_CHECK(iterator(t) == iterator());
TEST_CHECK(raw_iterator(t) == raw_iterator());
t["foo"] = "bar";
TEST_CHECK(object_cast<std::string>(t["foo"]) == "bar");
TEST_CHECK(object_cast<std::string>(*iterator(t)) == "bar");
TEST_CHECK(object_cast<std::string>(*raw_iterator(t)) == "bar");
t["foo"] = nil; // luabind::nil_type
TEST_CHECK(iterator(t) == iterator());
TEST_CHECK(raw_iterator(t) == raw_iterator());
t["foo"] = "bar";
iterator it1(t);
*it1 = nil;
TEST_CHECK(iterator(t) == iterator());
TEST_CHECK(raw_iterator(t) == raw_iterator());
t["foo"] = "bar";
raw_iterator it2(t);
*it2 = nil;
TEST_CHECK(iterator(t) == iterator());
TEST_CHECK(raw_iterator(t) == raw_iterator());
DOSTRING(L,
"p1 = {}\n"
"p2 = {}\n"
"p3 = {}\n"
"test_match_object(p1, p2, p3)\n"
"assert(p1.ret == 1)\n"
"assert(p2.ret == 2)\n"
"assert(p3.ret == 3)\n");
#ifndef LUABIND_NO_EXCEPTIONS
try
{
object not_initialized;
int i = object_cast<int>(not_initialized);
(void)i;
TEST_ERROR("invalid cast succeeded");
}
catch(luabind::cast_failed&) {}
#endif
object not_initialized;
TEST_CHECK(!object_cast_nothrow<int>(not_initialized));
TEST_CHECK(!not_initialized.is_valid());
TEST_CHECK(!not_initialized);
DOSTRING(L, "t = { {1}, {2}, {3}, {4} }");
int inner_sum = 0;
for (iterator i(globals(L)["t"]), e; i != e; ++i)
{
inner_sum += object_cast<int>((*i)[1]);
}
TEST_CHECK(inner_sum == 1 + 2 + 3 + 4);
DOSTRING(L, "t = { {1, 2}, {3, 4}, {5, 6}, {7, 8} }");
inner_sum = 0;
for (iterator i(globals(L)["t"]), e; i != e; ++i)
{
for (iterator j(*i), e; j != e; ++j)
{
inner_sum += object_cast<int>(*j);
}
}
TEST_CHECK(inner_sum == 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8);
TEST_CHECK(object_cast<int>(globals(L)["t"][2][2]) == 4);
test_call(L);
test_metatable(L);
test_upvalues(L);
test_explicit_conversions(L);
test_argument(L);
test_bool_convertible(L);
}
|