summaryrefslogtreecommitdiff
path: root/testes/nextvar.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/nextvar.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/nextvar.lua631
1 files changed, 631 insertions, 0 deletions
diff --git a/testes/nextvar.lua b/testes/nextvar.lua
new file mode 100644
index 00000000..ce7312e3
--- /dev/null
+++ b/testes/nextvar.lua
@@ -0,0 +1,631 @@
1-- $Id: nextvar.lua,v 1.79 2016/11/07 13:11:28 roberto Exp $
2-- See Copyright Notice in file all.lua
3
4print('testing tables, next, and for')
5
6local function checkerror (msg, f, ...)
7 local s, err = pcall(f, ...)
8 assert(not s and string.find(err, msg))
9end
10
11
12local a = {}
13
14-- make sure table has lots of space in hash part
15for i=1,100 do a[i.."+"] = true end
16for i=1,100 do a[i.."+"] = nil end
17-- fill hash part with numeric indices testing size operator
18for i=1,100 do
19 a[i] = true
20 assert(#a == i)
21end
22
23-- testing ipairs
24local x = 0
25for k,v in ipairs{10,20,30;x=12} do
26 x = x + 1
27 assert(k == x and v == x * 10)
28end
29
30for _ in ipairs{x=12, y=24} do assert(nil) end
31
32-- test for 'false' x ipair
33x = false
34local i = 0
35for k,v in ipairs{true,false,true,false} do
36 i = i + 1
37 x = not x
38 assert(x == v)
39end
40assert(i == 4)
41
42-- iterator function is always the same
43assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{})
44
45
46if not T then
47 (Message or print)
48 ('\n >>> testC not active: skipping tests for table sizes <<<\n')
49else --[
50-- testing table sizes
51
52local function log2 (x) return math.log(x, 2) end
53
54local function mp2 (n) -- minimum power of 2 >= n
55 local mp = 2^math.ceil(log2(n))
56 assert(n == 0 or (mp/2 < n and n <= mp))
57 return mp
58end
59
60local function fb (n)
61 local r, nn = T.int2fb(n)
62 assert(r < 256)
63 return nn
64end
65
66-- test fb function
67for a = 1, 10000 do -- all numbers up to 10^4
68 local n = fb(a)
69 assert(a <= n and n <= a*1.125)
70end
71local a = 1024 -- plus a few up to 2 ^30
72local lim = 2^30
73while a < lim do
74 local n = fb(a)
75 assert(a <= n and n <= a*1.125)
76 a = math.ceil(a*1.3)
77end
78
79
80local function check (t, na, nh)
81 local a, h = T.querytab(t)
82 if a ~= na or h ~= nh then
83 print(na, nh, a, h)
84 assert(nil)
85 end
86end
87
88
89-- testing C library sizes
90do
91 local s = 0
92 for _ in pairs(math) do s = s + 1 end
93 check(math, 0, mp2(s))
94end
95
96
97-- testing constructor sizes
98local lim = 40
99local s = 'return {'
100for i=1,lim do
101 s = s..i..','
102 local s = s
103 for k=0,lim do
104 local t = load(s..'}', '')()
105 assert(#t == i)
106 check(t, fb(i), mp2(k))
107 s = string.format('%sa%d=%d,', s, k, k)
108 end
109end
110
111
112-- tests with unknown number of elements
113local a = {}
114for i=1,lim do a[i] = i end -- build auxiliary table
115for k=0,lim do
116 local a = {table.unpack(a,1,k)}
117 assert(#a == k)
118 check(a, k, 0)
119 a = {1,2,3,table.unpack(a,1,k)}
120 check(a, k+3, 0)
121 assert(#a == k + 3)
122end
123
124
125-- testing tables dynamically built
126local lim = 130
127local a = {}; a[2] = 1; check(a, 0, 1)
128a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2)
129a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1)
130a = {}
131for i = 1,lim do
132 a[i] = 1
133 assert(#a == i)
134 check(a, mp2(i), 0)
135end
136
137a = {}
138for i = 1,lim do
139 a['a'..i] = 1
140 assert(#a == 0)
141 check(a, 0, mp2(i))
142end
143
144a = {}
145for i=1,16 do a[i] = i end
146check(a, 16, 0)
147do
148 for i=1,11 do a[i] = nil end
149 for i=30,50 do a[i] = nil end -- force a rehash (?)
150 check(a, 0, 8) -- only 5 elements in the table
151 a[10] = 1
152 for i=30,50 do a[i] = nil end -- force a rehash (?)
153 check(a, 0, 8) -- only 6 elements in the table
154 for i=1,14 do a[i] = nil end
155 for i=18,50 do a[i] = nil end -- force a rehash (?)
156 check(a, 0, 4) -- only 2 elements ([15] and [16])
157end
158
159-- reverse filling
160for i=1,lim do
161 local a = {}
162 for i=i,1,-1 do a[i] = i end -- fill in reverse
163 check(a, mp2(i), 0)
164end
165
166-- size tests for vararg
167lim = 35
168function foo (n, ...)
169 local arg = {...}
170 check(arg, n, 0)
171 assert(select('#', ...) == n)
172 arg[n+1] = true
173 check(arg, mp2(n+1), 0)
174 arg.x = true
175 check(arg, mp2(n+1), 1)
176end
177local a = {}
178for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end
179
180end --]
181
182
183-- test size operation on empty tables
184assert(#{} == 0)
185assert(#{nil} == 0)
186assert(#{nil, nil} == 0)
187assert(#{nil, nil, nil} == 0)
188assert(#{nil, nil, nil, nil} == 0)
189print'+'
190
191
192local nofind = {}
193
194a,b,c = 1,2,3
195a,b,c = nil
196
197
198-- next uses always the same iteraction function
199assert(next{} == next{})
200
201local function find (name)
202 local n,v
203 while 1 do
204 n,v = next(_G, n)
205 if not n then return nofind end
206 assert(v ~= nil)
207 if n == name then return v end
208 end
209end
210
211local function find1 (name)
212 for n,v in pairs(_G) do
213 if n==name then return v end
214 end
215 return nil -- not found
216end
217
218
219assert(print==find("print") and print == find1("print"))
220assert(_G["print"]==find("print"))
221assert(assert==find1("assert"))
222assert(nofind==find("return"))
223assert(not find1("return"))
224_G["ret" .. "urn"] = nil
225assert(nofind==find("return"))
226_G["xxx"] = 1
227assert(xxx==find("xxx"))
228
229-- invalid key to 'next'
230checkerror("invalid key", next, {10,20}, 3)
231
232-- both 'pairs' and 'ipairs' need an argument
233checkerror("bad argument", pairs)
234checkerror("bad argument", ipairs)
235
236print('+')
237
238a = {}
239for i=0,10000 do
240 if math.fmod(i,10) ~= 0 then
241 a['x'..i] = i
242 end
243end
244
245n = {n=0}
246for i,v in pairs(a) do
247 n.n = n.n+1
248 assert(i and v and a[i] == v)
249end
250assert(n.n == 9000)
251a = nil
252
253do -- clear global table
254 local a = {}
255 for n,v in pairs(_G) do a[n]=v end
256 for n,v in pairs(a) do
257 if not package.loaded[n] and type(v) ~= "function" and
258 not string.find(n, "^[%u_]") then
259 _G[n] = nil
260 end
261 collectgarbage()
262 end
263end
264
265
266--
267
268local function checknext (a)
269 local b = {}
270 do local k,v = next(a); while k do b[k] = v; k,v = next(a,k) end end
271 for k,v in pairs(b) do assert(a[k] == v) end
272 for k,v in pairs(a) do assert(b[k] == v) end
273end
274
275checknext{1,x=1,y=2,z=3}
276checknext{1,2,x=1,y=2,z=3}
277checknext{1,2,3,x=1,y=2,z=3}
278checknext{1,2,3,4,x=1,y=2,z=3}
279checknext{1,2,3,4,5,x=1,y=2,z=3}
280
281assert(#{} == 0)
282assert(#{[-1] = 2} == 0)
283assert(#{1,2,3,nil,nil} == 3)
284for i=0,40 do
285 local a = {}
286 for j=1,i do a[j]=j end
287 assert(#a == i)
288end
289
290-- 'maxn' is now deprecated, but it is easily defined in Lua
291function table.maxn (t)
292 local max = 0
293 for k in pairs(t) do
294 max = (type(k) == 'number') and math.max(max, k) or max
295 end
296 return max
297end
298
299assert(table.maxn{} == 0)
300assert(table.maxn{["1000"] = true} == 0)
301assert(table.maxn{["1000"] = true, [24.5] = 3} == 24.5)
302assert(table.maxn{[1000] = true} == 1000)
303assert(table.maxn{[10] = true, [100*math.pi] = print} == 100*math.pi)
304
305table.maxn = nil
306
307-- int overflow
308a = {}
309for i=0,50 do a[2^i] = true end
310assert(a[#a])
311
312print('+')
313
314
315-- erasing values
316local t = {[{1}] = 1, [{2}] = 2, [string.rep("x ", 4)] = 3,
317 [100.3] = 4, [4] = 5}
318
319local n = 0
320for k, v in pairs( t ) do
321 n = n+1
322 assert(t[k] == v)
323 t[k] = nil
324 collectgarbage()
325 assert(t[k] == nil)
326end
327assert(n == 5)
328
329
330local function test (a)
331 assert(not pcall(table.insert, a, 2, 20));
332 table.insert(a, 10); table.insert(a, 2, 20);
333 table.insert(a, 1, -1); table.insert(a, 40);
334 table.insert(a, #a+1, 50)
335 table.insert(a, 2, -2)
336 assert(not pcall(table.insert, a, 0, 20));
337 assert(not pcall(table.insert, a, #a + 2, 20));
338 assert(table.remove(a,1) == -1)
339 assert(table.remove(a,1) == -2)
340 assert(table.remove(a,1) == 10)
341 assert(table.remove(a,1) == 20)
342 assert(table.remove(a,1) == 40)
343 assert(table.remove(a,1) == 50)
344 assert(table.remove(a,1) == nil)
345 assert(table.remove(a) == nil)
346 assert(table.remove(a, #a) == nil)
347end
348
349a = {n=0, [-7] = "ban"}
350test(a)
351assert(a.n == 0 and a[-7] == "ban")
352
353a = {[-7] = "ban"};
354test(a)
355assert(a.n == nil and #a == 0 and a[-7] == "ban")
356
357a = {[-1] = "ban"}
358test(a)
359assert(#a == 0 and table.remove(a) == nil and a[-1] == "ban")
360
361a = {[0] = "ban"}
362assert(#a == 0 and table.remove(a) == "ban" and a[0] == nil)
363
364table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1)
365assert(table.remove(a) == 10)
366assert(table.remove(a) == 20)
367assert(table.remove(a) == -1)
368assert(table.remove(a) == nil)
369
370a = {'c', 'd'}
371table.insert(a, 3, 'a')
372table.insert(a, 'b')
373assert(table.remove(a, 1) == 'c')
374assert(table.remove(a, 1) == 'd')
375assert(table.remove(a, 1) == 'a')
376assert(table.remove(a, 1) == 'b')
377assert(table.remove(a, 1) == nil)
378assert(#a == 0 and a.n == nil)
379
380a = {10,20,30,40}
381assert(table.remove(a, #a + 1) == nil)
382assert(not pcall(table.remove, a, 0))
383assert(a[#a] == 40)
384assert(table.remove(a, #a) == 40)
385assert(a[#a] == 30)
386assert(table.remove(a, 2) == 20)
387assert(a[#a] == 30 and #a == 2)
388
389do -- testing table library with metamethods
390 local function test (proxy, t)
391 for i = 1, 10 do
392 table.insert(proxy, 1, i)
393 end
394 assert(#proxy == 10 and #t == 10)
395 for i = 1, 10 do
396 assert(t[i] == 11 - i)
397 end
398 table.sort(proxy)
399 for i = 1, 10 do
400 assert(t[i] == i and proxy[i] == i)
401 end
402 assert(table.concat(proxy, ",") == "1,2,3,4,5,6,7,8,9,10")
403 for i = 1, 8 do
404 assert(table.remove(proxy, 1) == i)
405 end
406 assert(#proxy == 2 and #t == 2)
407 local a, b, c = table.unpack(proxy)
408 assert(a == 9 and b == 10 and c == nil)
409 end
410
411 -- all virtual
412 local t = {}
413 local proxy = setmetatable({}, {
414 __len = function () return #t end,
415 __index = t,
416 __newindex = t,
417 })
418 test(proxy, t)
419
420 -- only __newindex
421 local count = 0
422 t = setmetatable({}, {
423 __newindex = function (t,k,v) count = count + 1; rawset(t,k,v) end})
424 test(t, t)
425 assert(count == 10) -- after first 10, all other sets are not new
426
427 -- no __newindex
428 t = setmetatable({}, {
429 __index = function (_,k) return k + 1 end,
430 __len = function (_) return 5 end})
431 assert(table.concat(t, ";") == "2;3;4;5;6")
432
433end
434
435
436if not T then
437 (Message or print)
438 ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n')
439else --[
440 local debug = require'debug'
441 local tab = {10, 20, 30}
442 local mt = {}
443 local u = T.newuserdata(0)
444 checkerror("table expected", table.insert, u, 40)
445 checkerror("table expected", table.remove, u)
446 debug.setmetatable(u, mt)
447 checkerror("table expected", table.insert, u, 40)
448 checkerror("table expected", table.remove, u)
449 mt.__index = tab
450 checkerror("table expected", table.insert, u, 40)
451 checkerror("table expected", table.remove, u)
452 mt.__newindex = tab
453 checkerror("table expected", table.insert, u, 40)
454 checkerror("table expected", table.remove, u)
455 mt.__len = function () return #tab end
456 table.insert(u, 40)
457 assert(#u == 4 and #tab == 4 and u[4] == 40 and tab[4] == 40)
458 assert(table.remove(u) == 40)
459 table.insert(u, 1, 50)
460 assert(#u == 4 and #tab == 4 and u[4] == 30 and tab[1] == 50)
461
462 mt.__newindex = nil
463 mt.__len = nil
464 local tab2 = {}
465 local u2 = T.newuserdata(0)
466 debug.setmetatable(u2, {__newindex = function (_, k, v) tab2[k] = v end})
467 table.move(u, 1, 4, 1, u2)
468 assert(#tab2 == 4 and tab2[1] == tab[1] and tab2[4] == tab[4])
469
470end -- ]
471
472print('+')
473
474a = {}
475for i=1,1000 do
476 a[i] = i; a[i-1] = nil
477end
478assert(next(a,nil) == 1000 and next(a,1000) == nil)
479
480assert(next({}) == nil)
481assert(next({}, nil) == nil)
482
483for a,b in pairs{} do error"not here" end
484for i=1,0 do error'not here' end
485for i=0,1,-1 do error'not here' end
486a = nil; for i=1,1 do assert(not a); a=1 end; assert(a)
487a = nil; for i=1,1,-1 do assert(not a); a=1 end; assert(a)
488
489do
490 print("testing floats in numeric for")
491 local a
492 -- integer count
493 a = 0; for i=1, 1, 1 do a=a+1 end; assert(a==1)
494 a = 0; for i=10000, 1e4, -1 do a=a+1 end; assert(a==1)
495 a = 0; for i=1, 0.99999, 1 do a=a+1 end; assert(a==0)
496 a = 0; for i=9999, 1e4, -1 do a=a+1 end; assert(a==0)
497 a = 0; for i=1, 0.99999, -1 do a=a+1 end; assert(a==1)
498
499 -- float count
500 a = 0; for i=0, 0.999999999, 0.1 do a=a+1 end; assert(a==10)
501 a = 0; for i=1.0, 1, 1 do a=a+1 end; assert(a==1)
502 a = 0; for i=-1.5, -1.5, 1 do a=a+1 end; assert(a==1)
503 a = 0; for i=1e6, 1e6, -1 do a=a+1 end; assert(a==1)
504 a = 0; for i=1.0, 0.99999, 1 do a=a+1 end; assert(a==0)
505 a = 0; for i=99999, 1e5, -1.0 do a=a+1 end; assert(a==0)
506 a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1)
507end
508
509-- conversion
510a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5)
511
512do -- checking types
513 local c
514 local function checkfloat (i)
515 assert(math.type(i) == "float")
516 c = c + 1
517 end
518
519 c = 0; for i = 1.0, 10 do checkfloat(i) end
520 assert(c == 10)
521
522 c = 0; for i = -1, -10, -1.0 do checkfloat(i) end
523 assert(c == 10)
524
525 local function checkint (i)
526 assert(math.type(i) == "integer")
527 c = c + 1
528 end
529
530 local m = math.maxinteger
531 c = 0; for i = m, m - 10, -1 do checkint(i) end
532 assert(c == 11)
533
534 c = 0; for i = 1, 10.9 do checkint(i) end
535 assert(c == 10)
536
537 c = 0; for i = 10, 0.001, -1 do checkint(i) end
538 assert(c == 10)
539
540 c = 0; for i = 1, "10.8" do checkint(i) end
541 assert(c == 10)
542
543 c = 0; for i = 9, "3.4", -1 do checkint(i) end
544 assert(c == 6)
545
546 c = 0; for i = 0, " -3.4 ", -1 do checkint(i) end
547 assert(c == 4)
548
549 c = 0; for i = 100, "96.3", -2 do checkint(i) end
550 assert(c == 2)
551
552 c = 0; for i = 1, math.huge do if i > 10 then break end; checkint(i) end
553 assert(c == 10)
554
555 c = 0; for i = -1, -math.huge, -1 do
556 if i < -10 then break end; checkint(i)
557 end
558 assert(c == 10)
559
560
561 for i = math.mininteger, -10e100 do assert(false) end
562 for i = math.maxinteger, 10e100, -1 do assert(false) end
563
564end
565
566collectgarbage()
567
568
569-- testing generic 'for'
570
571local function f (n, p)
572 local t = {}; for i=1,p do t[i] = i*10 end
573 return function (_,n)
574 if n > 0 then
575 n = n-1
576 return n, table.unpack(t)
577 end
578 end, nil, n
579end
580
581local x = 0
582for n,a,b,c,d in f(5,3) do
583 x = x+1
584 assert(a == 10 and b == 20 and c == 30 and d == nil)
585end
586assert(x == 5)
587
588
589
590-- testing __pairs and __ipairs metamethod
591a = {}
592do
593 local x,y,z = pairs(a)
594 assert(type(x) == 'function' and y == a and z == nil)
595end
596
597local function foo (e,i)
598 assert(e == a)
599 if i <= 10 then return i+1, i+2 end
600end
601
602local function foo1 (e,i)
603 i = i + 1
604 assert(e == a)
605 if i <= e.n then return i,a[i] end
606end
607
608setmetatable(a, {__pairs = function (x) return foo, x, 0 end})
609
610local i = 0
611for k,v in pairs(a) do
612 i = i + 1
613 assert(k == i and v == k+1)
614end
615
616a.n = 5
617a[3] = 30
618
619-- testing ipairs with metamethods
620a = {n=10}
621setmetatable(a, { __index = function (t,k)
622 if k <= t.n then return k * 10 end
623 end})
624i = 0
625for k,v in ipairs(a) do
626 i = i + 1
627 assert(k == i and v == i * 10)
628end
629assert(i == a.n)
630
631print"OK"