diff options
Diffstat (limited to 'testes')
-rw-r--r-- | testes/locals.lua | 56 |
1 files changed, 40 insertions, 16 deletions
diff --git a/testes/locals.lua b/testes/locals.lua index 8766b3d5..90a8b845 100644 --- a/testes/locals.lua +++ b/testes/locals.lua | |||
@@ -242,6 +242,7 @@ do | |||
242 | assert(a == 1 and b == 2 and c == 3 and X == 20 and Y == 10 and d == nil) | 242 | assert(a == 1 and b == 2 and c == 3 and X == 20 and Y == 10 and d == nil) |
243 | end | 243 | end |
244 | 244 | ||
245 | |||
245 | do -- errors in __close | 246 | do -- errors in __close |
246 | local log = {} | 247 | local log = {} |
247 | local function foo (err) | 248 | local function foo (err) |
@@ -264,7 +265,9 @@ do -- errors in __close | |||
264 | and #log == 4) | 265 | and #log == 4) |
265 | end | 266 | end |
266 | 267 | ||
268 | |||
267 | if rawget(_G, "T") then | 269 | if rawget(_G, "T") then |
270 | |||
268 | -- memory error inside closing function | 271 | -- memory error inside closing function |
269 | local function foo () | 272 | local function foo () |
270 | local *toclose y = function () T.alloccount() end | 273 | local *toclose y = function () T.alloccount() end |
@@ -296,7 +299,7 @@ if rawget(_G, "T") then | |||
296 | local function test () | 299 | local function test () |
297 | local *toclose x = enter(0) -- set a memory limit | 300 | local *toclose x = enter(0) -- set a memory limit |
298 | -- creation of previous upvalue will raise a memory error | 301 | -- creation of previous upvalue will raise a memory error |
299 | os.exit(false) -- should not run | 302 | assert(false) -- should not run |
300 | end | 303 | end |
301 | 304 | ||
302 | local _, msg = pcall(test) | 305 | local _, msg = pcall(test) |
@@ -326,33 +329,54 @@ if rawget(_G, "T") then | |||
326 | 329 | ||
327 | 330 | ||
328 | do -- testing 'toclose' in C string buffer | 331 | do -- testing 'toclose' in C string buffer |
329 | local s = string.rep("a", 10000) | 332 | collectgarbage() |
333 | local s = string.rep('a', 10000) -- large string | ||
334 | local m = T.totalmem() | ||
335 | collectgarbage("stop") | ||
336 | s = string.upper(s) -- allocate buffer + new string (10K each) | ||
337 | -- ensure buffer was deallocated | ||
338 | assert(T.totalmem() - m <= 11000) | ||
339 | collectgarbage("restart") | ||
340 | end | ||
341 | |||
342 | do -- now some tests for freeing buffer in case of errors | ||
343 | local lim = 10000 -- some size larger than the static buffer | ||
344 | local extra = 2000 -- some extra memory (for callinfo, etc.) | ||
345 | |||
346 | local s = string.rep("a", lim) | ||
347 | |||
348 | -- concat this table needs two buffer resizes (one for each 's') | ||
330 | local a = {s, s} | 349 | local a = {s, s} |
331 | 350 | ||
332 | -- ensure proper initialization (stack space, metatable) | 351 | collectgarbage() |
333 | table.concat(a) | ||
334 | collectgarbage(); collectgarbage() | ||
335 | 352 | ||
336 | local m = T.totalmem() | 353 | m = T.totalmem() |
354 | collectgarbage("stop") | ||
355 | |||
356 | -- error in the first buffer allocation | ||
357 | T. totalmem(m + extra) | ||
358 | assert(not pcall(table.concat, a)) | ||
359 | -- first buffer was not even allocated | ||
360 | assert(T.totalmem() - m <= extra) | ||
337 | 361 | ||
338 | -- error in the second buffer allocation | 362 | -- error in the second buffer allocation |
339 | T.alloccount(3) | 363 | T. totalmem(m + lim + extra) |
340 | assert(not pcall(table.concat, a)) | 364 | assert(not pcall(table.concat, a)) |
341 | T.alloccount() | ||
342 | -- first buffer was released by 'toclose' | 365 | -- first buffer was released by 'toclose' |
343 | assert(T.totalmem() - m <= 5000) | 366 | assert(T.totalmem() - m <= extra) |
344 | 367 | ||
345 | -- error in creation of final string | 368 | -- error in creation of final string |
346 | T.alloccount(4) | 369 | T.totalmem(m + 2 * lim + extra) |
347 | assert(not pcall(table.concat, a)) | 370 | assert(not pcall(table.concat, a)) |
348 | T.alloccount() | ||
349 | -- second buffer was released by 'toclose' | 371 | -- second buffer was released by 'toclose' |
350 | assert(T.totalmem() - m <= 5000) | 372 | assert(T.totalmem() - m <= extra) |
351 | 373 | ||
352 | -- userdata, upvalue, buffer, buffer, string | 374 | -- userdata, upvalue, buffer, buffer, final string |
353 | T.alloccount(5) | 375 | T.totalmem(m + 4*lim + extra) |
354 | assert(#table.concat(a) == 20000) | 376 | assert(#table.concat(a) == 2*lim) |
355 | T.alloccount() | 377 | |
378 | T.totalmem(0) -- remove memory limit | ||
379 | collectgarbage("restart") | ||
356 | 380 | ||
357 | print'+' | 381 | print'+' |
358 | end | 382 | end |