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