PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/test-suite/lua/operator_overload_runme.lua

#
Lua | 157 lines | 151 code | 3 blank | 3 comment | 1 complexity | f208b538f15f0980715d29fb530d53be MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. -- demo of lua swig capacilities (operator overloading)
  2. require("import") -- the import fn
  3. import("operator_overload") -- import lib
  4. for k,v in pairs(operator_overload) do _G[k]=v end -- move to global
  5. -- first check all the operators are implemented correctly from pure C++ code
  6. Op_sanity_check()
  7. -- catching undefined variables
  8. setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
  9. -- test routine:
  10. a=Op()
  11. b=Op(5)
  12. c=Op(b) -- copy construct
  13. d=Op(2)
  14. dd=d; -- assignment operator
  15. -- test equality
  16. assert(a~=b)
  17. assert(b==c)
  18. assert(a~=d)
  19. assert(d==dd)
  20. -- test <
  21. assert(a<b)
  22. assert(a<=b)
  23. assert(b<=c)
  24. assert(b>=c)
  25. assert(b>d)
  26. assert(b>=d)
  27. -- lua does not support += operators: skiping
  28. -- test +
  29. f=Op(1)
  30. g=Op(1)
  31. assert(f+g==Op(2))
  32. assert(f-g==Op(0))
  33. assert(f*g==Op(1))
  34. assert(f/g==Op(1))
  35. --assert(f%g==Op(0)) -- lua does not support %
  36. -- test unary operators
  37. --assert((not a)==true) -- lua does not allow overloading for not operator
  38. --assert((not b)==false) -- lua does not allow overloading for not operator
  39. --lua 5.0.2 defines that unary - is __unm(self,nil)
  40. --lua 5.1.2 defines that unary - is __unm(self,self)
  41. --C++ expectes unary - as operator-()
  42. --however the latest version of SWIG strictly checks the number of args
  43. --and will complain if too many args are provided
  44. --therefore disabling these tests for now
  45. -- (solution will to be not to check args for this test case)
  46. assert(-a==a)
  47. assert(-b==Op(-5))
  48. -- test []
  49. h=Op(3)
  50. assert(h[0]==3)
  51. assert(h[1]==0)
  52. h[0]=2 -- set
  53. assert(h[0]==2)
  54. h[1]=2 -- ignored
  55. assert(h[0]==2)
  56. assert(h[1]==0)
  57. -- test ()
  58. i=Op(3)
  59. assert(i()==3)
  60. assert(i(1)==4)
  61. assert(i(1,2)==6)
  62. -- plus add some code to check the __str__ fn
  63. assert(tostring(Op(1))=="Op(1)")
  64. assert(tostring(Op(-3))=="Op(-3)")
  65. --[[
  66. /* Sample test code in C++
  67. #include <assert.h>
  68. #include <stdio.h>
  69. int main(int argc,char** argv)
  70. {
  71. // test routine:
  72. Op a;
  73. Op b=5;
  74. Op c=b; // copy construct
  75. Op d=2;
  76. // test equality
  77. assert(a!=b);
  78. assert(b==c);
  79. assert(a!=d);
  80. // test <
  81. assert(a<b);
  82. assert(a<=b);
  83. assert(b<=c);
  84. assert(b>=c);
  85. assert(b>d);
  86. assert(b>=d);
  87. // test +=
  88. Op e=3;
  89. e+=d;
  90. assert(e==b);
  91. e-=c;
  92. assert(e==a);
  93. e=Op(1);
  94. e*=b;
  95. assert(e==c);
  96. e/=d;
  97. assert(e==d);
  98. e%=c;
  99. assert(e==d);
  100. // test +
  101. Op f(1),g(1);
  102. assert(f+g==Op(2));
  103. assert(f-g==Op(0));
  104. assert(f*g==Op(1));
  105. assert(f/g==Op(1));
  106. assert(f%g==Op(0));
  107. // test unary operators
  108. assert(!a==true);
  109. assert(!b==false);
  110. assert(-a==a);
  111. assert(-b==Op(-5));
  112. // test []
  113. Op h=3;
  114. assert(h[0]==3);
  115. assert(h[1]==0);
  116. h[0]=2; // set
  117. assert(h[0]==2);
  118. h[1]=2; // ignored
  119. assert(h[0]==2);
  120. assert(h[1]==0);
  121. // test ()
  122. Op i=3;
  123. assert(i()==3);
  124. assert(i(1)==4);
  125. assert(i(1,2)==6);
  126. // plus add some code to check the __str__ fn
  127. //assert(str(Op(1))=="Op(1)");
  128. //assert(str(Op(-3))=="Op(-3)");
  129. printf("ok\n");
  130. }
  131. */
  132. ]]