attrib.lua 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. do --[
  2. print "testing require"
  3. assert(require"string" == string)
  4. assert(require"math" == math)
  5. assert(require"table" == table)
  6. assert(require"io" == io)
  7. assert(require"os" == os)
  8. assert(require"debug" == debug)
  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. local DIR = "libs/"
  15. local function createfiles (files, preextras, posextras)
  16. for n,c in pairs(files) do
  17. io.output(DIR..n)
  18. io.write(string.format(preextras, n))
  19. io.write(c)
  20. io.write(string.format(posextras, n))
  21. io.close(io.output())
  22. end
  23. end
  24. function removefiles (files)
  25. for n in pairs(files) do
  26. os.remove(DIR..n)
  27. end
  28. end
  29. local files = {
  30. ["A.lua"] = "",
  31. ["B.lua"] = "assert(...=='B');require 'A'",
  32. ["A.lc"] = "",
  33. ["A"] = "",
  34. ["L"] = "",
  35. ["XXxX"] = "",
  36. ["C.lua"] = "package.loaded[...] = 25; require'C'"
  37. }
  38. AA = nil
  39. local extras = [[
  40. NAME = '%s'
  41. REQUIRED = ...
  42. return AA]]
  43. createfiles(files, "", extras)
  44. local oldpath = package.path
  45. package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR)
  46. local try = function (p, n, r)
  47. NAME = nil
  48. local rr = require(p)
  49. assert(NAME == n)
  50. assert(REQUIRED == p)
  51. assert(rr == r)
  52. end
  53. assert(require"C" == 25)
  54. assert(require"C" == 25)
  55. AA = nil
  56. try('B', 'B.lua', true)
  57. assert(package.loaded.B)
  58. assert(require"B" == true)
  59. assert(package.loaded.A)
  60. package.loaded.A = nil
  61. try('B', nil, true) -- should not reload package
  62. try('A', 'A.lua', true)
  63. package.loaded.A = nil
  64. os.remove(DIR..'A.lua')
  65. AA = {}
  66. try('A', 'A.lc', AA) -- now must find second option
  67. assert(require("A") == AA)
  68. AA = false
  69. try('K', 'L', false) -- default option
  70. try('K', 'L', false) -- default option (should reload it)
  71. assert(rawget(_G, "_REQUIREDNAME") == nil)
  72. AA = "x"
  73. try("X", "XXxX", AA)
  74. removefiles(files)
  75. -- testing require of sub-packages
  76. package.path = string.gsub("D/?.lua;D/?/init.lua", "D/", DIR)
  77. files = {
  78. ["P1/init.lua"] = "AA = 10",
  79. ["P1/xuxu.lua"] = "AA = 20",
  80. }
  81. createfiles(files, "module(..., package.seeall)\n", "")
  82. AA = 0
  83. local m = assert(require"P1")
  84. assert(m == P1 and m._NAME == "P1" and AA == 0 and m.AA == 10)
  85. assert(require"P1" == P1 and P1 == m)
  86. assert(require"P1" == P1)
  87. assert(P1._PACKAGE == "")
  88. local m = assert(require"P1.xuxu")
  89. assert(m == P1.xuxu and m._NAME == "P1.xuxu" and AA == 0 and m.AA == 20)
  90. assert(require"P1.xuxu" == P1.xuxu and P1.xuxu == m)
  91. assert(require"P1.xuxu" == P1.xuxu)
  92. assert(require"P1" == P1)
  93. assert(P1.xuxu._PACKAGE == "P1.")
  94. assert(P1.AA == 10 and P1._PACKAGE == "")
  95. assert(P1._G == _G and P1.xuxu._G == _G)
  96. removefiles(files)
  97. package.path = ""
  98. assert(not pcall(require, "file_does_not_exist"))
  99. package.path = "??\0?"
  100. assert(not pcall(require, "file_does_not_exist1"))
  101. package.path = oldpath
  102. -- check 'require' error message
  103. -- local fname = "file_does_not_exist2"
  104. -- local m, err = pcall(require, fname)
  105. -- for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do
  106. -- t = string.gsub(t, "?", fname)
  107. -- print(t, err)
  108. -- assert(string.find(err, t, 1, true))
  109. -- end
  110. local function import(...)
  111. local f = {...}
  112. return function (m)
  113. for i=1, #f do m[f[i]] = _G[f[i]] end
  114. end
  115. end
  116. local assert, module, package = assert, module, package
  117. X = nil; x = 0; assert(_G.x == 0) -- `x' must be a global variable
  118. module"X"; x = 1; assert(_M.x == 1)
  119. module"X.a.b.c"; x = 2; assert(_M.x == 2)
  120. module("X.a.b", package.seeall); x = 3
  121. assert(X._NAME == "X" and X.a.b.c._NAME == "X.a.b.c" and X.a.b._NAME == "X.a.b")
  122. assert(X._M == X and X.a.b.c._M == X.a.b.c and X.a.b._M == X.a.b)
  123. assert(X.x == 1 and X.a.b.c.x == 2 and X.a.b.x == 3)
  124. assert(X._PACKAGE == "" and X.a.b.c._PACKAGE == "X.a.b." and
  125. X.a.b._PACKAGE == "X.a.")
  126. assert(_PACKAGE.."c" == "X.a.c")
  127. assert(X.a._NAME == nil and X.a._M == nil)
  128. module("X.a", import("X")) ; x = 4
  129. assert(X.a._NAME == "X.a" and X.a.x == 4 and X.a._M == X.a)
  130. module("X.a.b", package.seeall); assert(x == 3); x = 5
  131. assert(_NAME == "X.a.b" and X.a.b.x == 5)
  132. assert(X._G == nil and X.a._G == nil and X.a.b._G == _G and X.a.b.c._G == nil)
  133. setfenv(1, _G)
  134. assert(x == 0)
  135. assert(not pcall(module, "x"))
  136. assert(not pcall(module, "math.sin"))
  137. -- testing C libraries
  138. local p = "" -- On Mac OS X, redefine this to "_"
  139. -- assert(loadlib == package.loadlib) -- only for compatibility
  140. -- local f, err, when = package.loadlib("libs/lib1.so", p.."luaopen_lib1")
  141. local f = nil
  142. if not f then
  143. (Message or print)('\a\n >>> cannot load dynamic library <<<\n\a')
  144. print(err, when)
  145. else
  146. f() -- open library
  147. assert(require("lib1") == lib1)
  148. collectgarbage()
  149. assert(lib1.id("x") == "x")
  150. f = assert(package.loadlib("libs/lib1.so", p.."anotherfunc"))
  151. assert(f(10, 20) == "1020\n")
  152. f, err, when = package.loadlib("libs/lib1.so", p.."xuxu")
  153. assert(not f and type(err) == "string" and when == "init")
  154. package.cpath = "libs/?.so"
  155. require"lib2"
  156. assert(lib2.id("x") == "x")
  157. local fs = require"lib1.sub"
  158. assert(fs == lib1.sub and next(lib1.sub) == nil)
  159. module("lib2", package.seeall)
  160. f = require"-lib2"
  161. assert(f.id("x") == "x" and _M == f and _NAME == "lib2")
  162. module("lib1.sub", package.seeall)
  163. assert(_M == fs)
  164. setfenv(1, _G)
  165. end
  166. -- f, err, when = package.loadlib("donotexist", p.."xuxu")
  167. -- assert(not f and type(err) == "string" and (when == "open" or when == "absent"))
  168. -- testing preload
  169. do
  170. local p = package
  171. package = {}
  172. p.preload.pl = function (...)
  173. module(...)
  174. function xuxu (x) return x+20 end
  175. end
  176. require"pl"
  177. assert(require"pl" == pl)
  178. assert(pl.xuxu(10) == 30)
  179. package = p
  180. assert(type(package.path) == "string")
  181. end
  182. end --]
  183. print('+')
  184. print("testing assignments, logical operators, and constructors")
  185. local res, res2 = 27
  186. a, b = 1, 2+3
  187. assert(a==1 and b==5)
  188. a={}
  189. function f() return 10, 11, 12 end
  190. a.x, b, a[1] = 1, 2, f()
  191. assert(a.x==1 and b==2 and a[1]==10)
  192. a[f()], b, a[f()+3] = f(), a, 'x'
  193. assert(a[10] == 10 and b == a and a[13] == 'x')
  194. do
  195. local f = function (n) local x = {}; for i=1,n do x[i]=i end;
  196. return unpack(x) end;
  197. local a,b,c
  198. a,b = 0, f(1)
  199. assert(a == 0 and b == 1)
  200. A,b = 0, f(1)
  201. assert(A == 0 and b == 1)
  202. a,b,c = 0,5,f(4)
  203. assert(a==0 and b==5 and c==1)
  204. a,b,c = 0,5,f(0)
  205. assert(a==0 and b==5 and c==nil)
  206. end
  207. a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
  208. assert(not a and b and c and d==6)
  209. d = 20
  210. a, b, c, d = f()
  211. assert(a==10 and b==11 and c==12 and d==nil)
  212. a,b = f(), 1, 2, 3, f()
  213. assert(a==10 and b==1)
  214. assert(a<b == false and a>b == true)
  215. assert((10 and 2) == 2)
  216. assert((10 or 2) == 10)
  217. assert((10 or assert(nil)) == 10)
  218. assert(not (nil and assert(nil)))
  219. assert((nil or "alo") == "alo")
  220. assert((nil and 10) == nil)
  221. assert((false and 10) == false)
  222. assert((true or 10) == true)
  223. assert((false or 10) == 10)
  224. assert(false ~= nil)
  225. assert(nil ~= false)
  226. assert(not nil == true)
  227. assert(not not nil == false)
  228. assert(not not 1 == true)
  229. assert(not not a == true)
  230. assert(not not (6 or nil) == true)
  231. assert(not not (nil and 56) == false)
  232. assert(not not (nil and true) == false)
  233. print('+')
  234. a = {}
  235. a[true] = 20
  236. a[false] = 10
  237. assert(a[1<2] == 20 and a[1>2] == 10)
  238. function f(a) return a end
  239. local a = {}
  240. for i=3000,-3000,-1 do a[i] = i; end
  241. a[10e30] = "alo"; a[true] = 10; a[false] = 20
  242. assert(a[10e30] == 'alo' and a[not 1] == 20 and a[10<20] == 10)
  243. for i=3000,-3000,-1 do assert(a[i] == i); end
  244. a[print] = assert
  245. a[f] = print
  246. a[a] = a
  247. assert(a[a][a][a][a][print] == assert)
  248. a[print](a[a[f]] == a[print])
  249. a = nil
  250. a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'}
  251. a, a.x, a.y = a, a[-3]
  252. assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y)
  253. a[1], f(a)[2], b, c = {['alo']=assert}, 10, a[1], a[f], 6, 10, 23, f(a), 2
  254. a[1].alo(a[2]==10 and b==10 and c==print)
  255. a[2^31] = 10; a[2^31+1] = 11; a[-2^31] = 12;
  256. a[2^32] = 13; a[-2^32] = 14; a[2^32+1] = 15; a[10^33] = 16;
  257. assert(a[2^31] == 10 and a[2^31+1] == 11 and a[-2^31] == 12 and
  258. a[2^32] == 13 and a[-2^32] == 14 and a[2^32+1] == 15 and
  259. a[10^33] == 16)
  260. a = nil
  261. -- do
  262. -- local a,i,j,b
  263. -- a = {'a', 'b'}; i=1; j=2; b=a
  264. -- i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i
  265. -- assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and
  266. -- b[3] == 1)
  267. -- end
  268. print('OK')
  269. return res