From 83dbad5dfb8632beeed0688c39e2162011871bf9 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 24 Feb 2004 00:49:32 +0000 Subject: [PATCH] *** empty log message *** --- test/Jamfile | 1 + test/main.cpp | 2 + test/test_object.cpp | 363 +++++++++++++++++++++++---------------------------- 3 files changed, 167 insertions(+), 199 deletions(-) rewrite test/test_object.cpp (85%) diff --git a/test/Jamfile b/test/Jamfile index 37fede2..1c75f30 100755 --- a/test/Jamfile +++ b/test/Jamfile @@ -1,6 +1,7 @@ import testing ; SOURCES = + test_object.cpp test_implicit_cast.cpp test_scope.cpp test_typetraits.cpp diff --git a/test/main.cpp b/test/main.cpp index fbba3ee..0ef7f87 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -123,6 +123,7 @@ void test_yield(); void test_construction(); void test_type_traits(); void test_implicit_cast(); +void test_object(); // -------------------------------------------------------------------------- @@ -149,6 +150,7 @@ test_suite* init_unit_test_suite( int argc, char* argv[] ) test->add(BOOST_TEST_CASE(&test_yield)); test->add(BOOST_TEST_CASE(&test_type_traits)); test->add(BOOST_TEST_CASE(&test_implicit_cast)); + test->add(BOOST_TEST_CASE(&test_object)); return test; } diff --git a/test/test_object.cpp b/test/test_object.cpp dissimilarity index 85% index c40b077..b442b2d 100644 --- a/test/test_object.cpp +++ b/test/test_object.cpp @@ -1,199 +1,164 @@ -#include "test.h" -#include - -namespace -{ - using namespace luabind; - LUABIND_ANONYMOUS_FIX int feedback1 = 0; - LUABIND_ANONYMOUS_FIX int feedback2 = 0; - LUABIND_ANONYMOUS_FIX int feedback3 = 0; - LUABIND_ANONYMOUS_FIX int feedback4 = 0; - LUABIND_ANONYMOUS_FIX int feedback5 = 0; - - void test_object_param(const object& table) - { - object current_object; - current_object = table; - - if (table.type() == LUA_TTABLE) - { - feedback1 = 1; - - feedback2 = object_cast(table["oh"]); - - feedback3 = 0; - for (object::array_iterator i = table.abegin(); i != table.aend(); ++i) - { - feedback3 += object_cast(*i); - } - - feedback4 = 0; - for (object::iterator j = table.begin(); j != table.end(); ++j) - { - feedback4 += object_cast(*j); - } - - feedback5 = 0; - for (object::raw_iterator j = table.raw_begin(); j != table.raw_end(); ++j) - { - feedback5 += object_cast(*j); - } - - table["blurp"] = 5; - - } - else - { - feedback1 = 2; - - if (table.type() != LUA_TNIL) - { - feedback2 = 1; - } - else - { - feedback2 = 2; - } - } - } - - int test_fun() - { - feedback1 = 3; - return 42; - } - - struct test_param - { - ~test_param() - { feedback1 = 30; } - luabind::object obj; - luabind::object obj2; - }; - - void test_match(const luabind::object& o) - { - feedback1 = 28; - } - - void test_match(int i) - { - feedback1 = 27; - } -/* - function_ multiret(object a, object b) - { - object c(a); - - c = 30; - - return b, a, c; - } -*/ -} // anonymous namespace - -bool test_object() -{ - using namespace luabind; - - lua_State* L = lua_open(); - int top = lua_gettop(L); - { - open(L); -/* - { - object a(L); - object b(L); - object c; - - a = 10; - b = 25; - - std::cout << object_cast(a) << ", " << object_cast(b) << "\n"; - - (a, b, c) = multiret(a, b); - - std::cout << object_cast(a) << ", " << object_cast(b) << ", " << object_cast(c) << "\n"; - } -*/ - module(L) - [ - def("test_object_param", &test_object_param), - def("test_fun", &test_fun), - def("test_match", (void(*)(const luabind::object&))&test_match), - def("test_match", (void(*)(int))&test_match), - - class_("test_param") - .def(constructor<>()) - .def_readwrite("obj", &test_param::obj) - .def_readonly("obj2", &test_param::obj2) - ]; - - dostring(L, "t = 2"); - dostring(L, "test_object_param(t)"); - if (feedback1 != 2) return false; - if (feedback2 != 1) return false; - - dostring(L, "test_object_param(nil)"); - if (feedback1 != 2) return false; - if (feedback2 != 2) return false; - - dostring(L, "t = { ['oh'] = 4, 3, 5, 7, 13 }"); - dostring(L, "test_object_param(t)"); - if (feedback1 != 1) return false; - if (feedback2 != 4) return false; - if (feedback3 != 28) return false; - if (feedback4 != 32) return false; - if (feedback5 != 32) return false; - - object g = get_globals(L); - - object t = g["t"]; - if (t.type() != LUA_TTABLE) return false; - - object blurp = t["blurp"]; - if (object_cast(blurp) != 5) return false; - - object fun = g["test_fun"]; - object ret = fun(); - if (object_cast(ret) != 42) return false; - if (feedback1 != 3) return false; - - dostring(L, "function test_param_policies(x, y) end"); - object test_param_policies = g["test_param_policies"]; - int a = test_param_policies.type(); - if (a != LUA_TFUNCTION) return false; - // 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, "test_match(7)"); - if (feedback1 != 27) return false; - dostring(L, "test_match('oo')"); - if (feedback1 != 28) return false; - - dostring(L, "t = test_param()\n" - "t.obj = 'foo'\n" - "if t.obj == 'foo' then test_fun() end\n"); - if (feedback1 != 3) return false; - feedback1 = 0; - dostring(L, "if t.obj2 == nil then test_fun() end\n"); - if (feedback1 != 3) return false; - - dostring(L, "function test_object_policies(a) glob = a\nreturn 6\nend"); - object test_object_policies = g["test_object_policies"]; - object ret_val = test_object_policies("teststring")[detail::null_type()]; - if (object_cast(ret_val) != 6) return false; - if (object_cast(g["glob"]) != "teststring") return false; - if (object_cast(g.at("glob")) != "teststring") return false; - if (object_cast(g.raw_at("glob")) != "teststring") return false; - } - - if (top != lua_gettop(L)) return false; - - lua_close(L); - - // make sure lua adopted the pointer by checking that it has been deleted - if (feedback1 != 30) return false; - - return true; -} +// 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 +#include + +namespace +{ + using namespace luabind; + + int test_object_param(const object& table) + { + + object current_object; + current_object = table; + + if (table.type() == LUA_TTABLE) + { + int sum = object_cast(table["oh"]);; + for (object::array_iterator i = table.abegin(); i != table.aend(); ++i) + { + sum += object_cast(*i); + } + + int sum2 = 0; + for (object::iterator j = table.begin(); j != table.end(); ++j) + { + sum2 += object_cast(*j); + } + + int sum3 = 0; + for (object::raw_iterator j = table.raw_begin(); j != table.raw_end(); ++j) + { + sum3 += object_cast(*j); + } + + table["sum"] = sum; + table["sum2"] = sum2; + table["sum3"] = sum3; + table["blurp"] = 5; + return 0; + } + else + { + if (table.type() != LUA_TNIL) + { + return 1; + } + else + { + return 2; + } + } + } + + int test_fun() + { + return 42; + } + + struct test_param : counted_type + { + luabind::object obj; + luabind::object obj2; + }; + + int test_match(const luabind::object& o) + { + return 0; + } + + int test_match(int i) + { + return 1; + } + +} // anonymous namespace + +void test_object() +{ + COUNTER_GUARD(test_param); + + 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), + + class_("test_param") + .def(constructor<>()) + .def_readwrite("obj", &test_param::obj) + .def_readonly("obj2", &test_param::obj2) + ]; + + 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.sum == 4 + 3 + 5 + 7 + 13)"); + DOSTRING(L, "assert(t.sum2 == 4 + 3 + 5 + 7 + 13)"); + DOSTRING(L, "assert(t.sum3 == 4 + 3 + 5 + 7 + 13)"); + DOSTRING(L, "assert(t.blurp == 5)"); + + object g = get_globals(L); + object fun = g["test_fun"]; + object ret = fun(); + BOOST_CHECK(object_cast(ret) == 42); + + DOSTRING(L, "function test_param_policies(x, y) end"); + object test_param_policies = g["test_param_policies"]; + int a = test_param_policies.type(); + BOOST_CHECK(a == LUA_TFUNCTION); + + // 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()]; + BOOST_CHECK(object_cast(ret_val) == 6); + BOOST_CHECK(object_cast(g["glob"]) == "teststring"); + BOOST_CHECK(object_cast(g.at("glob")) == "teststring"); + BOOST_CHECK(object_cast(g.raw_at("glob")) == "teststring"); +} -- 2.11.4.GIT