all.lua 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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, nci = T.stacklevel()
  102. return ncalls + nci -- number of free slots in the C stack
  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. do
  124. local next, setmetatable, stderr = next, setmetatable, io.stderr
  125. -- track collections
  126. local mt = {}
  127. -- each time a table is collected, remark it for finalization
  128. -- on next cycle
  129. mt.__gc = function (o)
  130. stderr:write'.' -- mark progress
  131. local n = setmetatable(o, mt) -- remark it
  132. end
  133. local n = setmetatable({}, mt) -- create object
  134. end
  135. report"gc.lua"
  136. local f = assert(loadfile('gc.lua'))
  137. f()
  138. dofile('db.lua')
  139. assert(dofile('calls.lua') == deep and deep)
  140. olddofile('strings.lua')
  141. olddofile('literals.lua')
  142. dofile('tpack.lua')
  143. assert(dofile('attrib.lua') == 27)
  144. dofile('gengc.lua')
  145. assert(dofile('locals.lua') == 5)
  146. dofile('constructs.lua')
  147. dofile('code.lua', true)
  148. if not _G._soft then
  149. report('big.lua')
  150. local f = coroutine.wrap(assert(loadfile('big.lua')))
  151. assert(f() == 'b')
  152. assert(f() == 'a')
  153. end
  154. dofile('cstack.lua')
  155. dofile('nextvar.lua')
  156. dofile('pm.lua')
  157. dofile('utf8.lua')
  158. dofile('api.lua')
  159. assert(dofile('events.lua') == 12)
  160. dofile('vararg.lua')
  161. dofile('closure.lua')
  162. dofile('coroutine.lua')
  163. dofile('goto.lua', true)
  164. dofile('errors.lua')
  165. dofile('math.lua')
  166. dofile('sort.lua', true)
  167. dofile('bitwise.lua')
  168. assert(dofile('verybig.lua', true) == 10); collectgarbage()
  169. dofile('files.lua')
  170. if #msgs > 0 then
  171. local m = table.concat(msgs, "\n ")
  172. warn("#tests not performed:\n ", m, "\n")
  173. end
  174. print("(there should be two warnings now)")
  175. warn("@on")
  176. warn("#This is ", "an expected", " warning")
  177. warn("@off")
  178. warn("******** THIS WARNING SHOULD NOT APPEAR **********")
  179. warn("******** THIS WARNING ALSO SHOULD NOT APPEAR **********")
  180. warn("@on")
  181. warn("#This is", " another one")
  182. -- no test module should define 'debug'
  183. assert(debug == nil)
  184. local debug = require "debug"
  185. print(string.format("%d-bit integers, %d-bit floats",
  186. string.packsize("j") * 8, string.packsize("n") * 8))
  187. debug.sethook(function (a) assert(type(a) == 'string') end, "cr")
  188. -- to survive outside block
  189. _G.showmem = showmem
  190. assert(Cstack == Cstacklevel(),
  191. "should be at the same C-stack level it was when started the tests")
  192. end --)
  193. local _G, showmem, print, format, clock, time, difftime,
  194. assert, open, warn =
  195. _G, showmem, print, string.format, os.clock, os.time, os.difftime,
  196. assert, io.open, warn
  197. -- file with time of last performed test
  198. local fname = T and "time-debug.txt" or "time.txt"
  199. local lasttime
  200. if not usertests then
  201. -- open file with time of last performed test
  202. local f = io.open(fname)
  203. if f then
  204. lasttime = assert(tonumber(f:read'a'))
  205. f:close();
  206. else -- no such file; assume it is recording time for first time
  207. lasttime = nil
  208. end
  209. end
  210. -- erase (almost) all globals
  211. print('cleaning all!!!!')
  212. for n in pairs(_G) do
  213. if not ({___Glob = 1, tostring = 1})[n] then
  214. _G[n] = undef
  215. end
  216. end
  217. collectgarbage()
  218. collectgarbage()
  219. collectgarbage()
  220. collectgarbage()
  221. collectgarbage()
  222. collectgarbage();showmem()
  223. local clocktime = clock() - initclock
  224. walltime = difftime(time(), walltime)
  225. print(format("\n\ntotal time: %.2fs (wall time: %gs)\n", clocktime, walltime))
  226. if not usertests then
  227. lasttime = lasttime or clocktime -- if no last time, ignore difference
  228. -- check whether current test time differs more than 5% from last time
  229. local diff = (clocktime - lasttime) / lasttime
  230. local tolerance = 0.05 -- 5%
  231. if (diff >= tolerance or diff <= -tolerance) then
  232. warn(format("#time difference from previous test: %+.1f%%",
  233. diff * 100))
  234. end
  235. assert(open(fname, "w")):write(clocktime):close()
  236. end
  237. print("final OK !!!")
  238. --[[
  239. *****************************************************************************
  240. * Copyright (C) 1994-2016 Lua.org, PUC-Rio.
  241. *
  242. * Permission is hereby granted, free of charge, to any person obtaining
  243. * a copy of this software and associated documentation files (the
  244. * "Software"), to deal in the Software without restriction, including
  245. * without limitation the rights to use, copy, modify, merge, publish,
  246. * distribute, sublicense, and/or sell copies of the Software, and to
  247. * permit persons to whom the Software is furnished to do so, subject to
  248. * the following conditions:
  249. *
  250. * The above copyright notice and this permission notice shall be
  251. * included in all copies or substantial portions of the Software.
  252. *
  253. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  254. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  255. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  256. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  257. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  258. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  259. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  260. *****************************************************************************
  261. ]]