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