new test system, no more boost.test
[luabind.git] / test / test_operators.cpp
bloba7f60903617f835c7296cb3b21a927dd7f2a7ff8
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;
57 int operator+(int a, const operator_tester&)
59 return 2 + a;
62 struct operator_tester2 : counted_type<operator_tester2>
66 int operator+(const operator_tester&, const operator_tester2&)
68 return 73;
71 struct operator_tester3: operator_tester, counted_type<operator_tester3> {};
73 std::ostream& operator<<(std::ostream& os, const operator_tester&)
75 os << "operator_tester"; return os;
78 struct op_test1
80 bool operator==(op_test1 const& rhs) const { return true; }
83 struct op_test2 : public op_test1
85 bool operator==(op_test2 const& rhs) const { return true; }
88 COUNTER_GUARD(operator_tester);
89 COUNTER_GUARD(operator_tester2);
90 COUNTER_GUARD(operator_tester3);
92 void test_main(lua_State* L)
94 using namespace luabind;
96 module(L)
98 class_<operator_tester>("operator_tester")
99 .def(constructor<>())
100 .def(tostring(const_self))
101 .def(self + int())
102 .def(other<int>() + self)
103 .def(-self)
104 .def(self + other<operator_tester2&>())
105 .def(self())
106 .def(const_self(int()))
107 .def(self(int()))
108 .def(tostring(const_self)),
109 // .def("clone", &clone, adopt(return_value)),
111 class_<operator_tester2>("operator_tester2")
112 .def(constructor<>())
113 .def(other<const operator_tester&>() + self),
115 class_<operator_tester3, operator_tester>("operator_tester3")
116 .def(constructor<>()),
118 class_<op_test1>("op_test1")
119 .def(constructor<>())
120 .def(const_self == const_self),
122 class_<op_test2, op_test1>("op_test2")
123 .def(constructor<>())
124 .def(self == self)
127 DOSTRING(L, "test = operator_tester()");
128 DOSTRING(L, "test2 = operator_tester2()");
129 DOSTRING(L, "test3 = operator_tester3()");
131 DOSTRING(L, "assert(tostring(test) == 'operator_tester')");
133 DOSTRING(L, "assert(test() == 3.5)");
134 DOSTRING(L, "assert(test(5) == 2.5 + 5)");
136 DOSTRING(L, "assert(-test == 46)");
137 DOSTRING(L, "assert(test + test2 == 73)");
138 DOSTRING(L, "assert(2 + test == 2 + 2)");
139 DOSTRING(L, "assert(test + 2 == 1 + 2)");
140 DOSTRING(L, "assert(test3 + 6 == 1 + 6)");
141 DOSTRING(L, "assert(test3 + test2 == 73)");
142 // DOSTRING(L, "assert(tostring(test) == 'operator_tester')");
144 const char* prog =
145 "class 'my_class'\n"
146 "function my_class:__add(lhs)\n"
147 " return my_class(self.val + lhs.val)\n"
148 "end\n"
149 "function my_class:__init(a)\n"
150 " self.val = a\n"
151 "end\n"
152 "function my_class:__sub(v)\n"
153 " if (type(self) == 'number') then\n"
154 " return my_class(self - v.val)\n"
155 " elseif (type(v) == 'number') then\n"
156 " return my_class(self.val - v)\n"
157 " else\n"
158 " return my_class(self.val - v.val)\n"
159 " end\n"
160 "end\n"
161 "a = my_class(3)\n"
162 "b = my_class(7)\n"
163 "c = a + b\n"
164 "d = a - 2\n"
165 "d = 10 - d\n"
166 "d = d - b\n";
168 DOSTRING(L, prog);
169 DOSTRING(L, "assert(c.val == 10)");
170 DOSTRING(L, "assert(d.val == 2)");
172 DOSTRING(L,
173 "a = op_test1()\n"
174 "b = op_test2()\n"
175 "assert(a == b)");