1#!../lua
2
3math.randomseed(0)
4
5collectgarbage("setstepmul", 180)
6collectgarbage("setpause", 190)
7
8
9--[=[
10  example of a long [comment],
11  [[spanning several [lines]]]
12
13]=]
14
15print("current path:\n  " .. string.gsub(package.path, ";", "\n  "))
16
17
18local msgs = {}
19function Message (m)
20  print(m)
21  msgs[#msgs+1] = string.sub(m, 3, -3)
22end
23
24
25local c = os.clock()
26
27assert(os.setlocale"C")
28
29local T,print,gcinfo,format,write,assert,type =
30      T,print,gcinfo,string.format,io.write,assert,type
31
32local function formatmem (m)
33  if m < 1024 then return m
34  else
35    m = m/1024 - m/1024%1
36    if m < 1024 then return m.."K"
37    else
38      m = m/1024 - m/1024%1
39      return m.."M"
40    end
41  end
42end
43
44local showmem = function ()
45  if not T then
46    print(format("    ---- total memory: %s ----\n", formatmem(gcinfo())))
47  else
48    T.checkmemory()
49    local a,b,c = T.totalmem()
50    local d,e = gcinfo()
51    print(format(
52  "\n    ---- total memory: %s (%dK), max use: %s,  blocks: %d\n",
53                        formatmem(a),  d,      formatmem(c),           b))
54  end
55end
56
57
58--
59-- redefine dofile to run files through dump/undump
60--
61dofile = function (n)
62  showmem()
63  local f = assert(loadfile(n))
64  local b = string.dump(f)
65  f = assert(loadstring(b))
66  return f()
67end
68
69dofile('main.lua')
70
71do
72  local u = newproxy(true)
73  local newproxy, stderr = newproxy, io.stderr
74  getmetatable(u).__gc = function (o)
75    stderr:write'.'
76    newproxy(o)
77  end
78end
79
80local f = assert(loadfile('gc.lua'))
81f()
82dofile('db.lua')
83assert(dofile('calls.lua') == deep and deep)
84dofile('strings.lua')
85dofile('literals.lua')
86assert(dofile('attrib.lua') == 27)
87assert(dofile('locals.lua') == 5)
88dofile('constructs.lua')
89dofile('code.lua')
90do
91  local f = coroutine.wrap(assert(loadfile('big.lua')))
92  assert(f() == 'b')
93  assert(f() == 'a')
94end
95dofile('nextvar.lua')
96dofile('pm.lua')
97dofile('api.lua')
98assert(dofile('events.lua') == 12)
99dofile('vararg.lua')
100dofile('closure.lua')
101dofile('errors.lua')
102dofile('math.lua')
103dofile('sort.lua')
104assert(dofile('verybig.lua') == 10); collectgarbage()
105dofile('files.lua')
106
107if #msgs > 0 then
108  print("\ntests not performed:")
109  for i=1,#msgs do
110    print(msgs[i])
111  end
112  print()
113end
114
115print("final OK !!!")
116print('cleaning all!!!!')
117
118debug.sethook(function (a) assert(type(a) == 'string') end, "cr")
119
120local _G, collectgarbage, showmem, print, format, clock =
121      _G, collectgarbage, showmem, print, format, os.clock
122
123local a={}
124for n in pairs(_G) do a[n] = 1 end
125a.tostring = nil
126a.___Glob = nil
127for n in pairs(a) do _G[n] = nil end
128
129a = nil
130collectgarbage()
131collectgarbage()
132collectgarbage()
133collectgarbage()
134collectgarbage()
135collectgarbage();showmem()
136
137print(format("\n\ntotal time: %.2f\n", clock()-c))
138