Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/lua/blob/master/testes/nextvar.lua # lua [Ported from github.com/lua/lua] Cast in 'rl_readline_name' Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `lua / testes / nextvar.lua` 973 lines ยท 23391 bytes Raw Blame History 1 -- $Id: testes/nextvar.lua $ 2 -- See Copyright Notice in file lua.h 3 4 global * 5 6 print('testing tables, next, and for') 7 8 local function checkerror (msg, f, ...) 9 local s, err = pcall(f, ...) 10 assert(not s and string.find(err, msg)) 11 end 12 13 14 15 ---------------------------------------------------------------- 16 local function printTable (t) 17 local a, h = T.querytab(t) 18 print("array:") 19 for i = 1, a do 20 print("", T.querytab(t, i - 1)) 21 end 22 print("hash:") 23 for i = 1, h do 24 print("", T.querytab(t, a + i - 1)) 25 end 26 end 27 ---------------------------------------------------------------- 28 local function countentries (t) 29 local e = 0 30 for _ in pairs(t) do e = e + 1 end 31 return e 32 end 33 ---------------------------------------------------------------- 34 35 36 local function check (t, na, nh) 37 if not T then return end 38 local a, h = T.querytab(t) 39 if a ~= na or h ~= nh then 40 print(na, nh, a, h) 41 assert(nil) 42 end 43 end 44 45 46 local a = {} 47 48 -- make sure table has lots of space in hash part 49 for i=1,100 do a[i.."+"] = true end 50 for i=1,100 do a[i.."+"] = undef end 51 -- fill hash part with numeric indices testing size operator 52 for i=1,100 do 53 a[i] = true 54 assert(#a == i) 55 end 56 57 58 do -- rehash moving elements from array to hash 59 local a = {} 60 for i = 1, 100 do a[i] = i end 61 check(a, 128, 0) 62 63 for i = 5, 95 do a[i] = nil end 64 check(a, 128, 0) 65 66 a[129] = 1 -- force a re-hash 67 check(a, 4, 8) -- keys larger than 4 go to the hash part 68 69 for i = 1, 4 do assert(a[i] == i) end 70 for i = 5, 95 do assert(a[i] == nil) end 71 for i = 96, 100 do assert(a[i] == i) end 72 assert(a[129] == 1) 73 end 74 75 76 do -- growing hash part keeping array part 77 local a = table.create(1000) 78 check(a, 1000, 0) 79 a.x = 10 80 check(a, 1000, 1) -- array part keeps its elements 81 end 82 83 84 do -- "growing" length of a prebuilt table 85 local N = 100 86 local a = table.create(N) 87 for i = 1, N do 88 a[#a + 1] = true 89 assert(#a == i) 90 end 91 check(a, N, 0) 92 end 93 94 95 -- testing ipairs 96 local x = 0 97 for k,v in ipairs{10,20,30;x=12} do 98 x = x + 1 99 assert(k == x and v == x * 10) 100 end 101 102 for _ in ipairs{x=12, y=24} do assert(nil) end 103 104 -- test for 'false' x ipair 105 x = false 106 local i = 0 107 for k,v in ipairs{true,false,true,false} do 108 i = i + 1 109 x = not x 110 assert(x == v) 111 end 112 assert(i == 4) 113 114 -- iterator function is always the same 115 assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{}) 116 117 118 do -- overflow (must wrap-around) 119 local f = ipairs{} 120 local k, v = f({[math.mininteger] = 10}, math.maxinteger) 121 assert(k == math.mininteger and v == 10) 122 k, v = f({[math.mininteger] = 10}, k) 123 assert(k == nil) 124 end 125 126 127 do 128 -- alternate insertions and deletions in an almost full hash. 129 -- In versions pre-5.5, that causes constant rehashings and 130 -- takes a long time to complete. 131 local a = {} 132 for i = 1, 2^11 - 1 do 133 a[i .. ""] = true 134 end 135 136 for i = 1, 1e5 do 137 local key = i .. "." 138 a[key] = true 139 a[key] = nil 140 end 141 assert(countentries(a) == 2^11 - 1) 142 end 143 144 if not T then 145 (Message or print) 146 ('\n >>> testC not active: skipping tests for table sizes <<<\n') 147 else --[ 148 -- testing table sizes 149 150 151 -- minimum power of 2 (or zero) >= n 152 local function mp2 (n) 153 local mp = 2^math.ceil(math.log(n, 2)) 154 assert((mp == 0 or mp/2 < n) and n <= mp) 155 return mp 156 end 157 158 159 -- testing C library sizes 160 do 161 local s = 0 162 for _ in pairs(math) do s = s + 1 end 163 check(math, 0, mp2(s)) 164 end 165 166 167 -- testing constructor sizes 168 local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 169 30, 31, 32, 33, 34, 254, 255, 256, 257, 500, 1001} 170 171 for _, sa in ipairs(sizes) do -- 'sa' is size of the array part 172 local arr = {"return {"} 173 for i = 1, sa do arr[1 + i] = "1," end -- build array part 174 for _, sh in ipairs(sizes) do -- 'sh' is size of the hash part 175 for j = 1, sh do -- build hash part 176 arr[1 + sa + j] = string.format('k%x=%d,', j, j) 177 end 178 arr[1 + sa + sh + 1] = "}" 179 local prog = table.concat(arr) 180 local f = assert(load(prog)) 181 collectgarbage("stop") 182 f() -- call once to ensure stack space 183 -- make sure table is not resized after being created 184 if sa == 0 or sh == 0 then 185 T.alloccount(2); -- header + array or hash part 186 else 187 T.alloccount(3); -- header + array part + hash part 188 end 189 local t = f() 190 T.alloccount(); 191 collectgarbage("restart") 192 assert(#t == sa) 193 check(t, sa, mp2(sh)) 194 end 195 end 196 197 198 -- tests with unknown number of elements 199 local a = {} 200 for i=1,sizes[#sizes] do a[i] = i end -- build auxiliary table 201 for k in ipairs(sizes) do 202 local t = {table.unpack(a,1,k)} 203 assert(#t == k) 204 check(t, k, 0) 205 t = {1,2,3,table.unpack(a,1,k)} 206 check(t, k+3, 0) 207 assert(#t == k + 3) 208 end 209 210 211 -- testing tables dynamically built 212 local lim = 130 213 local a = {}; a[2] = 1; check(a, 2, 0) 214 a = {}; a[0] = 1; check(a, 0, 1); 215 a[2] = 1; check(a, 2, 1) 216 a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1) 217 a = {} 218 for i = 1,lim do 219 a[i] = 1 220 assert(#a == i) 221 check(a, mp2(i), 0) 222 end 223 224 a = {} 225 for i = 1,lim do 226 a['a'..i] = 1 227 assert(#a == 0) 228 check(a, 0, mp2(i)) 229 end 230 231 232 -- insert and delete elements until a rehash occur. Caller must ensure 233 -- that a rehash will change the shape of the table. Must repeat because 234 -- the insertion may collide with the deleted element, and then there is 235 -- no rehash. 236 local function forcerehash (t) 237 local na, nh = T.querytab(t) 238 local i = 10000 239 repeat 240 i = i + 1 241 t[i] = true 242 t[i] = undef 243 local nna, nnh = T.querytab(t) 244 until nna ~= na or nnh ~= nh 245 end 246 247 248 do 249 local a = {} 250 for i=1,16 do a[i] = i end 251 check(a, 16, 0) 252 for i=1,11 do a[i] = undef end 253 check(a, 16, 0) 254 a[30] = true -- force a rehash 255 a[30] = undef 256 check(a, 0, 8) -- 5 elements in the hash part: [12]-[16] 257 a[10] = 1 258 forcerehash(a) 259 check(a, 16, 1) 260 for i=1,14 do a[i] = true; a[i] = undef end 261 check(a, 16, 1) -- no rehash... 262 a[31] = true; a[32] = true -- force a rehash 263 check(a, 0, 4) -- [15], [16], [31], [32] 264 end 265 266 -- reverse filling 267 do 268 local N = 2^10 269 local a = {} 270 for i = N, 1, -1 do a[i] = i end -- fill in reverse 271 check(a, mp2(N), 0) 272 end 273 274 275 do -- "almost sparse" arrays 276 -- create table with holes in 1/3 of its entries; all its 277 -- elements are always in the array part 278 local a = {} 279 for i = 1, 257 do 280 if i % 3 ~= 1 then 281 a[i] = true 282 check(a, mp2(i), 0) 283 end 284 end 285 end 286 287 288 do 289 -- alternate insertions and deletions should give some extra 290 -- space for the hash part. Otherwise, a mix of insertions/deletions 291 -- could cause too many rehashes. (See the other test for "alternate 292 -- insertions and deletions" in this file.) 293 local a = {} 294 for i = 1, 256 do 295 a[i .. ""] = true 296 end 297 check(a, 0, 256) -- hash part is full 298 a["256"] = nil -- delete a key 299 forcerehash(a) 300 -- table has only 255 elements, but it got some extra space; 301 -- otherwise, almost each delete-insert would rehash the table again. 302 assert(countentries(a) == 255) 303 check(a, 0, 512) 304 end 305 306 307 -- size tests for vararg 308 lim = 35 309 local function foo (n, ...) 310 local arg = {...} 311 check(arg, n, 0) 312 assert(select('#', ...) == n) 313 arg[n+1] = true 314 check(arg, mp2(n+1), 0) 315 arg.x = true 316 check(arg, mp2(n+1), 1) 317 end 318 local a = {} 319 for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end 320 321 end --] 322 323 324 -- test size operation on tables with nils 325 assert(#{} == 0) 326 assert(#{nil} == 0) 327 assert(#{nil, nil} == 0) 328 assert(#{nil, nil, nil} == 0) 329 assert(#{nil, nil, nil, nil} == 0) 330 assert(#{1, 2, 3, nil, nil} == 3) 331 print'+' 332 333 334 do 335 local s1, s2 = math.randomseed() 336 print(string.format( 337 "testing length for some random tables (seeds 0X%x:%x)", s1, s2)) 338 local N = 130 339 for i = 1, 1e3 do -- create that many random tables 340 local a = table.create(math.random(N)) -- initiate with random size 341 for j = 1, math.random(N) do -- add random number of random entries 342 a[math.random(N)] = true 343 end 344 assert(#a == 0 or a[#a] and not a[#a + 1]) 345 end 346 end 347 348 349 do print("testing attack on table length") 350 local t = {} 351 local lim = math.floor(math.log(math.maxinteger, 2)) - 1 352 for i = lim, 0, -1 do 353 t[2^i] = true 354 end 355 assert(t[1 << lim]) 356 -- next loop should not take forever 357 for i = 1, #t do end 358 end 359 360 local nofind = {} 361 362 363 -- next uses always the same iteration function 364 assert(next{} == next{}) 365 366 local function find (name) 367 local n,v 368 while 1 do 369 n,v = next(_G, n) 370 if not n then return nofind end 371 assert(_G[n] ~= undef) 372 if n == name then return v end 373 end 374 end 375 376 local function find1 (name) 377 for n,v in pairs(_G) do 378 if n==name then return v end 379 end 380 return nil -- not found 381 end 382 383 384 assert(print==find("print") and print == find1("print")) 385 assert(_G["print"]==find("print")) 386 assert(assert==find1("assert")) 387 assert(nofind==find("return")) 388 assert(not find1("return")) 389 _G["ret" .. "urn"] = undef 390 assert(nofind==find("return")) 391 _G["xxx"] = 1 392 assert(xxx==find("xxx")) 393 394 -- invalid key to 'next' 395 checkerror("invalid key", next, {10,20}, 3) 396 397 -- both 'pairs' and 'ipairs' need an argument 398 checkerror("bad argument", pairs) 399 checkerror("bad argument", ipairs) 400 401 print('+') 402 403 a = {} 404 for i=0,10000 do 405 if math.fmod(i,10) ~= 0 then 406 a['x'..i] = i 407 end 408 end 409 410 local n = {n=0} 411 for i,v in pairs(a) do 412 n.n = n.n+1 413 assert(i and v and a[i] == v) 414 end 415 assert(n.n == 9000) 416 a = nil 417 418 do -- clear global table 419 local a = {} 420 for n,v in pairs(_G) do a[n]=v end 421 for n,v in pairs(a) do 422 if not package.loaded[n] and type(v) ~= "function" and 423 not string.find(n, "^[%u_]") then 424 _G[n] = undef 425 end 426 collectgarbage() 427 end 428 end 429 430 431 -- 432 433 local function checknext (a) 434 local b = {} 435 do local k,v = next(a); while k do b[k] = v; k,v = next(a,k) end end 436 for k,v in pairs(b) do assert(a[k] == v) end 437 for k,v in pairs(a) do assert(b[k] == v) end 438 end 439 440 checknext{1,x=1,y=2,z=3} 441 checknext{1,2,x=1,y=2,z=3} 442 checknext{1,2,3,x=1,y=2,z=3} 443 checknext{1,2,3,4,x=1,y=2,z=3} 444 checknext{1,2,3,4,5,x=1,y=2,z=3} 445 446 assert(#{} == 0) 447 assert(#{[-1] = 2} == 0) 448 for i=0,40 do 449 local a = {} 450 for j=1,i do a[j]=j end 451 assert(#a == i) 452 end 453 454 -- 'maxn' is now deprecated, but it is easily defined in Lua 455 function table.maxn (t) 456 local max = 0 457 for k in pairs(t) do 458 max = (type(k) == 'number') and math.max(max, k) or max 459 end 460 return max 461 end 462 463 assert(table.maxn{} == 0) 464 assert(table.maxn{["1000"] = true} == 0) 465 assert(table.maxn{["1000"] = true, [24.5] = 3} == 24.5) 466 assert(table.maxn{[1000] = true} == 1000) 467 assert(table.maxn{[10] = true, [100*math.pi] = print} == 100*math.pi) 468 469 table.maxn = nil 470 471 -- int overflow 472 a = {} 473 for i=0,50 do a[2^i] = true end 474 assert(a[#a]) 475 476 print('+') 477 478 479 do -- testing 'next' with all kinds of keys 480 local a = { 481 [1] = 1, -- integer 482 [1.1] = 2, -- float 483 ['x'] = 3, -- short string 484 [string.rep('x', 1000)] = 4, -- long string 485 [print] = 5, -- C function 486 [checkerror] = 6, -- Lua function 487 [coroutine.running()] = 7, -- thread 488 [true] = 8, -- boolean 489 [io.stdin] = 9, -- userdata 490 [{}] = 10, -- table 491 } 492 local b = {}; for i = 1, 10 do b[i] = true end 493 for k, v in pairs(a) do 494 assert(b[v]); b[v] = undef 495 end 496 assert(next(b) == nil) -- 'b' now is empty 497 end 498 499 500 -- erasing values 501 local t = {[{1}] = 1, [{2}] = 2, [string.rep("x ", 4)] = 3, 502 [100.3] = 4, [4] = 5} 503 504 local n = 0 505 for k, v in pairs( t ) do 506 n = n+1 507 assert(t[k] == v) 508 t[k] = undef 509 collectgarbage() 510 assert(t[k] == undef) 511 end 512 assert(n == 5) 513 514 515 do 516 print("testing next x GC of deleted keys") 517 -- bug in 5.4.1 518 local co = coroutine.wrap(function (t) 519 for k, v in pairs(t) do 520 local k1 = next(t) -- all previous keys were deleted 521 assert(k == k1) -- current key is the first in the table 522 t[k] = nil 523 local expected = (type(k) == "table" and k[1] or 524 type(k) == "function" and k() or 525 string.sub(k, 1, 1)) 526 assert(expected == v) 527 coroutine.yield(v) 528 end 529 end) 530 local t = {} 531 t[{1}] = 1 -- add several unanchored, collectable keys 532 t[{2}] = 2 533 t[string.rep("a", 50)] = "a" -- long string 534 t[string.rep("b", 50)] = "b" 535 t[{3}] = 3 536 t[string.rep("c", 10)] = "c" -- short string 537 t[function () return 10 end] = 10 538 local count = 7 539 while co(t) do 540 collectgarbage("collect") -- collect dead keys 541 count = count - 1 542 end 543 assert(count == 0 and next(t) == nil) -- traversed the whole table 544 end 545 546 547 local function test (a) 548 assert(not pcall(table.insert, a, 2, 20)); 549 table.insert(a, 10); table.insert(a, 2, 20); 550 table.insert(a, 1, -1); table.insert(a, 40); 551 table.insert(a, #a+1, 50) 552 table.insert(a, 2, -2) 553 assert(a[2] ~= undef) 554 assert(a["2"] == undef) 555 assert(not pcall(table.insert, a, 0, 20)); 556 assert(not pcall(table.insert, a, #a + 2, 20)); 557 assert(table.remove(a,1) == -1) 558 assert(table.remove(a,1) == -2) 559 assert(table.remove(a,1) == 10) 560 assert(table.remove(a,1) == 20) 561 assert(table.remove(a,1) == 40) 562 assert(table.remove(a,1) == 50) 563 assert(table.remove(a,1) == nil) 564 assert(table.remove(a) == nil) 565 assert(table.remove(a, #a) == nil) 566 end 567 568 a = {n=0, [-7] = "ban"} 569 test(a) 570 assert(a.n == 0 and a[-7] == "ban") 571 572 a = {[-7] = "ban"}; 573 test(a) 574 assert(a.n == nil and #a == 0 and a[-7] == "ban") 575 576 a = {[-1] = "ban"} 577 test(a) 578 assert(#a == 0 and table.remove(a) == nil and a[-1] == "ban") 579 580 a = {[0] = "ban"} 581 assert(#a == 0 and table.remove(a) == "ban" and a[0] == undef) 582 583 table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1) 584 assert(table.remove(a) == 10) 585 assert(table.remove(a) == 20) 586 assert(table.remove(a) == -1) 587 assert(table.remove(a) == nil) 588 589 a = {'c', 'd'} 590 table.insert(a, 3, 'a') 591 table.insert(a, 'b') 592 assert(table.remove(a, 1) == 'c') 593 assert(table.remove(a, 1) == 'd') 594 assert(table.remove(a, 1) == 'a') 595 assert(table.remove(a, 1) == 'b') 596 assert(table.remove(a, 1) == nil) 597 assert(#a == 0 and a.n == nil) 598 599 a = {10,20,30,40} 600 assert(table.remove(a, #a + 1) == nil) 601 assert(not pcall(table.remove, a, 0)) 602 assert(a[#a] == 40) 603 assert(table.remove(a, #a) == 40) 604 assert(a[#a] == 30) 605 assert(table.remove(a, 2) == 20) 606 assert(a[#a] == 30 and #a == 2) 607 608 do -- testing table library with metamethods 609 local function test (proxy, t) 610 for i = 1, 10 do 611 table.insert(proxy, 1, i) 612 end 613 assert(#proxy == 10 and #t == 10 and proxy[1] ~= undef) 614 for i = 1, 10 do 615 assert(t[i] == 11 - i) 616 end 617 table.sort(proxy) 618 for i = 1, 10 do 619 assert(t[i] == i and proxy[i] == i) 620 end 621 assert(table.concat(proxy, ",") == "1,2,3,4,5,6,7,8,9,10") 622 for i = 1, 8 do 623 assert(table.remove(proxy, 1) == i) 624 end 625 assert(#proxy == 2 and #t == 2) 626 local a, b, c = table.unpack(proxy) 627 assert(a == 9 and b == 10 and c == nil) 628 end 629 630 -- all virtual 631 local t = {} 632 local proxy = setmetatable({}, { 633 __len = function () return #t end, 634 __index = t, 635 __newindex = t, 636 }) 637 test(proxy, t) 638 639 -- only __newindex 640 local count = 0 641 t = setmetatable({}, { 642 __newindex = function (t,k,v) count = count + 1; rawset(t,k,v) end}) 643 test(t, t) 644 assert(count == 10) -- after first 10, all other sets are not new 645 646 -- no __newindex 647 t = setmetatable({}, { 648 __index = function (_,k) return k + 1 end, 649 __len = function (_) return 5 end}) 650 assert(table.concat(t, ";") == "2;3;4;5;6") 651 652 end 653 654 655 do -- testing overflow in table.insert (must wrap-around) 656 657 local t = setmetatable({}, 658 {__len = function () return math.maxinteger end}) 659 table.insert(t, 20) 660 local k, v = next(t) 661 assert(k == math.mininteger and v == 20) 662 end 663 664 if not T then 665 (Message or print) 666 ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n') 667 else --[ 668 local debug = require'debug' 669 local tab = {10, 20, 30} 670 local mt = {} 671 local u = T.newuserdata(0) 672 checkerror("table expected", table.insert, u, 40) 673 checkerror("table expected", table.remove, u) 674 debug.setmetatable(u, mt) 675 checkerror("table expected", table.insert, u, 40) 676 checkerror("table expected", table.remove, u) 677 mt.__index = tab 678 checkerror("table expected", table.insert, u, 40) 679 checkerror("table expected", table.remove, u) 680 mt.__newindex = tab 681 checkerror("table expected", table.insert, u, 40) 682 checkerror("table expected", table.remove, u) 683 mt.__len = function () return #tab end 684 table.insert(u, 40) 685 assert(#u == 4 and #tab == 4 and u[4] == 40 and tab[4] == 40) 686 assert(table.remove(u) == 40) 687 table.insert(u, 1, 50) 688 assert(#u == 4 and #tab == 4 and u[4] == 30 and tab[1] == 50) 689 690 mt.__newindex = nil 691 mt.__len = nil 692 local tab2 = {} 693 local u2 = T.newuserdata(0) 694 debug.setmetatable(u2, {__newindex = function (_, k, v) tab2[k] = v end}) 695 table.move(u, 1, 4, 1, u2) 696 assert(#tab2 == 4 and tab2[1] == tab[1] and tab2[4] == tab[4]) 697 698 end -- ] 699 700 print('+') 701 702 a = {} 703 for i=1,1000 do 704 a[i] = i; a[i - 1] = undef 705 end 706 assert(next(a,nil) == 1000 and next(a,1000) == nil) 707 708 assert(next({}) == nil) 709 assert(next({}, nil) == nil) 710 711 for a,b in pairs{} do error"not here" end 712 for i=1,0 do error'not here' end 713 for i=0,1,-1 do error'not here' end 714 a = nil; for i=1,1 do assert(not a); a=1 end; assert(a) 715 a = nil; for i=1,1,-1 do assert(not a); a=1 end; assert(a) 716 717 do 718 print("testing floats in numeric for") 719 local a 720 -- integer count 721 a = 0; for i=1, 1, 1 do a=a+1 end; assert(a==1) 722 a = 0; for i=10000, 1e4, -1 do a=a+1 end; assert(a==1) 723 a = 0; for i=1, 0.99999, 1 do a=a+1 end; assert(a==0) 724 a = 0; for i=9999, 1e4, -1 do a=a+1 end; assert(a==0) 725 a = 0; for i=1, 0.99999, -1 do a=a+1 end; assert(a==1) 726 727 -- float count 728 a = 0; for i=0, 0.`999999999`, 0.1 do a=a+1 end; assert(a==10) 729 a = 0; for i=1.0, 1, 1 do a=a+1 end; assert(a==1) 730 a = 0; for i=-1.5, -1.5, 1 do a=a+1 end; assert(a==1) 731 a = 0; for i=1e6, 1e6, -1 do a=a+1 end; assert(a==1) 732 a = 0; for i=1.0, 0.99999, 1 do a=a+1 end; assert(a==0) 733 a = 0; for i=99999, 1e5, -1.0 do a=a+1 end; assert(a==0) 734 a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1) 735 end 736 737 do print("testing NaN in numeric for") 738 local NaN = 0/0 739 740 for i = 1, NaN, 1 do error("should not run the loop") end 741 for i = 1, NaN, -1 do error("should not run the loop") end 742 for i = 1.0, NaN do error("should not run the loop") end 743 for i = 1.0, NaN, -1 do error("should not run the loop") end 744 for i = NaN, 1 do error("should not run the loop") end 745 for i = NaN, 1, -1 do error("should not run the loop") end 746 747 -- NaN is not negative, so the loop must be ascending 748 -- (that's somewhat arbitrary) 749 for i = 2, 1, NaN do error("should not run the loop") end 750 local s = 0; for i = 1, 5, NaN do s = s + 1 end; assert(s == 1) 751 end 752 753 do -- attempt to change the control variable 754 local st, msg = load "for i = 1, 10 do i = 10 end" 755 assert(not st and string.find(msg, "assign to const variable 'i'")) 756 757 local st, msg = load "for v, k in pairs{} do v = 10 end" 758 assert(not st and string.find(msg, "assign to const variable 'v'")) 759 end 760 761 -- conversion 762 a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5) 763 764 do -- checking types 765 local c 766 local function checkfloat (i) 767 assert(math.type(i) == "float") 768 c = c + 1 769 end 770 771 c = 0; for i = 1.0, 10 do checkfloat(i) end 772 assert(c == 10) 773 774 c = 0; for i = -1, -10, -1.0 do checkfloat(i) end 775 assert(c == 10) 776 777 local function checkint (i) 778 assert(math.type(i) == "integer") 779 c = c + 1 780 end 781 782 local m = math.maxinteger 783 c = 0; for i = m, m - 10, -1 do checkint(i) end 784 assert(c == 11) 785 786 c = 0; for i = 1, 10.9 do checkint(i) end 787 assert(c == 10) 788 789 c = 0; for i = 10, 0.001, -1 do checkint(i) end 790 assert(c == 10) 791 792 c = 0; for i = 1, "10.8" do checkint(i) end 793 assert(c == 10) 794 795 c = 0; for i = 9, "3.4", -1 do checkint(i) end 796 assert(c == 6) 797 798 c = 0; for i = 0, " -3.4 ", -1 do checkint(i) end 799 assert(c == 4) 800 801 c = 0; for i = 100, "96.3", -2 do checkint(i) end 802 assert(c == 2) 803 804 c = 0; for i = 1, math.huge do if i > 10 then break end; checkint(i) end 805 assert(c == 10) 806 807 c = 0; for i = -1, -math.huge, -1 do 808 if i < -10 then break end; checkint(i) 809 end 810 assert(c == 10) 811 812 813 for i = math.mininteger, -10e100 do assert(false) end 814 for i = math.maxinteger, 10e100, -1 do assert(false) end 815 816 end 817 818 819 do -- testing other strange cases for numeric 'for' 820 821 local function checkfor (from, to, step, t) 822 local c = 0 823 for i = from, to, step do 824 c = c + 1 825 assert(i == t[c]) 826 end 827 assert(c == #t) 828 end 829 830 local maxi = math.maxinteger 831 local mini = math.mininteger 832 833 checkfor(mini, maxi, maxi, {mini, -1, maxi - 1}) 834 835 checkfor(mini, math.huge, maxi, {mini, -1, maxi - 1}) 836 837 checkfor(maxi, mini, mini, {maxi, -1}) 838 839 checkfor(maxi, mini, -maxi, {maxi, 0, -maxi}) 840 841 checkfor(maxi, -math.huge, mini, {maxi, -1}) 842 843 checkfor(maxi, mini, 1, {}) 844 checkfor(mini, maxi, -1, {}) 845 846 checkfor(maxi - 6, maxi, 3, {maxi - 6, maxi - 3, maxi}) 847 checkfor(mini + 4, mini, -2, {mini + 4, mini + 2, mini}) 848 849 local step = maxi // 10 850 local c = mini 851 for i = mini, maxi, step do 852 assert(i == c) 853 c = c + step 854 end 855 856 c = maxi 857 for i = maxi, mini, -step do 858 assert(i == c) 859 c = c - step 860 end 861 862 checkfor(maxi, maxi, maxi, {maxi}) 863 checkfor(maxi, maxi, mini, {maxi}) 864 checkfor(mini, mini, maxi, {mini}) 865 checkfor(mini, mini, mini, {mini}) 866 end 867 868 869 checkerror("'for' step is zero", function () 870 for i = 1, 10, 0 do end 871 end) 872 873 checkerror("'for' step is zero", function () 874 for i = 1, -10, 0 do end 875 end) 876 877 checkerror("'for' step is zero", function () 878 for i = 1.0, -10, 0.0 do end 879 end) 880 881 collectgarbage() 882 883 884 -- testing generic 'for' 885 886 local function f (n, p) 887 local t = {}; for i=1,p do t[i] = i*10 end 888 return function (_, n, ...) 889 assert(select("#", ...) == 0) -- no extra arguments 890 if n > 0 then 891 n = n-1 892 return n, table.unpack(t) 893 end 894 end, nil, n 895 end 896 897 local x = 0 898 for n,a,b,c,d in f(5,3) do 899 x = x+1 900 assert(a == 10 and b == 20 and c == 30 and d == nil) 901 end 902 assert(x == 5) 903 904 905 906 -- testing __pairs and __ipairs metamethod 907 a = {} 908 do 909 local x,y,z = pairs(a) 910 assert(type(x) == 'function' and y == a and z == nil) 911 end 912 913 local function foo (e,i) 914 assert(e == a) 915 if i <= 10 then return i+1, i+2 end 916 end 917 918 local function foo1 (e,i) 919 i = i + 1 920 assert(e == a) 921 if i <= e.n then return i,a[i] end 922 end 923 924 local closed = false 925 setmetatable(a, {__pairs = function (x) 926 local tbc = setmetatable({}, {__close = function () closed = true end}) 927 return foo, x, 0, tbc 928 end}) 929 930 local i = 0 931 for k,v in pairs(a) do 932 i = i + 1 933 assert(k == i and v == k+1) 934 end 935 assert(closed) -- 'tbc' has been closed 936 937 a.n = 5 938 a[3] = 30 939 940 -- testing ipairs with metamethods 941 a = {n=10} 942 setmetatable(a, { __index = function (t,k) 943 if k <= t.n then return k * 10 end 944 end}) 945 i = 0 946 for k,v in ipairs(a) do 947 i = i + 1 948 assert(k == i and v == i * 10) 949 end 950 assert(i == a.n) 951 952 953 -- testing yield inside __pairs 954 do 955 local t = setmetatable({10, 20, 30}, {__pairs = function (t) 956 local inc = coroutine.yield() 957 return function (t, i) 958 if i > 1 then return i - inc, t[i - inc] else return nil end 959 end, t, #t + 1 960 end}) 961 962 local res = {} 963 local co = coroutine.wrap(function () 964 for i,p in pairs(t) do res[#res + 1] = p end 965 end) 966 967 co() -- start coroutine 968 co(1) -- continue after yield 969 assert(res[1] == 30 and res[2] == 20 and res[3] == 10 and #res == 3) 970 971 end 972 973 print"OK"