PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms 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
  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
 48
 49--lua 5.0.2 defines that unary - is __unm(self,nil)
 50--lua 5.1.2 defines that unary - is __unm(self,self)
 51--C++ expectes unary - as operator-()
 52--however the latest version of SWIG strictly checks the number of args
 53--and will complain if too many args are provided
 54--therefore disabling these tests for now
 55-- (solution will to be not to check args for this test case)
 56assert(-a==a)
 57assert(-b==Op(-5))
 58
 59-- test []
 60h=Op(3)
 61assert(h[0]==3)
 62assert(h[1]==0)
 63h[0]=2	-- set
 64assert(h[0]==2)
 65h[1]=2	-- ignored
 66assert(h[0]==2)
 67assert(h[1]==0)
 68
 69-- test ()
 70i=Op(3)
 71assert(i()==3)
 72assert(i(1)==4)
 73assert(i(1,2)==6)
 74
 75-- plus add some code to check the __str__ fn
 76assert(tostring(Op(1))=="Op(1)")
 77assert(tostring(Op(-3))=="Op(-3)")
 78
 79--[[
 80/* Sample test code in C++
 81
 82#include <assert.h>
 83#include <stdio.h>
 84
 85int main(int argc,char** argv)
 86{
 87	// test routine:
 88	Op a;
 89	Op b=5;
 90	Op c=b;	// copy construct
 91	Op d=2;
 92
 93	// test equality
 94	assert(a!=b);
 95	assert(b==c);
 96	assert(a!=d);
 97
 98	// test <
 99	assert(a<b);
100	assert(a<=b);
101	assert(b<=c);
102	assert(b>=c);
103	assert(b>d);
104	assert(b>=d);
105
106	// test +=
107	Op e=3;
108	e+=d;
109	assert(e==b);
110	e-=c;
111	assert(e==a);
112	e=Op(1);
113	e*=b;
114	assert(e==c);
115	e/=d;
116	assert(e==d);
117	e%=c;
118	assert(e==d);
119
120	// test +
121	Op f(1),g(1);
122	assert(f+g==Op(2));
123	assert(f-g==Op(0));
124	assert(f*g==Op(1));
125	assert(f/g==Op(1));
126	assert(f%g==Op(0));
127
128	// test unary operators
129	assert(!a==true);
130	assert(!b==false);
131	assert(-a==a);
132	assert(-b==Op(-5));
133
134	// test []
135	Op h=3;
136	assert(h[0]==3);
137	assert(h[1]==0);
138	h[0]=2;	// set
139	assert(h[0]==2);
140	h[1]=2;	// ignored
141	assert(h[0]==2);
142	assert(h[1]==0);
143
144	// test ()
145	Op i=3;
146	assert(i()==3);
147	assert(i(1)==4);
148	assert(i(1,2)==6);
149
150	// plus add some code to check the __str__ fn
151	//assert(str(Op(1))=="Op(1)");
152	//assert(str(Op(-3))=="Op(-3)");
153
154	printf("ok\n");
155}
156*/
157]]