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