Merge branch '0.8' to sync with changes made after 0.8.1 release.
[luabind.git] / test / test_operators.cpp
blobf9ec09b1c7cfa2a36d2292f7bcad2d363a55608c
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"
24 #include <luabind/luabind.hpp>
25 #include <luabind/operator.hpp>
26 #include <iosfwd>
28 struct operator_tester : counted_type<operator_tester>
30 int operator+(int a) const
32 return 1 + a;
35 int operator-() const
37 return 46;
40 float operator()() const
42 return 3.5f;
45 float operator()(int a) const
47 return 3.5f + a;
50 float operator()(int a)
52 return 2.5f + a;
56 float operator*(operator_tester const& lhs, operator_tester const& rhs)
58 return 35.f;
61 std::string operator*(operator_tester const&, int v)
63 return "(operator_tester, int) overload";
66 int operator+(int a, const operator_tester&)
68 return 2 + a;
71 struct operator_tester2 : counted_type<operator_tester2>
75 int operator+(const operator_tester&, const operator_tester2&)
77 return 73;
80 struct operator_tester3: operator_tester, counted_type<operator_tester3> {};
82 std::ostream& operator<<(std::ostream& os, const operator_tester&)
84 os << "operator_tester"; return os;
87 struct op_test1
89 bool operator==(op_test1 const& rhs) const { return true; }
92 struct op_test2 : public op_test1
94 bool operator==(op_test2 const& rhs) const { return true; }
97 COUNTER_GUARD(operator_tester);
98 COUNTER_GUARD(operator_tester2);
99 COUNTER_GUARD(operator_tester3);
101 struct len_tester
103 len_tester(int len)
104 : len_(len)
107 int len() const
109 return len_;
112 int len_;
115 void test_main(lua_State* L)
117 using namespace luabind;
119 module(L)
121 class_<operator_tester>("operator_tester")
122 .def(constructor<>())
123 .def(tostring(const_self))
124 .def(self + int())
125 .def(other<int>() + self)
126 .def(-self)
127 .def(self + other<operator_tester2&>())
128 .def(self())
129 .def(const_self(int()))
130 .def(self(int()))
131 // .def(const_self * other<operator_tester const&>())
132 .def(const_self * const_self)
133 .def(const_self * other<int>()),
134 // .def("clone", &clone, adopt(return_value)),
136 class_<operator_tester2>("operator_tester2")
137 .def(constructor<>())
138 .def(other<const operator_tester&>() + self),
140 class_<operator_tester3, operator_tester>("operator_tester3")
141 .def(constructor<>()),
143 class_<op_test1>("op_test1")
144 .def(constructor<>())
145 .def(const_self == const_self),
147 class_<op_test2, op_test1>("op_test2")
148 .def(constructor<>())
149 .def(self == self),
151 class_<len_tester>("len_tester")
152 .def(constructor<int>())
153 .def("__len", &len_tester::len)
156 DOSTRING(L, "test = operator_tester()");
157 DOSTRING(L, "test2 = operator_tester2()");
158 DOSTRING(L, "test3 = operator_tester3()");
160 DOSTRING(L, "assert(tostring(test) == 'operator_tester')");
162 DOSTRING(L, "assert(test() == 3.5)");
163 DOSTRING(L, "assert(test(5) == 2.5 + 5)");
165 DOSTRING(L, "assert(-test == 46)");
166 DOSTRING(L, "assert(test * test == 35)");
167 DOSTRING(L, "assert(test * 3 == '(operator_tester, int) overload')")
168 DOSTRING(L, "assert(test + test2 == 73)");
169 DOSTRING(L, "assert(2 + test == 2 + 2)");
170 DOSTRING(L, "assert(test + 2 == 1 + 2)");
171 DOSTRING(L, "assert(test3 + 6 == 1 + 6)");
172 DOSTRING(L, "assert(test3 + test2 == 73)");
173 // DOSTRING(L, "assert(tostring(test) == 'operator_tester')");
175 const char* prog =
176 "class 'my_class'\n"
177 "function my_class:__add(lhs)\n"
178 " return my_class(self.val + lhs.val)\n"
179 "end\n"
180 "function my_class:__init(a)\n"
181 " self.val = a\n"
182 "end\n"
183 "function my_class:__sub(v)\n"
184 " if (type(self) == 'number') then\n"
185 " return my_class(self - v.val)\n"
186 " elseif (type(v) == 'number') then\n"
187 " return my_class(self.val - v)\n"
188 " else\n"
189 " return my_class(self.val - v.val)\n"
190 " end\n"
191 "end\n"
192 "a = my_class(3)\n"
193 "b = my_class(7)\n"
194 "c = a + b\n"
195 "d = a - 2\n"
196 "d = 10 - d\n"
197 "d = d - b\n";
199 DOSTRING(L, prog);
200 DOSTRING(L, "assert(c.val == 10)");
201 DOSTRING(L, "assert(d.val == 2)");
203 DOSTRING(L,
204 "a = op_test1()\n"
205 "b = op_test2()\n"
206 "assert(a == b)");
208 DOSTRING(L,
209 "x = len_tester(0)\n");
211 DOSTRING(L,
212 "x = len_tester(3)\n"
213 "assert(#x == 3)");