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