/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) 2require("import") -- the import fn 3import("operator_overload") -- import lib 4 5for k,v in pairs(operator_overload) do _G[k]=v end -- move to global 6 7-- first check all the operators are implemented correctly from pure C++ code 8Op_sanity_check() 9 10-- catching undefined variables 11setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) 12 13-- test routine: 14a=Op() 15b=Op(5) 16c=Op(b) -- copy construct 17d=Op(2) 18dd=d; -- assignment operator 19 20-- test equality 21assert(a~=b) 22assert(b==c) 23assert(a~=d) 24assert(d==dd) 25 26-- test < 27assert(a<b) 28assert(a<=b) 29assert(b<=c) 30assert(b>=c) 31assert(b>d) 32assert(b>=d) 33 34-- lua does not support += operators: skiping 35 36-- test + 37f=Op(1) 38g=Op(1) 39assert(f+g==Op(2)) 40assert(f-g==Op(0)) 41assert(f*g==Op(1)) 42assert(f/g==Op(1)) 43--assert(f%g==Op(0)) -- lua does not support % 44 45-- test unary operators 46--assert((not a)==true) -- lua does not allow overloading for not operator 47--assert((not b)==false) -- lua does not allow overloading for not operator 48assert(-a==a) 49assert(-b==Op(-5)) 50 51-- test [] 52h=Op(3) 53assert(h[0]==3) 54assert(h[1]==0) 55h[0]=2 -- set 56assert(h[0]==2) 57h[1]=2 -- ignored 58assert(h[0]==2) 59assert(h[1]==0) 60 61-- test () 62i=Op(3) 63assert(i()==3) 64assert(i(1)==4) 65assert(i(1,2)==6) 66 67-- plus add some code to check the __str__ fn 68assert(tostring(Op(1))=="Op(1)") 69assert(tostring(Op(-3))=="Op(-3)") 70 71--[[ 72/* Sample test code in C++ 73 74#include <assert.h> 75#include <stdio.h> 76 77int main(int argc,char** argv) 78{ 79 // test routine: 80 Op a; 81 Op b=5; 82 Op c=b; // copy construct 83 Op d=2; 84 85 // test equality 86 assert(a!=b); 87 assert(b==c); 88 assert(a!=d); 89 90 // test < 91 assert(a<b); 92 assert(a<=b); 93 assert(b<=c); 94 assert(b>=c); 95 assert(b>d); 96 assert(b>=d); 97 98 // test += 99 Op e=3; 100 e+=d; 101 assert(e==b); 102 e-=c; 103 assert(e==a); 104 e=Op(1); 105 e*=b; 106 assert(e==c); 107 e/=d; 108 assert(e==d); 109 e%=c; 110 assert(e==d); 111 112 // test + 113 Op f(1),g(1); 114 assert(f+g==Op(2)); 115 assert(f-g==Op(0)); 116 assert(f*g==Op(1)); 117 assert(f/g==Op(1)); 118 assert(f%g==Op(0)); 119 120 // test unary operators 121 assert(!a==true); 122 assert(!b==false); 123 assert(-a==a); 124 assert(-b==Op(-5)); 125 126 // test [] 127 Op h=3; 128 assert(h[0]==3); 129 assert(h[1]==0); 130 h[0]=2; // set 131 assert(h[0]==2); 132 h[1]=2; // ignored 133 assert(h[0]==2); 134 assert(h[1]==0); 135 136 // test () 137 Op i=3; 138 assert(i()==3); 139 assert(i(1)==4); 140 assert(i(1,2)==6); 141 142 // plus add some code to check the __str__ fn 143 //assert(str(Op(1))=="Op(1)"); 144 //assert(str(Op(-3))=="Op(-3)"); 145 146 printf("ok\n"); 147} 148*/ 149]]