added weak_ref
[luabind.git] / test / test_operators.cpp
blobf4784157f61bb441ae6bb49c352dd126820cb71a
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>
26 namespace
28 struct operator_tester : counted_type<operator_tester>
30 int operator+(int a) const
32 return 1 + a;
35 float operator-() const
37 return 4.6f;
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 float operator+(const operator_tester&, const operator_tester2&)
68 return 7.3f;
71 struct operator_tester3: operator_tester, counted_type<operator_tester3> {};
73 const operator_tester* make_const_test()
75 static operator_tester o;
76 return &o;
79 std::ostream& operator<<(std::ostream& os, const operator_tester&)
81 os << "operator_tester"; return os;
84 operator_tester* clone(const operator_tester* p)
85 { return new operator_tester(*p); }
87 } // anonymous namespace
89 void test_operators()
91 COUNTER_GUARD(operator_tester);
92 COUNTER_GUARD(operator_tester2);
93 COUNTER_GUARD(operator_tester3);
95 lua_state L;
97 using namespace luabind;
99 module(L)
101 class_<operator_tester>("operator_tester")
102 .def(constructor<>())
103 .def(tostring(const_self))
104 .def(self + int())
105 .def(other<int>() + self)
106 .def(-self)
107 .def(self())
108 .def(const_self(int()))
109 .def(self(int())),
110 // .def("clone", &clone, adopt(return_value)),
112 class_<operator_tester2>("operator_tester2")
113 .def(constructor<>())
114 .def(other<const operator_tester&>() + self),
116 class_<operator_tester3, bases<operator_tester> >("operator_tester3")
117 .def(constructor<>()),
119 def("make_const_test", &make_const_test)
122 DOSTRING(L, "test = operator_tester()");
123 DOSTRING(L, "test2 = operator_tester2()");
124 DOSTRING(L, "test3 = operator_tester3()");
125 DOSTRING(L, "const_test = make_const_test()");
127 DOSTRING(L, "assert(test() == 3.5)");
128 DOSTRING(L, "assert(test(5) == 2.5 + 5)");
129 DOSTRING(L, "assert(const_test(7) == 3.5 + 7)");
131 DOSTRING(L, "assert(-test == 4.6)");
132 DOSTRING(L, "assert(test + test2 == 7.3)");
133 DOSTRING(L, "assert(2 + test == 2 + 2)");
134 DOSTRING(L, "assert(test + 2 == 1 + 2)");
135 DOSTRING(L, "assert(test3 + 6 == 1 + 6)");
136 DOSTRING(L, "assert(test3 + test2 == 7.3)");
137 DOSTRING(L, "assert(tostring(test) == 'operator_tester')");
139 const char* prog =
140 "class 'my_class'\n"
141 "function my_class:__add(lhs)\n"
142 " return my_class(self.val + lhs.val)\n"
143 "end\n"
144 "function my_class:__init(a)\n"
145 " self.val = a\n"
146 "end\n"
147 "function my_class:__sub(v)\n"
148 " if (type(self) == 'number') then\n"
149 " return my_class(self - v.val)\n"
150 " elseif (type(v) == 'number') then\n"
151 " return my_class(self.val - v)\n"
152 " else\n"
153 " return my_class(self.val - v.val)\n"
154 " end\n"
155 "end\n"
156 "a = my_class(3)\n"
157 "b = my_class(7)\n"
158 "c = a + b\n"
159 "d = a - 2\n"
160 "d = 10 - d\n"
161 "d = d - b\n";
163 DOSTRING(L, prog);
164 DOSTRING(L, "assert(c == 10)");
165 DOSTRING(L, "assert(d == 2)");