diff options
Diffstat (limited to 'testes/constructs.lua')
-rw-r--r-- | testes/constructs.lua | 302 |
1 files changed, 302 insertions, 0 deletions
diff --git a/testes/constructs.lua b/testes/constructs.lua new file mode 100644 index 00000000..7796c46f --- /dev/null +++ b/testes/constructs.lua | |||
@@ -0,0 +1,302 @@ | |||
1 | -- $Id: constructs.lua,v 1.43 2018/02/21 17:41:07 roberto Exp $ | ||
2 | -- See Copyright Notice in file all.lua | ||
3 | |||
4 | ;;print "testing syntax";; | ||
5 | |||
6 | local debug = require "debug" | ||
7 | |||
8 | |||
9 | local function checkload (s, msg) | ||
10 | assert(string.find(select(2, load(s)), msg)) | ||
11 | end | ||
12 | |||
13 | -- testing semicollons | ||
14 | do ;;; end | ||
15 | ; do ; a = 3; assert(a == 3) end; | ||
16 | ; | ||
17 | |||
18 | |||
19 | -- invalid operations should not raise errors when not executed | ||
20 | if false then a = 3 // 0; a = 0 % 0 end | ||
21 | |||
22 | |||
23 | -- testing priorities | ||
24 | |||
25 | assert(2^3^2 == 2^(3^2)); | ||
26 | assert(2^3*4 == (2^3)*4); | ||
27 | assert(2.0^-2 == 1/4 and -2^- -2 == - - -4); | ||
28 | assert(not nil and 2 and not(2>3 or 3<2)); | ||
29 | assert(-3-1-5 == 0+0-9); | ||
30 | assert(-2^2 == -4 and (-2)^2 == 4 and 2*2-3-1 == 0); | ||
31 | assert(-3%5 == 2 and -3+5 == 2) | ||
32 | assert(2*1+3/3 == 3 and 1+2 .. 3*1 == "33"); | ||
33 | assert(not(2+1 > 3*1) and "a".."b" > "a"); | ||
34 | |||
35 | assert(0xF0 | 0xCC ~ 0xAA & 0xFD == 0xF4) | ||
36 | assert(0xFD & 0xAA ~ 0xCC | 0xF0 == 0xF4) | ||
37 | assert(0xF0 & 0x0F + 1 == 0x10) | ||
38 | |||
39 | assert(3^4//2^3//5 == 2) | ||
40 | |||
41 | assert(-3+4*5//2^3^2//9+4%10/3 == (-3)+(((4*5)//(2^(3^2)))//9)+((4%10)/3)) | ||
42 | |||
43 | assert(not ((true or false) and nil)) | ||
44 | assert( true or false and nil) | ||
45 | |||
46 | -- old bug | ||
47 | assert((((1 or false) and true) or false) == true) | ||
48 | assert((((nil and true) or false) and true) == false) | ||
49 | |||
50 | local a,b = 1,nil; | ||
51 | assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75); | ||
52 | x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); | ||
53 | x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x); | ||
54 | |||
55 | x,y=1,2; | ||
56 | assert((x>y) and x or y == 2); | ||
57 | x,y=2,1; | ||
58 | assert((x>y) and x or y == 2); | ||
59 | |||
60 | assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) | ||
61 | |||
62 | |||
63 | -- silly loops | ||
64 | repeat until 1; repeat until true; | ||
65 | while false do end; while nil do end; | ||
66 | |||
67 | do -- test old bug (first name could not be an `upvalue') | ||
68 | local a; function f(x) x={a=1}; x={x=1}; x={G=1} end | ||
69 | end | ||
70 | |||
71 | function f (i) | ||
72 | if type(i) ~= 'number' then return i,'jojo'; end; | ||
73 | if i > 0 then return i, f(i-1); end; | ||
74 | end | ||
75 | |||
76 | x = {f(3), f(5), f(10);}; | ||
77 | assert(x[1] == 3 and x[2] == 5 and x[3] == 10 and x[4] == 9 and x[12] == 1); | ||
78 | assert(x[nil] == nil) | ||
79 | x = {f'alo', f'xixi', nil}; | ||
80 | assert(x[1] == 'alo' and x[2] == 'xixi' and x[3] == nil); | ||
81 | x = {f'alo'..'xixi'}; | ||
82 | assert(x[1] == 'aloxixi') | ||
83 | x = {f{}} | ||
84 | assert(x[2] == 'jojo' and type(x[1]) == 'table') | ||
85 | |||
86 | |||
87 | local f = function (i) | ||
88 | if i < 10 then return 'a'; | ||
89 | elseif i < 20 then return 'b'; | ||
90 | elseif i < 30 then return 'c'; | ||
91 | end; | ||
92 | end | ||
93 | |||
94 | assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) | ||
95 | |||
96 | for i=1,1000 do break; end; | ||
97 | n=100; | ||
98 | i=3; | ||
99 | t = {}; | ||
100 | a=nil | ||
101 | while not a do | ||
102 | a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end; | ||
103 | end | ||
104 | assert(a == n*(n+1)/2 and i==3); | ||
105 | assert(t[1] and t[n] and not t[0] and not t[n+1]) | ||
106 | |||
107 | function f(b) | ||
108 | local x = 1; | ||
109 | repeat | ||
110 | local a; | ||
111 | if b==1 then local b=1; x=10; break | ||
112 | elseif b==2 then x=20; break; | ||
113 | elseif b==3 then x=30; | ||
114 | else local a,b,c,d=math.sin(1); x=x+1; | ||
115 | end | ||
116 | until x>=12; | ||
117 | return x; | ||
118 | end; | ||
119 | |||
120 | assert(f(1) == 10 and f(2) == 20 and f(3) == 30 and f(4)==12) | ||
121 | |||
122 | |||
123 | local f = function (i) | ||
124 | if i < 10 then return 'a' | ||
125 | elseif i < 20 then return 'b' | ||
126 | elseif i < 30 then return 'c' | ||
127 | else return 8 | ||
128 | end | ||
129 | end | ||
130 | |||
131 | assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == 8) | ||
132 | |||
133 | local a, b = nil, 23 | ||
134 | x = {f(100)*2+3 or a, a or b+2} | ||
135 | assert(x[1] == 19 and x[2] == 25) | ||
136 | x = {f=2+3 or a, a = b+2} | ||
137 | assert(x.f == 5 and x.a == 25) | ||
138 | |||
139 | a={y=1} | ||
140 | x = {a.y} | ||
141 | assert(x[1] == 1) | ||
142 | |||
143 | function f(i) | ||
144 | while 1 do | ||
145 | if i>0 then i=i-1; | ||
146 | else return; end; | ||
147 | end; | ||
148 | end; | ||
149 | |||
150 | function g(i) | ||
151 | while 1 do | ||
152 | if i>0 then i=i-1 | ||
153 | else return end | ||
154 | end | ||
155 | end | ||
156 | |||
157 | f(10); g(10); | ||
158 | |||
159 | do | ||
160 | function f () return 1,2,3; end | ||
161 | local a, b, c = f(); | ||
162 | assert(a==1 and b==2 and c==3) | ||
163 | a, b, c = (f()); | ||
164 | assert(a==1 and b==nil and c==nil) | ||
165 | end | ||
166 | |||
167 | local a,b = 3 and f(); | ||
168 | assert(a==1 and b==nil) | ||
169 | |||
170 | function g() f(); return; end; | ||
171 | assert(g() == nil) | ||
172 | function g() return nil or f() end | ||
173 | a,b = g() | ||
174 | assert(a==1 and b==nil) | ||
175 | |||
176 | print'+'; | ||
177 | |||
178 | |||
179 | f = [[ | ||
180 | return function ( a , b , c , d , e ) | ||
181 | local x = a >= b or c or ( d and e ) or nil | ||
182 | return x | ||
183 | end , { a = 1 , b = 2 >= 1 , } or { 1 }; | ||
184 | ]] | ||
185 | f = string.gsub(f, "%s+", "\n"); -- force a SETLINE between opcodes | ||
186 | f,a = load(f)(); | ||
187 | assert(a.a == 1 and a.b) | ||
188 | |||
189 | function g (a,b,c,d,e) | ||
190 | if not (a>=b or c or d and e or nil) then return 0; else return 1; end; | ||
191 | end | ||
192 | |||
193 | function h (a,b,c,d,e) | ||
194 | while (a>=b or c or (d and e) or nil) do return 1; end; | ||
195 | return 0; | ||
196 | end; | ||
197 | |||
198 | assert(f(2,1) == true and g(2,1) == 1 and h(2,1) == 1) | ||
199 | assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1) | ||
200 | assert(f(1,2,'a') | ||
201 | ~= -- force SETLINE before nil | ||
202 | nil, "") | ||
203 | assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1) | ||
204 | assert(f(1,2,nil,1,'x') == 'x' and g(1,2,nil,1,'x') == 1 and | ||
205 | h(1,2,nil,1,'x') == 1) | ||
206 | assert(f(1,2,nil,nil,'x') == nil and g(1,2,nil,nil,'x') == 0 and | ||
207 | h(1,2,nil,nil,'x') == 0) | ||
208 | assert(f(1,2,nil,1,nil) == nil and g(1,2,nil,1,nil) == 0 and | ||
209 | h(1,2,nil,1,nil) == 0) | ||
210 | |||
211 | assert(1 and 2<3 == true and 2<3 and 'a'<'b' == true) | ||
212 | x = 2<3 and not 3; assert(x==false) | ||
213 | x = 2<1 or (2>1 and 'a'); assert(x=='a') | ||
214 | |||
215 | |||
216 | do | ||
217 | local a; if nil then a=1; else a=2; end; -- this nil comes as PUSHNIL 2 | ||
218 | assert(a==2) | ||
219 | end | ||
220 | |||
221 | function F(a) | ||
222 | assert(debug.getinfo(1, "n").name == 'F') | ||
223 | return a,2,3 | ||
224 | end | ||
225 | |||
226 | a,b = F(1)~=nil; assert(a == true and b == nil); | ||
227 | a,b = F(nil)==nil; assert(a == true and b == nil) | ||
228 | |||
229 | ---------------------------------------------------------------- | ||
230 | ------------------------------------------------------------------ | ||
231 | |||
232 | -- sometimes will be 0, sometimes will not... | ||
233 | _ENV.GLOB1 = math.floor(os.time()) % 2 | ||
234 | |||
235 | -- basic expressions with their respective values | ||
236 | local basiccases = { | ||
237 | {"nil", nil}, | ||
238 | {"false", false}, | ||
239 | {"true", true}, | ||
240 | {"10", 10}, | ||
241 | {"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1}, | ||
242 | } | ||
243 | |||
244 | print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')') | ||
245 | |||
246 | |||
247 | -- operators with their respective values | ||
248 | local binops = { | ||
249 | {" and ", function (a,b) if not a then return a else return b end end}, | ||
250 | {" or ", function (a,b) if a then return a else return b end end}, | ||
251 | } | ||
252 | |||
253 | local cases = {} | ||
254 | |||
255 | -- creates all combinations of '(cases[i] op cases[n-i])' plus | ||
256 | -- 'not(cases[i] op cases[n-i])' (syntax + value) | ||
257 | local function createcases (n) | ||
258 | local res = {} | ||
259 | for i = 1, n - 1 do | ||
260 | for _, v1 in ipairs(cases[i]) do | ||
261 | for _, v2 in ipairs(cases[n - i]) do | ||
262 | for _, op in ipairs(binops) do | ||
263 | local t = { | ||
264 | "(" .. v1[1] .. op[1] .. v2[1] .. ")", | ||
265 | op[2](v1[2], v2[2]) | ||
266 | } | ||
267 | res[#res + 1] = t | ||
268 | res[#res + 1] = {"not" .. t[1], not t[2]} | ||
269 | end | ||
270 | end | ||
271 | end | ||
272 | end | ||
273 | return res | ||
274 | end | ||
275 | |||
276 | -- do not do too many combinations for soft tests | ||
277 | local level = _soft and 3 or 4 | ||
278 | |||
279 | cases[1] = basiccases | ||
280 | for i = 2, level do cases[i] = createcases(i) end | ||
281 | print("+") | ||
282 | |||
283 | local prog = [[if %s then IX = true end; return %s]] | ||
284 | |||
285 | local i = 0 | ||
286 | for n = 1, level do | ||
287 | for _, v in pairs(cases[n]) do | ||
288 | local s = v[1] | ||
289 | local p = load(string.format(prog, s, s), "") | ||
290 | IX = false | ||
291 | assert(p() == v[2] and IX == not not v[2]) | ||
292 | i = i + 1 | ||
293 | if i % 60000 == 0 then print('+') end | ||
294 | end | ||
295 | end | ||
296 | ------------------------------------------------------------------ | ||
297 | |||
298 | -- testing some syntax errors (chosen through 'gcov') | ||
299 | checkload("for x do", "expected") | ||
300 | checkload("x:call", "expected") | ||
301 | |||
302 | print'OK' | ||