attrib.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. -- The tests for 'require' assume some specific directories and libraries;
  2. -- better to avoid them in generic machines
  3. if not _port then --[
  4. print "testing require"
  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. assert(type(package.path) == "string")
  12. assert(type(package.cpath) == "string")
  13. assert(type(package.loaded) == "table")
  14. assert(type(package.preload) == "table")
  15. assert(type(package.config) == "string")
  16. print("package config: "..string.gsub(package.config, "\n", "|"))
  17. do
  18. -- create a path with 'max' templates,
  19. -- each with 1-10 repetitions of '?'
  20. local max = 2000
  21. local t = {}
  22. for i = 1,max do t[i] = string.rep("?", i%10 + 1) end
  23. t[#t + 1] = ";" -- empty template
  24. local path = table.concat(t, ";")
  25. -- use that path in a search
  26. local s, err = package.searchpath("xuxu", path)
  27. -- search fails; check that message has an occurence of
  28. -- '??????????' with ? replaced by xuxu and at least 'max' lines
  29. assert(not s and
  30. string.find(err, string.rep("xuxu", 10)) and
  31. #string.gsub(err, "[^\n]", "") >= max)
  32. -- path with one very long template
  33. local path = string.rep("?", max)
  34. local s, err = package.searchpath("xuxu", path)
  35. assert(not s and string.find(err, string.rep('xuxu', max)))
  36. end
  37. do
  38. local oldpath = package.path
  39. package.path = {}
  40. local s, err = pcall(require, "no-such-file")
  41. assert(not s and string.find(err, "package.path"))
  42. package.path = oldpath
  43. end
  44. print('+')
  45. -- auxiliary directory with C modules and temporary files
  46. local DIR = "libs/"
  47. -- prepend DIR to a name
  48. local function D (x) return DIR .. x end
  49. local function createfiles (files, preextras, posextras)
  50. for n,c in pairs(files) do
  51. io.output(D(n))
  52. io.write(string.format(preextras, n))
  53. io.write(c)
  54. io.write(string.format(posextras, n))
  55. io.close(io.output())
  56. end
  57. end
  58. function removefiles (files)
  59. for n in pairs(files) do
  60. os.remove(D(n))
  61. end
  62. end
  63. local files = {
  64. ["names.lua"] = "do return {...} end\n",
  65. ["err.lua"] = "B = 15; a = a + 1;",
  66. ["A.lua"] = "",
  67. ["B.lua"] = "assert(...=='B');require 'A'",
  68. ["A.lc"] = "",
  69. ["A"] = "",
  70. ["L"] = "",
  71. ["XXxX"] = "",
  72. ["C.lua"] = "package.loaded[...] = 25; require'C'"
  73. }
  74. AA = nil
  75. local extras = [[
  76. NAME = '%s'
  77. REQUIRED = ...
  78. return AA]]
  79. createfiles(files, "", extras)
  80. -- testing explicit "dir" separator in 'searchpath'
  81. assert(package.searchpath("C.lua", D"?", "", "") == D"C.lua")
  82. assert(package.searchpath("C.lua", D"?", ".", ".") == D"C.lua")
  83. assert(package.searchpath("--x-", D"?", "-", "X") == D"XXxX")
  84. assert(package.searchpath("---xX", D"?", "---", "XX") == D"XXxX")
  85. assert(package.searchpath(D"C.lua", "?", "/") == D"C.lua")
  86. assert(package.searchpath(".\\C.lua", D"?", "\\") == D"./C.lua")
  87. local oldpath = package.path
  88. package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR)
  89. local try = function (p, n, r)
  90. NAME = nil
  91. local rr = require(p)
  92. assert(NAME == n)
  93. assert(REQUIRED == p)
  94. assert(rr == r)
  95. end
  96. a = require"names"
  97. assert(a[1] == "names" and a[2] == D"names.lua")
  98. _G.a = nil
  99. assert(not pcall(require, "err"))
  100. assert(B == 15)
  101. assert(package.searchpath("C", package.path) == D"C.lua")
  102. assert(require"C" == 25)
  103. assert(require"C" == 25)
  104. AA = nil
  105. try('B', 'B.lua', true)
  106. assert(package.loaded.B)
  107. assert(require"B" == true)
  108. assert(package.loaded.A)
  109. assert(require"C" == 25)
  110. package.loaded.A = nil
  111. try('B', nil, true) -- should not reload package
  112. try('A', 'A.lua', true)
  113. package.loaded.A = nil
  114. os.remove(D'A.lua')
  115. AA = {}
  116. try('A', 'A.lc', AA) -- now must find second option
  117. assert(package.searchpath("A", package.path) == D"A.lc")
  118. assert(require("A") == AA)
  119. AA = false
  120. try('K', 'L', false) -- default option
  121. try('K', 'L', false) -- default option (should reload it)
  122. assert(rawget(_G, "_REQUIREDNAME") == nil)
  123. AA = "x"
  124. try("X", "XXxX", AA)
  125. removefiles(files)
  126. -- testing require of sub-packages
  127. local _G = _G
  128. package.path = string.gsub("D/?.lua;D/?/init.lua", "D/", DIR)
  129. files = {
  130. ["P1/init.lua"] = "AA = 10",
  131. ["P1/xuxu.lua"] = "AA = 20",
  132. }
  133. createfiles(files, "_ENV = {}\n", "\nreturn _ENV\n")
  134. AA = 0
  135. local m = assert(require"P1")
  136. assert(AA == 0 and m.AA == 10)
  137. assert(require"P1" == m)
  138. assert(require"P1" == m)
  139. assert(package.searchpath("P1.xuxu", package.path) == D"P1/xuxu.lua")
  140. m.xuxu = assert(require"P1.xuxu")
  141. assert(AA == 0 and m.xuxu.AA == 20)
  142. assert(require"P1.xuxu" == m.xuxu)
  143. assert(require"P1.xuxu" == m.xuxu)
  144. assert(require"P1" == m and m.AA == 10)
  145. removefiles(files)
  146. package.path = ""
  147. assert(not pcall(require, "file_does_not_exist"))
  148. package.path = "??\0?"
  149. assert(not pcall(require, "file_does_not_exist1"))
  150. package.path = oldpath
  151. -- check 'require' error message
  152. local fname = "file_does_not_exist2"
  153. local m, err = pcall(require, fname)
  154. for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do
  155. t = string.gsub(t, "?", fname)
  156. assert(string.find(err, t, 1, true))
  157. end
  158. local function import(...)
  159. local f = {...}
  160. return function (m)
  161. for i=1, #f do m[f[i]] = _G[f[i]] end
  162. end
  163. end
  164. -- cannot change environment of a C function
  165. assert(not pcall(module, 'XUXU'))
  166. -- testing require of C libraries
  167. local p = "" -- On Mac OS X, redefine this to "_"
  168. -- check whether loadlib works in this system
  169. local st, err, when = package.loadlib(D"lib1.so", "*")
  170. if not st then
  171. local f, err, when = package.loadlib("donotexist", p.."xuxu")
  172. assert(not f and type(err) == "string" and when == "absent")
  173. ;(Message or print)('\a\n >>> cannot load dynamic library <<<\n\a')
  174. print(err, when)
  175. else
  176. -- tests for loadlib
  177. local f = assert(package.loadlib(D"lib1.so", p.."onefunction"))
  178. local a, b = f(15, 25)
  179. assert(a == 25 and b == 15)
  180. f = assert(package.loadlib(D"lib1.so", p.."anotherfunc"))
  181. assert(f(10, 20) == "1020\n")
  182. -- check error messages
  183. local f, err, when = package.loadlib(D"lib1.so", p.."xuxu")
  184. assert(not f and type(err) == "string" and when == "init")
  185. f, err, when = package.loadlib("donotexist", p.."xuxu")
  186. assert(not f and type(err) == "string" and when == "open")
  187. -- symbols from 'lib1' must be visible to other libraries
  188. f = assert(package.loadlib(D"lib11.so", p.."luaopen_lib11"))
  189. assert(f() == "exported")
  190. -- test C modules with prefixes in names
  191. package.cpath = D"?.so"
  192. local lib2 = require"v-lib2"
  193. -- check correct access to global environment and correct
  194. -- parameters
  195. assert(_ENV.x == "v-lib2" and _ENV.y == D"v-lib2.so")
  196. assert(lib2.id("x") == "x")
  197. -- test C submodules
  198. local fs = require"lib1.sub"
  199. assert(_ENV.x == "lib1.sub" and _ENV.y == D"lib1.so")
  200. assert(fs.id(45) == 45)
  201. end
  202. _ENV = _G
  203. -- testing preload
  204. do
  205. local p = package
  206. package = {}
  207. p.preload.pl = function (...)
  208. local _ENV = {...}
  209. function xuxu (x) return x+20 end
  210. return _ENV
  211. end
  212. local pl = require"pl"
  213. assert(require"pl" == pl)
  214. assert(pl.xuxu(10) == 30)
  215. assert(pl[1] == "pl" and pl[2] == nil)
  216. package = p
  217. assert(type(package.path) == "string")
  218. end
  219. print('+')
  220. end --]
  221. print("testing assignments, logical operators, and constructors")
  222. local res, res2 = 27
  223. a, b = 1, 2+3
  224. assert(a==1 and b==5)
  225. a={}
  226. function f() return 10, 11, 12 end
  227. a.x, b, a[1] = 1, 2, f()
  228. assert(a.x==1 and b==2 and a[1]==10)
  229. a[f()], b, a[f()+3] = f(), a, 'x'
  230. assert(a[10] == 10 and b == a and a[13] == 'x')
  231. do
  232. local f = function (n) local x = {}; for i=1,n do x[i]=i end;
  233. return table.unpack(x) end;
  234. local a,b,c
  235. a,b = 0, f(1)
  236. assert(a == 0 and b == 1)
  237. A,b = 0, f(1)
  238. assert(A == 0 and b == 1)
  239. a,b,c = 0,5,f(4)
  240. assert(a==0 and b==5 and c==1)
  241. a,b,c = 0,5,f(0)
  242. assert(a==0 and b==5 and c==nil)
  243. end
  244. a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
  245. assert(not a and b and c and d==6)
  246. d = 20
  247. a, b, c, d = f()
  248. assert(a==10 and b==11 and c==12 and d==nil)
  249. a,b = f(), 1, 2, 3, f()
  250. assert(a==10 and b==1)
  251. assert(a<b == false and a>b == true)
  252. assert((10 and 2) == 2)
  253. assert((10 or 2) == 10)
  254. assert((10 or assert(nil)) == 10)
  255. assert(not (nil and assert(nil)))
  256. assert((nil or "alo") == "alo")
  257. assert((nil and 10) == nil)
  258. assert((false and 10) == false)
  259. assert((true or 10) == true)
  260. assert((false or 10) == 10)
  261. assert(false ~= nil)
  262. assert(nil ~= false)
  263. assert(not nil == true)
  264. assert(not not nil == false)
  265. assert(not not 1 == true)
  266. assert(not not a == true)
  267. assert(not not (6 or nil) == true)
  268. assert(not not (nil and 56) == false)
  269. assert(not not (nil and true) == false)
  270. assert({} ~= {})
  271. print('+')
  272. a = {}
  273. a[true] = 20
  274. a[false] = 10
  275. assert(a[1<2] == 20 and a[1>2] == 10)
  276. function f(a) return a end
  277. local a = {}
  278. for i=3000,-3000,-1 do a[i] = i; end
  279. a[10e30] = "alo"; a[true] = 10; a[false] = 20
  280. assert(a[10e30] == 'alo' and a[not 1] == 20 and a[10<20] == 10)
  281. for i=3000,-3000,-1 do assert(a[i] == i); end
  282. a[print] = assert
  283. a[f] = print
  284. a[a] = a
  285. assert(a[a][a][a][a][print] == assert)
  286. a[print](a[a[f]] == a[print])
  287. assert(not pcall(function () local a = {}; a[nil] = 10 end))
  288. assert(not pcall(function () local a = {[nil] = 10} end))
  289. assert(a[nil] == nil)
  290. a = nil
  291. a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'}
  292. a, a.x, a.y = a, a[-3]
  293. assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y)
  294. a[1], f(a)[2], b, c = {['alo']=assert}, 10, a[1], a[f], 6, 10, 23, f(a), 2
  295. a[1].alo(a[2]==10 and b==10 and c==print)
  296. a[2^31] = 10; a[2^31+1] = 11; a[-2^31] = 12;
  297. a[2^32] = 13; a[-2^32] = 14; a[2^32+1] = 15; a[10^33] = 16;
  298. assert(a[2^31] == 10 and a[2^31+1] == 11 and a[-2^31] == 12 and
  299. a[2^32] == 13 and a[-2^32] == 14 and a[2^32+1] == 15 and
  300. a[10^33] == 16)
  301. a = nil
  302. -- test conflicts in multiple assignment
  303. do
  304. local a,i,j,b
  305. a = {'a', 'b'}; i=1; j=2; b=a
  306. i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i
  307. assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and
  308. b[3] == 1)
  309. end
  310. -- repeat test with upvalues
  311. do
  312. local a,i,j,b
  313. a = {'a', 'b'}; i=1; j=2; b=a
  314. local function foo ()
  315. i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i
  316. end
  317. foo()
  318. assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and
  319. b[3] == 1)
  320. local t = {}
  321. (function (a) t[a], a = 10, 20 end)(1);
  322. assert(t[1] == 10)
  323. end
  324. -- bug in 5.2 beta
  325. local function foo ()
  326. local a
  327. return function ()
  328. local b
  329. a, b = 3, 14 -- local and upvalue have same index
  330. return a, b
  331. end
  332. end
  333. local a, b = foo()()
  334. assert(a == 3 and b == 14)
  335. print('OK')
  336. return res