all.lua 7.7 KB

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