cleanup
[luabind.git] / test / test_operators.cpp
blob7552b10724b6cf18d277cede2effbf05e44447a0
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 namespace
30 struct operator_tester : counted_type<operator_tester>
32 int operator+(int a) const
34 return 1 + a;
37 int operator-() const
39 return 46;
42 float operator()() const
44 return 3.5f;
47 float operator()(int a) const
49 return 3.5f + a;
52 float operator()(int a)
54 return 2.5f + a;
59 int operator+(int a, const operator_tester&)
61 return 2 + a;
64 struct operator_tester2 : counted_type<operator_tester2>
68 int operator+(const operator_tester&, const operator_tester2&)
70 return 73;
73 struct operator_tester3: operator_tester, counted_type<operator_tester3> {};
75 std::ostream& operator<<(std::ostream& os, const operator_tester&)
77 os << "operator_tester"; return os;
80 struct op_test1
82 bool operator==(op_test1 const& rhs) const { return true; }
85 struct op_test2 : public op_test1
87 bool operator==(op_test2 const& rhs) const { return true; }
90 } // anonymous namespace
92 void test_operators()
94 COUNTER_GUARD(operator_tester);
95 COUNTER_GUARD(operator_tester2);
96 COUNTER_GUARD(operator_tester3);
98 lua_state L;
100 using namespace luabind;
102 module(L)
104 class_<operator_tester>("operator_tester")
105 .def(constructor<>())
106 .def(tostring(const_self))
107 .def(self + int())
108 .def(other<int>() + self)
109 .def(-self)
110 .def(self + other<operator_tester2&>())
111 .def(self())
112 .def(const_self(int()))
113 .def(self(int()))
114 .def(tostring(const_self)),
115 // .def("clone", &clone, adopt(return_value)),
117 class_<operator_tester2>("operator_tester2")
118 .def(constructor<>())
119 .def(other<const operator_tester&>() + self),
121 class_<operator_tester3, operator_tester>("operator_tester3")
122 .def(constructor<>()),
124 class_<op_test1>("op_test1")
125 .def(constructor<>())
126 .def(const_self == const_self),
128 class_<op_test2, op_test1>("op_test2")
129 .def(constructor<>())
130 .def(self == self)
133 DOSTRING(L, "test = operator_tester()");
134 DOSTRING(L, "test2 = operator_tester2()");
135 DOSTRING(L, "test3 = operator_tester3()");
137 DOSTRING(L, "assert(tostring(test) == 'operator_tester')");
139 DOSTRING(L, "assert(test() == 3.5)");
140 DOSTRING(L, "assert(test(5) == 2.5 + 5)");
142 DOSTRING(L, "assert(-test == 46)");
143 DOSTRING(L, "assert(test + test2 == 73)");
144 DOSTRING(L, "assert(2 + test == 2 + 2)");
145 DOSTRING(L, "assert(test + 2 == 1 + 2)");
146 DOSTRING(L, "assert(test3 + 6 == 1 + 6)");
147 DOSTRING(L, "assert(test3 + test2 == 73)");
148 // DOSTRING(L, "assert(tostring(test) == 'operator_tester')");
150 const char* prog =
151 "class 'my_class'\n"
152 "function my_class:__add(lhs)\n"
153 " return my_class(self.val + lhs.val)\n"
154 "end\n"
155 "function my_class:__init(a)\n"
156 " self.val = a\n"
157 "end\n"
158 "function my_class:__sub(v)\n"
159 " if (type(self) == 'number') then\n"
160 " return my_class(self - v.val)\n"
161 " elseif (type(v) == 'number') then\n"
162 " return my_class(self.val - v)\n"
163 " else\n"
164 " return my_class(self.val - v.val)\n"
165 " end\n"
166 "end\n"
167 "a = my_class(3)\n"
168 "b = my_class(7)\n"
169 "c = a + b\n"
170 "d = a - 2\n"
171 "d = 10 - d\n"
172 "d = d - b\n";
174 DOSTRING(L, prog);
175 DOSTRING(L, "assert(c.val == 10)");
176 DOSTRING(L, "assert(d.val == 2)");
178 DOSTRING(L,
179 "a = op_test1()\n"
180 "b = op_test2()\n"
181 "assert(a == b)");