attrib.lua 12 KB

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