converted some tests to Boost.Test
[luabind.git] / test / test_policies.cpp
blob911c03b97b47f85c5ac318beb498ed006eb060bf
1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #include "test.hpp"
25 #include <luabind/out_value_policy.hpp>
26 #include <luabind/return_reference_to_policy.hpp>
27 #include <luabind/copy_policy.hpp>
28 #include <luabind/adopt_policy.hpp>
29 #include <luabind/discard_result_policy.hpp>
30 #include <luabind/dependency_policy.hpp>
31 #include <luabind/luabind.hpp>
33 namespace
36 struct policies_test_class: counted_type<policies_test_class>
38 policies_test_class(const char* name): name_(name) {}
39 policies_test_class() {}
40 std::string name_;
42 policies_test_class* make(const char* name) const
44 return new policies_test_class(name);
47 void f(policies_test_class* p) { delete p; }
48 const policies_test_class* internal_ref() { return this; }
49 policies_test_class* self_ref()
50 { return this; }
52 // private:
53 policies_test_class(const policies_test_class&) {}
56 policies_test_class global;
58 void out_val(float* f) { *f = 3.f; }
59 policies_test_class* copy_val() { return &global; }
61 struct secret_type {};
63 secret_type sec_;
65 secret_type* secret() { return &sec_; }
67 } // anonymous namespace
69 void test_policies()
71 lua_state L;
73 using namespace luabind;
75 module(L)
77 class_<policies_test_class>("test")
78 .def(constructor<>())
79 .def("f", &policies_test_class::f, adopt(_1))
80 .def("make", &policies_test_class::make, adopt(return_value))
81 .def("internal_ref", &policies_test_class::internal_ref, dependency(return_value, self))
82 .def("self_ref", &policies_test_class::self_ref, return_reference_to(self)),
84 def("out_val", &out_val, pure_out_value(_1)),
85 def("copy_val", &copy_val, copy(result)),
86 def("secret", &secret, discard_result)
89 // test copy
90 DOSTRING(L,
91 "a = secret()\n"
92 "a = copy_val()\n"
93 "a = nil\n"
94 "collectgarbage()\n");
96 BOOST_CHECK(policies_test_class::count == 1);
98 // out_value
99 DOSTRING(L, "a = out_val()\n");
101 // return_reference_to
102 DOSTRING(L,
103 "a = test()\n"
104 "b = a:self_ref()\n"
105 "a = nil\n"
106 "collectgarbage()");
108 BOOST_CHECK(policies_test_class::count == 2);
110 DOSTRING(L,
111 "b = nil\n"
112 "collectgarbage()");
114 BOOST_CHECK(policies_test_class::count == 1);
116 DOSTRING(L,
117 "a = test()\n"
118 "collectgarbage()");
120 BOOST_CHECK(policies_test_class::count == 2);
122 DOSTRING(L,
123 "b = a:internal_ref()\n"
124 "a = nil\n"
125 "collectgarbage()");
127 BOOST_CHECK(policies_test_class::count == 2);
129 // two gc-cycles because dependency-table won't be collected in the
130 // same cycle as the object_rep
131 DOSTRING(L,
132 "b = nil\n"
133 "collectgarbage()\n"
134 "collectgarbage()");
136 BOOST_CHECK(policies_test_class::count == 1);
138 // adopt
139 DOSTRING(L,
140 "a = test()\n"
141 "collectgarbage()");
143 BOOST_CHECK(policies_test_class::count == 2);
145 DOSTRING(L,
146 "b = a:make('tjosan')\n"
147 "collectgarbage()");
149 BOOST_CHECK(policies_test_class::count == 3);
151 DOSTRING(L,
152 "a:f(b)\n"
153 "a = nil\n"
154 "collectgarbage()");
156 BOOST_CHECK(policies_test_class::count == 1);