all.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #!../lua
  2. local version = "Lua 5.2"
  3. if _VERSION ~= version then
  4. io.stderr:write("\nThis test suite is for ", version, ", not for ", _VERSION,
  5. "\nExiting tests\n")
  6. return
  7. end
  8. -- next variables control the execution of some tests
  9. -- true means no test (so an undefined variable does not skip a test)
  10. -- defaults are for Linux; test everything
  11. _soft = false -- true to avoid long or memory consuming tests
  12. _port = false -- true to avoid non-portable tests
  13. _no32 = false -- true to avoid tests that assume 32 bits
  14. _nomsg = false -- true to avoid messages about tests not performed
  15. _noposix = false -- false assumes LUA_USE_POSIX
  16. _nolonglong = false -- false assumes LUA_USE_LONGLONG
  17. _noformatA = false -- false assumes LUA_USE_AFORMAT
  18. local usertests = rawget(_G, "_U")
  19. if usertests then
  20. -- tests for sissies ;) Avoid problems
  21. _soft = true
  22. _port = true
  23. _no32 = true
  24. _nomsg = true
  25. _noposix = true
  26. _nolonglong = true
  27. _noformatA = true;
  28. end
  29. -- no "internal" tests for user tests
  30. if usertests then T = nil end
  31. T = rawget(_G, "T") -- avoid problems with 'strict' module
  32. package.path = "?;./?.lua" .. package.path
  33. math.randomseed(0)
  34. collectgarbage("setstepmul", 200)
  35. collectgarbage("setpause", 200)
  36. --[=[
  37. example of a long [comment],
  38. [[spanning several [lines]]]
  39. ]=]
  40. print("current path:\n****" .. package.path .. "****\n")
  41. local c = os.clock()
  42. local collectgarbage = collectgarbage
  43. do -- (
  44. -- track messages for tests not performed
  45. local msgs = {}
  46. function Message (m)
  47. if not _nomsg then
  48. print(m)
  49. msgs[#msgs+1] = string.sub(m, 3, -3)
  50. end
  51. end
  52. assert(os.setlocale"C")
  53. local T,print,format,write,assert,type,unpack,floor =
  54. T,print,string.format,io.write,assert,type,table.unpack,math.floor
  55. -- use K for 1000 and M for 1000000 (not 2^10 -- 2^20)
  56. local function F (m)
  57. local function round (m)
  58. m = m + 0.04999
  59. return m - (m % 0.1) -- keep one decimal digit
  60. end
  61. if m < 1000 then return m
  62. else
  63. m = m / 1000
  64. if m < 1000 then return round(m).."K"
  65. else
  66. return round(m/1000).."M"
  67. end
  68. end
  69. end
  70. local showmem
  71. if not T then
  72. local max = 0
  73. showmem = function ()
  74. local m = collectgarbage("count") * 1024
  75. max = (m > max) and m or max
  76. print(format(" ---- total memory: %s, max memory: %s ----\n",
  77. F(m), F(max)))
  78. end
  79. else
  80. showmem = function ()
  81. T.checkmemory()
  82. local total, numblocks, maxmem = T.totalmem()
  83. local count = collectgarbage("count")
  84. print(format(
  85. "\n ---- total memory: %s (%.0fK), max use: %s, blocks: %d\n",
  86. F(total), count, F(maxmem), numblocks))
  87. print(format("\t(strings: %d, tables: %d, functions: %d, "..
  88. "\n\tudata: %d, threads: %d)",
  89. T.totalmem"string", T.totalmem"table", T.totalmem"function",
  90. T.totalmem"userdata", T.totalmem"thread"))
  91. end
  92. end
  93. --
  94. -- redefine dofile to run files through dump/undump
  95. --
  96. local function report (n) print("\n***** FILE '"..n.."'*****") end
  97. local olddofile = dofile
  98. dofile = function (n)
  99. showmem()
  100. report(n)
  101. local f = assert(loadfile(n))
  102. local b = string.dump(f)
  103. f = assert(load(b))
  104. return f()
  105. end
  106. dofile('main.lua')
  107. do
  108. local next, setmetatable, stderr = next, setmetatable, io.stderr
  109. local mt = {}
  110. -- each time a table is collected, create a new one to be
  111. -- collected next cycle
  112. mt.__gc = function (o)
  113. stderr:write'.' -- mark progress
  114. local n = setmetatable({}, mt) -- replicate object
  115. o = nil
  116. local a,b,c,d,e = nil -- erase 'o' from the stack
  117. end
  118. local n = setmetatable({}, mt) -- replicate object
  119. end
  120. report"gc.lua"
  121. local f = assert(loadfile('gc.lua'))
  122. f()
  123. collectgarbage("generational")
  124. dofile('db.lua')
  125. assert(dofile('calls.lua') == deep and deep)
  126. olddofile('strings.lua')
  127. olddofile('literals.lua')
  128. assert(dofile('attrib.lua') == 27)
  129. collectgarbage("incremental") -- redo some tests in incremental mode
  130. olddofile('strings.lua')
  131. olddofile('literals.lua')
  132. dofile('constructs.lua')
  133. dofile('api.lua')
  134. collectgarbage("generational") -- back to generational mode
  135. collectgarbage("setpause", 200)
  136. collectgarbage("setmajorinc", 500)
  137. assert(dofile('locals.lua') == 5)
  138. dofile('constructs.lua')
  139. dofile('code.lua')
  140. if not _G._soft then
  141. report('big.lua')
  142. local f = coroutine.wrap(assert(loadfile('big.lua')))
  143. assert(f() == 'b')
  144. assert(f() == 'a')
  145. end
  146. dofile('nextvar.lua')
  147. dofile('pm.lua')
  148. dofile('api.lua')
  149. assert(dofile('events.lua') == 12)
  150. dofile('vararg.lua')
  151. dofile('closure.lua')
  152. dofile('coroutine.lua')
  153. dofile('goto.lua')
  154. dofile('errors.lua')
  155. dofile('math.lua')
  156. dofile('sort.lua')
  157. dofile('bitwise.lua')
  158. assert(dofile('verybig.lua') == 10); collectgarbage()
  159. dofile('files.lua')
  160. if #msgs > 0 then
  161. print("\ntests not performed:")
  162. for i=1,#msgs do
  163. print(msgs[i])
  164. end
  165. print()
  166. end
  167. print("final OK !!!")
  168. local debug = require "debug"
  169. debug.sethook(function (a) assert(type(a) == 'string') end, "cr")
  170. -- to survive outside block
  171. _G.showmem = showmem
  172. end --)
  173. local _G, showmem, print, format, clock, assert, open =
  174. _G, showmem, print, string.format, os.clock, 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 time = clock() - c
  202. print(format("\n\ntotal time: %.2f\n", time))
  203. if not usertests then
  204. lasttime = lasttime or time -- if there is no last time, ignore difference
  205. -- check whether current test time differs more than 5% from last time
  206. local diff = (time - lasttime) / time
  207. local tolerance = 0.05 -- 5%
  208. assert(diff < tolerance and diff > -tolerance)
  209. assert(open(fname, "w")):write(time):close()
  210. end