diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-07-09 12:33:01 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-07-09 12:33:01 -0300 |
| commit | 7c519dfbd0c68b952f0849e01deaa3750e1f8153 (patch) | |
| tree | dde3ddbba310877db725df37a0d9f2cbe4e2a8f9 /testes/big.lua | |
| parent | f59e6a93c0ad38a27a420e51abf8f13d962446b5 (diff) | |
| download | lua-7c519dfbd0c68b952f0849e01deaa3750e1f8153.tar.gz lua-7c519dfbd0c68b952f0849e01deaa3750e1f8153.tar.bz2 lua-7c519dfbd0c68b952f0849e01deaa3750e1f8153.zip | |
Added manual and tests for version 5.4-w2
Diffstat (limited to 'testes/big.lua')
| -rw-r--r-- | testes/big.lua | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/testes/big.lua b/testes/big.lua new file mode 100644 index 00000000..ebee1ec0 --- /dev/null +++ b/testes/big.lua | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | -- $Id: big.lua,v 1.35 2018/03/09 14:23:48 roberto Exp $ | ||
| 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 | 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 concatentate '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' | ||
