aboutsummaryrefslogtreecommitdiff
path: root/testes/constructs.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testes/constructs.lua')
-rw-r--r--testes/constructs.lua302
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
6local debug = require "debug"
7
8
9local function checkload (s, msg)
10 assert(string.find(select(2, load(s)), msg))
11end
12
13-- testing semicollons
14do ;;; end
15; do ; a = 3; assert(a == 3) end;
16;
17
18
19-- invalid operations should not raise errors when not executed
20if false then a = 3 // 0; a = 0 % 0 end
21
22
23-- testing priorities
24
25assert(2^3^2 == 2^(3^2));
26assert(2^3*4 == (2^3)*4);
27assert(2.0^-2 == 1/4 and -2^- -2 == - - -4);
28assert(not nil and 2 and not(2>3 or 3<2));
29assert(-3-1-5 == 0+0-9);
30assert(-2^2 == -4 and (-2)^2 == 4 and 2*2-3-1 == 0);
31assert(-3%5 == 2 and -3+5 == 2)
32assert(2*1+3/3 == 3 and 1+2 .. 3*1 == "33");
33assert(not(2+1 > 3*1) and "a".."b" > "a");
34
35assert(0xF0 | 0xCC ~ 0xAA & 0xFD == 0xF4)
36assert(0xFD & 0xAA ~ 0xCC | 0xF0 == 0xF4)
37assert(0xF0 & 0x0F + 1 == 0x10)
38
39assert(3^4//2^3//5 == 2)
40
41assert(-3+4*5//2^3^2//9+4%10/3 == (-3)+(((4*5)//(2^(3^2)))//9)+((4%10)/3))
42
43assert(not ((true or false) and nil))
44assert( true or false and nil)
45
46-- old bug
47assert((((1 or false) and true) or false) == true)
48assert((((nil and true) or false) and true) == false)
49
50local a,b = 1,nil;
51assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75);
52x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x);
53x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x);
54
55x,y=1,2;
56assert((x>y) and x or y == 2);
57x,y=2,1;
58assert((x>y) and x or y == 2);
59
60assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891)
61
62
63-- silly loops
64repeat until 1; repeat until true;
65while false do end; while nil do end;
66
67do -- 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
69end
70
71function f (i)
72 if type(i) ~= 'number' then return i,'jojo'; end;
73 if i > 0 then return i, f(i-1); end;
74end
75
76x = {f(3), f(5), f(10);};
77assert(x[1] == 3 and x[2] == 5 and x[3] == 10 and x[4] == 9 and x[12] == 1);
78assert(x[nil] == nil)
79x = {f'alo', f'xixi', nil};
80assert(x[1] == 'alo' and x[2] == 'xixi' and x[3] == nil);
81x = {f'alo'..'xixi'};
82assert(x[1] == 'aloxixi')
83x = {f{}}
84assert(x[2] == 'jojo' and type(x[1]) == 'table')
85
86
87local 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;
92end
93
94assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil)
95
96for i=1,1000 do break; end;
97n=100;
98i=3;
99t = {};
100a=nil
101while 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;
103end
104assert(a == n*(n+1)/2 and i==3);
105assert(t[1] and t[n] and not t[0] and not t[n+1])
106
107function 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;
118end;
119
120assert(f(1) == 10 and f(2) == 20 and f(3) == 30 and f(4)==12)
121
122
123local 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
129end
130
131assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == 8)
132
133local a, b = nil, 23
134x = {f(100)*2+3 or a, a or b+2}
135assert(x[1] == 19 and x[2] == 25)
136x = {f=2+3 or a, a = b+2}
137assert(x.f == 5 and x.a == 25)
138
139a={y=1}
140x = {a.y}
141assert(x[1] == 1)
142
143function f(i)
144 while 1 do
145 if i>0 then i=i-1;
146 else return; end;
147 end;
148end;
149
150function g(i)
151 while 1 do
152 if i>0 then i=i-1
153 else return end
154 end
155end
156
157f(10); g(10);
158
159do
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)
165end
166
167local a,b = 3 and f();
168assert(a==1 and b==nil)
169
170function g() f(); return; end;
171assert(g() == nil)
172function g() return nil or f() end
173a,b = g()
174assert(a==1 and b==nil)
175
176print'+';
177
178
179f = [[
180return function ( a , b , c , d , e )
181 local x = a >= b or c or ( d and e ) or nil
182 return x
183end , { a = 1 , b = 2 >= 1 , } or { 1 };
184]]
185f = string.gsub(f, "%s+", "\n"); -- force a SETLINE between opcodes
186f,a = load(f)();
187assert(a.a == 1 and a.b)
188
189function 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;
191end
192
193function 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;
196end;
197
198assert(f(2,1) == true and g(2,1) == 1 and h(2,1) == 1)
199assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1)
200assert(f(1,2,'a')
201~= -- force SETLINE before nil
202nil, "")
203assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1)
204assert(f(1,2,nil,1,'x') == 'x' and g(1,2,nil,1,'x') == 1 and
205 h(1,2,nil,1,'x') == 1)
206assert(f(1,2,nil,nil,'x') == nil and g(1,2,nil,nil,'x') == 0 and
207 h(1,2,nil,nil,'x') == 0)
208assert(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
211assert(1 and 2<3 == true and 2<3 and 'a'<'b' == true)
212x = 2<3 and not 3; assert(x==false)
213x = 2<1 or (2>1 and 'a'); assert(x=='a')
214
215
216do
217 local a; if nil then a=1; else a=2; end; -- this nil comes as PUSHNIL 2
218 assert(a==2)
219end
220
221function F(a)
222 assert(debug.getinfo(1, "n").name == 'F')
223 return a,2,3
224end
225
226a,b = F(1)~=nil; assert(a == true and b == nil);
227a,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
236local basiccases = {
237 {"nil", nil},
238 {"false", false},
239 {"true", true},
240 {"10", 10},
241 {"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1},
242}
243
244print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')')
245
246
247-- operators with their respective values
248local 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
253local cases = {}
254
255-- creates all combinations of '(cases[i] op cases[n-i])' plus
256-- 'not(cases[i] op cases[n-i])' (syntax + value)
257local 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
274end
275
276-- do not do too many combinations for soft tests
277local level = _soft and 3 or 4
278
279cases[1] = basiccases
280for i = 2, level do cases[i] = createcases(i) end
281print("+")
282
283local prog = [[if %s then IX = true end; return %s]]
284
285local i = 0
286for 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
295end
296------------------------------------------------------------------
297
298-- testing some syntax errors (chosen through 'gcov')
299checkload("for x do", "expected")
300checkload("x:call", "expected")
301
302print'OK'