From 513559cc4760392b6fa33754c516683ef49dba22 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 16 Mar 2020 14:13:13 -0300 Subject: Fixed bug in 'string.format("%p")' The string "(null)" used for non-collectable values must be printed as a string, not as a pointer. (Bug introduced in commit e0cbaa50fa7). --- testes/strings.lua | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'testes') diff --git a/testes/strings.lua b/testes/strings.lua index 2ce3ebc3..4a10857e 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -158,28 +158,38 @@ 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 -- characters, in an implementation-defined manner.") - local null = string.format("%p", nil) - assert(string.format("%p", {}) ~= null) + local null = "(null)" -- nulls are formatted by Lua assert(string.format("%p", 4) == null) assert(string.format("%p", true) == null) + assert(string.format("%p", nil) == null) + assert(string.format("%p", {}) ~= null) assert(string.format("%p", print) ~= null) assert(string.format("%p", coroutine.running()) ~= null) assert(string.format("%p", io.stdin) ~= null) assert(string.format("%p", io.stdin) == string.format("%p", io.stdin)) + assert(string.format("%p", print) == string.format("%p", print)) + assert(string.format("%p", print) ~= string.format("%p", assert)) + + assert(#string.format("%90p", {}) == 90) + assert(#string.format("%-60p", {}) == 60) + assert(string.format("%10p", false) == string.rep(" ", 10 - #null) .. null) + assert(string.format("%-12p", 1.5) == null .. string.rep(" ", 12 - #null)) + do local t1 = {}; local t2 = {} assert(string.format("%p", t1) ~= string.format("%p", t2)) end + do -- short strings are internalized local s1 = string.rep("a", 10) - local s2 = string.rep("a", 10) + local s2 = string.rep("aa", 5) assert(string.format("%p", s1) == string.format("%p", 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)) end - assert(#string.format("%90p", {}) == 90) end x = '"ílo"\n\\' -- cgit v1.2.3-55-g6feb