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