github.com/jslyzt/glua@v0.0.0-20210819023911-4030c8e0234a/_lua5.1-tests/constructs.lua (about) 1 print "testing syntax" 2 3 -- testing priorities 4 5 assert(2^3^2 == 2^(3^2)); 6 assert(2^3*4 == (2^3)*4); 7 assert(2^-2 == 1/4 and -2^- -2 == - - -4); 8 assert(not nil and 2 and not(2>3 or 3<2)); 9 assert(-3-1-5 == 0+0-9); 10 assert(-2^2 == -4 and (-2)^2 == 4 and 2*2-3-1 == 0); 11 assert(2*1+3/3 == 3 and 1+2 .. 3*1 == "33"); 12 assert(not(2+1 > 3*1) and "a".."b" > "a"); 13 14 assert(not ((true or false) and nil)) 15 assert( true or false and nil) 16 17 local a,b = 1,nil; 18 assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75); 19 x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); 20 x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x); 21 22 x,y=1,2; 23 assert((x>y) and x or y == 2); 24 x,y=2,1; 25 assert((x>y) and x or y == 2); 26 27 assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) 28 29 30 -- silly loops 31 repeat until 1; repeat until true; 32 while false do end; while nil do end; 33 34 do -- test old bug (first name could not be an `upvalue') 35 local a; function f(x) x={a=1}; x={x=1}; x={G=1} end 36 end 37 38 function f (i) 39 if type(i) ~= 'number' then return i,'jojo'; end; 40 if i > 0 then return i, f(i-1); end; 41 end 42 43 x = {f(3), f(5), f(10);}; 44 assert(x[1] == 3 and x[2] == 5 and x[3] == 10 and x[4] == 9 and x[12] == 1); 45 assert(x[nil] == nil) 46 x = {f'alo', f'xixi', nil}; 47 assert(x[1] == 'alo' and x[2] == 'xixi' and x[3] == nil); 48 x = {f'alo'..'xixi'}; 49 assert(x[1] == 'aloxixi') 50 x = {f{}} 51 assert(x[2] == 'jojo' and type(x[1]) == 'table') 52 53 54 local f = function (i) 55 if i < 10 then return 'a'; 56 elseif i < 20 then return 'b'; 57 elseif i < 30 then return 'c'; 58 end; 59 end 60 61 assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) 62 63 for i=1,1000 do break; end; 64 n=100; 65 i=3; 66 t = {}; 67 a=nil 68 while not a do 69 a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end; 70 end 71 assert(a == n*(n+1)/2 and i==3); 72 assert(t[1] and t[n] and not t[0] and not t[n+1]) 73 74 function f(b) 75 local x = 1; 76 repeat 77 local a; 78 if b==1 then local b=1; x=10; break 79 elseif b==2 then x=20; break; 80 elseif b==3 then x=30; 81 else local a,b,c,d=math.sin(1); x=x+1; 82 end 83 until x>=12; 84 return x; 85 end; 86 87 assert(f(1) == 10 and f(2) == 20 and f(3) == 30 and f(4)==12) 88 89 90 local f = function (i) 91 if i < 10 then return 'a' 92 elseif i < 20 then return 'b' 93 elseif i < 30 then return 'c' 94 else return 8 95 end 96 end 97 98 assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == 8) 99 100 local a, b = nil, 23 101 x = {f(100)*2+3 or a, a or b+2} 102 assert(x[1] == 19 and x[2] == 25) 103 x = {f=2+3 or a, a = b+2} 104 assert(x.f == 5 and x.a == 25) 105 106 a={y=1} 107 x = {a.y} 108 assert(x[1] == 1) 109 110 function f(i) 111 while 1 do 112 if i>0 then i=i-1; 113 else return; end; 114 end; 115 end; 116 117 function g(i) 118 while 1 do 119 if i>0 then i=i-1 120 else return end 121 end 122 end 123 124 f(10); g(10); 125 126 do 127 function f () return 1,2,3; end 128 local a, b, c = f(); 129 assert(a==1 and b==2 and c==3) 130 a, b, c = (f()); 131 assert(a==1 and b==nil and c==nil) 132 end 133 134 local a,b = 3 and f(); 135 assert(a==1 and b==nil) 136 137 function g() f(); return; end; 138 assert(g() == nil) 139 function g() return nil or f() end 140 a,b = g() 141 assert(a==1 and b==nil) 142 143 print'+'; 144 145 146 f = [[ 147 return function ( a , b , c , d , e ) 148 local x = a >= b or c or ( d and e ) or nil 149 return x 150 end , { a = 1 , b = 2 >= 1 , } or { 1 }; 151 ]] 152 f = string.gsub(f, "%s+", "\n"); -- force a SETLINE between opcodes 153 f,a = loadstring(f)(); 154 assert(a.a == 1 and a.b) 155 156 function g (a,b,c,d,e) 157 if not (a>=b or c or d and e or nil) then return 0; else return 1; end; 158 end 159 160 function h (a,b,c,d,e) 161 while (a>=b or c or (d and e) or nil) do return 1; end; 162 return 0; 163 end; 164 165 assert(f(2,1) == true and g(2,1) == 1 and h(2,1) == 1) 166 assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1) 167 assert(f(1,2,'a') 168 ~= -- force SETLINE before nil 169 nil, "") 170 assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1) 171 assert(f(1,2,nil,1,'x') == 'x' and g(1,2,nil,1,'x') == 1 and 172 h(1,2,nil,1,'x') == 1) 173 assert(f(1,2,nil,nil,'x') == nil and g(1,2,nil,nil,'x') == 0 and 174 h(1,2,nil,nil,'x') == 0) 175 assert(f(1,2,nil,1,nil) == nil and g(1,2,nil,1,nil) == 0 and 176 h(1,2,nil,1,nil) == 0) 177 178 assert(1 and 2<3 == true and 2<3 and 'a'<'b' == true) 179 x = 2<3 and not 3; assert(x==false) 180 x = 2<1 or (2>1 and 'a'); assert(x=='a') 181 182 183 do 184 local a; if nil then a=1; else a=2; end; -- this nil comes as PUSHNIL 2 185 assert(a==2) 186 end 187 188 function F(a) 189 assert(debug.getinfo(1, "n").name == 'F') 190 return a,2,3 191 end 192 193 a,b = F(1)~=nil; assert(a == true and b == nil); 194 a,b = F(nil)==nil; assert(a == true and b == nil) 195 196 ---------------------------------------------------------------- 197 -- creates all combinations of 198 -- [not] ([not] arg op [not] (arg op [not] arg )) 199 -- and tests each one 200 201 function ID(x) return x end 202 203 function f(t, i) 204 local b = t.n 205 local res = math.mod(math.floor(i/c), b)+1 206 c = c*b 207 return t[res] 208 end 209 210 local arg = {" ( 1 < 2 ) ", " ( 1 >= 2 ) ", " F ( ) ", " nil "; n=4} 211 212 local op = {" and ", " or ", " == ", " ~= "; n=4} 213 214 local neg = {" ", " not "; n=2} 215 216 local i = 0 217 repeat 218 c = 1 219 local s = f(neg, i)..'ID('..f(neg, i)..f(arg, i)..f(op, i).. 220 f(neg, i)..'ID('..f(arg, i)..f(op, i)..f(neg, i)..f(arg, i)..'))' 221 local s1 = string.gsub(s, 'ID', '') 222 K,X,NX,WX1,WX2 = nil 223 s = string.format([[ 224 local a = %s 225 local b = not %s 226 K = b 227 local xxx; 228 if %s then X = a else X = b end 229 if %s then NX = b else NX = a end 230 while %s do WX1 = a; break end 231 while %s do WX2 = a; break end 232 repeat if (%s) then break end; assert(b) until not(%s) 233 ]], s1, s, s1, s, s1, s, s1, s, s) 234 assert(loadstring(s))() 235 assert(X and not NX and not WX1 == K and not WX2 == K) 236 if math.mod(i,4000) == 0 then print('+') end 237 i = i+1 238 until i==c 239 240 print'OK'