diff options
-rw-r--r-- | testes/all.lua | 1 | ||||
-rw-r--r-- | testes/api.lua | 243 | ||||
-rw-r--r-- | testes/memerr.lua | 266 | ||||
-rwxr-xr-x | testes/packtests | 1 |
4 files changed, 268 insertions, 243 deletions
diff --git a/testes/all.lua b/testes/all.lua index 4ffa9efe..c3fdac95 100644 --- a/testes/all.lua +++ b/testes/all.lua | |||
@@ -182,6 +182,7 @@ dofile('nextvar.lua') | |||
182 | dofile('pm.lua') | 182 | dofile('pm.lua') |
183 | dofile('utf8.lua') | 183 | dofile('utf8.lua') |
184 | dofile('api.lua') | 184 | dofile('api.lua') |
185 | dofile('memerr.lua') | ||
185 | assert(dofile('events.lua') == 12) | 186 | assert(dofile('events.lua') == 12) |
186 | dofile('vararg.lua') | 187 | dofile('vararg.lua') |
187 | dofile('closure.lua') | 188 | dofile('closure.lua') |
diff --git a/testes/api.lua b/testes/api.lua index 21f703fd..ee2de98b 100644 --- a/testes/api.lua +++ b/testes/api.lua | |||
@@ -11,9 +11,6 @@ local debug = require "debug" | |||
11 | local pack = table.pack | 11 | local pack = table.pack |
12 | 12 | ||
13 | 13 | ||
14 | -- standard error message for memory errors | ||
15 | local MEMERRMSG = "not enough memory" | ||
16 | |||
17 | local function tcheck (t1, t2) | 14 | local function tcheck (t1, t2) |
18 | assert(t1.n == (t2.n or #t2) + 1) | 15 | assert(t1.n == (t2.n or #t2) + 1) |
19 | for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end | 16 | for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end |
@@ -432,11 +429,6 @@ do | |||
432 | "bad argument #4 (string expected, got no value)") | 429 | "bad argument #4 (string expected, got no value)") |
433 | 430 | ||
434 | 431 | ||
435 | -- memory error | ||
436 | T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k) | ||
437 | assert(T.checkpanic("newuserdata 20000") == MEMERRMSG) | ||
438 | T.totalmem(0) -- restore high limit | ||
439 | |||
440 | -- memory error + thread status | 432 | -- memory error + thread status |
441 | local x = T.checkpanic( | 433 | local x = T.checkpanic( |
442 | [[ alloccount 0 # force a memory error in next line | 434 | [[ alloccount 0 # force a memory error in next line |
@@ -1306,241 +1298,6 @@ do | |||
1306 | end | 1298 | end |
1307 | 1299 | ||
1308 | 1300 | ||
1309 | --[[ | ||
1310 | ** {================================================================== | ||
1311 | ** Testing memory limits | ||
1312 | ** =================================================================== | ||
1313 | --]] | ||
1314 | |||
1315 | print("memory-allocation errors") | ||
1316 | |||
1317 | checkerr("block too big", T.newuserdata, math.maxinteger) | ||
1318 | collectgarbage() | ||
1319 | local f = load"local a={}; for i=1,100000 do a[i]=i end" | ||
1320 | T.alloccount(10) | ||
1321 | checkerr(MEMERRMSG, f) | ||
1322 | T.alloccount() -- remove limit | ||
1323 | |||
1324 | |||
1325 | -- test memory errors; increase limit for maximum memory by steps, | ||
1326 | -- o that we get memory errors in all allocations of a given | ||
1327 | -- task, until there is enough memory to complete the task without | ||
1328 | -- errors. | ||
1329 | local function testbytes (s, f) | ||
1330 | collectgarbage() | ||
1331 | local M = T.totalmem() | ||
1332 | local oldM = M | ||
1333 | local a,b = nil | ||
1334 | while true do | ||
1335 | collectgarbage(); collectgarbage() | ||
1336 | T.totalmem(M) | ||
1337 | a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) | ||
1338 | T.totalmem(0) -- remove limit | ||
1339 | if a and b == "OK" then break end -- stop when no more errors | ||
1340 | if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? | ||
1341 | error(a, 0) -- propagate it | ||
1342 | end | ||
1343 | M = M + 7 -- increase memory limit | ||
1344 | end | ||
1345 | print(string.format("minimum memory for %s: %d bytes", s, M - oldM)) | ||
1346 | return a | ||
1347 | end | ||
1348 | |||
1349 | -- test memory errors; increase limit for number of allocations one | ||
1350 | -- by one, so that we get memory errors in all allocations of a given | ||
1351 | -- task, until there is enough allocations to complete the task without | ||
1352 | -- errors. | ||
1353 | |||
1354 | local function testalloc (s, f) | ||
1355 | collectgarbage() | ||
1356 | local M = 0 | ||
1357 | local a,b = nil | ||
1358 | while true do | ||
1359 | collectgarbage(); collectgarbage() | ||
1360 | T.alloccount(M) | ||
1361 | a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) | ||
1362 | T.alloccount() -- remove limit | ||
1363 | if a and b == "OK" then break end -- stop when no more errors | ||
1364 | if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? | ||
1365 | error(a, 0) -- propagate it | ||
1366 | end | ||
1367 | M = M + 1 -- increase allocation limit | ||
1368 | end | ||
1369 | print(string.format("minimum allocations for %s: %d allocations", s, M)) | ||
1370 | return a | ||
1371 | end | ||
1372 | |||
1373 | |||
1374 | local function testamem (s, f) | ||
1375 | testalloc(s, f) | ||
1376 | return testbytes(s, f) | ||
1377 | end | ||
1378 | |||
1379 | |||
1380 | -- doing nothing | ||
1381 | b = testamem("doing nothing", function () return 10 end) | ||
1382 | assert(b == 10) | ||
1383 | |||
1384 | -- testing memory errors when creating a new state | ||
1385 | |||
1386 | testamem("state creation", function () | ||
1387 | local st = T.newstate() | ||
1388 | if st then T.closestate(st) end -- close new state | ||
1389 | return st | ||
1390 | end) | ||
1391 | |||
1392 | testamem("empty-table creation", function () | ||
1393 | return {} | ||
1394 | end) | ||
1395 | |||
1396 | testamem("string creation", function () | ||
1397 | return "XXX" .. "YYY" | ||
1398 | end) | ||
1399 | |||
1400 | testamem("coroutine creation", function() | ||
1401 | return coroutine.create(print) | ||
1402 | end) | ||
1403 | |||
1404 | |||
1405 | -- testing to-be-closed variables | ||
1406 | testamem("to-be-closed variables", function() | ||
1407 | local flag | ||
1408 | do | ||
1409 | local x <close> = | ||
1410 | setmetatable({}, {__close = function () flag = true end}) | ||
1411 | flag = false | ||
1412 | local x = {} | ||
1413 | end | ||
1414 | return flag | ||
1415 | end) | ||
1416 | |||
1417 | |||
1418 | -- testing threads | ||
1419 | |||
1420 | -- get main thread from registry | ||
1421 | local mt = T.testC("rawgeti R !M; return 1") | ||
1422 | assert(type(mt) == "thread" and coroutine.running() == mt) | ||
1423 | |||
1424 | |||
1425 | |||
1426 | local function expand (n,s) | ||
1427 | if n==0 then return "" end | ||
1428 | local e = string.rep("=", n) | ||
1429 | return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", | ||
1430 | e, s, expand(n-1,s), e) | ||
1431 | end | ||
1432 | |||
1433 | G=0; collectgarbage(); a =collectgarbage("count") | ||
1434 | load(expand(20,"G=G+1"))() | ||
1435 | assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) | ||
1436 | G = nil | ||
1437 | |||
1438 | testamem("running code on new thread", function () | ||
1439 | return T.doonnewstack("local x=1") == 0 -- try to create thread | ||
1440 | end) | ||
1441 | |||
1442 | |||
1443 | -- testing memory x compiler | ||
1444 | |||
1445 | testamem("loadstring", function () | ||
1446 | return load("x=1") -- try to do load a string | ||
1447 | end) | ||
1448 | |||
1449 | |||
1450 | local testprog = [[ | ||
1451 | local function foo () return end | ||
1452 | local t = {"x"} | ||
1453 | AA = "aaa" | ||
1454 | for i = 1, #t do AA = AA .. t[i] end | ||
1455 | return true | ||
1456 | ]] | ||
1457 | |||
1458 | -- testing memory x dofile | ||
1459 | _G.AA = nil | ||
1460 | local t =os.tmpname() | ||
1461 | local f = assert(io.open(t, "w")) | ||
1462 | f:write(testprog) | ||
1463 | f:close() | ||
1464 | testamem("dofile", function () | ||
1465 | local a = loadfile(t) | ||
1466 | return a and a() | ||
1467 | end) | ||
1468 | assert(os.remove(t)) | ||
1469 | assert(_G.AA == "aaax") | ||
1470 | |||
1471 | |||
1472 | -- other generic tests | ||
1473 | |||
1474 | testamem("gsub", function () | ||
1475 | local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end) | ||
1476 | return (a == 'ablo ablo') | ||
1477 | end) | ||
1478 | |||
1479 | testamem("dump/undump", function () | ||
1480 | local a = load(testprog) | ||
1481 | local b = a and string.dump(a) | ||
1482 | a = b and load(b) | ||
1483 | return a and a() | ||
1484 | end) | ||
1485 | |||
1486 | _G.AA = nil | ||
1487 | |||
1488 | local t = os.tmpname() | ||
1489 | testamem("file creation", function () | ||
1490 | local f = assert(io.open(t, 'w')) | ||
1491 | assert (not io.open"nomenaoexistente") | ||
1492 | io.close(f); | ||
1493 | return not loadfile'nomenaoexistente' | ||
1494 | end) | ||
1495 | assert(os.remove(t)) | ||
1496 | |||
1497 | testamem("table creation", function () | ||
1498 | local a, lim = {}, 10 | ||
1499 | for i=1,lim do a[i] = i; a[i..'a'] = {} end | ||
1500 | return (type(a[lim..'a']) == 'table' and a[lim] == lim) | ||
1501 | end) | ||
1502 | |||
1503 | testamem("constructors", function () | ||
1504 | local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5} | ||
1505 | return (type(a) == 'table' and a.e == 5) | ||
1506 | end) | ||
1507 | |||
1508 | local a = 1 | ||
1509 | local close = nil | ||
1510 | testamem("closure creation", function () | ||
1511 | function close (b) | ||
1512 | return function (x) return b + x end | ||
1513 | end | ||
1514 | return (close(2)(4) == 6) | ||
1515 | end) | ||
1516 | |||
1517 | testamem("using coroutines", function () | ||
1518 | local a = coroutine.wrap(function () | ||
1519 | coroutine.yield(string.rep("a", 10)) | ||
1520 | return {} | ||
1521 | end) | ||
1522 | assert(string.len(a()) == 10) | ||
1523 | return a() | ||
1524 | end) | ||
1525 | |||
1526 | do -- auxiliary buffer | ||
1527 | local lim = 100 | ||
1528 | local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end | ||
1529 | testamem("auxiliary buffer", function () | ||
1530 | return (#table.concat(a, ",") == 20*lim + lim - 1) | ||
1531 | end) | ||
1532 | end | ||
1533 | |||
1534 | testamem("growing stack", function () | ||
1535 | local function foo (n) | ||
1536 | if n == 0 then return 1 else return 1 + foo(n - 1) end | ||
1537 | end | ||
1538 | return foo(100) | ||
1539 | end) | ||
1540 | |||
1541 | -- }================================================================== | ||
1542 | |||
1543 | |||
1544 | do -- testing failing in 'lua_checkstack' | 1301 | do -- testing failing in 'lua_checkstack' |
1545 | local res = T.testC([[rawcheckstack 500000; return 1]]) | 1302 | local res = T.testC([[rawcheckstack 500000; return 1]]) |
1546 | assert(res == false) | 1303 | assert(res == false) |
diff --git a/testes/memerr.lua b/testes/memerr.lua new file mode 100644 index 00000000..cb236eb9 --- /dev/null +++ b/testes/memerr.lua | |||
@@ -0,0 +1,266 @@ | |||
1 | -- $Id: testes/memerr.lua $ | ||
2 | -- See Copyright Notice in file all.lua | ||
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 | -- test memory errors; increase limit for maximum memory by steps, | ||
46 | -- o that we get memory errors in all allocations of a given | ||
47 | -- task, until there is enough memory to complete the task without | ||
48 | -- errors. | ||
49 | local function testbytes (s, f) | ||
50 | collectgarbage() | ||
51 | local M = T.totalmem() | ||
52 | local oldM = M | ||
53 | local a,b = nil | ||
54 | while true do | ||
55 | collectgarbage(); collectgarbage() | ||
56 | T.totalmem(M) | ||
57 | a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) | ||
58 | T.totalmem(0) -- remove limit | ||
59 | if a and b == "OK" then break end -- stop when no more errors | ||
60 | if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? | ||
61 | error(a, 0) -- propagate it | ||
62 | end | ||
63 | M = M + 7 -- increase memory limit | ||
64 | end | ||
65 | print(string.format("minimum memory for %s: %d bytes", s, M - oldM)) | ||
66 | return a | ||
67 | end | ||
68 | |||
69 | -- test memory errors; increase limit for number of allocations one | ||
70 | -- by one, so that we get memory errors in all allocations of a given | ||
71 | -- task, until there is enough allocations to complete the task without | ||
72 | -- errors. | ||
73 | |||
74 | local function testalloc (s, f) | ||
75 | collectgarbage() | ||
76 | local M = 0 | ||
77 | local a,b = nil | ||
78 | while true do | ||
79 | collectgarbage(); collectgarbage() | ||
80 | T.alloccount(M) | ||
81 | a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) | ||
82 | T.alloccount() -- remove limit | ||
83 | if a and b == "OK" then break end -- stop when no more errors | ||
84 | if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? | ||
85 | error(a, 0) -- propagate it | ||
86 | end | ||
87 | M = M + 1 -- increase allocation limit | ||
88 | end | ||
89 | print(string.format("minimum allocations for %s: %d allocations", s, M)) | ||
90 | return a | ||
91 | end | ||
92 | |||
93 | |||
94 | local function testamem (s, f) | ||
95 | testalloc(s, f) | ||
96 | return testbytes(s, f) | ||
97 | end | ||
98 | |||
99 | |||
100 | -- doing nothing | ||
101 | b = testamem("doing nothing", function () return 10 end) | ||
102 | assert(b == 10) | ||
103 | |||
104 | -- testing memory errors when creating a new state | ||
105 | |||
106 | testamem("state creation", function () | ||
107 | local st = T.newstate() | ||
108 | if st then T.closestate(st) end -- close new state | ||
109 | return st | ||
110 | end) | ||
111 | |||
112 | testamem("empty-table creation", function () | ||
113 | return {} | ||
114 | end) | ||
115 | |||
116 | testamem("string creation", function () | ||
117 | return "XXX" .. "YYY" | ||
118 | end) | ||
119 | |||
120 | testamem("coroutine creation", function() | ||
121 | return coroutine.create(print) | ||
122 | end) | ||
123 | |||
124 | |||
125 | -- testing to-be-closed variables | ||
126 | testamem("to-be-closed variables", function() | ||
127 | local flag | ||
128 | do | ||
129 | local x <close> = | ||
130 | setmetatable({}, {__close = function () flag = true end}) | ||
131 | flag = false | ||
132 | local x = {} | ||
133 | end | ||
134 | return flag | ||
135 | end) | ||
136 | |||
137 | |||
138 | -- testing threads | ||
139 | |||
140 | -- get main thread from registry | ||
141 | local mt = T.testC("rawgeti R !M; return 1") | ||
142 | assert(type(mt) == "thread" and coroutine.running() == mt) | ||
143 | |||
144 | |||
145 | |||
146 | local function expand (n,s) | ||
147 | if n==0 then return "" end | ||
148 | local e = string.rep("=", n) | ||
149 | return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", | ||
150 | e, s, expand(n-1,s), e) | ||
151 | end | ||
152 | |||
153 | G=0; collectgarbage(); a =collectgarbage("count") | ||
154 | load(expand(20,"G=G+1"))() | ||
155 | assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) | ||
156 | G = nil | ||
157 | |||
158 | testamem("running code on new thread", function () | ||
159 | return T.doonnewstack("local x=1") == 0 -- try to create thread | ||
160 | end) | ||
161 | |||
162 | |||
163 | -- testing memory x compiler | ||
164 | |||
165 | testamem("loadstring", function () | ||
166 | return load("x=1") -- try to do load a string | ||
167 | end) | ||
168 | |||
169 | |||
170 | local testprog = [[ | ||
171 | local function foo () return end | ||
172 | local t = {"x"} | ||
173 | AA = "aaa" | ||
174 | for i = 1, #t do AA = AA .. t[i] end | ||
175 | return true | ||
176 | ]] | ||
177 | |||
178 | -- testing memory x dofile | ||
179 | _G.AA = nil | ||
180 | local t =os.tmpname() | ||
181 | local f = assert(io.open(t, "w")) | ||
182 | f:write(testprog) | ||
183 | f:close() | ||
184 | testamem("dofile", function () | ||
185 | local a = loadfile(t) | ||
186 | return a and a() | ||
187 | end) | ||
188 | assert(os.remove(t)) | ||
189 | assert(_G.AA == "aaax") | ||
190 | |||
191 | |||
192 | -- other generic tests | ||
193 | |||
194 | testamem("gsub", function () | ||
195 | local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end) | ||
196 | return (a == 'ablo ablo') | ||
197 | end) | ||
198 | |||
199 | testamem("dump/undump", function () | ||
200 | local a = load(testprog) | ||
201 | local b = a and string.dump(a) | ||
202 | a = b and load(b) | ||
203 | return a and a() | ||
204 | end) | ||
205 | |||
206 | _G.AA = nil | ||
207 | |||
208 | local t = os.tmpname() | ||
209 | testamem("file creation", function () | ||
210 | local f = assert(io.open(t, 'w')) | ||
211 | assert (not io.open"nomenaoexistente") | ||
212 | io.close(f); | ||
213 | return not loadfile'nomenaoexistente' | ||
214 | end) | ||
215 | assert(os.remove(t)) | ||
216 | |||
217 | testamem("table creation", function () | ||
218 | local a, lim = {}, 10 | ||
219 | for i=1,lim do a[i] = i; a[i..'a'] = {} end | ||
220 | return (type(a[lim..'a']) == 'table' and a[lim] == lim) | ||
221 | end) | ||
222 | |||
223 | testamem("constructors", function () | ||
224 | local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5} | ||
225 | return (type(a) == 'table' and a.e == 5) | ||
226 | end) | ||
227 | |||
228 | local a = 1 | ||
229 | local close = nil | ||
230 | testamem("closure creation", function () | ||
231 | function close (b) | ||
232 | return function (x) return b + x end | ||
233 | end | ||
234 | return (close(2)(4) == 6) | ||
235 | end) | ||
236 | |||
237 | testamem("using coroutines", function () | ||
238 | local a = coroutine.wrap(function () | ||
239 | coroutine.yield(string.rep("a", 10)) | ||
240 | return {} | ||
241 | end) | ||
242 | assert(string.len(a()) == 10) | ||
243 | return a() | ||
244 | end) | ||
245 | |||
246 | do -- auxiliary buffer | ||
247 | local lim = 100 | ||
248 | local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end | ||
249 | testamem("auxiliary buffer", function () | ||
250 | return (#table.concat(a, ",") == 20*lim + lim - 1) | ||
251 | end) | ||
252 | end | ||
253 | |||
254 | testamem("growing stack", function () | ||
255 | local function foo (n) | ||
256 | if n == 0 then return 1 else return 1 + foo(n - 1) end | ||
257 | end | ||
258 | return foo(100) | ||
259 | end) | ||
260 | |||
261 | -- }================================================================== | ||
262 | |||
263 | |||
264 | print "Ok" | ||
265 | |||
266 | |||
diff --git a/testes/packtests b/testes/packtests index 0dbb92fe..855c054a 100755 --- a/testes/packtests +++ b/testes/packtests | |||
@@ -28,6 +28,7 @@ $NAME/literals.lua \ | |||
28 | $NAME/locals.lua \ | 28 | $NAME/locals.lua \ |
29 | $NAME/main.lua \ | 29 | $NAME/main.lua \ |
30 | $NAME/math.lua \ | 30 | $NAME/math.lua \ |
31 | $NAME/memerr.lua \ | ||
31 | $NAME/nextvar.lua \ | 32 | $NAME/nextvar.lua \ |
32 | $NAME/pm.lua \ | 33 | $NAME/pm.lua \ |
33 | $NAME/sort.lua \ | 34 | $NAME/sort.lua \ |