github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/testdata/lua-5.3.3-tests/all.lua (about)

     1  #!../lua
     2  -- $Id: all.lua,v 1.94 2015/09/17 16:45:19 roberto Exp $
     3  
     4  local version = "Lua 5.3"
     5  if _VERSION ~= version then
     6    io.stderr:write("\nThis test suite is for ", version, ", not for ", _VERSION,
     7      "\nExiting tests\n")
     8    return
     9  end
    10  
    11  
    12  _G._ARG = arg   -- save arg for other tests
    13  
    14  
    15  -- next variables control the execution of some tests
    16  -- true means no test (so an undefined variable does not skip a test)
    17  -- defaults are for Linux; test everything.
    18  -- Make true to avoid long or memory consuming tests
    19  _soft = rawget(_G, "_soft") or false
    20  -- Make true to avoid non-portable tests
    21  _port = rawget(_G, "_port") or false
    22  -- Make true to avoid messages about tests not performed
    23  _nomsg = rawget(_G, "_nomsg") or false
    24  
    25  
    26  local usertests = rawget(_G, "_U")
    27  
    28  if usertests then
    29    -- tests for sissies ;)  Avoid problems
    30    _soft = true
    31    _port = true
    32    _nomsg = true
    33  end
    34  
    35  -- tests should require debug when needed
    36  debug = nil
    37  
    38  if usertests then
    39    T = nil    -- no "internal" tests for user tests
    40  else
    41    T = rawget(_G, "T")  -- avoid problems with 'strict' module
    42  end
    43  
    44  math.randomseed(0)
    45  
    46  --[=[
    47    example of a long [comment],
    48    [[spanning several [lines]]]
    49  
    50  ]=]
    51  
    52  print("current path:\n****" .. package.path .. "****\n")
    53  
    54  
    55  local initclock = os.clock()
    56  local lastclock = initclock
    57  local walltime = os.time()
    58  
    59  local collectgarbage = collectgarbage
    60  
    61  do   -- (
    62  
    63  -- track messages for tests not performed
    64  local msgs = {}
    65  function Message (m)
    66    if not _nomsg then
    67      print(m)
    68      msgs[#msgs+1] = string.sub(m, 3, -3)
    69    end
    70  end
    71  
    72  -- assert(os.setlocale"C")
    73  
    74  local T,print,format,write,assert,type,unpack,floor =
    75        T,print,string.format,io.write,assert,type,table.unpack,math.floor
    76  
    77  -- use K for 1000 and M for 1000000 (not 2^10 -- 2^20)
    78  local function F (m)
    79    local function round (m)
    80      m = m + 0.04999
    81      return format("%.1f", m)      -- keep one decimal digit
    82    end
    83    if m < 1000 then return m
    84    else
    85      m = m / 1000
    86      if m < 1000 then return round(m).."K"
    87      else
    88        return round(m/1000).."M"
    89      end
    90    end
    91  end
    92  
    93  local showmem
    94  if not T then
    95    local max = 0
    96    showmem = function ()
    97      local m = collectgarbage("count") * 1024
    98      max = (m > max) and m or max
    99      print(format("    ---- total memory: %s, max memory: %s ----\n",
   100            F(m), F(max)))
   101    end
   102  else
   103    showmem = function ()
   104      T.checkmemory()
   105      local total, numblocks, maxmem = T.totalmem()
   106      local count = collectgarbage("count")
   107      print(format(
   108        "\n    ---- total memory: %s (%.0fK), max use: %s,  blocks: %d\n",
   109        F(total), count, F(maxmem), numblocks))
   110      print(format("\t(strings:  %d, tables: %d, functions: %d, "..
   111                   "\n\tudata: %d, threads: %d)",
   112                   T.totalmem"string", T.totalmem"table", T.totalmem"function",
   113                   T.totalmem"userdata", T.totalmem"thread"))
   114    end
   115  end
   116  
   117  
   118  --
   119  -- redefine dofile to run files through dump/undump
   120  --
   121  local function report (n) print("\n***** FILE '"..n.."'*****") end
   122  local olddofile = dofile
   123  local dofile = function (n, strip)
   124    showmem()
   125    local c = os.clock()
   126    print(string.format("time: %g (+%g)", c - initclock, c - lastclock))
   127    lastclock = c
   128    report(n)
   129    local f = assert(loadfile(n))
   130    local b = string.dump(f, strip)
   131    f = assert(load(b))
   132    return f()
   133  end
   134  
   135  dofile('main.lua')
   136  
   137  do
   138    local next, setmetatable, stderr = next, setmetatable, io.stderr
   139    -- track collections
   140    local mt = {}
   141    -- each time a table is collected, remark it for finalization
   142    -- on next cycle
   143    mt.__gc = function (o)
   144       stderr:write'.'    -- mark progress
   145       local n = setmetatable(o, mt)   -- remark it
   146     end
   147     local n = setmetatable({}, mt)    -- create object
   148  end
   149  
   150  report"gc.lua"
   151  local f = assert(loadfile('gc.lua'))
   152  f()
   153  
   154  dofile('db.lua')
   155  assert(dofile('calls.lua') == deep and deep)
   156  olddofile('strings.lua')
   157  olddofile('literals.lua')
   158  dofile('tpack.lua')
   159  assert(dofile('attrib.lua') == 27)
   160  
   161  assert(dofile('locals.lua') == 5)
   162  dofile('constructs.lua')
   163  dofile('code.lua', true)
   164  if not _G._soft then
   165    report('big.lua')
   166    local f = coroutine.wrap(assert(loadfile('big.lua')))
   167    assert(f() == 'b')
   168    assert(f() == 'a')
   169  end
   170  dofile('nextvar.lua')
   171  dofile('pm.lua')
   172  dofile('utf8.lua')
   173  dofile('api.lua')
   174  assert(dofile('events.lua') == 12)
   175  dofile('vararg.lua')
   176  dofile('closure.lua')
   177  dofile('coroutine.lua')
   178  dofile('goto.lua', true)
   179  dofile('errors.lua')
   180  dofile('math.lua')
   181  dofile('sort.lua', true)
   182  dofile('bitwise.lua')
   183  assert(dofile('verybig.lua', true) == 10); collectgarbage()
   184  dofile('files.lua')
   185  
   186  if #msgs > 0 then
   187    print("\ntests not performed:")
   188    for i=1,#msgs do
   189      print(msgs[i])
   190    end
   191    print()
   192  end
   193  
   194  -- no test module should define 'debug'
   195  assert(debug == nil)
   196  
   197  local debug = require "debug"
   198  
   199  print(string.format("%d-bit integers, %d-bit floats",
   200          string.packsize("j") * 8, string.packsize("n") * 8))
   201  
   202  debug.sethook(function (a) assert(type(a) == 'string') end, "cr")
   203  
   204  -- to survive outside block
   205  _G.showmem = showmem
   206  
   207  end   --)
   208  
   209  local _G, showmem, print, format, clock, time, difftime, assert, open =
   210        _G, showmem, print, string.format, os.clock, os.time, os.difftime,
   211        assert, io.open
   212  
   213  -- file with time of last performed test
   214  local fname = T and "time-debug.txt" or "time.txt"
   215  local lasttime
   216  
   217  if not usertests then
   218    -- open file with time of last performed test
   219    local f = io.open(fname)
   220    if f then
   221      lasttime = assert(tonumber(f:read'a'))
   222      f:close();
   223    else   -- no such file; assume it is recording time for first time
   224      lasttime = nil
   225    end
   226  end
   227  
   228  -- erase (almost) all globals
   229  print('cleaning all!!!!')
   230  for n in pairs(_G) do
   231    if not ({___Glob = 1, tostring = 1})[n] then
   232      _G[n] = nil
   233    end
   234  end
   235  
   236  
   237  collectgarbage()
   238  collectgarbage()
   239  collectgarbage()
   240  collectgarbage()
   241  collectgarbage()
   242  collectgarbage();showmem()
   243  
   244  local clocktime = clock() - initclock
   245  walltime = difftime(time(), walltime)
   246  
   247  print(format("\n\ntotal time: %.2fs (wall time: %gs)\n", clocktime, walltime))
   248  
   249  if not usertests then
   250    lasttime = lasttime or clocktime    -- if no last time, ignore difference
   251    -- check whether current test time differs more than 5% from last time
   252    local diff = (clocktime - lasttime) / lasttime
   253    local tolerance = 0.05    -- 5%
   254    if (diff >= tolerance or diff <= -tolerance) then
   255      print(format("WARNING: time difference from previous test: %+.1f%%",
   256                    diff * 100))
   257    end
   258    assert(open(fname, "w")):write(clocktime):close()
   259  end
   260  
   261  print("final OK !!!")
   262