lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- $Id: testes/attrib.lua $ |
| 2 | -- See Copyright Notice in file lua.h |
| 3 | |
| 4 | print "testing require" |
| 5 | |
| 6 | assert(require"string" == string) |
| 7 | assert(require"math" == math) |
| 8 | assert(require"table" == table) |
| 9 | assert(require"io" == io) |
| 10 | assert(require"os" == os) |
| 11 | assert(require"coroutine" == coroutine) |
| 12 | |
| 13 | assert(type(package.path) == "string") |
| 14 | assert(type(package.cpath) == "string") |
| 15 | assert(type(package.loaded) == "table") |
| 16 | assert(type(package.preload) == "table") |
| 17 | |
| 18 | assert(type(package.config) == "string") |
| 19 | print("package config: "..string.gsub(package.config, "\n", "|")) |
| 20 | |
| 21 | do |
| 22 | -- create a path with 'max' templates, |
| 23 | -- each with 1-10 repetitions of '?' |
| 24 | local max = _soft and 100 or 2000 |
| 25 | local t = {} |
| 26 | for i = 1,max do t[i] = string.rep("?", i%10 + 1) end |
| 27 | t[#t + 1] = ";" -- empty template |
| 28 | local path = table.concat(t, ";") |
| 29 | -- use that path in a search |
| 30 | local s, err = package.searchpath("xuxu", path) |
| 31 | -- search fails; check that message has an occurrence of |
| 32 | -- '??????????' with ? replaced by xuxu and at least 'max' lines |
| 33 | assert(not s and |
| 34 | string.find(err, string.rep("xuxu", 10)) and |
| 35 | #string.gsub(err, "[^\n]", "") >= max) |
| 36 | -- path with one very long template |
| 37 | local path = string.rep("?", max) |
| 38 | local s, err = package.searchpath("xuxu", path) |
| 39 | assert(not s and string.find(err, string.rep('xuxu', max))) |
| 40 | end |
| 41 | |
| 42 | do |
| 43 | local oldpath = package.path |
| 44 | package.path = {} |
| 45 | local s, err = pcall(require, "no-such-file") |
| 46 | assert(not s and string.find(err, "package.path")) |
| 47 | package.path = oldpath |
| 48 | end |
| 49 | |
| 50 | |
| 51 | do print"testing 'require' message" |
| 52 | local oldpath = package.path |
| 53 | local oldcpath = package.cpath |
| 54 | |
| 55 | package.path = "?.lua;?/?" |
| 56 | package.cpath = "?.so;?/init" |
| 57 | |
| 58 | local st, msg = pcall(require, 'XXX') |
| 59 | |
| 60 | local expected = [[module 'XXX' not found: |
| 61 | no field package.preload['XXX'] |
| 62 | no file 'XXX.lua' |
| 63 | no file 'XXX/XXX' |
| 64 | no file 'XXX.so' |
| 65 | no file 'XXX/init']] |
| 66 | |
| 67 | assert(msg == expected) |
| 68 | |
| 69 | package.path = oldpath |
| 70 | package.cpath = oldcpath |
| 71 | end |
| 72 | |
| 73 | print('+') |
| 74 | |
| 75 | |
| 76 | -- The next tests for 'require' assume some specific directories and |
| 77 | -- libraries. |
| 78 | |
| 79 | if not _port then --[ |
| 80 | |
| 81 | local dirsep = string.match(package.config, "^([^\n]+)\n") |
| 82 | |
| 83 | -- auxiliary directory with C modules and temporary files |
| 84 | local DIR = "libs" .. dirsep |
| 85 | |
| 86 | -- prepend DIR to a name and correct directory separators |
| 87 | local function D (x) |
| 88 | local x = string.gsub(x, "/", dirsep) |
| 89 | return DIR .. x |
| 90 | end |
| 91 | |
| 92 | -- prepend DIR and pospend proper C lib. extension to a name |
| 93 | local function DC (x) |
| 94 | local ext = (dirsep == '\\') and ".dll" or ".so" |
| 95 | return D(x .. ext) |
| 96 | end |
| 97 | |
| 98 | |
| 99 | local function createfiles (files, preextras, posextras) |
| 100 | for n,c in pairs(files) do |
| 101 | io.output(D(n)) |
| 102 | io.write(string.format(preextras, n)) |
| 103 | io.write(c) |
| 104 | io.write(string.format(posextras, n)) |
| 105 | io.close(io.output()) |
| 106 | end |
| 107 | end |
| 108 | |
| 109 | local function removefiles (files) |
| 110 | for n in pairs(files) do |
| 111 | os.remove(D(n)) |
| 112 | end |
| 113 | end |
| 114 | |
| 115 | local files = { |
| 116 | ["names.lua"] = "do return {...} end\n", |
| 117 | ["err.lua"] = "B = 15; a = a + 1;", |
| 118 | ["synerr.lua"] = "B =", |
| 119 | ["A.lua"] = "", |
| 120 | ["B.lua"] = "assert(...=='B');require 'A'", |
| 121 | ["A.lc"] = "", |
| 122 | ["A"] = "", |
| 123 | ["L"] = "", |
| 124 | ["XXxX"] = "", |
| 125 | ["C.lua"] = "package.loaded[...] = 25; require'C'", |
| 126 | } |
| 127 | |
| 128 | AA = nil |
| 129 | local extras = [[ |
| 130 | NAME = '%s' |
| 131 | REQUIRED = ... |
| 132 | return AA]] |
| 133 | |
| 134 | createfiles(files, "", extras) |
| 135 | |
| 136 | -- testing explicit "dir" separator in 'searchpath' |
| 137 | assert(package.searchpath("C.lua", D"?", "", "") == D"C.lua") |
| 138 | assert(package.searchpath("C.lua", D"?", ".", ".") == D"C.lua") |
| 139 | assert(package.searchpath("--x-", D"?", "-", "X") == D"XXxX") |
| 140 | assert(package.searchpath("---xX", D"?", "---", "XX") == D"XXxX") |
| 141 | assert(package.searchpath(D"C.lua", "?", dirsep) == D"C.lua") |
| 142 | assert(package.searchpath(".\\C.lua", D"?", "\\") == D"./C.lua") |
| 143 | |
| 144 | local oldpath = package.path |
| 145 | |
| 146 | package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR) |
| 147 | |
| 148 | local try = function (p, n, r, ext) |
| 149 | NAME = nil |
| 150 | local rr, x = require(p) |
| 151 | assert(NAME == n) |
| 152 | assert(REQUIRED == p) |
| 153 | assert(rr == r) |
| 154 | assert(ext == x) |
| 155 | end |
| 156 | |
| 157 | local a = require"names" |
| 158 | assert(a[1] == "names" and a[2] == D"names.lua") |
| 159 | |
| 160 | local st, msg = pcall(require, "err") |
| 161 | assert(not st and string.find(msg, "arithmetic") and B == 15) |
| 162 | st, msg = pcall(require, "synerr") |
| 163 | assert(not st and string.find(msg, "error loading module")) |
| 164 | |
| 165 | assert(package.searchpath("C", package.path) == D"C.lua") |
| 166 | assert(require"C" == 25) |
| 167 | assert(require"C" == 25) |
| 168 | AA = nil |
| 169 | try('B', 'B.lua', true, "libs/B.lua") |
| 170 | assert(package.loaded.B) |
| 171 | assert(require"B" == true) |
| 172 | assert(package.loaded.A) |
| 173 | assert(require"C" == 25) |
| 174 | package.loaded.A = nil |
| 175 | try('B', nil, true, nil) -- should not reload package |
| 176 | try('A', 'A.lua', true, "libs/A.lua") |
| 177 | package.loaded.A = nil |
| 178 | os.remove(D'A.lua') |
| 179 | AA = {} |
| 180 | try('A', 'A.lc', AA, "libs/A.lc") -- now must find second option |
| 181 | assert(package.searchpath("A", package.path) == D"A.lc") |
| 182 | assert(require("A") == AA) |
| 183 | AA = false |
| 184 | try('K', 'L', false, "libs/L") -- default option |
| 185 | try('K', 'L', false, "libs/L") -- default option (should reload it) |
| 186 | assert(rawget(_G, "_REQUIREDNAME") == nil) |
| 187 | |
| 188 | AA = "x" |
| 189 | try("X", "XXxX", AA, "libs/XXxX") |
| 190 | |
| 191 | |
| 192 | removefiles(files) |
| 193 | NAME, REQUIRED, AA, B = nil |
| 194 | |
| 195 | |
| 196 | -- testing require of sub-packages |
| 197 | |
| 198 | local _G = _G |
| 199 | |
| 200 | package.path = string.gsub("D/?.lua;D/?/init.lua", "D/", DIR) |
| 201 | |
| 202 | files = { |
| 203 | ["P1/init.lua"] = "AA = 10", |
| 204 | ["P1/xuxu.lua"] = "AA = 20", |
| 205 | } |
| 206 | |
| 207 | createfiles(files, "_ENV = {}\n", "\nreturn _ENV\n") |
| 208 | AA = 0 |
| 209 | |
| 210 | local m, ext = assert(require"P1") |
| 211 | assert(ext == "libs/P1/init.lua") |
| 212 | assert(AA == 0 and m.AA == 10) |
| 213 | assert(require"P1" == m) |
| 214 | assert(require"P1" == m) |
| 215 | |
| 216 | assert(package.searchpath("P1.xuxu", package.path) == D"P1/xuxu.lua") |
| 217 | m.xuxu, ext = assert(require"P1.xuxu") |
| 218 | assert(AA == 0 and m.xuxu.AA == 20) |
| 219 | assert(ext == "libs/P1/xuxu.lua") |
| 220 | assert(require"P1.xuxu" == m.xuxu) |
| 221 | assert(require"P1.xuxu" == m.xuxu) |
| 222 | assert(require"P1" == m and m.AA == 10) |
| 223 | |
| 224 | |
| 225 | removefiles(files) |
| 226 | AA = nil |
| 227 | |
| 228 | package.path = "" |
| 229 | assert(not pcall(require, "file_does_not_exist")) |
| 230 | package.path = "??\0?" |
| 231 | assert(not pcall(require, "file_does_not_exist1")) |
| 232 | |
| 233 | package.path = oldpath |
| 234 | |
| 235 | -- check 'require' error message |
| 236 | local fname = "file_does_not_exist2" |
| 237 | local m, err = pcall(require, fname) |
| 238 | for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do |
| 239 | local t = string.gsub(t, "?", fname) |
| 240 | assert(string.find(err, t, 1, true)) |
| 241 | end |
| 242 | |
| 243 | do -- testing 'package.searchers' not being a table |
| 244 | local searchers = package.searchers |
| 245 | package.searchers = 3 |
| 246 | local st, msg = pcall(require, 'a') |
| 247 | assert(not st and string.find(msg, "must be a table")) |
| 248 | package.searchers = searchers |
| 249 | end |
| 250 | |
| 251 | local function import(...) |
| 252 | local f = {...} |
| 253 | return function (m) |
| 254 | for i=1, #f do m[f[i]] = _G[f[i]] end |
| 255 | end |
| 256 | end |
| 257 | |
| 258 | -- cannot change environment of a C function |
| 259 | assert(not pcall(module, 'XUXU')) |
| 260 | |
| 261 | |
| 262 | |
| 263 | -- testing require of C libraries |
| 264 | |
| 265 | |
| 266 | local p = "" -- On Mac OS X, redefine this to "_" |
| 267 | |
| 268 | -- check whether loadlib works in this system |
| 269 | local st, err, when = package.loadlib(DC"lib1", "*") |
| 270 | if not st then |
| 271 | local f, err, when = package.loadlib("donotexist", p.."xuxu") |
| 272 | assert(not f and type(err) == "string" and when == "absent") |
| 273 | ;(Message or print)('\n >>> cannot load dynamic library <<<\n') |
| 274 | print(err, when) |
| 275 | else |
| 276 | -- tests for loadlib |
| 277 | local f = assert(package.loadlib(DC"lib1", p.."onefunction")) |
| 278 | local a, b = f(15, 25) |
| 279 | assert(a == 25 and b == 15) |
| 280 | |
| 281 | f = assert(package.loadlib(DC"lib1", p.."anotherfunc")) |
| 282 | assert(f(10, 20) == "10%20\n") |
| 283 | |
| 284 | -- check error messages |
| 285 | local f, err, when = package.loadlib(DC"lib1", p.."xuxu") |
| 286 | assert(not f and type(err) == "string" and when == "init") |
| 287 | f, err, when = package.loadlib("donotexist", p.."xuxu") |
| 288 | assert(not f and type(err) == "string" and when == "open") |
| 289 | |
| 290 | -- symbols from 'lib1' must be visible to other libraries |
| 291 | f = assert(package.loadlib(DC"lib11", p.."luaopen_lib11")) |
| 292 | assert(f() == "exported") |
| 293 | |
| 294 | -- test C modules with prefixes in names |
| 295 | package.cpath = DC"?" |
| 296 | local lib2, ext = require"lib2-v2" |
| 297 | assert(string.find(ext, "libs/lib2-v2", 1, true)) |
| 298 | -- check correct access to global environment and correct |
| 299 | -- parameters |
| 300 | assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") |
| 301 | assert(lib2.id("x") == true) -- a different "id" implementation |
| 302 | |
| 303 | -- test C submodules |
| 304 | local fs, ext = require"lib1.sub" |
| 305 | assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") |
| 306 | assert(string.find(ext, "libs/lib1", 1, true)) |
| 307 | assert(fs.id(45) == 45) |
| 308 | _ENV.x, _ENV.y = nil |
| 309 | end |
| 310 | |
| 311 | |
| 312 | _ENV = _G |
| 313 | |
| 314 | |
| 315 | -- testing preload |
| 316 | do |
| 317 | local p = package |
| 318 | package = {} |
| 319 | p.preload.pl = function (...) |
| 320 | local _ENV = {...} |
| 321 | function xuxu (x) return x+20 end |
| 322 | return _ENV |
| 323 | end |
| 324 | |
| 325 | local pl, ext = require"pl" |
| 326 | assert(require"pl" == pl) |
| 327 | assert(pl.xuxu(10) == 30) |
| 328 | assert(pl[1] == "pl" and pl[2] == ":preload:" and ext == ":preload:") |
| 329 | |
| 330 | package = p |
| 331 | assert(type(package.path) == "string") |
| 332 | end |
| 333 | |
| 334 | |
| 335 | do print("testing external strings") |
| 336 | package.cpath = DC"?" |
| 337 | local lib2 = require"lib2-v2" |
| 338 | local t = {} |
| 339 | for _, len in ipairs{0, 10, 39, 40, 41, 1000} do |
| 340 | local str = string.rep("a", len) |
| 341 | local str1 = lib2.newstr(str) |
| 342 | assert(str == str1) |
| 343 | assert(not T or T.hash(str) == T.hash(str1)) |
| 344 | t[str1] = 20; assert(t[str] == 20 and t[str1] == 20) |
| 345 | t[str] = 10; assert(t[str1] == 10) |
| 346 | local tt = {[str1] = str1} |
| 347 | assert(next(tt) == str1 and next(tt, str1) == nil) |
| 348 | assert(tt[str] == str) |
| 349 | local str2 = lib2.newstr(str1) |
| 350 | assert(str == str2 and t[str2] == 10 and tt[str2] == str) |
| 351 | end |
| 352 | end |
| 353 | |
| 354 | print('+') |
| 355 | |
| 356 | end --] |
| 357 | |
| 358 | print("testing assignments, logical operators, and constructors") |
| 359 | |
| 360 | local res, res2 = 27 |
| 361 | |
| 362 | local a, b = 1, 2+3 |
| 363 | assert(a==1 and b==5) |
| 364 | a={} |
| 365 | local function f() return 10, 11, 12 end |
| 366 | a.x, b, a[1] = 1, 2, f() |
| 367 | assert(a.x==1 and b==2 and a[1]==10) |
| 368 | a[f()], b, a[f()+3] = f(), a, 'x' |
| 369 | assert(a[10] == 10 and b == a and a[13] == 'x') |
| 370 | |
| 371 | do |
| 372 | local f = function (n) local x = {}; for i=1,n do x[i]=i end; |
| 373 | return table.unpack(x) end; |
| 374 | local a,b,c |
| 375 | a,b = 0, f(1) |
| 376 | assert(a == 0 and b == 1) |
| 377 | a,b = 0, f(1) |
| 378 | assert(a == 0 and b == 1) |
| 379 | a,b,c = 0,5,f(4) |
| 380 | assert(a==0 and b==5 and c==1) |
| 381 | a,b,c = 0,5,f(0) |
| 382 | assert(a==0 and b==5 and c==nil) |
| 383 | end |
| 384 | |
| 385 | local a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 |
| 386 | assert(not a and b and c and d==6) |
| 387 | |
| 388 | d = 20 |
| 389 | a, b, c, d = f() |
| 390 | assert(a==10 and b==11 and c==12 and d==nil) |
| 391 | a,b = f(), 1, 2, 3, f() |
| 392 | assert(a==10 and b==1) |
| 393 | |
| 394 | assert(a<b == false and a>b == true) |
| 395 | assert((10 and 2) == 2) |
| 396 | assert((10 or 2) == 10) |
| 397 | assert((10 or assert(nil)) == 10) |
| 398 | assert(not (nil and assert(nil))) |
| 399 | assert((nil or "alo") == "alo") |
| 400 | assert((nil and 10) == nil) |
| 401 | assert((false and 10) == false) |
| 402 | assert((true or 10) == true) |
| 403 | assert((false or 10) == 10) |
| 404 | assert(false ~= nil) |
| 405 | assert(nil ~= false) |
| 406 | assert(not nil == true) |
| 407 | assert(not not nil == false) |
| 408 | assert(not not 1 == true) |
| 409 | assert(not not a == true) |
| 410 | assert(not not (6 or nil) == true) |
| 411 | assert(not not (nil and 56) == false) |
| 412 | assert(not not (nil and true) == false) |
| 413 | assert(not 10 == false) |
| 414 | assert(not {} == false) |
| 415 | assert(not 0.5 == false) |
| 416 | assert(not "x" == false) |
| 417 | |
| 418 | assert({} ~= {}) |
| 419 | print('+') |
| 420 | |
| 421 | a = {} |
| 422 | a[true] = 20 |
| 423 | a[false] = 10 |
| 424 | assert(a[1<2] == 20 and a[1>2] == 10) |
| 425 | |
| 426 | function f(a) return a end |
| 427 | |
| 428 | local a = {} |
| 429 | for i=3000,-3000,-1 do a[i + 0.0] = i; end |
| 430 | a[10e30] = "alo"; a[true] = 10; a[false] = 20 |
| 431 | assert(a[10e30] == 'alo' and a[not 1] == 20 and a[10<20] == 10) |
| 432 | for i=3000,-3000,-1 do assert(a[i] == i); end |
| 433 | a[print] = assert |
| 434 | a[f] = print |
| 435 | a[a] = a |
| 436 | assert(a[a][a][a][a][print] == assert) |
| 437 | a[print](a[a[f]] == a[print]) |
| 438 | assert(not pcall(function () local a = {}; a[nil] = 10 end)) |
| 439 | assert(not pcall(function () local a = {[nil] = 10} end)) |
| 440 | assert(a[nil] == undef) |
| 441 | a = nil |
| 442 | |
| 443 | local a, b, c |
| 444 | a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} |
| 445 | a, a.x, a.y = a, a[-3] |
| 446 | assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y) |
| 447 | a[1], f(a)[2], b, c = {['alo']=assert}, 10, a[1], a[f], 6, 10, 23, f(a), 2 |
| 448 | a[1].alo(a[2]==10 and b==10 and c==print) |
| 449 | |
| 450 | a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 = 10 |
| 451 | local function foo () |
| 452 | return a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 |
| 453 | end |
| 454 | assert(foo() == 10 and |
| 455 | a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 == |
| 456 | 10) |
| 457 | |
| 458 | |
| 459 | do |
| 460 | -- _ENV constant |
| 461 | local function foo () |
| 462 | local _ENV <const> = 11 |
| 463 | X = "hi" |
| 464 | end |
| 465 | local st, msg = pcall(foo) |
| 466 | assert(not st and string.find(msg, "number")) |
| 467 | end |
| 468 | |
| 469 | |
| 470 | -- test of large float/integer indices |
| 471 | |
| 472 | -- compute maximum integer where all bits fit in a float |
| 473 | local maxint = math.maxinteger |
| 474 | |
| 475 | -- trim (if needed) to fit in a float |
| 476 | while maxint ~= (maxint + 0.0) or (maxint - 1) ~= (maxint - 1.0) do |
| 477 | maxint = maxint // 2 |
| 478 | end |
| 479 | |
| 480 | local maxintF = maxint + 0.0 -- float version |
| 481 | |
| 482 | assert(maxintF == maxint and math.type(maxintF) == "float" and |
| 483 | maxintF >= 2.0^14) |
| 484 | |
| 485 | -- floats and integers must index the same places |
| 486 | a[maxintF] = 10; a[maxintF - 1.0] = 11; |
| 487 | a[-maxintF] = 12; a[-maxintF + 1.0] = 13; |
| 488 | |
| 489 | assert(a[maxint] == 10 and a[maxint - 1] == 11 and |
| 490 | a[-maxint] == 12 and a[-maxint + 1] == 13) |
| 491 | |
| 492 | a[maxint] = 20 |
| 493 | a[-maxint] = 22 |
| 494 | |
| 495 | assert(a[maxintF] == 20 and a[maxintF - 1.0] == 11 and |
| 496 | a[-maxintF] == 22 and a[-maxintF + 1.0] == 13) |
| 497 | |
| 498 | a = nil |
| 499 | |
| 500 | |
| 501 | -- test conflicts in multiple assignment |
| 502 | do |
| 503 | local a,i,j,b |
| 504 | a = {'a', 'b'}; i=1; j=2; b=a |
| 505 | i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i |
| 506 | assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and |
| 507 | b[3] == 1) |
| 508 | a = {} |
| 509 | local function foo () -- assigining to upvalues |
| 510 | b, a.x, a = a, 10, 20 |
| 511 | end |
| 512 | foo() |
| 513 | assert(a == 20 and b.x == 10) |
| 514 | end |
| 515 | |
| 516 | -- repeat test with upvalues |
| 517 | do |
| 518 | local a,i,j,b |
| 519 | a = {'a', 'b'}; i=1; j=2; b=a |
| 520 | local function foo () |
| 521 | i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i |
| 522 | end |
| 523 | foo() |
| 524 | assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and |
| 525 | b[3] == 1) |
| 526 | local t = {} |
| 527 | (function (a) t[a], a = 10, 20 end)(1); |
| 528 | assert(t[1] == 10) |
| 529 | end |
| 530 | |
| 531 | -- bug in 5.2 beta |
| 532 | local function foo () |
| 533 | local a |
| 534 | return function () |
| 535 | local b |
| 536 | a, b = 3, 14 -- local and upvalue have same index |
| 537 | return a, b |
| 538 | end |
| 539 | end |
| 540 | |
| 541 | local a, b = foo()() |
| 542 | assert(a == 3 and b == 14) |
| 543 | |
| 544 | print('OK') |
| 545 | |
| 546 | return res |
| 547 |