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