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

@@ -184,7 +184,7 @@ do
if not T then
warn("@on")
else -- test library
assert(string.find(_WARN, "200")); _WARN = nil
assert(string.find(_WARN, "200")); _WARN = false
warn("@normal")
end
assert(st == false and coroutine.status(co) == "dead" and msg == 111)

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

View File

@@ -337,7 +337,7 @@ local function endwarn ()
if not T then
warn("@on") -- back to normal
else
assert(_WARN == nil)
assert(_WARN == false)
warn("@normal")
end
end
@@ -346,7 +346,7 @@ end
local function checkwarn (msg)
if T then
assert(string.find(_WARN, msg))
_WARN = nil -- reset variable to check next warning
_WARN = false -- reset variable to check next warning
end
end

View File

@@ -393,12 +393,12 @@ if T then -- test library?
-- testing 'warn'
warn("@store")
warn("@123", "456", "789")
assert(_WARN == "@123456789"); _WARN = nil
assert(_WARN == "@123456789"); _WARN = false
warn("zip", "", " ", "zap")
assert(_WARN == "zip zap"); _WARN = nil
assert(_WARN == "zip zap"); _WARN = false
warn("ZIP", "", " ", "ZAP")
assert(_WARN == "ZIP ZAP"); _WARN = nil
assert(_WARN == "ZIP ZAP"); _WARN = false
warn("@normal")
end