From 024f9064f1b43758eb36aba52547edc0312bf4ba Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 9 Nov 2023 17:05:42 -0300 Subject: External strings Strings can use external buffers to store their contents. --- testes/strings.lua | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'testes') diff --git a/testes/strings.lua b/testes/strings.lua index 90983edd..c124b369 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -157,6 +157,12 @@ else -- compatible coercion assert(tostring(-1203 + 0.0) == "-1203") end + +local function topointer (s) + return string.format("%p", s) +end + + do -- tests for '%p' format -- not much to test, as C does not specify what '%p' does. -- ("The value of the pointer is converted to a sequence of printing @@ -180,18 +186,18 @@ do -- tests for '%p' format do local t1 = {}; local t2 = {} - assert(string.format("%p", t1) ~= string.format("%p", t2)) + assert(topointer(t1) ~= topointer(t2)) end do -- short strings are internalized local s1 = string.rep("a", 10) local s2 = string.rep("aa", 5) - assert(string.format("%p", s1) == string.format("%p", s2)) + assert(topointer(s1) == topointer(s2)) end do -- long strings aren't internalized local s1 = string.rep("a", 300); local s2 = string.rep("a", 300) - assert(string.format("%p", s1) ~= string.format("%p", s2)) + assert(topointer(s1) ~= topointer(s2)) end end @@ -521,6 +527,20 @@ else testpfs("P", str, {}) end +if T == nil then + (Message or print)('\n >>> testC not active: skipping external strings tests <<<\n') +else + print("testing external strings") + local x = T.externKstr("hello") -- external fixed short string + assert(x == "hello") + local x = T.externstr("hello") -- external allocated short string + assert(x == "hello") + x = string.rep("a", 100) -- long string + local y = T.externKstr(x) -- external fixed long string + assert(y == x) + local z = T.externstr(x) -- external allocated long string + assert(z == y) +end print('OK') -- cgit v1.2.3-55-g6feb