all.lua 8.0 KB

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