all.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #!../lua
  2. -- $Id: all.lua,v 1.95 2016/11/07 13:11:28 roberto Exp $
  3. -- See Copyright Notice at the end of this file
  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. _G._ARG = arg -- save arg for other tests
  11. -- next variables control the execution of some tests
  12. -- true means no test (so an undefined variable does not skip a test)
  13. -- defaults are for Linux; test everything.
  14. -- Make true to avoid long or memory consuming tests
  15. _soft = rawget(_G, "_soft") or false
  16. -- Make true to avoid non-portable tests
  17. _port = rawget(_G, "_port") or false
  18. -- Make true to avoid messages about tests not performed
  19. _nomsg = rawget(_G, "_nomsg") or false
  20. local usertests = rawget(_G, "_U")
  21. if usertests then
  22. -- tests for sissies ;) Avoid problems
  23. _soft = true
  24. _port = true
  25. _nomsg = true
  26. end
  27. -- tests should require debug when needed
  28. debug = nil
  29. if usertests then
  30. T = nil -- no "internal" tests for user tests
  31. else
  32. T = rawget(_G, "T") -- avoid problems with 'strict' module
  33. end
  34. math.randomseed(0)
  35. --[=[
  36. example of a long [comment],
  37. [[spanning several [lines]]]
  38. ]=]
  39. print("current path:\n****" .. package.path .. "****\n")
  40. local initclock = os.clock()
  41. local lastclock = initclock
  42. local walltime = os.time()
  43. local collectgarbage = collectgarbage
  44. do -- (
  45. -- track messages for tests not performed
  46. local msgs = {}
  47. function Message (m)
  48. if not _nomsg then
  49. print(m)
  50. msgs[#msgs+1] = string.sub(m, 3, -3)
  51. end
  52. end
  53. assert(os.setlocale"C")
  54. local T,print,format,write,assert,type,unpack,floor =
  55. T,print,string.format,io.write,assert,type,table.unpack,math.floor
  56. -- use K for 1000 and M for 1000000 (not 2^10 -- 2^20)
  57. local function F (m)
  58. local function round (m)
  59. m = m + 0.04999
  60. return format("%.1f", m) -- keep one decimal digit
  61. end
  62. if m < 1000 then return m
  63. else
  64. m = m / 1000
  65. if m < 1000 then return round(m).."K"
  66. else
  67. return round(m/1000).."M"
  68. end
  69. end
  70. end
  71. local showmem
  72. if not T then
  73. local max = 0
  74. showmem = function ()
  75. local m = collectgarbage("count") * 1024
  76. max = (m > max) and m or max
  77. print(format(" ---- total memory: %s, max memory: %s ----\n",
  78. F(m), F(max)))
  79. end
  80. else
  81. showmem = function ()
  82. T.checkmemory()
  83. local total, numblocks, maxmem = T.totalmem()
  84. local count = collectgarbage("count")
  85. print(format(
  86. "\n ---- total memory: %s (%.0fK), max use: %s, blocks: %d\n",
  87. F(total), count, F(maxmem), numblocks))
  88. print(format("\t(strings: %d, tables: %d, functions: %d, "..
  89. "\n\tudata: %d, threads: %d)",
  90. T.totalmem"string", T.totalmem"table", T.totalmem"function",
  91. T.totalmem"userdata", T.totalmem"thread"))
  92. end
  93. end
  94. --
  95. -- redefine dofile to run files through dump/undump
  96. --
  97. local function report (n) print("\n***** FILE '"..n.."'*****") end
  98. local olddofile = dofile
  99. local dofile = function (n, strip)
  100. showmem()
  101. local c = os.clock()
  102. print(string.format("time: %g (+%g)", c - initclock, c - lastclock))
  103. lastclock = c
  104. report(n)
  105. local f = assert(loadfile(n))
  106. local b = string.dump(f, strip)
  107. f = assert(load(b))
  108. return f()
  109. end
  110. dofile('main.lua')
  111. do
  112. local next, setmetatable, stderr = next, setmetatable, io.stderr
  113. -- track collections
  114. local mt = {}
  115. -- each time a table is collected, remark it for finalization
  116. -- on next cycle
  117. mt.__gc = function (o)
  118. stderr:write'.' -- mark progress
  119. local n = setmetatable(o, mt) -- remark it
  120. end
  121. local n = setmetatable({}, mt) -- create object
  122. end
  123. report"gc.lua"
  124. local f = assert(loadfile('gc.lua'))
  125. f()
  126. dofile('db.lua')
  127. assert(dofile('calls.lua') == deep and deep)
  128. olddofile('strings.lua')
  129. olddofile('literals.lua')
  130. dofile('tpack.lua')
  131. assert(dofile('attrib.lua') == 27)
  132. assert(dofile('locals.lua') == 5)
  133. dofile('constructs.lua')
  134. dofile('code.lua', true)
  135. if not _G._soft then
  136. report('big.lua')
  137. local f = coroutine.wrap(assert(loadfile('big.lua')))
  138. assert(f() == 'b')
  139. assert(f() == 'a')
  140. end
  141. dofile('nextvar.lua')
  142. dofile('pm.lua')
  143. dofile('utf8.lua')
  144. dofile('api.lua')
  145. assert(dofile('events.lua') == 12)
  146. dofile('vararg.lua')
  147. dofile('closure.lua')
  148. dofile('coroutine.lua')
  149. dofile('goto.lua', true)
  150. dofile('errors.lua')
  151. dofile('math.lua')
  152. dofile('sort.lua', true)
  153. dofile('bitwise.lua')
  154. assert(dofile('verybig.lua', true) == 10); collectgarbage()
  155. dofile('files.lua')
  156. if #msgs > 0 then
  157. print("\ntests not performed:")
  158. for i=1,#msgs do
  159. print(msgs[i])
  160. end
  161. print()
  162. end
  163. -- no test module should define 'debug'
  164. assert(debug == nil)
  165. local debug = require "debug"
  166. print(string.format("%d-bit integers, %d-bit floats",
  167. string.packsize("j") * 8, string.packsize("n") * 8))
  168. debug.sethook(function (a) assert(type(a) == 'string') end, "cr")
  169. -- to survive outside block
  170. _G.showmem = showmem
  171. end --)
  172. local _G, showmem, print, format, clock, time, difftime, assert, open =
  173. _G, showmem, print, string.format, os.clock, os.time, os.difftime,
  174. assert, io.open
  175. -- file with time of last performed test
  176. local fname = T and "time-debug.txt" or "time.txt"
  177. local lasttime
  178. if not usertests then
  179. -- open file with time of last performed test
  180. local f = io.open(fname)
  181. if f then
  182. lasttime = assert(tonumber(f:read'a'))
  183. f:close();
  184. else -- no such file; assume it is recording time for first time
  185. lasttime = nil
  186. end
  187. end
  188. -- erase (almost) all globals
  189. print('cleaning all!!!!')
  190. for n in pairs(_G) do
  191. if not ({___Glob = 1, tostring = 1})[n] then
  192. _G[n] = nil
  193. end
  194. end
  195. collectgarbage()
  196. collectgarbage()
  197. collectgarbage()
  198. collectgarbage()
  199. collectgarbage()
  200. collectgarbage();showmem()
  201. local clocktime = clock() - initclock
  202. walltime = difftime(time(), walltime)
  203. print(format("\n\ntotal time: %.2fs (wall time: %gs)\n", clocktime, walltime))
  204. if not usertests then
  205. lasttime = lasttime or clocktime -- if no last time, ignore difference
  206. -- check whether current test time differs more than 5% from last time
  207. local diff = (clocktime - lasttime) / lasttime
  208. local tolerance = 0.05 -- 5%
  209. if (diff >= tolerance or diff <= -tolerance) then
  210. print(format("WARNING: time difference from previous test: %+.1f%%",
  211. diff * 100))
  212. end
  213. assert(open(fname, "w")):write(clocktime):close()
  214. end
  215. print("final OK !!!")
  216. --[[
  217. *****************************************************************************
  218. * Copyright (C) 1994-2016 Lua.org, PUC-Rio.
  219. *
  220. * Permission is hereby granted, free of charge, to any person obtaining
  221. * a copy of this software and associated documentation files (the
  222. * "Software"), to deal in the Software without restriction, including
  223. * without limitation the rights to use, copy, modify, merge, publish,
  224. * distribute, sublicense, and/or sell copies of the Software, and to
  225. * permit persons to whom the Software is furnished to do so, subject to
  226. * the following conditions:
  227. *
  228. * The above copyright notice and this permission notice shall be
  229. * included in all copies or substantial portions of the Software.
  230. *
  231. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  232. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  233. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  234. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  235. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  236. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  237. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  238. *****************************************************************************
  239. ]]