big.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. if _soft then
  2. return 'a'
  3. end
  4. print "testing large tables"
  5. local debug = require"debug"
  6. local lim = 2^18 + 1000
  7. local prog = { "local y = {0" }
  8. for i = 1, lim do prog[#prog + 1] = i end
  9. prog[#prog + 1] = "}\n"
  10. prog[#prog + 1] = "X = y\n"
  11. prog[#prog + 1] = ("assert(X[%d] == %d)"):format(lim - 1, lim - 2)
  12. prog[#prog + 1] = "return 0"
  13. prog = table.concat(prog, ";")
  14. local env = {string = string, assert = assert}
  15. local f = assert(load(prog, nil, nil, env))
  16. f()
  17. assert(env.X[lim] == lim - 1 and env.X[lim + 1] == lim)
  18. for k in pairs(env) do env[k] = nil end
  19. -- yields during accesses larger than K (in RK)
  20. setmetatable(env, {
  21. __index = function (t, n) coroutine.yield('g'); return _G[n] end,
  22. __newindex = function (t, n, v) coroutine.yield('s'); _G[n] = v end,
  23. })
  24. X = nil
  25. co = coroutine.wrap(f)
  26. assert(co() == 's')
  27. assert(co() == 'g')
  28. assert(co() == 'g')
  29. assert(co() == 0)
  30. assert(X[lim] == lim - 1 and X[lim + 1] == lim)
  31. -- errors in accesses larger than K (in RK)
  32. getmetatable(env).__index = function () end
  33. getmetatable(env).__newindex = function () end
  34. local e, m = pcall(f)
  35. assert(not e and m:find("global 'X'"))
  36. -- errors in metamethods
  37. getmetatable(env).__newindex = function () error("hi") end
  38. local e, m = xpcall(f, debug.traceback)
  39. assert(not e and m:find("'__newindex'"))
  40. f, X = nil
  41. coroutine.yield'b'
  42. if not _no32 then -- {
  43. print "testing string length overflow"
  44. local repstrings = 192 -- number of strings to be concatenated
  45. local ssize = math.ceil(2^32 / repstrings) + 1 -- size of each string
  46. assert(repstrings * ssize > 2^32) -- this should be larger than maximum size_t
  47. local longs = string.rep("\0", ssize) -- create one long string
  48. -- create function to concatentate 'repstrings' copies of its argument
  49. local rep = assert(load(
  50. "local a = ...; return " .. string.rep("a", repstrings, "..")))
  51. local a, b = pcall(rep, longs) -- call that function
  52. -- it should fail without creating string (result would be too large)
  53. assert(not a and string.find(b, "overflow"))
  54. end -- }
  55. print'OK'
  56. return 'a'