Avoid memory allocation in some functions from 'ltests.c'

To allow their use in memory tests, some functions in 'ltests.c'
should never allocate memory. To avoid this allocation, the
library registers the strings used for status codes, and keeps
the variable '_WARN' always defined (with false instead of nil).
This commit is contained in:
Roberto Ierusalimschy
2020-07-04 16:40:18 -03:00
parent 0280407fc5
commit bfcf06d91a
5 changed files with 30 additions and 17 deletions

View File

@@ -372,7 +372,7 @@ if T then
warn("@on"); warn("@store")
collectgarbage()
assert(string.find(_WARN, "error in __gc metamethod"))
assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = nil
assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = false
for i = 8, 10 do assert(s[i]) end
for i = 1, 5 do
@@ -481,7 +481,7 @@ if T then
u = setmetatable({}, {__gc = function () error "@expected error" end})
u = nil
collectgarbage()
assert(string.find(_WARN, "@expected error")); _WARN = nil
assert(string.find(_WARN, "@expected error")); _WARN = false
warn("@normal")
end
@@ -657,14 +657,14 @@ if T then
n = n + 1
assert(n == o[1])
if n == 1 then
_WARN = nil
_WARN = false
elseif n == 2 then
assert(find(_WARN, "@expected warning"))
lastmsg = _WARN -- get message from previous error (first 'o')
else
assert(lastmsg == _WARN) -- subsequent error messages are equal
end
warn("@store"); _WARN = nil
warn("@store"); _WARN = false
error"@expected warning"
end}
for i = 10, 1, -1 do