aboutsummaryrefslogtreecommitdiff
path: root/testes
diff options
context:
space:
mode:
Diffstat (limited to 'testes')
-rw-r--r--testes/code.lua82
-rw-r--r--testes/db.lua2
-rw-r--r--testes/events.lua2
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 '...'
43function check (f, ...) 44function 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, ...)
52end 53end
53 54
54 55
56-- check that 'f' opcodes match '...' and that 'f(p) == r'.
57function checkR (f, p, r, ...)
58 local r1 = f(p)
59 assert(r == r1 and math.type(r) == math.type(r1))
60 check(f, ...)
61end
62
63
64-- check that 'a' and 'b' has the same opcodes
55function checkequal (a, b) 65function 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
168check(function (a) if a == 1 then return 2 end end, 179checkR(function (a) if a == 1 then return 2 end end, 1, 2,
169 'EQI', 'JMP', 'LOADI', 'RETURN1') 180 'EQI', 'JMP', 'LOADI', 'RETURN1')
170 181
171check(function (a) if -4.0 == a then return 2 end end, 182checkR(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
174check(function (a) if a == "hi" then return 2 end end, 185checkR(function (a) if a == "hi" then return 2 end end, 10, nil,
175 'EQK', 'JMP', 'LOADI', 'RETURN1') 186 'EQK', 'JMP', 'LOADI', 'RETURN1')
176 187
177check(function (a) if a == 10000 then return 2 end end, 188checkR(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
180check(function (a) if -10000 == a then return 2 end end, 191checkR(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
185check(function (a) if -10 <= a then return 2 end end, 196checkR(function (a) if -10 <= a then return 2 end end, -10, 2,
186 'GEI', 'JMP', 'LOADI', 'RETURN1') 197 'GEI', 'JMP', 'LOADI', 'RETURN1')
187 198
188check(function (a) if 128.0 > a then return 2 end end, 199checkR(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
191check(function (a) if -127.0 < a then return 2 end end, 202checkR(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
194check(function (a) if 10 < a then return 2 end end, 205checkR(function (a) if 10 < a then return 2 end end, 11, 2,
195 'GTI', 'JMP', 'LOADI', 'RETURN1') 206 'GTI', 'JMP', 'LOADI', 'RETURN1')
196 207
197check(function (a) if 129 < a then return 2 end end, 208checkR(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
200check(function (a) if a >= 23.0 then return 2 end end, 211checkR(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
203check(function (a) if a >= 23.1 then return 2 end end, 214checkR(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
206check(function (a) if a > 2300.0 then return 2 end end, 217checkR(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
211local function checkK (func, val) 222local 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)
216end 226end
217 227
218local function checkI (func, val) 228local 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)
222end 232end
223 233
224local function checkF (func, val) 234local 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)
228end 238end
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
261check(function (x) return x + 1 end, 'ADDI', 'RETURN1') 271checkR(function (x) return x + 1 end, 10, 11, 'ADDI', 'RETURN1')
262check(function (x) return 128 + x end, 'ADDI', 'RETURN1') 272checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'RETURN1')
263check(function (x) return x * -127 end, 'MULI', 'RETURN1') 273checkR(function (x) return x * -127 end, -1.0, 127.0, 'MULI', 'RETURN1')
264check(function (x) return 20 * x end, 'MULI', 'RETURN1') 274checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'RETURN1')
265check(function (x) return x ^ -2 end, 'POWI', 'RETURN1') 275checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'RETURN1')
266check(function (x) return x / 40 end, 'DIVI', 'RETURN1') 276checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'RETURN1')
267check(function (x) return x // 1 end, 'IDIVI', 'RETURN1') 277checkR(function (x) return x // 1 end, 10.0, 10.0, 'IDIVI', 'RETURN1')
268check(function (x) return x % (100 - 10) end, 'MODI', 'RETURN1') 278checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODI', 'RETURN1')
269check(function (x) return 1 << x end, 'SHLI', 'RETURN1') 279checkR(function (x) return 1 << x end, 3, 8, 'SHLI', 'RETURN1')
270check(function (x) return x << 2 end, 'SHRI', 'RETURN1') 280checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1')
271check(function (x) return x >> 2 end, 'SHRI', 'RETURN1') 281checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1')
272check(function (x) return x & 1 end, 'BANDK', 'RETURN1') 282checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'RETURN1')
273check(function (x) return 10 | x end, 'BORK', 'RETURN1') 283checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'RETURN1')
274check(function (x) return -10 ~ x end, 'BXORK', 'RETURN1') 284checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'RETURN1')
285
286-- K operands in arithmetic operations
287checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'RETURN1')
288-- check(function (x) return 128 + x end, 'ADDK', 'RETURN1')
289checkR(function (x) return x * -10000 end, 2, -20000, 'MULK', 'RETURN1')
290-- check(function (x) return 20 * x end, 'MULK', 'RETURN1')
291checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'RETURN1')
292checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'RETURN1')
293checkR(function (x) return x // 10000 end, 10000, 1, 'IDIVK', 'RETURN1')
294checkR(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)
277check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') 297check(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")
794assert(a/3 == "div" and 3%a == "mod") 794assert(a/3 == "div" and 3%a == "mod")
795assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and 795assert(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")
797assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and
798 -a == "unm" and #a == "len" and a & 3 == "band")
797assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shift" and 799assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shift" and
798 a>>1 == "shift") 800 a>>1 == "shift")
799assert (a==b and a.op == "eq") 801assert (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.
145assert(b+5 == b) 145assert(b+5 == b)
146assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==undef) 146assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==undef)
147assert(5.2 + b == 5.2)
148assert(cap[0] == "add" and cap[1] == 5.2 and cap[2] == b and cap[3]==undef)
147assert(b+'5' == b) 149assert(b+'5' == b)
148assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==undef) 150assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==undef)
149assert(5+b == 5) 151assert(5+b == 5)