lua

A copy of the Lua development repository
Log | Files | Refs | README

big.lua (2150B)


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