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