big.lua 2.1 KB

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