github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/testdata/lua-5.3.3-tests/big.lua (about) 1 -- $Id: big.lua,v 1.31 2014/12/26 17:20:53 roberto Exp $ 2 3 if _soft then 4 return 'a' 5 end 6 7 print "testing large tables" 8 9 local debug = require"debug" 10 11 local lim = 2^18 + 1000 12 local prog = { "local y = {0" } 13 for i = 1, lim do prog[#prog + 1] = i end 14 prog[#prog + 1] = "}\n" 15 prog[#prog + 1] = "X = y\n" 16 prog[#prog + 1] = ("assert(X[%d] == %d)"):format(lim - 1, lim - 2) 17 prog[#prog + 1] = "return 0" 18 prog = table.concat(prog, ";") 19 20 local env = {string = string, assert = assert} 21 local f = assert(load(prog, nil, nil, env)) 22 23 f() 24 assert(env.X[lim] == lim - 1 and env.X[lim + 1] == lim) 25 for k in pairs(env) do env[k] = nil end 26 27 -- yields during accesses larger than K (in RK) 28 setmetatable(env, { 29 __index = function (t, n) coroutine.yield('g'); return _G[n] end, 30 __newindex = function (t, n, v) coroutine.yield('s'); _G[n] = v end, 31 }) 32 33 X = nil 34 co = coroutine.wrap(f) 35 assert(co() == 's') 36 assert(co() == 'g') 37 assert(co() == 'g') 38 assert(co() == 0) 39 40 assert(X[lim] == lim - 1 and X[lim + 1] == lim) 41 42 -- errors in accesses larger than K (in RK) 43 getmetatable(env).__index = function () end 44 getmetatable(env).__newindex = function () end 45 local e, m = pcall(f) 46 assert(not e and m:find("global 'X'")) 47 48 -- errors in metamethods 49 getmetatable(env).__newindex = function () error("hi") end 50 local e, m = xpcall(f, debug.traceback) 51 assert(not e and m:find("'__newindex'")) 52 53 f, X = nil 54 55 coroutine.yield'b' 56 57 if 2^32 == 0 then -- (small integers) { 58 59 print "testing string length overflow" 60 61 local repstrings = 192 -- number of strings to be concatenated 62 local ssize = math.ceil(2.0^32 / repstrings) + 1 -- size of each string 63 64 assert(repstrings * ssize > 2.0^32) -- it should be larger than maximum size 65 66 local longs = string.rep("\0", ssize) -- create one long string 67 68 -- create function to concatentate 'repstrings' copies of its argument 69 local rep = assert(load( 70 "local a = ...; return " .. string.rep("a", repstrings, ".."))) 71 72 local a, b = pcall(rep, longs) -- call that function 73 74 -- it should fail without creating string (result would be too large) 75 assert(not a and string.find(b, "overflow")) 76 77 end -- } 78 79 print'OK' 80 81 return 'a'