lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- $Id: testes/literals.lua $ |
| 2 | -- See Copyright Notice in file lua.h |
| 3 | |
| 4 | print('testing scanner') |
| 5 | |
| 6 | global <const> * |
| 7 | |
| 8 | local debug = require "debug" |
| 9 | |
| 10 | |
| 11 | local function dostring (x) return assert(load(x), "")() end |
| 12 | |
| 13 | dostring("x \v\f = \t\r 'a\0a' \v\f\f") |
| 14 | assert(x == 'a\0a' and string.len(x) == 3) |
| 15 | _G.x = nil |
| 16 | |
| 17 | -- escape sequences |
| 18 | assert('\n\"\'\\' == [[ |
| 19 | |
| 20 | "'\]]) |
| 21 | |
| 22 | assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$")) |
| 23 | |
| 24 | -- assume ASCII just for tests: |
| 25 | assert("\09912" == 'c12') |
| 26 | assert("\99ab" == 'cab') |
| 27 | assert("\099" == '\99') |
| 28 | assert("\099\n" == 'c\10') |
| 29 | assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo') |
| 30 | |
| 31 | assert(010 .. 020 .. -030 == "1020-30") |
| 32 | |
| 33 | -- hexadecimal escapes |
| 34 | assert("\x00\x05\x10\x1f\x3C\xfF\xe8" == "\0\5\16\31\60\255\232") |
| 35 | |
| 36 | local 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) |
| 41 | end |
| 42 | |
| 43 | lexstring("'abc\\z \n efg'", "abcefg", 2) |
| 44 | lexstring("'abc\\z \n\n\n'", "abc", 4) |
| 45 | lexstring("'\\z \n\t\f\v\n'", "", 3) |
| 46 | lexstring("[[\nalo\nalo\n\n]]", "alo\nalo\n\n", 5) |
| 47 | lexstring("[[\nalo\ralo\n\n]]", "alo\nalo\n\n", 5) |
| 48 | lexstring("[[\nalo\ralo\r\n]]", "alo\nalo\n", 4) |
| 49 | lexstring("[[\ralo\n\ralo\r\n]]", "alo\nalo\n", 4) |
| 50 | lexstring("[[alo]\n]alo]]", "alo]\n]alo", 2) |
| 51 | |
| 52 | assert("abc\z |
| 53 | def\z |
| 54 | ghi\z |
| 55 | " == 'abcdefghi') |
| 56 | |
| 57 | |
| 58 | -- UTF-8 sequences |
| 59 | assert("\u{0}\u{00000000}\x00\0" == string.char(0, 0, 0, 0)) |
| 60 | |
| 61 | -- limits for 1-byte sequences |
| 62 | assert("\u{0}\u{7F}" == "\x00\x7F") |
| 63 | |
| 64 | -- limits for 2-byte sequences |
| 65 | assert("\u{80}\u{7FF}" == "\xC2\x80\xDF\xBF") |
| 66 | |
| 67 | -- limits for 3-byte sequences |
| 68 | assert("\u{800}\u{FFFF}" == "\xE0\xA0\x80\xEF\xBF\xBF") |
| 69 | |
| 70 | -- limits for 4-byte sequences |
| 71 | assert("\u{10000}\u{1FFFFF}" == "\xF0\x90\x80\x80\xF7\xBF\xBF\xBF") |
| 72 | |
| 73 | -- limits for 5-byte sequences |
| 74 | assert("\u{200000}\u{3FFFFFF}" == "\xF8\x88\x80\x80\x80\xFB\xBF\xBF\xBF\xBF") |
| 75 | |
| 76 | -- limits for 6-byte sequences |
| 77 | assert("\u{4000000}\u{7FFFFFFF}" == |
| 78 | "\xFC\x84\x80\x80\x80\x80\xFD\xBF\xBF\xBF\xBF\xBF") |
| 79 | |
| 80 | |
| 81 | -- Error in escape sequences |
| 82 | local 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)) |
| 86 | end |
| 87 | |
| 88 | lexerror([["abc\x"]], [[\x"]]) |
| 89 | lexerror([["abc\x]], [[\x]]) |
| 90 | lexerror([["\x]], [[\x]]) |
| 91 | lexerror([["\x5"]], [[\x5"]]) |
| 92 | lexerror([["\x5]], [[\x5]]) |
| 93 | lexerror([["\xr"]], [[\xr]]) |
| 94 | lexerror([["\xr]], [[\xr]]) |
| 95 | lexerror([["\x.]], [[\x.]]) |
| 96 | lexerror([["\x8%"]], [[\x8%%]]) |
| 97 | lexerror([["\xAG]], [[\xAG]]) |
| 98 | lexerror([["\g"]], [[\g]]) |
| 99 | lexerror([["\g]], [[\g]]) |
| 100 | lexerror([["\."]], [[\%.]]) |
| 101 | |
| 102 | lexerror([["\999"]], [[\999"]]) |
| 103 | lexerror([["xyz\300"]], [[\300"]]) |
| 104 | lexerror([[" \256"]], [[\256"]]) |
| 105 | |
| 106 | -- errors in UTF-8 sequences |
| 107 | lexerror([["abc\u{100000000}"]], [[abc\u{100000000]]) -- too large |
| 108 | lexerror([["abc\u11r"]], [[abc\u1]]) -- missing '{' |
| 109 | lexerror([["abc\u"]], [[abc\u"]]) -- missing '{' |
| 110 | lexerror([["abc\u{11r"]], [[abc\u{11r]]) -- missing '}' |
| 111 | lexerror([["abc\u{11"]], [[abc\u{11"]]) -- missing '}' |
| 112 | lexerror([["abc\u{11]], [[abc\u{11]]) -- missing '}' |
| 113 | lexerror([["abc\u{r"]], [[abc\u{r]]) -- no digits |
| 114 | |
| 115 | -- unfinished strings |
| 116 | lexerror("[=[alo]]", "<eof>") |
| 117 | lexerror("[=[alo]=", "<eof>") |
| 118 | lexerror("[=[alo]", "<eof>") |
| 119 | lexerror("'alo", "<eof>") |
| 120 | lexerror("'alo \\z \n\n", "<eof>") |
| 121 | lexerror("'alo \\z", "<eof>") |
| 122 | lexerror([['alo \98]], "<eof>") |
| 123 | |
| 124 | -- valid characters in variable names |
| 125 | for 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", "")) |
| 130 | end |
| 131 | |
| 132 | |
| 133 | -- long variable names |
| 134 | |
| 135 | local var1 = string.rep('a', 15000) .. '1' |
| 136 | local var2 = string.rep('a', 15000) .. '2' |
| 137 | local prog = string.format([[ |
| 138 | %s = 5 |
| 139 | %s = %s + 1 |
| 140 | return function () return %s - %s end |
| 141 | ]], var1, var2, var1, var1, var2) |
| 142 | local f = dostring(prog) |
| 143 | assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1) |
| 144 | _G[var1], _G[var2] = nil |
| 145 | print('+') |
| 146 | |
| 147 | -- escapes -- |
| 148 | assert("\n\t" == [[ |
| 149 | |
| 150 | ]]) |
| 151 | assert([[ |
| 152 | |
| 153 | $debug]] == "\n $debug") |
| 154 | assert([[ [ ]] ~= [[ ] ]]) |
| 155 | -- long strings -- |
| 156 | local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" |
| 157 | assert(string.len(b) == 960) |
| 158 | prog = [=[ |
| 159 | print('+') |
| 160 | |
| 161 | local a1 = [["this is a 'string' with several 'quotes'"]] |
| 162 | local a2 = "'quotes'" |
| 163 | |
| 164 | assert(string.find(a1, a2) == 34) |
| 165 | print('+') |
| 166 | |
| 167 | a1 = [==[temp = [[an arbitrary value]]; ]==] |
| 168 | assert(load(a1))() |
| 169 | assert(temp == 'an arbitrary value') |
| 170 | _G.temp = nil |
| 171 | -- long strings -- |
| 172 | local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" |
| 173 | assert(string.len(b) == 960) |
| 174 | print('+') |
| 175 | |
| 176 | local a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 177 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 178 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 179 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 180 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 181 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 182 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 183 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 184 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 185 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 186 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 187 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 188 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 189 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 190 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 191 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 192 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 193 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 194 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 195 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 196 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 197 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 198 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 |
| 199 | ]] |
| 200 | assert(string.len(a) == 1863) |
| 201 | assert(string.sub(a, 1, 40) == string.sub(b, 1, 40)) |
| 202 | x = 1 |
| 203 | ]=] |
| 204 | |
| 205 | print('+') |
| 206 | _G.x = nil |
| 207 | dostring(prog) |
| 208 | assert(x) |
| 209 | _G.x = nil |
| 210 | |
| 211 | |
| 212 | |
| 213 | do -- 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) |
| 234 | end |
| 235 | |
| 236 | |
| 237 | -- testing line ends |
| 238 | prog = [[ |
| 239 | local a = 1 -- a comment |
| 240 | local b = 2 |
| 241 | |
| 242 | |
| 243 | x = [=[ |
| 244 | hi |
| 245 | ]=] |
| 246 | y = "\ |
| 247 | hello\r\n\ |
| 248 | " |
| 249 | return require"debug".getinfo(1).currentline |
| 250 | ]] |
| 251 | |
| 252 | for _, 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") |
| 256 | end |
| 257 | _G.x, _G.y = nil |
| 258 | |
| 259 | |
| 260 | -- testing comments and strings with long brackets |
| 261 | local a = [==[]=]==] |
| 262 | assert(a == "]=") |
| 263 | |
| 264 | a = [==[[===[[=[]]=][====[]]===]===]==] |
| 265 | assert(a == "[===[[=[]]=][====[]]===]===") |
| 266 | |
| 267 | a = [====[[===[[=[]]=][====[]]===]===]====] |
| 268 | assert(a == "[===[[=[]]=][====[]]===]===") |
| 269 | |
| 270 | a = [=[]]]]]]]]]=] |
| 271 | assert(a == "]]]]]]]]") |
| 272 | |
| 273 | |
| 274 | --[===[ |
| 275 | x y z [==[ blu foo |
| 276 | ]== |
| 277 | ] |
| 278 | ]=]==] |
| 279 | error error]=]===] |
| 280 | |
| 281 | -- generate all strings of four of these chars |
| 282 | local x = {"=", "[", "]", "\n"} |
| 283 | local len = 4 |
| 284 | local 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 |
| 291 | end |
| 292 | |
| 293 | for s in coroutine.wrap(function () gen("", len) end) do |
| 294 | assert(s == load("return [====[\n"..s.."]====]", "")()) |
| 295 | end |
| 296 | |
| 297 | |
| 298 | -- testing decimal point locale |
| 299 | if 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")) |
| 319 | else |
| 320 | (Message or print)( |
| 321 | '\n >>> pt_BR locale not available: skipping decimal point tests <<<\n') |
| 322 | end |
| 323 | |
| 324 | |
| 325 | -- testing %q x line ends |
| 326 | local s = "a string with \r and \n and \r\n and \n\r" |
| 327 | local c = string.format("return %q", s) |
| 328 | assert(assert(load(c))() == s) |
| 329 | |
| 330 | -- testing errors |
| 331 | assert(not load"a = 'non-ending string") |
| 332 | assert(not load"a = 'non-ending string\n'") |
| 333 | assert(not load"a = '\\345'") |
| 334 | assert(not load"a = [=x]") |
| 335 | |
| 336 | local function malformednum (n, exp) |
| 337 | local s, msg = load("return " .. n) |
| 338 | assert(not s and string.find(msg, exp)) |
| 339 | end |
| 340 | |
| 341 | malformednum("0xe-", "near <eof>") |
| 342 | malformednum("0xep-p", "malformed number") |
| 343 | malformednum("1print()", "malformed number") |
| 344 | |
| 345 | print('OK') |