all.lua 6.9 KB

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