1-- $Id: testes/strings.lua $
2-- See Copyright Notice in file all.lua
3
4-- ISO Latin encoding
5
6
7print('testing strings and string library')
8
9local maxi <const> = math.maxinteger
10local mini <const> = math.mininteger
11
12
13local function checkerror (msg, f, ...)
14 local s, err = pcall(f, ...)
15 assert(not s and string.find(err, msg))
16end
17
18
19-- testing string comparisons
20assert('alo' < 'alo1')
21assert('' < 'a')
22assert('alo\0alo' < 'alo\0b')
23assert('alo\0alo\0\0' > 'alo\0alo\0')
24assert('alo' < 'alo\0')
25assert('alo\0' > 'alo')
26assert('\0' < '\1')
27assert('\0\0' < '\0\1')
28assert('\1\0a\0a' <= '\1\0a\0a')
29assert(not ('\1\0a\0b' <= '\1\0a\0a'))
30assert('\0\0\0' < '\0\0\0\0')
31assert(not('\0\0\0\0' < '\0\0\0'))
32assert('\0\0\0' <= '\0\0\0\0')
33assert(not('\0\0\0\0' <= '\0\0\0'))
34assert('\0\0\0' <= '\0\0\0')
35assert('\0\0\0' >= '\0\0\0')
36assert(not ('\0\0b' < '\0\0a\0'))
37
38-- testing string.sub
39assert(string.sub("123456789",2,4) == "234")
40assert(string.sub("123456789",7) == "789")
41assert(string.sub("123456789",7,6) == "")
42assert(string.sub("123456789",7,7) == "7")
43assert(string.sub("123456789",0,0) == "")
44assert(string.sub("123456789",-10,10) == "123456789")
45assert(string.sub("123456789",1,9) == "123456789")
46assert(string.sub("123456789",-10,-20) == "")
47assert(string.sub("123456789",-1) == "9")
48assert(string.sub("123456789",-4) == "6789")
49assert(string.sub("123456789",-6, -4) == "456")
50assert(string.sub("123456789", mini, -4) == "123456")
51assert(string.sub("123456789", mini, maxi) == "123456789")
52assert(string.sub("123456789", mini, mini) == "")
53assert(string.sub("\000123456789",3,5) == "234")
54assert(("\000123456789"):sub(8) == "789")
55
56-- testing string.find
57assert(string.find("123456789", "345") == 3)
58local a,b = string.find("123456789", "345")
59assert(string.sub("123456789", a, b) == "345")
60assert(string.find("1234567890123456789", "345", 3) == 3)
61assert(string.find("1234567890123456789", "345", 4) == 13)
62assert(not string.find("1234567890123456789", "346", 4))
63assert(string.find("1234567890123456789", ".45", -9) == 13)
64assert(not string.find("abcdefg", "\0", 5, 1))
65assert(string.find("", "") == 1)
66assert(string.find("", "", 1) == 1)
67assert(not string.find("", "", 2))
68assert(not string.find('', 'aaa', 1))
69assert(('alo(.)alo'):find('(.)', 1, 1) == 4)
70
71assert(string.len("") == 0)
72assert(string.len("\0\0\0") == 3)
73assert(string.len("1234567890") == 10)
74
75assert(#"" == 0)
76assert(#"\0\0\0" == 3)
77assert(#"1234567890" == 10)
78
79-- testing string.byte/string.char
80assert(string.byte("a") == 97)
81assert(string.byte("\xe4") > 127)
82assert(string.byte(string.char(255)) == 255)
83assert(string.byte(string.char(0)) == 0)
84assert(string.byte("\0") == 0)
85assert(string.byte("\0\0alo\0x", -1) == string.byte('x'))
86assert(string.byte("ba", 2) == 97)
87assert(string.byte("\n\n", 2, -1) == 10)
88assert(string.byte("\n\n", 2, 2) == 10)
89assert(string.byte("") == nil)
90assert(string.byte("hi", -3) == nil)
91assert(string.byte("hi", 3) == nil)
92assert(string.byte("hi", 9, 10) == nil)
93assert(string.byte("hi", 2, 1) == nil)
94assert(string.char() == "")
95assert(string.char(0, 255, 0) == "\0\255\0")
96assert(string.char(0, string.byte("\xe4"), 0) == "\0\xe4\0")
97assert(string.char(string.byte("\xe4l\0�u", 1, -1)) == "\xe4l\0�u")
98assert(string.char(string.byte("\xe4l\0�u", 1, 0)) == "")
99assert(string.char(string.byte("\xe4l\0�u", -10, 100)) == "\xe4l\0�u")
100
101checkerror("out of range", string.char, 256)
102checkerror("out of range", string.char, -1)
103checkerror("out of range", string.char, math.maxinteger)
104checkerror("out of range", string.char, math.mininteger)
105
106assert(string.upper("ab\0c") == "AB\0C")
107assert(string.lower("\0ABCc%$") == "\0abcc%$")
108assert(string.rep('teste', 0) == '')
109assert(string.rep('t�s\00t�', 2) == 't�s\0t�t�s\000t�')
110assert(string.rep('', 10) == '')
111
112if string.packsize("i") == 4 then
113 -- result length would be 2^31 (int overflow)
114 checkerror("too large", string.rep, 'aa', (1 << 30))
115 checkerror("too large", string.rep, 'a', (1 << 30), ',')
116end
117
118-- repetitions with separator
119assert(string.rep('teste', 0, 'xuxu') == '')
120assert(string.rep('teste', 1, 'xuxu') == 'teste')
121assert(string.rep('\1\0\1', 2, '\0\0') == '\1\0\1\0\0\1\0\1')
122assert(string.rep('', 10, '.') == string.rep('.', 9))
123assert(not pcall(string.rep, "aa", maxi // 2 + 10))
124assert(not pcall(string.rep, "", maxi // 2 + 10, "aa"))
125
126assert(string.reverse"" == "")
127assert(string.reverse"\0\1\2\3" == "\3\2\1\0")
128assert(string.reverse"\0001234" == "4321\0")
129
130for i=0,30 do assert(string.len(string.rep('a', i)) == i) end
131
132assert(type(tostring(nil)) == 'string')
133assert(type(tostring(12)) == 'string')
134assert(string.find(tostring{}, 'table:'))
135assert(string.find(tostring(print), 'function:'))
136assert(#tostring('\0') == 1)
137assert(tostring(true) == "true")
138assert(tostring(false) == "false")
139assert(tostring(-1203) == "-1203")
140assert(tostring(1203.125) == "1203.125")
141assert(tostring(-0.5) == "-0.5")
142assert(tostring(-32767) == "-32767")
143if math.tointeger(2147483647) then -- no overflow? (32 bits)
144 assert(tostring(-2147483647) == "-2147483647")
145end
146if math.tointeger(4611686018427387904) then -- no overflow? (64 bits)
147 assert(tostring(4611686018427387904) == "4611686018427387904")
148 assert(tostring(-4611686018427387904) == "-4611686018427387904")
149end
150
151if tostring(0.0) == "0.0" then -- "standard" coercion float->string
152 assert('' .. 12 == '12' and 12.0 .. '' == '12.0')
153 assert(tostring(-1203 + 0.0) == "-1203.0")
154else -- compatible coercion
155 assert(tostring(0.0) == "0")
156 assert('' .. 12 == '12' and 12.0 .. '' == '12')
157 assert(tostring(-1203 + 0.0) == "-1203")
158end
159
160do -- tests for '%p' format
161 -- not much to test, as C does not specify what '%p' does.
162 -- ("The value of the pointer is converted to a sequence of printing
163 -- characters, in an implementation-defined manner.")
164 local null = "(null)" -- nulls are formatted by Lua
165 assert(string.format("%p", 4) == null)
166 assert(string.format("%p", true) == null)
167 assert(string.format("%p", nil) == null)
168 assert(string.format("%p", {}) ~= null)
169 assert(string.format("%p", print) ~= null)
170 assert(string.format("%p", coroutine.running()) ~= null)
171 assert(string.format("%p", io.stdin) ~= null)
172 assert(string.format("%p", io.stdin) == string.format("%p", io.stdin))
173 assert(string.format("%p", print) == string.format("%p", print))
174 assert(string.format("%p", print) ~= string.format("%p", assert))
175
176 assert(#string.format("%90p", {}) == 90)
177 assert(#string.format("%-60p", {}) == 60)
178 assert(string.format("%10p", false) == string.rep(" ", 10 - #null) .. null)
179 assert(string.format("%-12p", 1.5) == null .. string.rep(" ", 12 - #null))
180
181 do
182 local t1 = {}; local t2 = {}
183 assert(string.format("%p", t1) ~= string.format("%p", t2))
184 end
185
186 do -- short strings are internalized
187 local s1 = string.rep("a", 10)
188 local s2 = string.rep("aa", 5)
189 assert(string.format("%p", s1) == string.format("%p", s2))
190 end
191
192 do -- long strings aren't internalized
193 local s1 = string.rep("a", 300); local s2 = string.rep("a", 300)
194 assert(string.format("%p", s1) ~= string.format("%p", s2))
195 end
196end
197
198local x = '"�lo"\n\\'
199assert(string.format('%q%s', x, x) == '"\\"�lo\\"\\\n\\\\""�lo"\n\\')
200assert(string.format('%q', "\0") == [["\0"]])
201assert(load(string.format('return %q', x))() == x)
202x = "\0\1\0023\5\0009"
203assert(load(string.format('return %q', x))() == x)
204assert(string.format("\0%c\0%c%x\0", string.byte("\xe4"), string.byte("b"), 140) ==
205 "\0\xe4\0b8c\0")
206assert(string.format('') == "")
207assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) ==
208 string.format("%1c%-c%-1c%c", 34, 48, 90, 100))
209assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be')
210assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023")
211assert(tonumber(string.format("%f", 10.3)) == 10.3)
212assert(string.format('"%-50s"', 'a') == '"a' .. string.rep(' ', 49) .. '"')
213
214assert(string.format("-%.20s.20s", string.rep("%", 2000)) ==
215 "-"..string.rep("%", 20)..".20s")
216assert(string.format('"-%20s.20s"', string.rep("%", 2000)) ==
217 string.format("%q", "-"..string.rep("%", 2000)..".20s"))
218
219do
220 local function checkQ (v)
221 local s = string.format("%q", v)
222 local nv = load("return " .. s)()
223 assert(v == nv and math.type(v) == math.type(nv))
224 end
225 checkQ("\0\0\1\255\u{234}")
226 checkQ(math.maxinteger)
227 checkQ(math.mininteger)
228 checkQ(math.pi)
229 checkQ(0.1)
230 checkQ(true)
231 checkQ(nil)
232 checkQ(false)
233 checkQ(math.huge)
234 checkQ(-math.huge)
235 assert(string.format("%q", 0/0) == "(0/0)") -- NaN
236 checkerror("no literal", string.format, "%q", {})
237end
238
239assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0")
240checkerror("contains zeros", string.format, "%10s", "\0")
241
242-- format x tostring
243assert(string.format("%s %s", nil, true) == "nil true")
244assert(string.format("%s %.4s", false, true) == "false true")
245assert(string.format("%.3s %.3s", false, true) == "fal tru")
246local m = setmetatable({}, {__tostring = function () return "hello" end,
247 __name = "hi"})
248assert(string.format("%s %.10s", m, m) == "hello hello")
249getmetatable(m).__tostring = nil -- will use '__name' from now on
250assert(string.format("%.4s", m) == "hi: ")
251
252getmetatable(m).__tostring = function () return {} end
253checkerror("'__tostring' must return a string", tostring, m)
254
255
256assert(string.format("%x", 0.0) == "0")
257assert(string.format("%02x", 0.0) == "00")
258assert(string.format("%08X", 0xFFFFFFFF) == "FFFFFFFF")
259assert(string.format("%+08d", 31501) == "+0031501")
260assert(string.format("%+08d", -30927) == "-0030927")
261
262
263do -- longest number that can be formatted
264 local i = 1
265 local j = 10000
266 while i + 1 < j do -- binary search for maximum finite float
267 local m = (i + j) // 2
268 if 10^m < math.huge then i = m else j = m end
269 end
270 assert(10^i < math.huge and 10^j == math.huge)
271 local s = string.format('%.99f', -(10^i))
272 assert(string.len(s) >= i + 101)
273 assert(tonumber(s) == -(10^i))
274
275 -- limit for floats
276 assert(10^38 < math.huge)
277 local s = string.format('%.99f', -(10^38))
278 assert(string.len(s) >= 38 + 101)
279 assert(tonumber(s) == -(10^38))
280end
281
282
283-- testing large numbers for format
284do -- assume at least 32 bits
285 local max, min = 0x7fffffff, -0x80000000 -- "large" for 32 bits
286 assert(string.sub(string.format("%8x", -1), -8) == "ffffffff")
287 assert(string.format("%x", max) == "7fffffff")
288 assert(string.sub(string.format("%x", min), -8) == "80000000")
289 assert(string.format("%d", max) == "2147483647")
290 assert(string.format("%d", min) == "-2147483648")
291 assert(string.format("%u", 0xffffffff) == "4294967295")
292 assert(string.format("%o", 0xABCD) == "125715")
293
294 max, min = 0x7fffffffffffffff, -0x8000000000000000
295 if max > 2.0^53 then -- only for 64 bits
296 assert(string.format("%x", (2^52 | 0) - 1) == "fffffffffffff")
297 assert(string.format("0x%8X", 0x8f000003) == "0x8F000003")
298 assert(string.format("%d", 2^53) == "9007199254740992")
299 assert(string.format("%i", -2^53) == "-9007199254740992")
300 assert(string.format("%x", max) == "7fffffffffffffff")
301 assert(string.format("%x", min) == "8000000000000000")
302 assert(string.format("%d", max) == "9223372036854775807")
303 assert(string.format("%d", min) == "-9223372036854775808")
304 assert(string.format("%u", ~(-1 << 64)) == "18446744073709551615")
305 assert(tostring(1234567890123) == '1234567890123')
306 end
307end
308
309
310do print("testing 'format %a %A'")
311 local function matchhexa (n)
312 local s = string.format("%a", n)
313 -- result matches ISO C requirements
314 assert(string.find(s, "^%-?0x[1-9a-f]%.?[0-9a-f]*p[-+]?%d+$"))
315 assert(tonumber(s) == n) -- and has full precision
316 s = string.format("%A", n)
317 assert(string.find(s, "^%-?0X[1-9A-F]%.?[0-9A-F]*P[-+]?%d+$"))
318 assert(tonumber(s) == n)
319 end
320 for _, n in ipairs{0.1, -0.1, 1/3, -1/3, 1e30, -1e30,
321 -45/247, 1, -1, 2, -2, 3e-20, -3e-20} do
322 matchhexa(n)
323 end
324
325 assert(string.find(string.format("%A", 0.0), "^0X0%.?0*P%+?0$"))
326 assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0*p%+?0$"))
327
328 if not _port then -- test inf, -inf, NaN, and -0.0
329 assert(string.find(string.format("%a", 1/0), "^inf"))
330 assert(string.find(string.format("%A", -1/0), "^%-INF"))
331 assert(string.find(string.format("%a", 0/0), "^%-?nan"))
332 assert(string.find(string.format("%a", -0.0), "^%-0x0"))
333 end
334
335 if not pcall(string.format, "%.3a", 0) then
336 (Message or print)("\n >>> modifiers for format '%a' not available <<<\n")
337 else
338 assert(string.find(string.format("%+.2A", 12), "^%+0X%x%.%x0P%+?%d$"))
339 assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$"))
340 end
341end
342
343
344-- testing some flags (all these results are required by ISO C)
345assert(string.format("%#12o", 10) == " 012")
346assert(string.format("%#10x", 100) == " 0x64")
347assert(string.format("%#-17X", 100) == "0X64 ")
348assert(string.format("%013i", -100) == "-000000000100")
349assert(string.format("%2.5d", -100) == "-00100")
350assert(string.format("%.u", 0) == "")
351assert(string.format("%+#014.0f", 100) == "+000000000100.")
352assert(string.format("%-16c", 97) == "a ")
353assert(string.format("%+.3G", 1.5) == "+1.5")
354assert(string.format("%.0s", "alo") == "")
355assert(string.format("%.s", "alo") == "")
356
357-- ISO C89 says that "The exponent always contains at least two digits",
358-- but unlike ISO C99 it does not ensure that it contains "only as many
359-- more digits as necessary".
360assert(string.match(string.format("% 1.0E", 100), "^ 1E%+0+2$"))
361assert(string.match(string.format("% .1g", 2^10), "^ 1e%+0+3$"))
362
363
364-- errors in format
365
366local function check (fmt, msg)
367 checkerror(msg, string.format, fmt, 10)
368end
369
370local aux = string.rep('0', 600)
371check("%100.3d", "invalid conversion")
372check("%1"..aux..".3d", "too long")
373check("%1.100d", "invalid conversion")
374check("%10.1"..aux.."004d", "too long")
375check("%t", "invalid conversion")
376check("%"..aux.."d", "too long")
377check("%d %d", "no value")
378check("%010c", "invalid conversion")
379check("%.10c", "invalid conversion")
380check("%0.34s", "invalid conversion")
381check("%#i", "invalid conversion")
382check("%3.1p", "invalid conversion")
383check("%0.s", "invalid conversion")
384check("%10q", "cannot have modifiers")
385check("%F", "invalid conversion") -- useless and not in C89
386
387
388assert(load("return 1\n--comment without ending EOL")() == 1)
389
390
391checkerror("table expected", table.concat, 3)
392checkerror("at index " .. maxi, table.concat, {}, " ", maxi, maxi)
393-- '%' escapes following minus signal
394checkerror("at index %" .. mini, table.concat, {}, " ", mini, mini)
395assert(table.concat{} == "")
396assert(table.concat({}, 'x') == "")
397assert(table.concat({'\0', '\0\1', '\0\1\2'}, '.\0.') == "\0.\0.\0\1.\0.\0\1\2")
398local a = {}; for i=1,300 do a[i] = "xuxu" end
399assert(table.concat(a, "123").."123" == string.rep("xuxu123", 300))
400assert(table.concat(a, "b", 20, 20) == "xuxu")
401assert(table.concat(a, "", 20, 21) == "xuxuxuxu")
402assert(table.concat(a, "x", 22, 21) == "")
403assert(table.concat(a, "3", 299) == "xuxu3xuxu")
404assert(table.concat({}, "x", maxi, maxi - 1) == "")
405assert(table.concat({}, "x", mini + 1, mini) == "")
406assert(table.concat({}, "x", maxi, mini) == "")
407assert(table.concat({[maxi] = "alo"}, "x", maxi, maxi) == "alo")
408assert(table.concat({[maxi] = "alo", [maxi - 1] = "y"}, "-", maxi - 1, maxi)
409 == "y-alo")
410
411assert(not pcall(table.concat, {"a", "b", {}}))
412
413a = {"a","b","c"}
414assert(table.concat(a, ",", 1, 0) == "")
415assert(table.concat(a, ",", 1, 1) == "a")
416assert(table.concat(a, ",", 1, 2) == "a,b")
417assert(table.concat(a, ",", 2) == "b,c")
418assert(table.concat(a, ",", 3) == "c")
419assert(table.concat(a, ",", 4) == "")
420
421if not _port then
422
423 local locales = { "ptb", "pt_BR.iso88591", "ISO-8859-1" }
424 local function trylocale (w)
425 for i = 1, #locales do
426 if os.setlocale(locales[i], w) then
427 print(string.format("'%s' locale set to '%s'", w, locales[i]))
428 return locales[i]
429 end
430 end
431 print(string.format("'%s' locale not found", w))
432 return false
433 end
434
435 if trylocale("collate") then
436 assert("alo" < "�lo" and "�lo" < "amo")
437 end
438
439 if trylocale("ctype") then
440 assert(string.gsub("�����", "%a", "x") == "xxxxx")
441 assert(string.gsub("����", "%l", "x") == "x�x�")
442 assert(string.gsub("����", "%u", "x") == "�x�x")
443 assert(string.upper"���{xuxu}��o" == "���{XUXU}��O")
444 end
445
446 os.setlocale("C")
447 assert(os.setlocale() == 'C')
448 assert(os.setlocale(nil, "numeric") == 'C')
449
450end
451
452
453-- bug in Lua 5.3.2
454-- 'gmatch' iterator does not work across coroutines
455do
456 local f = string.gmatch("1 2 3 4 5", "%d+")
457 assert(f() == "1")
458 local co = coroutine.wrap(f)
459 assert(co() == "2")
460end
461
462
463if T==nil then
464 (Message or print)
465 ("\n >>> testC not active: skipping 'pushfstring' tests <<<\n")
466else
467
468 print"testing 'pushfstring'"
469
470 -- formats %U, %f, %I already tested elsewhere
471
472 local blen = 200 -- internal buffer length in 'luaO_pushfstring'
473
474 local function callpfs (op, fmt, n)
475 local x = {T.testC("pushfstring" .. op .. "; return *", fmt, n)}
476 -- stack has code, 'fmt', 'n', and result from operation
477 assert(#x == 4) -- make sure nothing else was left in the stack
478 return x[4]
479 end
480
481 local function testpfs (op, fmt, n)
482 assert(callpfs(op, fmt, n) == string.format(fmt, n))
483 end
484
485 testpfs("I", "", 0)
486 testpfs("I", string.rep("a", blen - 1), 0)
487 testpfs("I", string.rep("a", blen), 0)
488 testpfs("I", string.rep("a", blen + 1), 0)
489
490 local str = string.rep("ab", blen) .. "%d" .. string.rep("d", blen / 2)
491 testpfs("I", str, 2^14)
492 testpfs("I", str, -2^15)
493
494 str = "%d" .. string.rep("cd", blen)
495 testpfs("I", str, 2^14)
496 testpfs("I", str, -2^15)
497
498 str = string.rep("c", blen - 2) .. "%d"
499 testpfs("I", str, 2^14)
500 testpfs("I", str, -2^15)
501
502 for l = 12, 14 do
503 local str1 = string.rep("a", l)
504 for i = 0, 500, 13 do
505 for j = 0, 500, 13 do
506 str = string.rep("a", i) .. "%s" .. string.rep("d", j)
507 testpfs("S", str, str1)
508 testpfs("S", str, str)
509 end
510 end
511 end
512
513 str = "abc %c def"
514 testpfs("I", str, string.byte("A"))
515 testpfs("I", str, 255)
516
517 str = string.rep("a", blen - 1) .. "%p" .. string.rep("cd", blen)
518 testpfs("P", str, {})
519
520 str = string.rep("%%", 3 * blen) .. "%p" .. string.rep("%%", 2 * blen)
521 testpfs("P", str, {})
522end
523
524
525print('OK')
526