diff options
Diffstat (limited to 'testes/literals.lua')
| -rw-r--r-- | testes/literals.lua | 302 |
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 | |||
| 4 | print('testing scanner') | ||
| 5 | |||
| 6 | local debug = require "debug" | ||
| 7 | |||
| 8 | |||
| 9 | local function dostring (x) return assert(load(x), "")() end | ||
| 10 | |||
| 11 | dostring("x \v\f = \t\r 'a\0a' \v\f\f") | ||
| 12 | assert(x == 'a\0a' and string.len(x) == 3) | ||
| 13 | |||
| 14 | -- escape sequences | ||
| 15 | assert('\n\"\'\\' == [[ | ||
| 16 | |||
| 17 | "'\]]) | ||
| 18 | |||
| 19 | assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$")) | ||
| 20 | |||
| 21 | -- assume ASCII just for tests: | ||
| 22 | assert("\09912" == 'c12') | ||
| 23 | assert("\99ab" == 'cab') | ||
| 24 | assert("\099" == '\99') | ||
| 25 | assert("\099\n" == 'c\10') | ||
| 26 | assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo') | ||
| 27 | |||
| 28 | assert(010 .. 020 .. -030 == "1020-30") | ||
| 29 | |||
| 30 | -- hexadecimal escapes | ||
| 31 | assert("\x00\x05\x10\x1f\x3C\xfF\xe8" == "\0\5\16\31\60\255\232") | ||
| 32 | |||
| 33 | local 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) | ||
| 38 | end | ||
| 39 | |||
| 40 | lexstring("'abc\\z \n efg'", "abcefg", 2) | ||
| 41 | lexstring("'abc\\z \n\n\n'", "abc", 4) | ||
| 42 | lexstring("'\\z \n\t\f\v\n'", "", 3) | ||
| 43 | lexstring("[[\nalo\nalo\n\n]]", "alo\nalo\n\n", 5) | ||
| 44 | lexstring("[[\nalo\ralo\n\n]]", "alo\nalo\n\n", 5) | ||
| 45 | lexstring("[[\nalo\ralo\r\n]]", "alo\nalo\n", 4) | ||
| 46 | lexstring("[[\ralo\n\ralo\r\n]]", "alo\nalo\n", 4) | ||
| 47 | lexstring("[[alo]\n]alo]]", "alo]\n]alo", 2) | ||
| 48 | |||
| 49 | assert("abc\z | ||
| 50 | def\z | ||
| 51 | ghi\z | ||
| 52 | " == 'abcdefghi') | ||
| 53 | |||
| 54 | |||
| 55 | -- UTF-8 sequences | ||
| 56 | assert("\u{0}\u{00000000}\x00\0" == string.char(0, 0, 0, 0)) | ||
| 57 | |||
| 58 | -- limits for 1-byte sequences | ||
| 59 | assert("\u{0}\u{7F}" == "\x00\z\x7F") | ||
| 60 | |||
| 61 | -- limits for 2-byte sequences | ||
| 62 | assert("\u{80}\u{7FF}" == "\xC2\x80\z\xDF\xBF") | ||
| 63 | |||
| 64 | -- limits for 3-byte sequences | ||
| 65 | assert("\u{800}\u{FFFF}" == "\xE0\xA0\x80\z\xEF\xBF\xBF") | ||
| 66 | |||
| 67 | -- limits for 4-byte sequences | ||
| 68 | assert("\u{10000}\u{10FFFF}" == "\xF0\x90\x80\x80\z\xF4\x8F\xBF\xBF") | ||
| 69 | |||
| 70 | |||
| 71 | -- Error in escape sequences | ||
| 72 | local 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)) | ||
| 76 | end | ||
| 77 | |||
| 78 | lexerror([["abc\x"]], [[\x"]]) | ||
| 79 | lexerror([["abc\x]], [[\x]]) | ||
| 80 | lexerror([["\x]], [[\x]]) | ||
| 81 | lexerror([["\x5"]], [[\x5"]]) | ||
| 82 | lexerror([["\x5]], [[\x5]]) | ||
| 83 | lexerror([["\xr"]], [[\xr]]) | ||
| 84 | lexerror([["\xr]], [[\xr]]) | ||
| 85 | lexerror([["\x.]], [[\x.]]) | ||
| 86 | lexerror([["\x8%"]], [[\x8%%]]) | ||
| 87 | lexerror([["\xAG]], [[\xAG]]) | ||
| 88 | lexerror([["\g"]], [[\g]]) | ||
| 89 | lexerror([["\g]], [[\g]]) | ||
| 90 | lexerror([["\."]], [[\%.]]) | ||
| 91 | |||
| 92 | lexerror([["\999"]], [[\999"]]) | ||
| 93 | lexerror([["xyz\300"]], [[\300"]]) | ||
| 94 | lexerror([[" \256"]], [[\256"]]) | ||
| 95 | |||
| 96 | -- errors in UTF-8 sequences | ||
| 97 | lexerror([["abc\u{110000}"]], [[abc\u{110000]]) -- too large | ||
| 98 | lexerror([["abc\u11r"]], [[abc\u1]]) -- missing '{' | ||
| 99 | lexerror([["abc\u"]], [[abc\u"]]) -- missing '{' | ||
| 100 | lexerror([["abc\u{11r"]], [[abc\u{11r]]) -- missing '}' | ||
| 101 | lexerror([["abc\u{11"]], [[abc\u{11"]]) -- missing '}' | ||
| 102 | lexerror([["abc\u{11]], [[abc\u{11]]) -- missing '}' | ||
| 103 | lexerror([["abc\u{r"]], [[abc\u{r]]) -- no digits | ||
| 104 | |||
| 105 | -- unfinished strings | ||
| 106 | lexerror("[=[alo]]", "<eof>") | ||
| 107 | lexerror("[=[alo]=", "<eof>") | ||
| 108 | lexerror("[=[alo]", "<eof>") | ||
| 109 | lexerror("'alo", "<eof>") | ||
| 110 | lexerror("'alo \\z \n\n", "<eof>") | ||
| 111 | lexerror("'alo \\z", "<eof>") | ||
| 112 | lexerror([['alo \98]], "<eof>") | ||
| 113 | |||
| 114 | -- valid characters in variable names | ||
| 115 | for 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", "")) | ||
| 120 | end | ||
| 121 | |||
| 122 | |||
| 123 | -- long variable names | ||
| 124 | |||
| 125 | var1 = string.rep('a', 15000) .. '1' | ||
| 126 | var2 = string.rep('a', 15000) .. '2' | ||
| 127 | prog = string.format([[ | ||
| 128 | %s = 5 | ||
| 129 | %s = %s + 1 | ||
| 130 | return function () return %s - %s end | ||
| 131 | ]], var1, var2, var1, var1, var2) | ||
| 132 | local f = dostring(prog) | ||
| 133 | assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1) | ||
| 134 | var1, var2, f = nil | ||
| 135 | print('+') | ||
| 136 | |||
| 137 | -- escapes -- | ||
| 138 | assert("\n\t" == [[ | ||
| 139 | |||
| 140 | ]]) | ||
| 141 | assert([[ | ||
| 142 | |||
| 143 | $debug]] == "\n $debug") | ||
| 144 | assert([[ [ ]] ~= [[ ] ]]) | ||
| 145 | -- long strings -- | ||
| 146 | b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" | ||
| 147 | assert(string.len(b) == 960) | ||
| 148 | prog = [=[ | ||
| 149 | print('+') | ||
| 150 | |||
| 151 | a1 = [["this is a 'string' with several 'quotes'"]] | ||
| 152 | a2 = "'quotes'" | ||
| 153 | |||
| 154 | assert(string.find(a1, a2) == 34) | ||
| 155 | print('+') | ||
| 156 | |||
| 157 | a1 = [==[temp = [[an arbitrary value]]; ]==] | ||
| 158 | assert(load(a1))() | ||
| 159 | assert(temp == 'an arbitrary value') | ||
| 160 | -- long strings -- | ||
| 161 | b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" | ||
| 162 | assert(string.len(b) == 960) | ||
| 163 | print('+') | ||
| 164 | |||
| 165 | a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 166 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 167 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 168 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 169 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 170 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 171 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 172 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 173 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 174 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 175 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 176 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 177 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 178 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 179 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 180 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 181 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 182 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 183 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 184 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 185 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 186 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 187 | 00123456789012345678901234567890123456789123456789012345678901234567890123456789 | ||
| 188 | ]] | ||
| 189 | assert(string.len(a) == 1863) | ||
| 190 | assert(string.sub(a, 1, 40) == string.sub(b, 1, 40)) | ||
| 191 | x = 1 | ||
| 192 | ]=] | ||
| 193 | |||
| 194 | print('+') | ||
| 195 | x = nil | ||
| 196 | dostring(prog) | ||
| 197 | assert(x) | ||
| 198 | |||
| 199 | prog = nil | ||
| 200 | a = nil | ||
| 201 | b = nil | ||
| 202 | |||
| 203 | |||
| 204 | -- testing line ends | ||
| 205 | prog = [[ | ||
| 206 | a = 1 -- a comment | ||
| 207 | b = 2 | ||
| 208 | |||
| 209 | |||
| 210 | x = [=[ | ||
| 211 | hi | ||
| 212 | ]=] | ||
| 213 | y = "\ | ||
| 214 | hello\r\n\ | ||
| 215 | " | ||
| 216 | return require"debug".getinfo(1).currentline | ||
| 217 | ]] | ||
| 218 | |||
| 219 | for _, 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") | ||
| 223 | end | ||
| 224 | |||
| 225 | |||
| 226 | -- testing comments and strings with long brackets | ||
| 227 | a = [==[]=]==] | ||
| 228 | assert(a == "]=") | ||
| 229 | |||
| 230 | a = [==[[===[[=[]]=][====[]]===]===]==] | ||
| 231 | assert(a == "[===[[=[]]=][====[]]===]===") | ||
| 232 | |||
| 233 | a = [====[[===[[=[]]=][====[]]===]===]====] | ||
| 234 | assert(a == "[===[[=[]]=][====[]]===]===") | ||
| 235 | |||
| 236 | a = [=[]]]]]]]]]=] | ||
| 237 | assert(a == "]]]]]]]]") | ||
| 238 | |||
| 239 | |||
| 240 | --[===[ | ||
| 241 | x y z [==[ blu foo | ||
| 242 | ]== | ||
| 243 | ] | ||
| 244 | ]=]==] | ||
| 245 | error error]=]===] | ||
| 246 | |||
| 247 | -- generate all strings of four of these chars | ||
| 248 | local x = {"=", "[", "]", "\n"} | ||
| 249 | local len = 4 | ||
| 250 | local 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 | ||
| 257 | end | ||
| 258 | |||
| 259 | for s in coroutine.wrap(function () gen("", len) end) do | ||
| 260 | assert(s == load("return [====[\n"..s.."]====]", "")()) | ||
| 261 | end | ||
| 262 | |||
| 263 | |||
| 264 | -- testing decimal point locale | ||
| 265 | if 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")) | ||
| 285 | else | ||
| 286 | (Message or print)( | ||
| 287 | '\n >>> pt_BR locale not available: skipping decimal point tests <<<\n') | ||
| 288 | end | ||
| 289 | |||
| 290 | |||
| 291 | -- testing %q x line ends | ||
| 292 | local s = "a string with \r and \n and \r\n and \n\r" | ||
| 293 | local c = string.format("return %q", s) | ||
| 294 | assert(assert(load(c))() == s) | ||
| 295 | |||
| 296 | -- testing errors | ||
| 297 | assert(not load"a = 'non-ending string") | ||
| 298 | assert(not load"a = 'non-ending string\n'") | ||
| 299 | assert(not load"a = '\\345'") | ||
| 300 | assert(not load"a = [=x]") | ||
| 301 | |||
| 302 | print('OK') | ||
