lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- $Id: testes/memerr.lua $ |
| 2 | -- See Copyright Notice in file lua.h |
| 3 | |
| 4 | |
| 5 | local function checkerr (msg, f, ...) |
| 6 | local stat, err = pcall(f, ...) |
| 7 | assert(not stat and string.find(err, msg)) |
| 8 | end |
| 9 | |
| 10 | if T==nil then |
| 11 | (Message or print) |
| 12 | ('\n >>> testC not active: skipping memory error tests <<<\n') |
| 13 | return |
| 14 | end |
| 15 | |
| 16 | print("testing memory-allocation errors") |
| 17 | |
| 18 | local debug = require "debug" |
| 19 | |
| 20 | local pack = table.pack |
| 21 | |
| 22 | -- standard error message for memory errors |
| 23 | local MEMERRMSG = "not enough memory" |
| 24 | |
| 25 | |
| 26 | -- memory error in panic function |
| 27 | T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k) |
| 28 | assert(T.checkpanic("newuserdata 20000") == MEMERRMSG) |
| 29 | T.totalmem(0) -- restore high limit |
| 30 | |
| 31 | |
| 32 | |
| 33 | -- {================================================================== |
| 34 | -- Testing memory limits |
| 35 | -- =================================================================== |
| 36 | |
| 37 | checkerr("block too big", T.newuserdata, math.maxinteger) |
| 38 | collectgarbage() |
| 39 | local f = load"local a={}; for i=1,100000 do a[i]=i end" |
| 40 | T.alloccount(10) |
| 41 | checkerr(MEMERRMSG, f) |
| 42 | T.alloccount() -- remove limit |
| 43 | |
| 44 | |
| 45 | -- preallocate stack space |
| 46 | local function deep (n) if n > 0 then deep(n - 1) end end |
| 47 | |
| 48 | |
| 49 | -- test memory errors; increase limit for maximum memory by steps, |
| 50 | -- so that we get memory errors in all allocations of a given |
| 51 | -- task, until there is enough memory to complete the task without |
| 52 | -- errors. |
| 53 | local function testbytes (s, f) |
| 54 | collectgarbage() |
| 55 | local M = T.totalmem() |
| 56 | local oldM = M |
| 57 | local a,b = nil |
| 58 | while true do |
| 59 | collectgarbage(); collectgarbage() |
| 60 | deep(4) |
| 61 | T.totalmem(M) |
| 62 | a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) |
| 63 | T.totalmem(0) -- remove limit |
| 64 | if a and b == "OK" then break end -- stop when no more errors |
| 65 | if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? |
| 66 | error(a, 0) -- propagate it |
| 67 | end |
| 68 | M = M + 7 -- increase memory limit |
| 69 | end |
| 70 | print(string.format("minimum memory for %s: %d bytes", s, M - oldM)) |
| 71 | return a |
| 72 | end |
| 73 | |
| 74 | -- test memory errors; increase limit for number of allocations one |
| 75 | -- by one, so that we get memory errors in all allocations of a given |
| 76 | -- task, until there is enough allocations to complete the task without |
| 77 | -- errors. |
| 78 | |
| 79 | local function testalloc (s, f) |
| 80 | collectgarbage() |
| 81 | local M = 0 |
| 82 | local a,b = nil |
| 83 | while true do |
| 84 | collectgarbage(); collectgarbage() |
| 85 | deep(4) |
| 86 | T.alloccount(M) |
| 87 | a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) |
| 88 | T.alloccount() -- remove limit |
| 89 | if a and b == "OK" then break end -- stop when no more errors |
| 90 | if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? |
| 91 | error(a, 0) -- propagate it |
| 92 | end |
| 93 | M = M + 1 -- increase allocation limit |
| 94 | end |
| 95 | print(string.format("minimum allocations for %s: %d allocations", s, M)) |
| 96 | return M |
| 97 | end |
| 98 | |
| 99 | |
| 100 | local function testamem (s, f) |
| 101 | local aloc = testalloc(s, f) |
| 102 | local res = testbytes(s, f) |
| 103 | return {aloc = aloc, res = res} |
| 104 | end |
| 105 | |
| 106 | |
| 107 | local b = testamem("function call", function () return 10 end) |
| 108 | assert(b.res == 10 and b.aloc == 0) |
| 109 | |
| 110 | testamem("state creation", function () |
| 111 | local st = T.newstate() |
| 112 | if st then T.closestate(st) end -- close new state |
| 113 | return st |
| 114 | end) |
| 115 | |
| 116 | testamem("empty-table creation", function () |
| 117 | return {} |
| 118 | end) |
| 119 | |
| 120 | testamem("string creation", function () |
| 121 | return "XXX" .. "YYY" |
| 122 | end) |
| 123 | |
| 124 | testamem("coroutine creation", function() |
| 125 | return coroutine.create(print) |
| 126 | end) |
| 127 | |
| 128 | do -- vararg tables |
| 129 | local function pack (...t) return t end |
| 130 | local b = testamem("vararg table", function () |
| 131 | return pack(10, 20, 30, 40, "hello") |
| 132 | end) |
| 133 | assert(b.aloc == 3) -- new table uses three memory blocks |
| 134 | -- table optimized away |
| 135 | local function sel (n, ...arg) return arg[n] + arg.n end |
| 136 | local b = testamem("optimized vararg table", |
| 137 | function () return sel(2.0, 20, 30) end) |
| 138 | assert(b.res == 32 and b.aloc == 0) -- no memory needed for this case |
| 139 | end |
| 140 | |
| 141 | -- testing to-be-closed variables |
| 142 | testamem("to-be-closed variables", function() |
| 143 | local flag |
| 144 | do |
| 145 | local x <close> = |
| 146 | setmetatable({}, {__close = function () flag = true end}) |
| 147 | flag = false |
| 148 | local x = {} |
| 149 | end |
| 150 | return flag |
| 151 | end) |
| 152 | |
| 153 | |
| 154 | -- testing threads |
| 155 | |
| 156 | -- get main thread from registry |
| 157 | local mt = T.testC("rawgeti R !M; return 1") |
| 158 | assert(type(mt) == "thread" and coroutine.running() == mt) |
| 159 | |
| 160 | |
| 161 | |
| 162 | local function expand (n,s) |
| 163 | if n==0 then return "" end |
| 164 | local e = string.rep("=", n) |
| 165 | return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", |
| 166 | e, s, expand(n-1,s), e) |
| 167 | end |
| 168 | |
| 169 | G=0; collectgarbage() |
| 170 | load(expand(20,"G=G+1"))() |
| 171 | assert(G==20); collectgarbage() |
| 172 | G = nil |
| 173 | |
| 174 | testamem("running code on new thread", function () |
| 175 | return T.doonnewstack("local x=1") == 0 -- try to create thread |
| 176 | end) |
| 177 | |
| 178 | |
| 179 | do -- external strings |
| 180 | local str = string.rep("a", 100) |
| 181 | testamem("creating external strings", function () |
| 182 | return T.externstr(str) |
| 183 | end) |
| 184 | end |
| 185 | |
| 186 | |
| 187 | -- testing memory x compiler |
| 188 | |
| 189 | testamem("loadstring", function () |
| 190 | return load("x=1") -- try to do load a string |
| 191 | end) |
| 192 | |
| 193 | |
| 194 | local testprog = [[ |
| 195 | local function foo () return end |
| 196 | local t = {"x"} |
| 197 | AA = "aaa" |
| 198 | for i = 1, #t do AA = AA .. t[i] end |
| 199 | return true |
| 200 | ]] |
| 201 | |
| 202 | -- testing memory x dofile |
| 203 | _G.AA = nil |
| 204 | local t =os.tmpname() |
| 205 | local f = assert(io.open(t, "w")) |
| 206 | f:write(testprog) |
| 207 | f:close() |
| 208 | testamem("dofile", function () |
| 209 | local a = loadfile(t) |
| 210 | return a and a() |
| 211 | end) |
| 212 | assert(os.remove(t)) |
| 213 | assert(_G.AA == "aaax") |
| 214 | |
| 215 | |
| 216 | -- other generic tests |
| 217 | |
| 218 | testamem("gsub", function () |
| 219 | local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end) |
| 220 | return (a == 'ablo ablo') |
| 221 | end) |
| 222 | |
| 223 | testamem("dump/undump", function () |
| 224 | local a = load(testprog) |
| 225 | local b = a and string.dump(a) |
| 226 | a = b and load(b) |
| 227 | return a and a() |
| 228 | end) |
| 229 | |
| 230 | _G.AA = nil |
| 231 | |
| 232 | local t = os.tmpname() |
| 233 | testamem("file creation", function () |
| 234 | local f = assert(io.open(t, 'w')) |
| 235 | assert (not io.open"nomenaoexistente") |
| 236 | io.close(f); |
| 237 | return not loadfile'nomenaoexistente' |
| 238 | end) |
| 239 | assert(os.remove(t)) |
| 240 | |
| 241 | testamem("table creation", function () |
| 242 | local a, lim = {}, 10 |
| 243 | for i=1,lim do a[i] = i; a[i..'a'] = {} end |
| 244 | return (type(a[lim..'a']) == 'table' and a[lim] == lim) |
| 245 | end) |
| 246 | |
| 247 | testamem("constructors", function () |
| 248 | local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5} |
| 249 | return (type(a) == 'table' and a.e == 5) |
| 250 | end) |
| 251 | |
| 252 | local a = 1 |
| 253 | local close = nil |
| 254 | testamem("closure creation", function () |
| 255 | function close (b) |
| 256 | return function (x) return b + x end |
| 257 | end |
| 258 | return (close(2)(4) == 6) |
| 259 | end) |
| 260 | |
| 261 | testamem("using coroutines", function () |
| 262 | local a = coroutine.wrap(function () |
| 263 | coroutine.yield(string.rep("a", 10)) |
| 264 | return {} |
| 265 | end) |
| 266 | assert(string.len(a()) == 10) |
| 267 | return a() |
| 268 | end) |
| 269 | |
| 270 | do -- auxiliary buffer |
| 271 | local lim = 100 |
| 272 | local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end |
| 273 | testamem("auxiliary buffer", function () |
| 274 | return (#table.concat(a, ",") == 20*lim + lim - 1) |
| 275 | end) |
| 276 | end |
| 277 | |
| 278 | testamem("growing stack", function () |
| 279 | local function foo (n) |
| 280 | if n == 0 then return 1 else return 1 + foo(n - 1) end |
| 281 | end |
| 282 | return foo(100) |
| 283 | end) |
| 284 | |
| 285 | |
| 286 | collectgarbage() |
| 287 | collectgarbage() |
| 288 | global io, T, setmetatable, collectgarbage, print |
| 289 | |
| 290 | local Count = 0 |
| 291 | testamem("finalizers", function () |
| 292 | local X = false |
| 293 | local obj = setmetatable({}, {__gc = function () X = true end}) |
| 294 | obj = nil |
| 295 | T.resetCI() -- remove extra CallInfos |
| 296 | T.reallocstack(18) -- remove extra stack slots |
| 297 | Count = Count + 1 |
| 298 | io.stderr:write(Count, "\n") |
| 299 | T.trick(io) |
| 300 | collectgarbage() |
| 301 | return X |
| 302 | end) |
| 303 | |
| 304 | -- }================================================================== |
| 305 | |
| 306 | |
| 307 | print "Ok" |
| 308 | |
| 309 |