1#!../lua
  2-- $Id: testes/all.lua $
  3-- See Copyright Notice at the end of this file
  4
  5
  6local version = "Lua 5.4"
  7if _VERSION ~= version then
  8  io.stderr:write("This test suite is for ", version,
  9                  ", not for ", _VERSION, "\nExiting tests")
 10  return
 11end
 12
 13
 14_G.ARG = arg   -- save arg for other tests
 15
 16
 17-- next variables control the execution of some tests
 18-- true means no test (so an undefined variable does not skip a test)
 19-- defaults are for Linux; test everything.
 20-- Make true to avoid long or memory consuming tests
 21_soft = rawget(_G, "_soft") or false
 22-- Make true to avoid non-portable tests
 23_port = rawget(_G, "_port") or false
 24-- Make true to avoid messages about tests not performed
 25_nomsg = rawget(_G, "_nomsg") or false
 26
 27
 28local usertests = rawget(_G, "_U")
 29
 30if usertests then
 31  -- tests for sissies ;)  Avoid problems
 32  _soft = true
 33  _port = true
 34  _nomsg = true
 35end
 36
 37-- tests should require debug when needed
 38debug = nil
 39
 40
 41if usertests then
 42  T = nil    -- no "internal" tests for user tests
 43else
 44  T = rawget(_G, "T")  -- avoid problems with 'strict' module
 45end
 46
 47
 48--[=[
 49  example of a long [comment],
 50  [[spanning several [lines]]]
 51
 52]=]
 53
 54print("\n\tStarting Tests")
 55
 56do
 57  -- set random seed
 58  local random_x, random_y = math.randomseed()
 59  print(string.format("random seeds: %d, %d", random_x, random_y))
 60end
 61
 62print("current path:\n****" .. package.path .. "****\n")
 63
 64
 65local initclock = os.clock()
 66local lastclock = initclock
 67local walltime = os.time()
 68
 69local collectgarbage = collectgarbage
 70
 71do   -- (
 72
 73-- track messages for tests not performed
 74local msgs = {}
 75function Message (m)
 76  if not _nomsg then
 77    print(m)
 78    msgs[#msgs+1] = string.sub(m, 3, -3)
 79  end
 80end
 81
 82assert(os.setlocale"C")
 83
 84local T,print,format,write,assert,type,unpack,floor =
 85      T,print,string.format,io.write,assert,type,table.unpack,math.floor
 86
 87-- use K for 1000 and M for 1000000 (not 2^10 -- 2^20)
 88local function F (m)
 89  local function round (m)
 90    m = m + 0.04999
 91    return format("%.1f", m)      -- keep one decimal digit
 92  end
 93  if m < 1000 then return m
 94  else
 95    m = m / 1000
 96    if m < 1000 then return round(m).."K"
 97    else
 98      return round(m/1000).."M"
 99    end
100  end
101end
102
103local Cstacklevel
104
105local showmem
106if not T then
107  local max = 0
108  showmem = function ()
109    local m = collectgarbage("count") * 1024
110    max = (m > max) and m or max
111    print(format("    ---- total memory: %s, max memory: %s ----\n",
112          F(m), F(max)))
113  end
114  Cstacklevel = function () return 0 end   -- no info about stack level
115else
116  showmem = function ()
117    T.checkmemory()
118    local total, numblocks, maxmem = T.totalmem()
119    local count = collectgarbage("count")
120    print(format(
121      "\n    ---- total memory: %s (%.0fK), max use: %s,  blocks: %d\n",
122      F(total), count, F(maxmem), numblocks))
123    print(format("\t(strings:  %d, tables: %d, functions: %d, "..
124                 "\n\tudata: %d, threads: %d)",
125                 T.totalmem"string", T.totalmem"table", T.totalmem"function",
126                 T.totalmem"userdata", T.totalmem"thread"))
127  end
128
129  Cstacklevel = function ()
130    local _, _, ncalls = T.stacklevel()
131    return ncalls    -- number of C calls
132  end
133end
134
135
136local Cstack = Cstacklevel()
137
138--
139-- redefine dofile to run files through dump/undump
140--
141local function report (n) print("\n***** FILE '"..n.."'*****") end
142local olddofile = dofile
143local dofile = function (n, strip)
144  showmem()
145  local c = os.clock()
146  print(string.format("time: %g (+%g)", c - initclock, c - lastclock))
147  lastclock = c
148  report(n)
149  local f = assert(loadfile(n))
150  local b = string.dump(f, strip)
151  f = assert(load(b))
152  return f()
153end
154
155dofile('main.lua')
156
157-- trace GC cycles
158require"tracegc".start()
159
160report"gc.lua"
161local f = assert(loadfile('gc.lua'))
162f()
163
164dofile('db.lua')
165assert(dofile('calls.lua') == deep and deep)
166_G.deep = nil
167olddofile('strings.lua')
168olddofile('literals.lua')
169dofile('tpack.lua')
170assert(dofile('attrib.lua') == 27)
171dofile('gengc.lua')
172assert(dofile('locals.lua') == 5)
173dofile('constructs.lua')
174dofile('code.lua', true)
175if not _G._soft then
176  report('big.lua')
177  local f = coroutine.wrap(assert(loadfile('big.lua')))
178  assert(f() == 'b')
179  assert(f() == 'a')
180end
181dofile('cstack.lua')
182dofile('nextvar.lua')
183dofile('pm.lua')
184dofile('utf8.lua')
185dofile('api.lua')
186assert(dofile('events.lua') == 12)
187dofile('vararg.lua')
188dofile('closure.lua')
189dofile('coroutine.lua')
190dofile('goto.lua', true)
191dofile('errors.lua')
192dofile('math.lua')
193dofile('sort.lua', true)
194dofile('bitwise.lua')
195assert(dofile('verybig.lua', true) == 10); collectgarbage()
196dofile('files.lua')
197
198if #msgs > 0 then
199  local m = table.concat(msgs, "\n  ")
200  warn("#tests not performed:\n  ", m, "\n")
201end
202
203print("(there should be two warnings now)")
204warn("@on")
205warn("#This is ", "an expected", " warning")
206warn("@off")
207warn("******** THIS WARNING SHOULD NOT APPEAR **********")
208warn("******** THIS WARNING ALSO SHOULD NOT APPEAR **********")
209warn("@on")
210warn("#This is", " another one")
211
212-- no test module should define 'debug'
213assert(debug == nil)
214
215local debug = require "debug"
216
217print(string.format("%d-bit integers, %d-bit floats",
218        string.packsize("j") * 8, string.packsize("n") * 8))
219
220debug.sethook(function (a) assert(type(a) == 'string') end, "cr")
221
222-- to survive outside block
223_G.showmem = showmem
224
225
226assert(Cstack == Cstacklevel(),
227  "should be at the same C-stack level it was when started the tests")
228
229end   --)
230
231local _G, showmem, print, format, clock, time, difftime,
232      assert, open, warn =
233      _G, showmem, print, string.format, os.clock, os.time, os.difftime,
234      assert, io.open, warn
235
236-- file with time of last performed test
237local fname = T and "time-debug.txt" or "time.txt"
238local lasttime
239
240if not usertests then
241  -- open file with time of last performed test
242  local f = io.open(fname)
243  if f then
244    lasttime = assert(tonumber(f:read'a'))
245    f:close();
246  else   -- no such file; assume it is recording time for first time
247    lasttime = nil
248  end
249end
250
251-- erase (almost) all globals
252print('cleaning all!!!!')
253for n in pairs(_G) do
254  if not ({___Glob = 1, tostring = 1})[n] then
255    _G[n] = undef
256  end
257end
258
259
260collectgarbage()
261collectgarbage()
262collectgarbage()
263collectgarbage()
264collectgarbage()
265collectgarbage();showmem()
266
267local clocktime = clock() - initclock
268walltime = difftime(time(), walltime)
269
270print(format("\n\ntotal time: %.2fs (wall time: %gs)\n", clocktime, walltime))
271
272if not usertests then
273  lasttime = lasttime or clocktime    -- if no last time, ignore difference
274  -- check whether current test time differs more than 5% from last time
275  local diff = (clocktime - lasttime) / lasttime
276  local tolerance = 0.05    -- 5%
277  if (diff >= tolerance or diff <= -tolerance) then
278    warn(format("#time difference from previous test: %+.1f%%",
279                  diff * 100))
280  end
281  assert(open(fname, "w")):write(clocktime):close()
282end
283
284print("final OK !!!")
285
286
287
288--[[
289*****************************************************************************
290* Copyright (C) 1994-2016 Lua.org, PUC-Rio.
291*
292* Permission is hereby granted, free of charge, to any person obtaining
293* a copy of this software and associated documentation files (the
294* "Software"), to deal in the Software without restriction, including
295* without limitation the rights to use, copy, modify, merge, publish,
296* distribute, sublicense, and/or sell copies of the Software, and to
297* permit persons to whom the Software is furnished to do so, subject to
298* the following conditions:
299*
300* The above copyright notice and this permission notice shall be
301* included in all copies or substantial portions of the Software.
302*
303* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
304* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
305* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
306* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
307* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
308* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
309* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
310*****************************************************************************
311]]
312