diff options
Diffstat (limited to 'testes')
-rw-r--r-- | testes/code.lua | 82 | ||||
-rw-r--r-- | testes/db.lua | 2 | ||||
-rw-r--r-- | testes/events.lua | 2 |
3 files changed, 55 insertions, 31 deletions
diff --git a/testes/code.lua b/testes/code.lua index 4d44fa6a..834ff5e2 100644 --- a/testes/code.lua +++ b/testes/code.lua | |||
@@ -40,6 +40,7 @@ checkKlist(foo, {3.78/4, -3.78/4, -3.79/4}) | |||
40 | 40 | ||
41 | -- testing opcodes | 41 | -- testing opcodes |
42 | 42 | ||
43 | -- check that 'f' opcodes match '...' | ||
43 | function check (f, ...) | 44 | function check (f, ...) |
44 | local arg = {...} | 45 | local arg = {...} |
45 | local c = T.listcode(f) | 46 | local c = T.listcode(f) |
@@ -52,9 +53,19 @@ function check (f, ...) | |||
52 | end | 53 | end |
53 | 54 | ||
54 | 55 | ||
56 | -- check that 'f' opcodes match '...' and that 'f(p) == r'. | ||
57 | function checkR (f, p, r, ...) | ||
58 | local r1 = f(p) | ||
59 | assert(r == r1 and math.type(r) == math.type(r1)) | ||
60 | check(f, ...) | ||
61 | end | ||
62 | |||
63 | |||
64 | -- check that 'a' and 'b' has the same opcodes | ||
55 | function checkequal (a, b) | 65 | function checkequal (a, b) |
56 | a = T.listcode(a) | 66 | a = T.listcode(a) |
57 | b = T.listcode(b) | 67 | b = T.listcode(b) |
68 | assert(#a == #b) | ||
58 | for i = 1, #a do | 69 | for i = 1, #a do |
59 | a[i] = string.gsub(a[i], '%b()', '') -- remove line number | 70 | a[i] = string.gsub(a[i], '%b()', '') -- remove line number |
60 | b[i] = string.gsub(b[i], '%b()', '') -- remove line number | 71 | b[i] = string.gsub(b[i], '%b()', '') -- remove line number |
@@ -165,65 +176,64 @@ end, | |||
165 | 176 | ||
166 | 177 | ||
167 | -- equalities | 178 | -- equalities |
168 | check(function (a) if a == 1 then return 2 end end, | 179 | checkR(function (a) if a == 1 then return 2 end end, 1, 2, |
169 | 'EQI', 'JMP', 'LOADI', 'RETURN1') | 180 | 'EQI', 'JMP', 'LOADI', 'RETURN1') |
170 | 181 | ||
171 | check(function (a) if -4.0 == a then return 2 end end, | 182 | checkR(function (a) if -4.0 == a then return 2 end end, -4, 2, |
172 | 'EQI', 'JMP', 'LOADI', 'RETURN1') | 183 | 'EQI', 'JMP', 'LOADI', 'RETURN1') |
173 | 184 | ||
174 | check(function (a) if a == "hi" then return 2 end end, | 185 | checkR(function (a) if a == "hi" then return 2 end end, 10, nil, |
175 | 'EQK', 'JMP', 'LOADI', 'RETURN1') | 186 | 'EQK', 'JMP', 'LOADI', 'RETURN1') |
176 | 187 | ||
177 | check(function (a) if a == 10000 then return 2 end end, | 188 | checkR(function (a) if a == 10000 then return 2 end end, 1, nil, |
178 | 'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large | 189 | 'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large |
179 | 190 | ||
180 | check(function (a) if -10000 == a then return 2 end end, | 191 | checkR(function (a) if -10000 == a then return 2 end end, -10000, 2, |
181 | 'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large | 192 | 'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large |
182 | 193 | ||
183 | -- comparisons | 194 | -- comparisons |
184 | 195 | ||
185 | check(function (a) if -10 <= a then return 2 end end, | 196 | checkR(function (a) if -10 <= a then return 2 end end, -10, 2, |
186 | 'GEI', 'JMP', 'LOADI', 'RETURN1') | 197 | 'GEI', 'JMP', 'LOADI', 'RETURN1') |
187 | 198 | ||
188 | check(function (a) if 128.0 > a then return 2 end end, | 199 | checkR(function (a) if 128.0 > a then return 2 end end, 129, nil, |
189 | 'LTI', 'JMP', 'LOADI', 'RETURN1') | 200 | 'LTI', 'JMP', 'LOADI', 'RETURN1') |
190 | 201 | ||
191 | check(function (a) if -127.0 < a then return 2 end end, | 202 | checkR(function (a) if -127.0 < a then return 2 end end, -127, nil, |
192 | 'GTI', 'JMP', 'LOADI', 'RETURN1') | 203 | 'GTI', 'JMP', 'LOADI', 'RETURN1') |
193 | 204 | ||
194 | check(function (a) if 10 < a then return 2 end end, | 205 | checkR(function (a) if 10 < a then return 2 end end, 11, 2, |
195 | 'GTI', 'JMP', 'LOADI', 'RETURN1') | 206 | 'GTI', 'JMP', 'LOADI', 'RETURN1') |
196 | 207 | ||
197 | check(function (a) if 129 < a then return 2 end end, | 208 | checkR(function (a) if 129 < a then return 2 end end, 130, 2, |
198 | 'LOADI', 'LT', 'JMP', 'LOADI', 'RETURN1') | 209 | 'LOADI', 'LT', 'JMP', 'LOADI', 'RETURN1') |
199 | 210 | ||
200 | check(function (a) if a >= 23.0 then return 2 end end, | 211 | checkR(function (a) if a >= 23.0 then return 2 end end, 25, 2, |
201 | 'GEI', 'JMP', 'LOADI', 'RETURN1') | 212 | 'GEI', 'JMP', 'LOADI', 'RETURN1') |
202 | 213 | ||
203 | check(function (a) if a >= 23.1 then return 2 end end, | 214 | checkR(function (a) if a >= 23.1 then return 2 end end, 0, nil, |
204 | 'LOADK', 'LE', 'JMP', 'LOADI', 'RETURN1') | 215 | 'LOADK', 'LE', 'JMP', 'LOADI', 'RETURN1') |
205 | 216 | ||
206 | check(function (a) if a > 2300.0 then return 2 end end, | 217 | checkR(function (a) if a > 2300.0 then return 2 end end, 0, nil, |
207 | 'LOADF', 'LT', 'JMP', 'LOADI', 'RETURN1') | 218 | 'LOADF', 'LT', 'JMP', 'LOADI', 'RETURN1') |
208 | 219 | ||
209 | 220 | ||
210 | -- constant folding | 221 | -- constant folding |
211 | local function checkK (func, val) | 222 | local function checkK (func, val) |
212 | check(func, 'LOADK', 'RETURN1') | 223 | check(func, 'LOADK', 'RETURN1') |
213 | local k = T.listk(func) | 224 | checkKlist(func, {val}) |
214 | assert(#k == 1 and k[1] == val and math.type(k[1]) == math.type(val)) | ||
215 | assert(func() == val) | 225 | assert(func() == val) |
216 | end | 226 | end |
217 | 227 | ||
218 | local function checkI (func, val) | 228 | local function checkI (func, val) |
219 | check(func, 'LOADI', 'RETURN1') | 229 | check(func, 'LOADI', 'RETURN1') |
220 | assert(#T.listk(func) == 0) | 230 | checkKlist(func, {}) |
221 | assert(func() == val) | 231 | assert(func() == val) |
222 | end | 232 | end |
223 | 233 | ||
224 | local function checkF (func, val) | 234 | local function checkF (func, val) |
225 | check(func, 'LOADF', 'RETURN1') | 235 | check(func, 'LOADF', 'RETURN1') |
226 | assert(#T.listk(func) == 0) | 236 | checkKlist(func, {}) |
227 | assert(func() == val) | 237 | assert(func() == val) |
228 | end | 238 | end |
229 | 239 | ||
@@ -258,20 +268,30 @@ checkK(function () return -65536.0 end, -(sbx + 1.0)) | |||
258 | 268 | ||
259 | 269 | ||
260 | -- immediate operands | 270 | -- immediate operands |
261 | check(function (x) return x + 1 end, 'ADDI', 'RETURN1') | 271 | checkR(function (x) return x + 1 end, 10, 11, 'ADDI', 'RETURN1') |
262 | check(function (x) return 128 + x end, 'ADDI', 'RETURN1') | 272 | checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'RETURN1') |
263 | check(function (x) return x * -127 end, 'MULI', 'RETURN1') | 273 | checkR(function (x) return x * -127 end, -1.0, 127.0, 'MULI', 'RETURN1') |
264 | check(function (x) return 20 * x end, 'MULI', 'RETURN1') | 274 | checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'RETURN1') |
265 | check(function (x) return x ^ -2 end, 'POWI', 'RETURN1') | 275 | checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'RETURN1') |
266 | check(function (x) return x / 40 end, 'DIVI', 'RETURN1') | 276 | checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'RETURN1') |
267 | check(function (x) return x // 1 end, 'IDIVI', 'RETURN1') | 277 | checkR(function (x) return x // 1 end, 10.0, 10.0, 'IDIVI', 'RETURN1') |
268 | check(function (x) return x % (100 - 10) end, 'MODI', 'RETURN1') | 278 | checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODI', 'RETURN1') |
269 | check(function (x) return 1 << x end, 'SHLI', 'RETURN1') | 279 | checkR(function (x) return 1 << x end, 3, 8, 'SHLI', 'RETURN1') |
270 | check(function (x) return x << 2 end, 'SHRI', 'RETURN1') | 280 | checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1') |
271 | check(function (x) return x >> 2 end, 'SHRI', 'RETURN1') | 281 | checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1') |
272 | check(function (x) return x & 1 end, 'BANDK', 'RETURN1') | 282 | checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'RETURN1') |
273 | check(function (x) return 10 | x end, 'BORK', 'RETURN1') | 283 | checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'RETURN1') |
274 | check(function (x) return -10 ~ x end, 'BXORK', 'RETURN1') | 284 | checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'RETURN1') |
285 | |||
286 | -- K operands in arithmetic operations | ||
287 | checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'RETURN1') | ||
288 | -- check(function (x) return 128 + x end, 'ADDK', 'RETURN1') | ||
289 | checkR(function (x) return x * -10000 end, 2, -20000, 'MULK', 'RETURN1') | ||
290 | -- check(function (x) return 20 * x end, 'MULK', 'RETURN1') | ||
291 | checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'RETURN1') | ||
292 | checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'RETURN1') | ||
293 | checkR(function (x) return x // 10000 end, 10000, 1, 'IDIVK', 'RETURN1') | ||
294 | checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, 'MODK', 'RETURN1') | ||
275 | 295 | ||
276 | -- no foldings (and immediate operands) | 296 | -- no foldings (and immediate operands) |
277 | check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') | 297 | check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') |
diff --git a/testes/db.lua b/testes/db.lua index 9da68210..5b243c39 100644 --- a/testes/db.lua +++ b/testes/db.lua | |||
@@ -794,6 +794,8 @@ assert(a[3] == "index" and a^3 == "pow" and a..a == "concat") | |||
794 | assert(a/3 == "div" and 3%a == "mod") | 794 | assert(a/3 == "div" and 3%a == "mod") |
795 | assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and | 795 | assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and |
796 | -a == "unm" and #a == "len" and a&3 == "band") | 796 | -a == "unm" and #a == "len" and a&3 == "band") |
797 | assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and | ||
798 | -a == "unm" and #a == "len" and a & 3 == "band") | ||
797 | assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shift" and | 799 | assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shift" and |
798 | a>>1 == "shift") | 800 | a>>1 == "shift") |
799 | assert (a==b and a.op == "eq") | 801 | assert (a==b and a.op == "eq") |
diff --git a/testes/events.lua b/testes/events.lua index b071c2a3..ac630d89 100644 --- a/testes/events.lua +++ b/testes/events.lua | |||
@@ -144,6 +144,8 @@ t.__bnot = f("bnot") | |||
144 | -- when the constant table is very small. | 144 | -- when the constant table is very small. |
145 | assert(b+5 == b) | 145 | assert(b+5 == b) |
146 | assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==undef) | 146 | assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==undef) |
147 | assert(5.2 + b == 5.2) | ||
148 | assert(cap[0] == "add" and cap[1] == 5.2 and cap[2] == b and cap[3]==undef) | ||
147 | assert(b+'5' == b) | 149 | assert(b+'5' == b) |
148 | assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==undef) | 150 | assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==undef) |
149 | assert(5+b == 5) | 151 | assert(5+b == 5) |