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 / literals.lua

345 lines · 11669 bytes Raw Blame History
1-- $Id: testes/literals.lua $
2-- See Copyright Notice in file lua.h
3
4print('testing scanner')
5
6global <const> *
7
8local debug = require "debug"
9
10
11local function dostring (x) return assert(load(x), "")() end
12
13dostring("x \v\f = \t\r 'a\0a' \v\f\f")
14assert(x == 'a\0a' and string.len(x) == 3)
15_G.x = nil
16
17-- escape sequences
18assert('\n\"\'\\' == [[
19
20"'\]])
21
22assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$"))
23
24-- assume ASCII just for tests:
25assert("\09912" == 'c12')
26assert("\99ab" == 'cab')
27assert("\099" == '\99')
28assert("\099\n" == 'c\10')
29assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo')
30
31assert(010 .. 020 .. -030 == "1020-30")
32
33-- hexadecimal escapes
34assert("\x00\x05\x10\x1f\x3C\xfF\xe8" == "\0\5\16\31\60\255\232")
35
36local function lexstring (x, y, n)
37 local f = assert(load('return ' .. x ..
38 ', require"debug".getinfo(1).currentline', ''))
39 local s, l = f()
40 assert(s == y and l == n)
41end
42
43lexstring("'abc\\z \n efg'", "abcefg", 2)
44lexstring("'abc\\z \n\n\n'", "abc", 4)
45lexstring("'\\z \n\t\f\v\n'", "", 3)
46lexstring("[[\nalo\nalo\n\n]]", "alo\nalo\n\n", 5)
47lexstring("[[\nalo\ralo\n\n]]", "alo\nalo\n\n", 5)
48lexstring("[[\nalo\ralo\r\n]]", "alo\nalo\n", 4)
49lexstring("[[\ralo\n\ralo\r\n]]", "alo\nalo\n", 4)
50lexstring("[[alo]\n]alo]]", "alo]\n]alo", 2)
51
52assert("abc\z
53 def\z
54 ghi\z
55 " == 'abcdefghi')
56
57
58-- UTF-8 sequences
59assert("\u{0}\u{00000000}\x00\0" == string.char(0, 0, 0, 0))
60
61-- limits for 1-byte sequences
62assert("\u{0}\u{7F}" == "\x00\x7F")
63
64-- limits for 2-byte sequences
65assert("\u{80}\u{7FF}" == "\xC2\x80\xDF\xBF")
66
67-- limits for 3-byte sequences
68assert("\u{800}\u{FFFF}" == "\xE0\xA0\x80\xEF\xBF\xBF")
69
70-- limits for 4-byte sequences
71assert("\u{10000}\u{1FFFFF}" == "\xF0\x90\x80\x80\xF7\xBF\xBF\xBF")
72
73-- limits for 5-byte sequences
74assert("\u{200000}\u{3FFFFFF}" == "\xF8\x88\x80\x80\x80\xFB\xBF\xBF\xBF\xBF")
75
76-- limits for 6-byte sequences
77assert("\u{4000000}\u{7FFFFFFF}" ==
78 "\xFC\x84\x80\x80\x80\x80\xFD\xBF\xBF\xBF\xBF\xBF")
79
80
81-- Error in escape sequences
82local function lexerror (s, err)
83 local st, msg = load('return ' .. s, '')
84 if err ~= '<eof>' then err = err .. "'" end
85 assert(not st and string.find(msg, "near .-" .. err))
86end
87
88lexerror([["abc\x"]], [[\x"]])
89lexerror([["abc\x]], [[\x]])
90lexerror([["\x]], [[\x]])
91lexerror([["\x5"]], [[\x5"]])
92lexerror([["\x5]], [[\x5]])
93lexerror([["\xr"]], [[\xr]])
94lexerror([["\xr]], [[\xr]])
95lexerror([["\x.]], [[\x.]])
96lexerror([["\x8%"]], [[\x8%%]])
97lexerror([["\xAG]], [[\xAG]])
98lexerror([["\g"]], [[\g]])
99lexerror([["\g]], [[\g]])
100lexerror([["\."]], [[\%.]])
101
102lexerror([["\999"]], [[\999"]])
103lexerror([["xyz\300"]], [[\300"]])
104lexerror([[" \256"]], [[\256"]])
105
106-- errors in UTF-8 sequences
107lexerror([["abc\u{100000000}"]], [[abc\u{100000000]]) -- too large
108lexerror([["abc\u11r"]], [[abc\u1]]) -- missing '{'
109lexerror([["abc\u"]], [[abc\u"]]) -- missing '{'
110lexerror([["abc\u{11r"]], [[abc\u{11r]]) -- missing '}'
111lexerror([["abc\u{11"]], [[abc\u{11"]]) -- missing '}'
112lexerror([["abc\u{11]], [[abc\u{11]]) -- missing '}'
113lexerror([["abc\u{r"]], [[abc\u{r]]) -- no digits
114
115-- unfinished strings
116lexerror("[=[alo]]", "<eof>")
117lexerror("[=[alo]=", "<eof>")
118lexerror("[=[alo]", "<eof>")
119lexerror("'alo", "<eof>")
120lexerror("'alo \\z \n\n", "<eof>")
121lexerror("'alo \\z", "<eof>")
122lexerror([['alo \98]], "<eof>")
123
124-- valid characters in variable names
125for i = 0, 255 do
126 local s = string.char(i)
127 assert(not string.find(s, "[a-zA-Z_]") == not load(s .. "=1", ""))
128 assert(not string.find(s, "[a-zA-Z_0-9]") ==
129 not load("a" .. s .. "1 = 1", ""))
130end
131
132
133-- long variable names
134
135local var1 = string.rep('a', 15000) .. '1'
136local var2 = string.rep('a', 15000) .. '2'
137local prog = string.format([[
138 %s = 5
139 %s = %s + 1
140 return function () return %s - %s end
141]], var1, var2, var1, var1, var2)
142local f = dostring(prog)
143assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1)
144_G[var1], _G[var2] = nil
145print('+')
146
147-- escapes --
148assert("\n\t" == [[
149
150 ]])
151assert([[
152
153 $debug]] == "\n $debug")
154assert([[ [ ]] ~= [[ ] ]])
155-- long strings --
156local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
157assert(string.len(b) == 960)
158prog = [=[
159print('+')
160
161local a1 = [["this is a 'string' with several 'quotes'"]]
162local a2 = "'quotes'"
163
164assert(string.find(a1, a2) == 34)
165print('+')
166
167a1 = [==[temp = [[an arbitrary value]]; ]==]
168assert(load(a1))()
169assert(temp == 'an arbitrary value')
170_G.temp = nil
171-- long strings --
172local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
173assert(string.len(b) == 960)
174print('+')
175
176local a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
17700123456789012345678901234567890123456789123456789012345678901234567890123456789
17800123456789012345678901234567890123456789123456789012345678901234567890123456789
17900123456789012345678901234567890123456789123456789012345678901234567890123456789
18000123456789012345678901234567890123456789123456789012345678901234567890123456789
18100123456789012345678901234567890123456789123456789012345678901234567890123456789
18200123456789012345678901234567890123456789123456789012345678901234567890123456789
18300123456789012345678901234567890123456789123456789012345678901234567890123456789
18400123456789012345678901234567890123456789123456789012345678901234567890123456789
18500123456789012345678901234567890123456789123456789012345678901234567890123456789
18600123456789012345678901234567890123456789123456789012345678901234567890123456789
18700123456789012345678901234567890123456789123456789012345678901234567890123456789
18800123456789012345678901234567890123456789123456789012345678901234567890123456789
18900123456789012345678901234567890123456789123456789012345678901234567890123456789
19000123456789012345678901234567890123456789123456789012345678901234567890123456789
19100123456789012345678901234567890123456789123456789012345678901234567890123456789
19200123456789012345678901234567890123456789123456789012345678901234567890123456789
19300123456789012345678901234567890123456789123456789012345678901234567890123456789
19400123456789012345678901234567890123456789123456789012345678901234567890123456789
19500123456789012345678901234567890123456789123456789012345678901234567890123456789
19600123456789012345678901234567890123456789123456789012345678901234567890123456789
19700123456789012345678901234567890123456789123456789012345678901234567890123456789
19800123456789012345678901234567890123456789123456789012345678901234567890123456789
199]]
200assert(string.len(a) == 1863)
201assert(string.sub(a, 1, 40) == string.sub(b, 1, 40))
202x = 1
203]=]
204
205print('+')
206_G.x = nil
207dostring(prog)
208assert(x)
209_G.x = nil
210
211
212
213do -- reuse of long strings
214
215 -- get the address of a string
216 local function getadd (s) return string.format("%p", s) end
217
218 local s1 <const> = "01234567890123456789012345678901234567890123456789"
219 local s2 <const> = "01234567890123456789012345678901234567890123456789"
220 local s3 = "01234567890123456789012345678901234567890123456789"
221 local function foo() return s1 end
222 local function foo1() return s3 end
223 local function foo2()
224 return "01234567890123456789012345678901234567890123456789"
225 end
226 local a1 = getadd(s1)
227 assert(a1 == getadd(s2))
228 assert(a1 == getadd(foo()))
229 assert(a1 == getadd(foo1()))
230 assert(a1 == getadd(foo2()))
231
232 local sd = "0123456789" .. "0123456789012345678901234567890123456789"
233 assert(sd == s1 and getadd(sd) ~= a1)
234end
235
236
237-- testing line ends
238prog = [[
239local a = 1 -- a comment
240local b = 2
241
242
243x = [=[
244hi
245]=]
246y = "\
247hello\r\n\
248"
249return require"debug".getinfo(1).currentline
250]]
251
252for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do
253 local prog, nn = string.gsub(prog, "\n", n)
254 assert(dostring(prog) == nn)
255 assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
256end
257_G.x, _G.y = nil
258
259
260-- testing comments and strings with long brackets
261local a = [==[]=]==]
262assert(a == "]=")
263
264a = [==[[===[[=[]]=][====[]]===]===]==]
265assert(a == "[===[[=[]]=][====[]]===]===")
266
267a = [====[[===[[=[]]=][====[]]===]===]====]
268assert(a == "[===[[=[]]=][====[]]===]===")
269
270a = [=[]]]]]]]]]=]
271assert(a == "]]]]]]]]")
272
273
274--[===[
275x y z [==[ blu foo
276]==
277]
278]=]==]
279error error]=]===]
280
281-- generate all strings of four of these chars
282local x = {"=", "[", "]", "\n"}
283local len = 4
284local function gen (c, n)
285 if n==0 then coroutine.yield(c)
286 else
287 for _, a in pairs(x) do
288 gen(c..a, n-1)
289 end
290 end
291end
292
293for s in coroutine.wrap(function () gen("", len) end) do
294 assert(s == load("return [====[\n"..s.."]====]", "")())
295end
296
297
298-- testing decimal point locale
299if os.setlocale("pt_BR") or os.setlocale("ptb") then
300 assert(tonumber("3,4") == 3.4 and tonumber"3.4" == 3.4)
301 assert(tonumber(" -.4 ") == -0.4)
302 assert(tonumber(" +0x.41 ") == 0X0.41)
303 assert(not load("a = (3,4)"))
304 assert(assert(load("return 3.4"))() == 3.4)
305 assert(assert(load("return .4,3"))() == .4)
306 assert(assert(load("return 4."))() == 4.)
307 assert(assert(load("return 4.+.5"))() == 4.5)
308
309 assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1)
310
311 assert(not tonumber"inf" and not tonumber"NAN")
312
313 assert(assert(load(string.format("return %q", 4.51)))() == 4.51)
314
315 local a,b = load("return 4.5.")
316 assert(string.find(b, "'4%.5%.'"))
317
318 assert(os.setlocale("C"))
319else
320 (Message or print)(
321 '\n >>> pt_BR locale not available: skipping decimal point tests <<<\n')
322end
323
324
325-- testing %q x line ends
326local s = "a string with \r and \n and \r\n and \n\r"
327local c = string.format("return %q", s)
328assert(assert(load(c))() == s)
329
330-- testing errors
331assert(not load"a = 'non-ending string")
332assert(not load"a = 'non-ending string\n'")
333assert(not load"a = '\\345'")
334assert(not load"a = [=x]")
335
336local function malformednum (n, exp)
337 local s, msg = load("return " .. n)
338 assert(not s and string.find(msg, exp))
339end
340
341malformednum("0xe-", "near <eof>")
342malformednum("0xep-p", "malformed number")
343malformednum("1print()", "malformed number")
344
345print('OK')