diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-17 14:46:37 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-17 14:46:37 -0200 |
commit | 063d4e4543088e7a21965bda8ee5a0f952a9029e (patch) | |
tree | 6c3f2f8e98c26f071a94a32f9f2754396a66a9de /testes/constructs.lua | |
parent | e354c6355e7f48e087678ec49e340ca0696725b1 (diff) | |
download | lua-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.lua | 313 |
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 | |||
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("7" .. 3 << 1 == 146) | ||
36 | assert(10 >> 1 .. "9" == 0) | ||
37 | assert(10 | 1 .. "9" == 27) | ||
38 | |||
39 | assert(0xF0 | 0xCC ~ 0xAA & 0xFD == 0xF4) | ||
40 | assert(0xFD & 0xAA ~ 0xCC | 0xF0 == 0xF4) | ||
41 | assert(0xF0 & 0x0F + 1 == 0x10) | ||
42 | |||
43 | assert(3^4//2^3//5 == 2) | ||
44 | |||
45 | assert(-3+4*5//2^3^2//9+4%10/3 == (-3)+(((4*5)//(2^(3^2)))//9)+((4%10)/3)) | ||
46 | |||
47 | assert(not ((true or false) and nil)) | ||
48 | assert( true or false and nil) | ||
49 | |||
50 | -- old bug | ||
51 | assert((((1 or false) and true) or false) == true) | ||
52 | assert((((nil and true) or false) and true) == false) | ||
53 | |||
54 | local a,b = 1,nil; | ||
55 | assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75); | ||
56 | x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); | ||
57 | x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x); | ||
58 | |||
59 | x,y=1,2; | ||
60 | assert((x>y) and x or y == 2); | ||
61 | x,y=2,1; | ||
62 | assert((x>y) and x or y == 2); | ||
63 | |||
64 | assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) | ||
65 | |||
66 | |||
67 | -- silly loops | ||
68 | repeat until 1; repeat until true; | ||
69 | while false do end; while nil do end; | ||
70 | |||
71 | do -- 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 | ||
73 | end | ||
74 | |||
75 | function f (i) | ||
76 | if type(i) ~= 'number' then return i,'jojo'; end; | ||
77 | if i > 0 then return i, f(i-1); end; | ||
78 | end | ||
79 | |||
80 | x = {f(3), f(5), f(10);}; | ||
81 | assert(x[1] == 3 and x[2] == 5 and x[3] == 10 and x[4] == 9 and x[12] == 1); | ||
82 | assert(x[nil] == nil) | ||
83 | x = {f'alo', f'xixi', nil}; | ||
84 | assert(x[1] == 'alo' and x[2] == 'xixi' and x[3] == nil); | ||
85 | x = {f'alo'..'xixi'}; | ||
86 | assert(x[1] == 'aloxixi') | ||
87 | x = {f{}} | ||
88 | assert(x[2] == 'jojo' and type(x[1]) == 'table') | ||
89 | |||
90 | |||
91 | local 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; | ||
96 | end | ||
97 | |||
98 | assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) | ||
99 | |||
100 | for i=1,1000 do break; end; | ||
101 | n=100; | ||
102 | i=3; | ||
103 | t = {}; | ||
104 | a=nil | ||
105 | while 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; | ||
107 | end | ||
108 | assert(a == n*(n+1)/2 and i==3); | ||
109 | assert(t[1] and t[n] and not t[0] and not t[n+1]) | ||
110 | |||
111 | function 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; | ||
122 | end; | ||
123 | |||
124 | assert(f(1) == 10 and f(2) == 20 and f(3) == 30 and f(4)==12) | ||
125 | |||
126 | |||
127 | local 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 | ||
133 | end | ||
134 | |||
135 | assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == 8) | ||
136 | |||
137 | local a, b = nil, 23 | ||
138 | x = {f(100)*2+3 or a, a or b+2} | ||
139 | assert(x[1] == 19 and x[2] == 25) | ||
140 | x = {f=2+3 or a, a = b+2} | ||
141 | assert(x.f == 5 and x.a == 25) | ||
142 | |||
143 | a={y=1} | ||
144 | x = {a.y} | ||
145 | assert(x[1] == 1) | ||
146 | |||
147 | function f(i) | ||
148 | while 1 do | ||
149 | if i>0 then i=i-1; | ||
150 | else return; end; | ||
151 | end; | ||
152 | end; | ||
153 | |||
154 | function g(i) | ||
155 | while 1 do | ||
156 | if i>0 then i=i-1 | ||
157 | else return end | ||
158 | end | ||
159 | end | ||
160 | |||
161 | f(10); g(10); | ||
162 | |||
163 | do | ||
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) | ||
169 | end | ||
170 | |||
171 | local a,b = 3 and f(); | ||
172 | assert(a==1 and b==nil) | ||
173 | |||
174 | function g() f(); return; end; | ||
175 | assert(g() == nil) | ||
176 | function g() return nil or f() end | ||
177 | a,b = g() | ||
178 | assert(a==1 and b==nil) | ||
179 | |||
180 | print'+'; | ||
181 | |||
182 | |||
183 | f = [[ | ||
184 | return function ( a , b , c , d , e ) | ||
185 | local x = a >= b or c or ( d and e ) or nil | ||
186 | return x | ||
187 | end , { a = 1 , b = 2 >= 1 , } or { 1 }; | ||
188 | ]] | ||
189 | f = string.gsub(f, "%s+", "\n"); -- force a SETLINE between opcodes | ||
190 | f,a = load(f)(); | ||
191 | assert(a.a == 1 and a.b) | ||
192 | |||
193 | function 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; | ||
195 | end | ||
196 | |||
197 | function 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; | ||
200 | end; | ||
201 | |||
202 | assert(f(2,1) == true and g(2,1) == 1 and h(2,1) == 1) | ||
203 | assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1) | ||
204 | assert(f(1,2,'a') | ||
205 | ~= -- force SETLINE before nil | ||
206 | nil, "") | ||
207 | assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1) | ||
208 | assert(f(1,2,nil,1,'x') == 'x' and g(1,2,nil,1,'x') == 1 and | ||
209 | h(1,2,nil,1,'x') == 1) | ||
210 | assert(f(1,2,nil,nil,'x') == nil and g(1,2,nil,nil,'x') == 0 and | ||
211 | h(1,2,nil,nil,'x') == 0) | ||
212 | assert(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 | |||
215 | assert(1 and 2<3 == true and 2<3 and 'a'<'b' == true) | ||
216 | x = 2<3 and not 3; assert(x==false) | ||
217 | x = 2<1 or (2>1 and 'a'); assert(x=='a') | ||
218 | |||
219 | |||
220 | do | ||
221 | local a; if nil then a=1; else a=2; end; -- this nil comes as PUSHNIL 2 | ||
222 | assert(a==2) | ||
223 | end | ||
224 | |||
225 | function F(a) | ||
226 | assert(debug.getinfo(1, "n").name == 'F') | ||
227 | return a,2,3 | ||
228 | end | ||
229 | |||
230 | a,b = F(1)~=nil; assert(a == true and b == nil); | ||
231 | a,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 | ||
240 | local basiccases = { | ||
241 | {"nil", nil}, | ||
242 | {"false", false}, | ||
243 | {"true", true}, | ||
244 | {"10", 10}, | ||
245 | {"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1}, | ||
246 | } | ||
247 | |||
248 | print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')') | ||
249 | |||
250 | |||
251 | -- operators with their respective values | ||
252 | local 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 | |||
257 | local cases = {} | ||
258 | |||
259 | -- creates all combinations of '(cases[i] op cases[n-i])' plus | ||
260 | -- 'not(cases[i] op cases[n-i])' (syntax + value) | ||
261 | local 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 | ||
278 | end | ||
279 | |||
280 | -- do not do too many combinations for soft tests | ||
281 | local level = _soft and 3 or 4 | ||
282 | |||
283 | cases[1] = basiccases | ||
284 | for i = 2, level do cases[i] = createcases(i) end | ||
285 | print("+") | ||
286 | |||
287 | local prog = [[if %s then IX = true end; return %s]] | ||
288 | |||
289 | local i = 0 | ||
290 | for 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 | ||
299 | end | ||
300 | ------------------------------------------------------------------ | ||
301 | |||
302 | -- testing some syntax errors (chosen through 'gcov') | ||
303 | checkload("for x do", "expected") | ||
304 | checkload("x:call", "expected") | ||
305 | |||
306 | if 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") | ||
311 | end | ||
312 | |||
313 | print'OK' | ||