summaryrefslogtreecommitdiff
path: root/testes/literals.lua
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-12-17 14:46:37 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-12-17 14:46:37 -0200
commit063d4e4543088e7a21965bda8ee5a0f952a9029e (patch)
tree6c3f2f8e98c26f071a94a32f9f2754396a66a9de /testes/literals.lua
parente354c6355e7f48e087678ec49e340ca0696725b1 (diff)
downloadlua-5.3.5.tar.gz
lua-5.3.5.tar.bz2
lua-5.3.5.zip
Lua 5.3.5 ported to gitv5.3.5
This is the first commit for the branch Lua 5.3. All source files were copied from the official distribution of 5.3.5 in the Lua site. The test files are the same of 5.3.4. The manual came from the previous RCS repository, revision 1.167.1.2.
Diffstat (limited to 'testes/literals.lua')
-rw-r--r--testes/literals.lua302
1 files changed, 302 insertions, 0 deletions
diff --git a/testes/literals.lua b/testes/literals.lua
new file mode 100644
index 00000000..3922b3f5
--- /dev/null
+++ b/testes/literals.lua
@@ -0,0 +1,302 @@
1-- $Id: literals.lua,v 1.36 2016/11/07 13:11:28 roberto Exp $
2-- See Copyright Notice in file all.lua
3
4print('testing scanner')
5
6local debug = require "debug"
7
8
9local function dostring (x) return assert(load(x), "")() end
10
11dostring("x \v\f = \t\r 'a\0a' \v\f\f")
12assert(x == 'a\0a' and string.len(x) == 3)
13
14-- escape sequences
15assert('\n\"\'\\' == [[
16
17"'\]])
18
19assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$"))
20
21-- assume ASCII just for tests:
22assert("\09912" == 'c12')
23assert("\99ab" == 'cab')
24assert("\099" == '\99')
25assert("\099\n" == 'c\10')
26assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo')
27
28assert(010 .. 020 .. -030 == "1020-30")
29
30-- hexadecimal escapes
31assert("\x00\x05\x10\x1f\x3C\xfF\xe8" == "\0\5\16\31\60\255\232")
32
33local function lexstring (x, y, n)
34 local f = assert(load('return ' .. x ..
35 ', require"debug".getinfo(1).currentline', ''))
36 local s, l = f()
37 assert(s == y and l == n)
38end
39
40lexstring("'abc\\z \n efg'", "abcefg", 2)
41lexstring("'abc\\z \n\n\n'", "abc", 4)
42lexstring("'\\z \n\t\f\v\n'", "", 3)
43lexstring("[[\nalo\nalo\n\n]]", "alo\nalo\n\n", 5)
44lexstring("[[\nalo\ralo\n\n]]", "alo\nalo\n\n", 5)
45lexstring("[[\nalo\ralo\r\n]]", "alo\nalo\n", 4)
46lexstring("[[\ralo\n\ralo\r\n]]", "alo\nalo\n", 4)
47lexstring("[[alo]\n]alo]]", "alo]\n]alo", 2)
48
49assert("abc\z
50 def\z
51 ghi\z
52 " == 'abcdefghi')
53
54
55-- UTF-8 sequences
56assert("\u{0}\u{00000000}\x00\0" == string.char(0, 0, 0, 0))
57
58-- limits for 1-byte sequences
59assert("\u{0}\u{7F}" == "\x00\z\x7F")
60
61-- limits for 2-byte sequences
62assert("\u{80}\u{7FF}" == "\xC2\x80\z\xDF\xBF")
63
64-- limits for 3-byte sequences
65assert("\u{800}\u{FFFF}" == "\xE0\xA0\x80\z\xEF\xBF\xBF")
66
67-- limits for 4-byte sequences
68assert("\u{10000}\u{10FFFF}" == "\xF0\x90\x80\x80\z\xF4\x8F\xBF\xBF")
69
70
71-- Error in escape sequences
72local function lexerror (s, err)
73 local st, msg = load('return ' .. s, '')
74 if err ~= '<eof>' then err = err .. "'" end
75 assert(not st and string.find(msg, "near .-" .. err))
76end
77
78lexerror([["abc\x"]], [[\x"]])
79lexerror([["abc\x]], [[\x]])
80lexerror([["\x]], [[\x]])
81lexerror([["\x5"]], [[\x5"]])
82lexerror([["\x5]], [[\x5]])
83lexerror([["\xr"]], [[\xr]])
84lexerror([["\xr]], [[\xr]])
85lexerror([["\x.]], [[\x.]])
86lexerror([["\x8%"]], [[\x8%%]])
87lexerror([["\xAG]], [[\xAG]])
88lexerror([["\g"]], [[\g]])
89lexerror([["\g]], [[\g]])
90lexerror([["\."]], [[\%.]])
91
92lexerror([["\999"]], [[\999"]])
93lexerror([["xyz\300"]], [[\300"]])
94lexerror([[" \256"]], [[\256"]])
95
96-- errors in UTF-8 sequences
97lexerror([["abc\u{110000}"]], [[abc\u{110000]]) -- too large
98lexerror([["abc\u11r"]], [[abc\u1]]) -- missing '{'
99lexerror([["abc\u"]], [[abc\u"]]) -- missing '{'
100lexerror([["abc\u{11r"]], [[abc\u{11r]]) -- missing '}'
101lexerror([["abc\u{11"]], [[abc\u{11"]]) -- missing '}'
102lexerror([["abc\u{11]], [[abc\u{11]]) -- missing '}'
103lexerror([["abc\u{r"]], [[abc\u{r]]) -- no digits
104
105-- unfinished strings
106lexerror("[=[alo]]", "<eof>")
107lexerror("[=[alo]=", "<eof>")
108lexerror("[=[alo]", "<eof>")
109lexerror("'alo", "<eof>")
110lexerror("'alo \\z \n\n", "<eof>")
111lexerror("'alo \\z", "<eof>")
112lexerror([['alo \98]], "<eof>")
113
114-- valid characters in variable names
115for i = 0, 255 do
116 local s = string.char(i)
117 assert(not string.find(s, "[a-zA-Z_]") == not load(s .. "=1", ""))
118 assert(not string.find(s, "[a-zA-Z_0-9]") ==
119 not load("a" .. s .. "1 = 1", ""))
120end
121
122
123-- long variable names
124
125var1 = string.rep('a', 15000) .. '1'
126var2 = string.rep('a', 15000) .. '2'
127prog = string.format([[
128 %s = 5
129 %s = %s + 1
130 return function () return %s - %s end
131]], var1, var2, var1, var1, var2)
132local f = dostring(prog)
133assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1)
134var1, var2, f = nil
135print('+')
136
137-- escapes --
138assert("\n\t" == [[
139
140 ]])
141assert([[
142
143 $debug]] == "\n $debug")
144assert([[ [ ]] ~= [[ ] ]])
145-- long strings --
146b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
147assert(string.len(b) == 960)
148prog = [=[
149print('+')
150
151a1 = [["this is a 'string' with several 'quotes'"]]
152a2 = "'quotes'"
153
154assert(string.find(a1, a2) == 34)
155print('+')
156
157a1 = [==[temp = [[an arbitrary value]]; ]==]
158assert(load(a1))()
159assert(temp == 'an arbitrary value')
160-- long strings --
161b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
162assert(string.len(b) == 960)
163print('+')
164
165a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
16600123456789012345678901234567890123456789123456789012345678901234567890123456789
16700123456789012345678901234567890123456789123456789012345678901234567890123456789
16800123456789012345678901234567890123456789123456789012345678901234567890123456789
16900123456789012345678901234567890123456789123456789012345678901234567890123456789
17000123456789012345678901234567890123456789123456789012345678901234567890123456789
17100123456789012345678901234567890123456789123456789012345678901234567890123456789
17200123456789012345678901234567890123456789123456789012345678901234567890123456789
17300123456789012345678901234567890123456789123456789012345678901234567890123456789
17400123456789012345678901234567890123456789123456789012345678901234567890123456789
17500123456789012345678901234567890123456789123456789012345678901234567890123456789
17600123456789012345678901234567890123456789123456789012345678901234567890123456789
17700123456789012345678901234567890123456789123456789012345678901234567890123456789
17800123456789012345678901234567890123456789123456789012345678901234567890123456789
17900123456789012345678901234567890123456789123456789012345678901234567890123456789
18000123456789012345678901234567890123456789123456789012345678901234567890123456789
18100123456789012345678901234567890123456789123456789012345678901234567890123456789
18200123456789012345678901234567890123456789123456789012345678901234567890123456789
18300123456789012345678901234567890123456789123456789012345678901234567890123456789
18400123456789012345678901234567890123456789123456789012345678901234567890123456789
18500123456789012345678901234567890123456789123456789012345678901234567890123456789
18600123456789012345678901234567890123456789123456789012345678901234567890123456789
18700123456789012345678901234567890123456789123456789012345678901234567890123456789
188]]
189assert(string.len(a) == 1863)
190assert(string.sub(a, 1, 40) == string.sub(b, 1, 40))
191x = 1
192]=]
193
194print('+')
195x = nil
196dostring(prog)
197assert(x)
198
199prog = nil
200a = nil
201b = nil
202
203
204-- testing line ends
205prog = [[
206a = 1 -- a comment
207b = 2
208
209
210x = [=[
211hi
212]=]
213y = "\
214hello\r\n\
215"
216return require"debug".getinfo(1).currentline
217]]
218
219for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do
220 local prog, nn = string.gsub(prog, "\n", n)
221 assert(dostring(prog) == nn)
222 assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
223end
224
225
226-- testing comments and strings with long brackets
227a = [==[]=]==]
228assert(a == "]=")
229
230a = [==[[===[[=[]]=][====[]]===]===]==]
231assert(a == "[===[[=[]]=][====[]]===]===")
232
233a = [====[[===[[=[]]=][====[]]===]===]====]
234assert(a == "[===[[=[]]=][====[]]===]===")
235
236a = [=[]]]]]]]]]=]
237assert(a == "]]]]]]]]")
238
239
240--[===[
241x y z [==[ blu foo
242]==
243]
244]=]==]
245error error]=]===]
246
247-- generate all strings of four of these chars
248local x = {"=", "[", "]", "\n"}
249local len = 4
250local function gen (c, n)
251 if n==0 then coroutine.yield(c)
252 else
253 for _, a in pairs(x) do
254 gen(c..a, n-1)
255 end
256 end
257end
258
259for s in coroutine.wrap(function () gen("", len) end) do
260 assert(s == load("return [====[\n"..s.."]====]", "")())
261end
262
263
264-- testing decimal point locale
265if os.setlocale("pt_BR") or os.setlocale("ptb") then
266 assert(tonumber("3,4") == 3.4 and tonumber"3.4" == 3.4)
267 assert(tonumber(" -.4 ") == -0.4)
268 assert(tonumber(" +0x.41 ") == 0X0.41)
269 assert(not load("a = (3,4)"))
270 assert(assert(load("return 3.4"))() == 3.4)
271 assert(assert(load("return .4,3"))() == .4)
272 assert(assert(load("return 4."))() == 4.)
273 assert(assert(load("return 4.+.5"))() == 4.5)
274
275 assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1)
276
277 assert(tonumber"inf" == nil and tonumber"NAN" == nil)
278
279 assert(assert(load(string.format("return %q", 4.51)))() == 4.51)
280
281 local a,b = load("return 4.5.")
282 assert(string.find(b, "'4%.5%.'"))
283
284 assert(os.setlocale("C"))
285else
286 (Message or print)(
287 '\n >>> pt_BR locale not available: skipping decimal point tests <<<\n')
288end
289
290
291-- testing %q x line ends
292local s = "a string with \r and \n and \r\n and \n\r"
293local c = string.format("return %q", s)
294assert(assert(load(c))() == s)
295
296-- testing errors
297assert(not load"a = 'non-ending string")
298assert(not load"a = 'non-ending string\n'")
299assert(not load"a = '\\345'")
300assert(not load"a = [=x]")
301
302print('OK')