PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-29/SWIG/Examples/test-suite/lua/operator_overload_runme.lua

#
Lua | 149 lines | 143 code | 3 blank | 3 comment | 1 complexity | 297a6c7ce7c8ba24be04467aa6b88f57 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. assert(-a==a)
  40. assert(-b==Op(-5))
  41. -- test []
  42. h=Op(3)
  43. assert(h[0]==3)
  44. assert(h[1]==0)
  45. h[0]=2 -- set
  46. assert(h[0]==2)
  47. h[1]=2 -- ignored
  48. assert(h[0]==2)
  49. assert(h[1]==0)
  50. -- test ()
  51. i=Op(3)
  52. assert(i()==3)
  53. assert(i(1)==4)
  54. assert(i(1,2)==6)
  55. -- plus add some code to check the __str__ fn
  56. assert(tostring(Op(1))=="Op(1)")
  57. assert(tostring(Op(-3))=="Op(-3)")
  58. --[[
  59. /* Sample test code in C++
  60. #include <assert.h>
  61. #include <stdio.h>
  62. int main(int argc,char** argv)
  63. {
  64. // test routine:
  65. Op a;
  66. Op b=5;
  67. Op c=b; // copy construct
  68. Op d=2;
  69. // test equality
  70. assert(a!=b);
  71. assert(b==c);
  72. assert(a!=d);
  73. // test <
  74. assert(a<b);
  75. assert(a<=b);
  76. assert(b<=c);
  77. assert(b>=c);
  78. assert(b>d);
  79. assert(b>=d);
  80. // test +=
  81. Op e=3;
  82. e+=d;
  83. assert(e==b);
  84. e-=c;
  85. assert(e==a);
  86. e=Op(1);
  87. e*=b;
  88. assert(e==c);
  89. e/=d;
  90. assert(e==d);
  91. e%=c;
  92. assert(e==d);
  93. // test +
  94. Op f(1),g(1);
  95. assert(f+g==Op(2));
  96. assert(f-g==Op(0));
  97. assert(f*g==Op(1));
  98. assert(f/g==Op(1));
  99. assert(f%g==Op(0));
  100. // test unary operators
  101. assert(!a==true);
  102. assert(!b==false);
  103. assert(-a==a);
  104. assert(-b==Op(-5));
  105. // test []
  106. Op h=3;
  107. assert(h[0]==3);
  108. assert(h[1]==0);
  109. h[0]=2; // set
  110. assert(h[0]==2);
  111. h[1]=2; // ignored
  112. assert(h[0]==2);
  113. assert(h[1]==0);
  114. // test ()
  115. Op i=3;
  116. assert(i()==3);
  117. assert(i(1)==4);
  118. assert(i(1,2)==6);
  119. // plus add some code to check the __str__ fn
  120. //assert(str(Op(1))=="Op(1)");
  121. //assert(str(Op(-3))=="Op(-3)");
  122. printf("ok\n");
  123. }
  124. */
  125. ]]