aboutsummaryrefslogtreecommitdiff
path: root/testes
diff options
context:
space:
mode:
Diffstat (limited to 'testes')
-rw-r--r--testes/calls.lua14
-rw-r--r--testes/db.lua8
-rw-r--r--testes/errors.lua11
-rw-r--r--testes/files.lua8
-rw-r--r--testes/main.lua50
-rw-r--r--testes/pm.lua56
-rw-r--r--testes/sort.lua2
-rw-r--r--testes/strings.lua3
-rw-r--r--testes/utf8.lua2
9 files changed, 87 insertions, 67 deletions
diff --git a/testes/calls.lua b/testes/calls.lua
index 2d562a24..a1938584 100644
--- a/testes/calls.lua
+++ b/testes/calls.lua
@@ -342,20 +342,6 @@ do -- another bug (in 5.4.0)
342end 342end
343 343
344 344
345do -- another bug (since 5.2)
346 -- corrupted binary dump: list of upvalue names is larger than number
347 -- of upvalues, overflowing the array of upvalues.
348 local code =
349 "\x1b\x4c\x75\x61\x54\x00\x19\x93\x0d\x0a\x1a\x0a\x04\x08\x08\x78\x56\z
350 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x28\x77\x40\x00\x86\x40\z
351 \x74\x65\x6d\x70\x81\x81\x01\x00\x02\x82\x48\x00\x02\x00\xc7\x00\x01\z
352 \x00\x80\x80\x80\x82\x00\x00\x80\x81\x82\x78\x80\x82\x81\x86\x40\x74\z
353 \x65\x6d\x70"
354
355 assert(load(code)) -- segfaults in previous versions
356end
357
358
359x = string.dump(load("x = 1; return x")) 345x = string.dump(load("x = 1; return x"))
360a = assert(load(read1(x), nil, "b")) 346a = assert(load(read1(x), nil, "b"))
361assert(a() == 1 and _G.x == 1) 347assert(a() == 1 and _G.x == 1)
diff --git a/testes/db.lua b/testes/db.lua
index 02b96aca..d3758c41 100644
--- a/testes/db.lua
+++ b/testes/db.lua
@@ -345,7 +345,7 @@ function f(a,b)
345 local _, y = debug.getlocal(1, 2) 345 local _, y = debug.getlocal(1, 2)
346 assert(x == a and y == b) 346 assert(x == a and y == b)
347 assert(debug.setlocal(2, 3, "pera") == "AA".."AA") 347 assert(debug.setlocal(2, 3, "pera") == "AA".."AA")
348 assert(debug.setlocal(2, 4, "maçã") == "B") 348 assert(debug.setlocal(2, 4, "manga") == "B")
349 x = debug.getinfo(2) 349 x = debug.getinfo(2)
350 assert(x.func == g and x.what == "Lua" and x.name == 'g' and 350 assert(x.func == g and x.what == "Lua" and x.name == 'g' and
351 x.nups == 2 and string.find(x.source, "^@.*db%.lua$")) 351 x.nups == 2 and string.find(x.source, "^@.*db%.lua$"))
@@ -373,9 +373,9 @@ function g (...)
373 local arg = {...} 373 local arg = {...}
374 do local a,b,c; a=math.sin(40); end 374 do local a,b,c; a=math.sin(40); end
375 local feijao 375 local feijao
376 local AAAA,B = "xuxu", "mamão" 376 local AAAA,B = "xuxu", "abacate"
377 f(AAAA,B) 377 f(AAAA,B)
378 assert(AAAA == "pera" and B == "maçã") 378 assert(AAAA == "pera" and B == "manga")
379 do 379 do
380 local B = 13 380 local B = 13
381 local x,y = debug.getlocal(1,5) 381 local x,y = debug.getlocal(1,5)
@@ -928,7 +928,7 @@ do
928 local cl = countlines(rest) 928 local cl = countlines(rest)
929 -- at most 10 lines in first part, 11 in second, plus '...' 929 -- at most 10 lines in first part, 11 in second, plus '...'
930 assert(cl <= 10 + 11 + 1) 930 assert(cl <= 10 + 11 + 1)
931 local brk = string.find(rest, "%.%.%.") 931 local brk = string.find(rest, "%.%.%.\t%(skip")
932 if brk then -- does message have '...'? 932 if brk then -- does message have '...'?
933 local rest1 = string.sub(rest, 1, brk) 933 local rest1 = string.sub(rest, 1, brk)
934 local rest2 = string.sub(rest, brk, #rest) 934 local rest2 = string.sub(rest, brk, #rest)
diff --git a/testes/errors.lua b/testes/errors.lua
index bf6f389d..01cfe906 100644
--- a/testes/errors.lua
+++ b/testes/errors.lua
@@ -121,6 +121,9 @@ assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'"))
121checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number") 121checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number")
122checkmessage("aaa=(1)..{}", "a table value") 122checkmessage("aaa=(1)..{}", "a table value")
123 123
124-- bug in 5.4.6
125checkmessage("a = {_ENV = {}}; print(a._ENV.x + 1)", "field 'x'")
126
124_G.aaa, _G.bbbb = nil 127_G.aaa, _G.bbbb = nil
125 128
126-- calls 129-- calls
@@ -392,19 +395,19 @@ lineerror("a\n=\n-\n\nprint\n;", 3)
392 395
393lineerror([[ 396lineerror([[
394a 397a
395( 398( -- <<
39623) 39923)
397]], 1) 400]], 2)
398 401
399lineerror([[ 402lineerror([[
400local a = {x = 13} 403local a = {x = 13}
401a 404a
402. 405.
403x 406x
404( 407( -- <<
40523 40823
406) 409)
407]], 2) 410]], 5)
408 411
409lineerror([[ 412lineerror([[
410local a = {x = 13} 413local a = {x = 13}
diff --git a/testes/files.lua b/testes/files.lua
index be00bf3f..1476006e 100644
--- a/testes/files.lua
+++ b/testes/files.lua
@@ -92,8 +92,8 @@ assert(io.output():seek("end") == string.len("alo joao"))
92 92
93assert(io.output():seek("set") == 0) 93assert(io.output():seek("set") == 0)
94 94
95assert(io.write('"álo"', "{a}\n", "second line\n", "third line \n")) 95assert(io.write('"alo"', "{a}\n", "second line\n", "third line \n"))
96assert(io.write('çfourth_line')) 96assert(io.write('Xfourth_line'))
97io.output(io.stdout) 97io.output(io.stdout)
98collectgarbage() -- file should be closed by GC 98collectgarbage() -- file should be closed by GC
99assert(io.input() == io.stdin and rawequal(io.output(), io.stdout)) 99assert(io.input() == io.stdin and rawequal(io.output(), io.stdout))
@@ -300,14 +300,14 @@ do -- test error returns
300end 300end
301checkerr("invalid format", io.read, "x") 301checkerr("invalid format", io.read, "x")
302assert(io.read(0) == "") -- not eof 302assert(io.read(0) == "") -- not eof
303assert(io.read(5, 'l') == '"álo"') 303assert(io.read(5, 'l') == '"alo"')
304assert(io.read(0) == "") 304assert(io.read(0) == "")
305assert(io.read() == "second line") 305assert(io.read() == "second line")
306local x = io.input():seek() 306local x = io.input():seek()
307assert(io.read() == "third line ") 307assert(io.read() == "third line ")
308assert(io.input():seek("set", x)) 308assert(io.input():seek("set", x))
309assert(io.read('L') == "third line \n") 309assert(io.read('L') == "third line \n")
310assert(io.read(1) == "ç") 310assert(io.read(1) == "X")
311assert(io.read(string.len"fourth_line") == "fourth_line") 311assert(io.read(string.len"fourth_line") == "fourth_line")
312assert(io.input():seek("cur", -string.len"fourth_line")) 312assert(io.input():seek("cur", -string.len"fourth_line"))
313assert(io.read() == "fourth_line") 313assert(io.read() == "fourth_line")
diff --git a/testes/main.lua b/testes/main.lua
index f59badcf..11b14b44 100644
--- a/testes/main.lua
+++ b/testes/main.lua
@@ -27,17 +27,19 @@ do
27end 27end
28print("progname: "..progname) 28print("progname: "..progname)
29 29
30local prepfile = function (s, p) 30
31 p = p or prog 31local prepfile = function (s, mod, p)
32 io.output(p) 32 mod = mod and "wb" or "w" -- mod true means binary files
33 io.write(s) 33 p = p or prog -- file to write the program
34 assert(io.close()) 34 local f = io.open(p, mod)
35 f:write(s)
36 assert(f:close())
35end 37end
36 38
37local function getoutput () 39local function getoutput ()
38 io.input(out) 40 local f = io.open(out)
39 local t = io.read("a") 41 local t = f:read("a")
40 io.input():close() 42 f:close()
41 assert(os.remove(out)) 43 assert(os.remove(out))
42 return t 44 return t
43end 45end
@@ -65,10 +67,11 @@ local function RUN (p, ...)
65 assert(os.execute(s)) 67 assert(os.execute(s))
66end 68end
67 69
70
68local function NoRun (msg, p, ...) 71local function NoRun (msg, p, ...)
69 p = string.gsub(p, "lua", '"'..progname..'"', 1) 72 p = string.gsub(p, "lua", '"'..progname..'"', 1)
70 local s = string.format(p, ...) 73 local s = string.format(p, ...)
71 s = string.format("%s 2> %s", s, out) -- will send error to 'out' 74 s = string.format("%s >%s 2>&1", s, out) -- send output and error to 'out'
72 assert(not os.execute(s)) 75 assert(not os.execute(s))
73 assert(string.find(getoutput(), msg, 1, true)) -- check error message 76 assert(string.find(getoutput(), msg, 1, true)) -- check error message
74end 77end
@@ -108,17 +111,17 @@ RUN('lua %s > %s', prog, out)
108checkout("3\n") 111checkout("3\n")
109 112
110-- bad BOMs 113-- bad BOMs
111prepfile("\xEF") 114prepfile("\xEF", true)
112NoRun("unexpected symbol", 'lua %s > %s', prog, out) 115NoRun("unexpected symbol", 'lua %s', prog)
113 116
114prepfile("\xEF\xBB") 117prepfile("\xEF\xBB", true)
115NoRun("unexpected symbol", 'lua %s > %s', prog, out) 118NoRun("unexpected symbol", 'lua %s', prog)
116 119
117prepfile("\xEFprint(3)") 120prepfile("\xEFprint(3)", true)
118NoRun("unexpected symbol", 'lua %s > %s', prog, out) 121NoRun("unexpected symbol", 'lua %s', prog)
119 122
120prepfile("\xEF\xBBprint(3)") 123prepfile("\xEF\xBBprint(3)", true)
121NoRun("unexpected symbol", 'lua %s > %s', prog, out) 124NoRun("unexpected symbol", 'lua %s', prog)
122 125
123 126
124-- test option '-' 127-- test option '-'
@@ -213,7 +216,7 @@ convert("a;b;;c")
213 216
214-- test -l over multiple libraries 217-- test -l over multiple libraries
215prepfile("print(1); a=2; return {x=15}") 218prepfile("print(1); a=2; return {x=15}")
216prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog) 219prepfile(("print(a); print(_G['%s'].x)"):format(prog), false, otherprog)
217RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out) 220RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out)
218checkout("1\n2\n15\n2\n15\n") 221checkout("1\n2\n15\n2\n15\n")
219 222
@@ -222,6 +225,13 @@ prepfile("print(str.upper'alo alo', m.max(10, 20))")
222RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out) 225RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out)
223checkout("0.0\nALO ALO\t20\n") 226checkout("0.0\nALO ALO\t20\n")
224 227
228
229-- test module names with version sufix ("libs/lib2-v2")
230RUN("env LUA_CPATH='./libs/?.so' lua -l lib2-v2 -e 'print(lib2.id())' > %s",
231 out)
232checkout("true\n")
233
234
225-- test 'arg' table 235-- test 'arg' table
226local a = [[ 236local a = [[
227 assert(#arg == 3 and arg[1] == 'a' and 237 assert(#arg == 3 and arg[1] == 'a' and
@@ -237,7 +247,7 @@ RUN('lua "-e " -- %s a b c', prog) -- "-e " runs an empty command
237 247
238-- test 'arg' availability in libraries 248-- test 'arg' availability in libraries
239prepfile"assert(arg)" 249prepfile"assert(arg)"
240prepfile("assert(arg)", otherprog) 250prepfile("assert(arg)", false, otherprog)
241RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog) 251RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog)
242 252
243-- test messing up the 'arg' table 253-- test messing up the 'arg' table
@@ -413,7 +423,7 @@ prepfile[[#comment in 1st line without \n at the end]]
413RUN('lua %s', prog) 423RUN('lua %s', prog)
414 424
415-- first-line comment with binary file 425-- first-line comment with binary file
416prepfile("#comment\n" .. string.dump(load("print(3)"))) 426prepfile("#comment\n" .. string.dump(load("print(3)")), true)
417RUN('lua %s > %s', prog, out) 427RUN('lua %s > %s', prog, out)
418checkout('3\n') 428checkout('3\n')
419 429
diff --git a/testes/pm.lua b/testes/pm.lua
index 795596d4..44454dff 100644
--- a/testes/pm.lua
+++ b/testes/pm.lua
@@ -1,6 +1,9 @@
1-- $Id: testes/pm.lua $ 1-- $Id: testes/pm.lua $
2-- See Copyright Notice in file all.lua 2-- See Copyright Notice in file all.lua
3 3
4-- UTF-8 file
5
6
4print('testing pattern matching') 7print('testing pattern matching')
5 8
6local function checkerror (msg, f, ...) 9local function checkerror (msg, f, ...)
@@ -50,6 +53,19 @@ assert(f('aLo_ALO', '%a*') == 'aLo')
50 53
51assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu") 54assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu")
52 55
56
57-- Adapt a pattern to UTF-8
58local function PU (p)
59 -- break '?' into each individual byte of a character
60 p = string.gsub(p, "(" .. utf8.charpattern .. ")%?", function (c)
61 return string.gsub(c, ".", "%0?")
62 end)
63 -- change '.' to utf-8 character patterns
64 p = string.gsub(p, "%.", utf8.charpattern)
65 return p
66end
67
68
53assert(f('aaab', 'a*') == 'aaa'); 69assert(f('aaab', 'a*') == 'aaa');
54assert(f('aaa', '^.*$') == 'aaa'); 70assert(f('aaa', '^.*$') == 'aaa');
55assert(f('aaa', 'b*') == ''); 71assert(f('aaa', 'b*') == '');
@@ -73,16 +89,16 @@ assert(f('aaa', '^.-$') == 'aaa')
73assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab') 89assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab')
74assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab') 90assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab')
75assert(f('alo xo', '.o$') == 'xo') 91assert(f('alo xo', '.o$') == 'xo')
76assert(f(' \n isto é assim', '%S%S*') == 'isto') 92assert(f(' \n isto é assim', '%S%S*') == 'isto')
77assert(f(' \n isto é assim', '%S*$') == 'assim') 93assert(f(' \n isto é assim', '%S*$') == 'assim')
78assert(f(' \n isto é assim', '[a-z]*$') == 'assim') 94assert(f(' \n isto é assim', '[a-z]*$') == 'assim')
79assert(f('um caracter ? extra', '[^%sa-z]') == '?') 95assert(f('um caracter ? extra', '[^%sa-z]') == '?')
80assert(f('', 'a?') == '') 96assert(f('', 'a?') == '')
81assert(f('á', 'á?') == 'á') 97assert(f('á', PU'á?') == 'á')
82assert(f('ábl', 'á?b?l?') == 'ábl') 98assert(f('ábl', PU'á?b?l?') == 'ábl')
83assert(f(' ábl', 'á?b?l?') == '') 99assert(f(' ábl', PU'á?b?l?') == '')
84assert(f('aa', '^aa?a?a') == 'aa') 100assert(f('aa', '^aa?a?a') == 'aa')
85assert(f(']]]áb', '[^]]') == 'á') 101assert(f(']]]áb', '[^]]+') == 'áb')
86assert(f("0alo alo", "%x*") == "0a") 102assert(f("0alo alo", "%x*") == "0a")
87assert(f("alo alo", "%C+") == "alo alo") 103assert(f("alo alo", "%C+") == "alo alo")
88print('+') 104print('+')
@@ -136,28 +152,28 @@ assert(string.match("alo xyzK", "(%w+)K") == "xyz")
136assert(string.match("254 K", "(%d*)K") == "") 152assert(string.match("254 K", "(%d*)K") == "")
137assert(string.match("alo ", "(%w*)$") == "") 153assert(string.match("alo ", "(%w*)$") == "")
138assert(not string.match("alo ", "(%w+)$")) 154assert(not string.match("alo ", "(%w+)$"))
139assert(string.find("(álo)", "%(á") == 1) 155assert(string.find("(álo)", "%(á") == 1)
140local a, b, c, d, e = string.match("âlo alo", "^(((.).).* (%w*))$") 156local a, b, c, d, e = string.match("âlo alo", PU"^(((.).). (%w*))$")
141assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil) 157assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil)
142a, b, c, d = string.match('0123456789', '(.+(.?)())') 158a, b, c, d = string.match('0123456789', '(.+(.?)())')
143assert(a == '0123456789' and b == '' and c == 11 and d == nil) 159assert(a == '0123456789' and b == '' and c == 11 and d == nil)
144print('+') 160print('+')
145 161
146assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo') 162assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo')
147assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim 163assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim
148assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim 164assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim
149assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') 165assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ')
150local t = "abç d" 166local t = "abç d"
151a, b = string.gsub(t, '(.)', '%1@') 167a, b = string.gsub(t, PU'(.)', '%1@')
152assert('@'..a == string.gsub(t, '', '@') and b == 5) 168assert(a == "a@b@ç@ @d@" and b == 5)
153a, b = string.gsub('abçd', '(.)', '%0@', 2) 169a, b = string.gsub('abçd', PU'(.)', '%0@', 2)
154assert(a == 'a@b@çd' and b == 2) 170assert(a == 'a@b@çd' and b == 2)
155assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o') 171assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o')
156assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") == 172assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") ==
157 "xyz=abc-abc=xyz") 173 "xyz=abc-abc=xyz")
158assert(string.gsub("abc", "%w", "%1%0") == "aabbcc") 174assert(string.gsub("abc", "%w", "%1%0") == "aabbcc")
159assert(string.gsub("abc", "%w+", "%0%1") == "abcabc") 175assert(string.gsub("abc", "%w+", "%0%1") == "abcabc")
160assert(string.gsub('áéí', '$', '\0óú') == 'áéí\0óú') 176assert(string.gsub('áéí', '$', '\0óú') == 'áéí\0óú')
161assert(string.gsub('', '^', 'r') == 'r') 177assert(string.gsub('', '^', 'r') == 'r')
162assert(string.gsub('', '$', 'r') == 'r') 178assert(string.gsub('', '$', 'r') == 'r')
163print('+') 179print('+')
@@ -188,8 +204,8 @@ do
188end 204end
189 205
190function f(a,b) return string.gsub(a,'.',b) end 206function f(a,b) return string.gsub(a,'.',b) end
191assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) == 207assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) ==
192 "trocar tudo em bbbbb é alalalalalal") 208 "trocar tudo em bbbbb é alalalalalal")
193 209
194local function dostring (s) return load(s, "")() or "" end 210local function dostring (s) return load(s, "")() or "" end
195assert(string.gsub("alo $a='x'$ novamente $return a$", 211assert(string.gsub("alo $a='x'$ novamente $return a$",
diff --git a/testes/sort.lua b/testes/sort.lua
index 52919b8c..40bb2d8a 100644
--- a/testes/sort.lua
+++ b/testes/sort.lua
@@ -289,7 +289,7 @@ timesort(a, limit, function(x,y) return nil end, "equal")
289 289
290for i,v in pairs(a) do assert(v == false) end 290for i,v in pairs(a) do assert(v == false) end
291 291
292AA = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"} 292AA = {"\xE1lo", "\0first :-)", "alo", "then this one", "45", "and a new"}
293table.sort(AA) 293table.sort(AA)
294check(AA) 294check(AA)
295 295
diff --git a/testes/strings.lua b/testes/strings.lua
index b033c6ab..90983edd 100644
--- a/testes/strings.lua
+++ b/testes/strings.lua
@@ -1,6 +1,9 @@
1-- $Id: testes/strings.lua $ 1-- $Id: testes/strings.lua $
2-- See Copyright Notice in file all.lua 2-- See Copyright Notice in file all.lua
3 3
4-- ISO Latin encoding
5
6
4print('testing strings and string library') 7print('testing strings and string library')
5 8
6local maxi <const> = math.maxinteger 9local maxi <const> = math.maxinteger
diff --git a/testes/utf8.lua b/testes/utf8.lua
index c5a9dd3f..efadbd5c 100644
--- a/testes/utf8.lua
+++ b/testes/utf8.lua
@@ -1,6 +1,8 @@
1-- $Id: testes/utf8.lua $ 1-- $Id: testes/utf8.lua $
2-- See Copyright Notice in file all.lua 2-- See Copyright Notice in file all.lua
3 3
4-- UTF-8 file
5
4print "testing UTF-8 library" 6print "testing UTF-8 library"
5 7
6local utf8 = require'utf8' 8local utf8 = require'utf8'