test_operators.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #include "test.hpp"
  20. #include <luabind/luabind.hpp>
  21. #include <luabind/operator.hpp>
  22. #include <iosfwd>
  23. struct operator_tester : counted_type<operator_tester>
  24. {
  25. int operator+(int a) const
  26. {
  27. return 1 + a;
  28. }
  29. int operator-() const
  30. {
  31. return 46;
  32. }
  33. float operator()() const
  34. {
  35. return 3.5f;
  36. }
  37. float operator()(int a) const
  38. {
  39. return 3.5f + a;
  40. }
  41. float operator()(int a)
  42. {
  43. return 2.5f + a;
  44. }
  45. };
  46. float operator*(operator_tester const& lhs, operator_tester const& rhs)
  47. {
  48. return 35.f;
  49. }
  50. std::string operator*(operator_tester const&, int v)
  51. {
  52. return "(operator_tester, int) overload";
  53. }
  54. int operator+(int a, const operator_tester&)
  55. {
  56. return 2 + a;
  57. }
  58. struct operator_tester2 : counted_type<operator_tester2>
  59. {
  60. };
  61. int operator+(const operator_tester&, const operator_tester2&)
  62. {
  63. return 73;
  64. }
  65. struct operator_tester3: operator_tester, counted_type<operator_tester3> {};
  66. std::ostream& operator<<(std::ostream& os, const operator_tester&)
  67. {
  68. os << "operator_tester"; return os;
  69. }
  70. struct op_test1
  71. {
  72. bool operator==(op_test1 const& rhs) const { return true; }
  73. };
  74. struct op_test2 : public op_test1
  75. {
  76. bool operator==(op_test2 const& rhs) const { return true; }
  77. };
  78. COUNTER_GUARD(operator_tester);
  79. COUNTER_GUARD(operator_tester2);
  80. COUNTER_GUARD(operator_tester3);
  81. struct len_tester
  82. {
  83. len_tester(int len)
  84. : len_(len)
  85. {}
  86. int len() const
  87. {
  88. return len_;
  89. }
  90. int len_;
  91. };
  92. void test_main(lua_State* L)
  93. {
  94. using namespace luabind;
  95. module(L)
  96. [
  97. class_<operator_tester>("operator_tester")
  98. .def(constructor<>())
  99. .def(tostring(const_self))
  100. .def(self + int())
  101. .def(other<int>() + self)
  102. .def(-self)
  103. .def(self + other<operator_tester2&>())
  104. .def(self())
  105. .def(const_self(int()))
  106. .def(self(int()))
  107. // .def(const_self * other<operator_tester const&>())
  108. .def(const_self * const_self)
  109. .def(const_self * other<int>()),
  110. // .def("clone", &clone, adopt(return_value)),
  111. class_<operator_tester2>("operator_tester2")
  112. .def(constructor<>())
  113. .def(other<const operator_tester&>() + self),
  114. class_<operator_tester3, operator_tester>("operator_tester3")
  115. .def(constructor<>()),
  116. class_<op_test1>("op_test1")
  117. .def(constructor<>())
  118. .def(const_self == const_self),
  119. class_<op_test2, op_test1>("op_test2")
  120. .def(constructor<>())
  121. .def(self == self),
  122. class_<len_tester>("len_tester")
  123. .def(constructor<int>())
  124. .def("__len", &len_tester::len)
  125. ];
  126. DOSTRING(L, "test = operator_tester()");
  127. DOSTRING(L, "test2 = operator_tester2()");
  128. DOSTRING(L, "test3 = operator_tester3()");
  129. DOSTRING(L, "assert(tostring(test) == 'operator_tester')");
  130. DOSTRING(L, "assert(test() == 3.5)");
  131. DOSTRING(L, "assert(test(5) == 2.5 + 5)");
  132. DOSTRING(L, "assert(-test == 46)");
  133. DOSTRING(L, "assert(test * test == 35)");
  134. DOSTRING(L, "assert(test * 3 == '(operator_tester, int) overload')")
  135. DOSTRING(L, "assert(test + test2 == 73)");
  136. DOSTRING(L, "assert(2 + test == 2 + 2)");
  137. DOSTRING(L, "assert(test + 2 == 1 + 2)");
  138. DOSTRING(L, "assert(test3 + 6 == 1 + 6)");
  139. DOSTRING(L, "assert(test3 + test2 == 73)");
  140. // DOSTRING(L, "assert(tostring(test) == 'operator_tester')");
  141. const char* prog =
  142. "class 'my_class'\n"
  143. "function my_class:__add(lhs)\n"
  144. " return my_class(self.val + lhs.val)\n"
  145. "end\n"
  146. "function my_class:__init(a)\n"
  147. " self.val = a\n"
  148. "end\n"
  149. "function my_class:__sub(v)\n"
  150. " if (type(self) == 'number') then\n"
  151. " return my_class(self - v.val)\n"
  152. " elseif (type(v) == 'number') then\n"
  153. " return my_class(self.val - v)\n"
  154. " else\n"
  155. " return my_class(self.val - v.val)\n"
  156. " end\n"
  157. "end\n"
  158. "a = my_class(3)\n"
  159. "b = my_class(7)\n"
  160. "c = a + b\n"
  161. "d = a - 2\n"
  162. "d = 10 - d\n"
  163. "d = d - b\n";
  164. DOSTRING(L, prog);
  165. DOSTRING(L, "assert(c.val == 10)");
  166. DOSTRING(L, "assert(d.val == 2)");
  167. DOSTRING(L,
  168. "a = op_test1()\n"
  169. "b = op_test2()\n"
  170. "assert(a == b)");
  171. DOSTRING(L,
  172. "x = len_tester(0)\n");
  173. DOSTRING(L,
  174. "x = len_tester(3)\n"
  175. "assert(#x == 3)");
  176. }