interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / testes / all.lua

289 lines · 7047 bytes Raw Blame History
1#!../lua
2-- $Id: testes/all.lua $
3-- See Copyright Notice in file lua.h
4
5global <const> *
6
7global _soft, _port, _nomsg
8global T
9
10local version = "Lua 5.5"
11if _VERSION ~= version then
12 io.stderr:write("This test suite is for ", version,
13 ", not for ", _VERSION, "\nExiting tests")
14 return
15end
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
32local usertests = rawget(_G, "_U")
33
34if 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
38end
39
40-- tests should require debug when needed
41global debug; debug = nil
42
43
44if usertests then
45 T = nil -- no "internal" tests for user tests
46else
47 T = rawget(_G, "T") -- avoid problems with 'strict' module
48end
49
50
51--[=[
52 example of a long [comment],
53 [[spanning several [lines]]]
54
55]=]
56
57print("\n\tStarting Tests")
58
59do
60 -- set random seed
61 local random_x, random_y = math.randomseed()
62 print(string.format("random seeds: %d, %d", random_x, random_y))
63end
64
65print("current path:\n****" .. package.path .. "****\n")
66
67
68local initclock = os.clock()
69local lastclock = initclock
70local walltime = os.time()
71
72local collectgarbage = collectgarbage
73
74do -- (
75
76-- track messages for tests not performed
77local msgs = {}
78global function Message (m)
79 if not _nomsg then
80 print(m)
81 msgs[#msgs+1] = string.sub(m, 3, -3)
82 end
83end
84
85assert(os.setlocale"C")
86
87local 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)
91local 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
104end
105
106local Cstacklevel
107
108local showmem
109if 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
118else
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
136end
137
138
139local Cstack = Cstacklevel()
140
141--
142-- redefine dofile to run files through dump/undump
143--
144local function report (n) print("\n***** FILE '"..n.."'*****") end
145local olddofile = dofile
146local 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()
156end
157
158dofile('main.lua')
159
160-- trace GC cycles
161require"tracegc".start()
162
163report"gc.lua"
164local f = assert(loadfile('gc.lua'))
165f()
166
167dofile('db.lua')
168assert(dofile('calls.lua') == deep and deep)
169_G.deep = nil
170olddofile('strings.lua')
171olddofile('literals.lua')
172dofile('tpack.lua')
173assert(dofile('attrib.lua') == 27)
174dofile('gengc.lua')
175assert(dofile('locals.lua') == 5)
176dofile('constructs.lua')
177dofile('code.lua', true)
178if 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')
183end
184dofile('cstack.lua')
185dofile('nextvar.lua')
186dofile('pm.lua')
187dofile('utf8.lua')
188dofile('api.lua')
189dofile('memerr.lua')
190assert(dofile('events.lua') == 12)
191dofile('vararg.lua')
192dofile('closure.lua')
193dofile('coroutine.lua')
194dofile('goto.lua', true)
195dofile('errors.lua')
196dofile('math.lua')
197dofile('sort.lua', true)
198dofile('bitwise.lua')
199assert(dofile('verybig.lua', true) == 10); collectgarbage()
200dofile('files.lua')
201
202if #msgs > 0 then
203 local m = table.concat(msgs, "\n ")
204 warn("#tests not performed:\n ", m, "\n")
205end
206
207print("(there should be two warnings now)")
208warn("@on")
209warn("#This is ", "an expected", " warning")
210warn("@off")
211warn("******** THIS WARNING SHOULD NOT APPEAR **********")
212warn("******** THIS WARNING ALSO SHOULD NOT APPEAR **********")
213warn("@on")
214warn("#This is", " another one")
215
216-- no test module should define 'debug'
217assert(debug == nil)
218
219local debug = require "debug"
220
221print(string.format("%d-bit integers, %d-bit floats",
222 string.packsize("j") * 8, string.packsize("n") * 8))
223
224debug.sethook(function (a) assert(type(a) == 'string') end, "cr")
225
226-- to survive outside block
227_G.showmem = showmem
228
229
230assert(Cstack == Cstacklevel(),
231 "should be at the same C-stack level it was when started the tests")
232
233end --)
234
235local _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
241local fname = T and "time-debug.txt" or "time.txt"
242local lasttime
243
244if 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
253end
254
255-- erase (almost) all globals
256print('cleaning all!!!!')
257for n in pairs(_G) do
258 if not ({___Glob = 1, tostring = 1})[n] then
259 _G[n] = undef
260 end
261end
262
263
264collectgarbage()
265collectgarbage()
266collectgarbage()
267collectgarbage()
268collectgarbage()
269collectgarbage();showmem()
270
271local clocktime = clock() - initclock
272walltime = difftime(time(), walltime)
273
274print(format("\n\ntotal time: %.2fs (wall time: %gs)\n", clocktime, walltime))
275
276if 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()
286end
287
288print("final OK !!!")
289