interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / testes / strings.lua

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