attrib.lua 13 KB

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