From ec1f2caa508a6521781a6ce26c00c02601b0d221 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 1 Apr 2004 22:12:15 +0000 Subject: [PATCH] converted some tests to Boost.Test --- luabind/detail/policy.hpp | 1 + test/Jamfile | 2 + test/main.cpp | 2 + test/makefile | 2 + test/test_null_pointer.cpp | 122 +++++++++++-------- test/test_policies.cpp | 292 ++++++++++++++++++++++++--------------------- 6 files changed, 235 insertions(+), 186 deletions(-) rewrite test/test_null_pointer.cpp (66%) rewrite test/test_policies.cpp (78%) diff --git a/luabind/detail/policy.hpp b/luabind/detail/policy.hpp index c52af7b..7ed9832 100644 --- a/luabind/detail/policy.hpp +++ b/luabind/detail/policy.hpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include diff --git a/test/Jamfile b/test/Jamfile index e257b84..1f55d64 100755 --- a/test/Jamfile +++ b/test/Jamfile @@ -9,7 +9,9 @@ SOURCES = test_scope.cpp test_typetraits.cpp test_construction.cpp + test_null_pointer.cpp test_operators.cpp + test_policies.cpp test_yield.cpp test_exceptions.cpp test_lua_classes.cpp diff --git a/test/main.cpp b/test/main.cpp index adbc984..df50ff6 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -125,6 +125,7 @@ void test_type_traits(); void test_implicit_cast(); void test_const(); void test_object(); +void test_policies(); // -------------------------------------------------------------------------- @@ -153,6 +154,7 @@ test_suite* init_unit_test_suite( int argc, char* argv[] ) test->add(BOOST_TEST_CASE(&test_implicit_cast)); test->add(BOOST_TEST_CASE(&test_const)); test->add(BOOST_TEST_CASE(&test_object)); + test->add(BOOST_TEST_CASE(&test_policies)); return test; } diff --git a/test/makefile b/test/makefile index 9a2be74..4dd0cb9 100644 --- a/test/makefile +++ b/test/makefile @@ -9,8 +9,10 @@ SOURCES = \ test_held_type.cpp \ test_implicit_cast.cpp \ test_lua_classes.cpp \ + test_null_pointer.cpp \ test_object.cpp \ test_operators.cpp \ + test_policies.cpp \ test_scope.cpp \ test_separate_registration.cpp \ test_separation.cpp \ diff --git a/test/test_null_pointer.cpp b/test/test_null_pointer.cpp dissimilarity index 66% index 9466796..857e5ce 100644 --- a/test/test_null_pointer.cpp +++ b/test/test_null_pointer.cpp @@ -1,53 +1,69 @@ -#include "test.h" - -namespace -{ - - struct A - { - A* f() { return 0; } - }; - - A* get_pointer() - { - return 0; - } - -} // anonymous namespace - - -bool test_null_pointer() -{ - using namespace luabind; - - lua_State* L = lua_open(); - lua_closer c(L); - int top = lua_gettop(L); - - open(L); - - module(L) - [ - class_("A") - .def(constructor<>()) - .def("f", &A::f), - - def("get_pointer", get_pointer) - ]; - if (dostring(L, "e = get_pointer()")) return false; - - lua_pushstring(L, "e"); - lua_gettable(L, LUA_GLOBALSINDEX); - if (!lua_isnil(L, -1)) return false; - lua_pop(L, 1); - - if (dostring(L, "a = A() e = a:f()")) return false; - lua_pushstring(L, "e"); - lua_gettable(L, LUA_GLOBALSINDEX); - if (!lua_isnil(L, -1)) return false; - lua_pop(L, 1); - - if (top != lua_gettop(L)) 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 + +namespace +{ + + struct A: counted_type + { + A* f() { return 0; } + }; + + A* get_pointer() + { + return 0; + } + +} // anonymous namespace + + +void test_null_pointer() +{ + COUNTER_GUARD(A); + + lua_state L; + + using namespace luabind; + + module(L) + [ + class_("A") + .def(constructor<>()) + .def("f", &A::f), + + def("get_pointer", get_pointer) + ]; + + + DOSTRING(L, + "e = get_pointer()\n" + "assert(e == nil)"); + + DOSTRING(L, + "a = A()\n" + "e = a:f()\n" + "assert(e == nil)"); +} diff --git a/test/test_policies.cpp b/test/test_policies.cpp dissimilarity index 78% index 187856a..911c03b 100644 --- a/test/test_policies.cpp +++ b/test/test_policies.cpp @@ -1,133 +1,159 @@ -#include "test.h" - -#include -#include -#include - -namespace -{ - - LUABIND_ANONYMOUS_FIX int feedback = 0; - - struct policies_test_class - { - std::string name_; - - policies_test_class() { feedback++; } - policies_test_class(const char* name): name_(name) { feedback++; } - ~policies_test_class() { feedback--; } - - policies_test_class* make(const char* name) const - { - return new policies_test_class(name); - } - - void f(policies_test_class* p) { delete p; } - - const policies_test_class* internal_ref() { return this; } - - policies_test_class* self_ref() - { - return this; - } - -// private: - policies_test_class(const policies_test_class&) {} - }; - - policies_test_class global; - - void out_val(float* f) { *f = 3.f; } - policies_test_class* copy_val() { return &global; } - - struct secret_type {}; - - secret_type sec_; - - secret_type* secret() { return &sec_; } - -} // anonymous namespace - -bool test_policies() -{ - try - { - using namespace luabind; - - lua_State* L = lua_open(); - lua_closer c(L); - lua_baselibopen(L); - - int top = lua_gettop(L); - - luabind::open(L); - - module(L) - [ - class_("test") - .def(constructor<>()) - .def("f", &policies_test_class::f, adopt(_1)) - .def("make", &policies_test_class::make, adopt(return_value)) - .def("internal_ref", &policies_test_class::internal_ref, dependency(return_value, self)) - .def("self_ref", &policies_test_class::self_ref, return_reference_to(self)), - - def("out_val", &out_val, pure_out_value(_1)), - def("copy_val", ©_val, copy(result)), - def("secret", &secret, discard_result) - ]; - - feedback = 1; - - if (dostring(L, "a = secret()")) return false; - - // copy - if (dostring(L, "a = copy_val()")) return false; - if (dostring(L, "a = nil")) return false; - if (dostring(L, "collectgarbage(0)")) return false; - if (feedback != 0) return false; - - // out_value - if (dostring(L, "a = out_val()")) return false; - - // return_reference_to - if (dostring(L, "a = test()")) return false; - if (feedback != 1) return false; - if (dostring(L, "b = a:self_ref()")) return false; - if (dostring(L, "a = nil")) return false; - if (dostring(L, "collectgarbage(0)")) return false; - if (feedback != 1) return false; - if (dostring(L, "b = nil")) return false; - if (dostring(L, "collectgarbage(0)")) return false; - if (feedback != 0) return false; - - // dependency - if (dostring(L, "a = test()")) return false; - if (feedback != 1) return false; - if (dostring(L, "b = a:internal_ref()")) return false; - if (dostring(L, "a = nil")) return false; - if (dostring(L, "collectgarbage(0)")) return false; - if (feedback != 1) return false; - if (dostring(L, "b = nil")) return false; - if (dostring(L, "collectgarbage(0)")) return false; // two gc-cycles because dependency-table won't be collected in the same cycle - if (dostring(L, "collectgarbage(0)")) return false; // as the object_rep - if (feedback != 0) return false; - - // adopt - if (dostring(L, "a = test()")) return false; - if (feedback != 1) return false; - if (dostring(L, "b = a:make('tjosan')")) return false; - if (feedback != 2) return false; - if (dostring(L, "a:f(b)")) return false; - - if (top != lua_gettop(L)) return false; - - c.release(); - if (feedback != 0) return false; - } - catch(...) - { - 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 +#include +#include +#include +#include +#include + +namespace +{ + + struct policies_test_class: counted_type + { + policies_test_class(const char* name): name_(name) {} + policies_test_class() {} + std::string name_; + + policies_test_class* make(const char* name) const + { + return new policies_test_class(name); + } + + void f(policies_test_class* p) { delete p; } + const policies_test_class* internal_ref() { return this; } + policies_test_class* self_ref() + { return this; } + +// private: + policies_test_class(const policies_test_class&) {} + }; + + policies_test_class global; + + void out_val(float* f) { *f = 3.f; } + policies_test_class* copy_val() { return &global; } + + struct secret_type {}; + + secret_type sec_; + + secret_type* secret() { return &sec_; } + +} // anonymous namespace + +void test_policies() +{ + lua_state L; + + using namespace luabind; + + module(L) + [ + class_("test") + .def(constructor<>()) + .def("f", &policies_test_class::f, adopt(_1)) + .def("make", &policies_test_class::make, adopt(return_value)) + .def("internal_ref", &policies_test_class::internal_ref, dependency(return_value, self)) + .def("self_ref", &policies_test_class::self_ref, return_reference_to(self)), + + def("out_val", &out_val, pure_out_value(_1)), + def("copy_val", ©_val, copy(result)), + def("secret", &secret, discard_result) + ]; + + // test copy + DOSTRING(L, + "a = secret()\n" + "a = copy_val()\n" + "a = nil\n" + "collectgarbage()\n"); + + BOOST_CHECK(policies_test_class::count == 1); + + // out_value + DOSTRING(L, "a = out_val()\n"); + + // return_reference_to + DOSTRING(L, + "a = test()\n" + "b = a:self_ref()\n" + "a = nil\n" + "collectgarbage()"); + + BOOST_CHECK(policies_test_class::count == 2); + + DOSTRING(L, + "b = nil\n" + "collectgarbage()"); + + BOOST_CHECK(policies_test_class::count == 1); + + DOSTRING(L, + "a = test()\n" + "collectgarbage()"); + + BOOST_CHECK(policies_test_class::count == 2); + + DOSTRING(L, + "b = a:internal_ref()\n" + "a = nil\n" + "collectgarbage()"); + + BOOST_CHECK(policies_test_class::count == 2); + + // two gc-cycles because dependency-table won't be collected in the + // same cycle as the object_rep + DOSTRING(L, + "b = nil\n" + "collectgarbage()\n" + "collectgarbage()"); + + BOOST_CHECK(policies_test_class::count == 1); + + // adopt + DOSTRING(L, + "a = test()\n" + "collectgarbage()"); + + BOOST_CHECK(policies_test_class::count == 2); + + DOSTRING(L, + "b = a:make('tjosan')\n" + "collectgarbage()"); + + BOOST_CHECK(policies_test_class::count == 3); + + DOSTRING(L, + "a:f(b)\n" + "a = nil\n" + "collectgarbage()"); + + BOOST_CHECK(policies_test_class::count == 1); + +} + -- 2.11.4.GIT