-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.lua
434 lines (361 loc) · 11.9 KB
/
test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
-- luacheck: ignore class
---@diagnostics disable: lowercase-global
function _G.class(base)
local classdef = {}
setmetatable(classdef, {
__index = base,
__call = function(_, ...)
local inst = {
class = classdef,
super = base or nil
}
setmetatable(inst, { __index = classdef })
if inst.init then
inst:init(...)
end
return inst
end
})
return classdef
end
function string.usub(...)
return string.sub(...)
end
function math.log10(x)
return math.log(x, 10)
end
local RichTextStub = class()
function RichTextStub:setReadOnly()
return self
end
function RichTextStub:setBorder()
return self
end
_G.D2Editor = {
newRichText = function() return RichTextStub() end
}
local GC = {
getStringWidth = function() return 1 end,
getStringHeight = function() return 1 end,
drawRect = function() end,
fillRect = function() end,
setColorRGB = function() end,
setFont = function() end,
}
-- luacheck: ignore platform
_G.platform = {
withGC = function(fn) return fn(GC) end,
window = {
width = function() return 1 end,
height = function() return 1 end,
invalidate = function() end,
}
}
math.evalStr = function(str)
return str
end
if not unpack then
_G.unpack = table.unpack
end
-- luacheck: ignore on
_G.on = {}
---@diagnostics enable: lowercase-global
require 'tableext'
require 'stringext'
local expr = require 'expressiontree'
local lexer = require 'ti.lexer'
local sym = require 'ti.sym'
local Test = require 'testlib'
local ui = require 'ui'
local config = require 'config.config'
config.use_document_settings = false
local test = {}
function test.tokenize_infix()
local lexer = require 'ti.lexer'
local function fail(str, reason)
local res = lexer.tokenize(str)
Test.assert((not res), reason)
end
local function expect(str, tokens)
local res = lexer.tokenize(str)
Test.assert(res ~= nil, "Token list is nil")
if not res then return end
Test.assert(#res == #tokens, "Token count missmatch " .. str)
for i = 1, #tokens do
Test.assert(res[i][1] == tokens[i][1],
"Token value missmatch. Expected '" .. tokens[i][1] .. "' got '" .. res[i][1] .. "'")
Test.assert(res[i][2]:sub(1, #tokens[i][2]) == tokens[i][2],
"Token type missmatch. Expected '" .. tokens[i][1] .. "' got '" .. res[i][1] .. "' (" .. str .. ")")
end
end
expect("", {})
expect(" ", {})
-- Number
expect("1", { { '1', 'n' } })
expect(".1", { { '.1', 'n' } })
expect("1.1", { { '1.1', 'n' } })
expect("123", { { '123', 'n' } })
fail("1.", 'Trailing point is invalid')
fail("1.1.", 'Trailing point is invalid')
fail(".", 'Point is not a valid number')
-- Number exponents
expect("3" .. sym.EE .. "5", { { '3' .. sym.EE .. '5', 'n' } })
expect("3" .. sym.EE .. "+5", { { '3' .. sym.EE .. '+5', 'n' } })
expect("3" .. sym.EE .. "-5", { { '3' .. sym.EE .. '-5', 'n' } })
expect("3" .. sym.EE .. sym.NEGATE .. "5", { { '3' .. sym.EE .. sym.NEGATE .. '5', 'n' } })
fail("3" .. sym.EE .. "5.1", 'Point after exponent')
-- Number bases
expect("0b10", { { '0b10', 'n' } })
expect("0hf0", { { '0hf0', 'n' } })
expect("0hfx", { { '0hf', 'n' }, { 'x', 'w' } }) -- As allowed by TI
fail("0b12", 'Non binary digit in binary number')
-- Operators
expect("+", { { '+', 'o' } })
expect("-", { { '-', 'o' } })
expect("*", { { '*', 'o' } })
expect("/", { { '/', 'o' } })
expect(sym.NEGATE, { { sym.NEGATE, 'o' } })
-- Units
expect("_m", { { '_m', 'u' } })
expect("_tonne", { { '_tonne', 'u' } })
expect("_mmH2O", { { '_mmH2O', 'u' } })
expect("_a_b", { { '_a', 'u' }, { '_b', 'u' } })
-- String
expect('"hello"', { { '"hello"', 'str' } })
-- List
expect('{1,2}', { { '{', 'sy' }, { '1', 'n' }, { ',', 'sy' }, { '2', 'n' }, { '}', 'sy' } })
-- Matrix
expect('[1,2]', { { '[', 'sy' }, { '1', 'n' }, { ',', 'sy' }, { '2', 'n' }, { ']', 'sy' } })
-- Function
expect("f(1)", { { 'f', 'f' }, { '(', 'sy' }, { '1', 'n' }, { ')', 'sy' } })
-- Expressions
expect("solve(0.5((x + 3)^2 - 10.53)=0, {x,y})", {
{ 'solve', 'f' },
{ '(', 'sy' },
{ '0.5', 'n' },
{ '(', 'sy' },
{ '(', 'sy' },
{ 'x', 'w' },
{ '+', 'o' },
{ '3', 'n' },
{ ')', 'sy' },
{ '^', 'o' },
{ '2', 'n' },
{ '-', 'o' },
{ '10.53', 'n' },
{ ')', 'sy' },
{ '=', 'o' },
{ '0', 'n' },
{ ',', 'sy' },
{ '{', 'sy' },
{ 'x', 'w' },
{ ',', 'sy' },
{ 'y', 'w' },
{ '}', 'sy' },
{ ')', 'sy' },
})
end
function test.infix_to_rpn_to_infix()
local function expect(str, other)
other = other or str
do
print('Expression: ' .. str)
local tree = expr.from_infix(lexer.tokenize(str))
Test.assert(tree)
local new = tree:infix_string()
Test.assert(new and new == other, "Expected " .. (other or 'nil') .. " got " .. (new or "nil"))
end
end
local function fail(str)
Test.expect_fail(function()
local expr = expr.from_infix(lexer.tokenize(str))
Test.assert(not expr,
"Expected from_infix to return nil (input: '" .. str .. "')\n")
local infix = expr:infix_string()
Test.assert(not infix,
"Expected infix string to be nil, is '" .. (infix or 'nil') .. "'")
end)
end
expect("1+2")
expect("1+2+3")
expect("1+2+3+4")
expect("1+2+3+4+5")
-- Operator associativity
expect("(1-2)-3", "1-2-3")
expect("1-(2-3)")
expect("((1-2)-3)-4", "1-2-3-4")
expect("(2^3)^4")
expect("2^3^4", "2^(3^4)")
expect("2^(3^4)")
expect("x^(y-1)=0")
expect("solve(x^(y-1)=0,x)") -- bug#16
expect("solve((((x*2)))=(((1))),((((((x)))))))", "solve(x*2=1,x)")
-- Left assoc
for _, v in ipairs({ '-', '/' }) do
expect("x" .. v .. "x" .. v .. "x")
expect("(x" .. v .. "x)" .. v .. "x", "x" .. v .. "x" .. v .. "x")
expect("x" .. v .. "(x" .. v .. "x)")
end
expect("1/1/1")
expect("1/(1/1)")
expect("1*1*1")
expect("1*1/1*1")
expect("1*1/(1*1)")
-- Implicit multiplication
expect("1(3)", "1*3")
expect("(3)1", "3*1")
expect("5_m", "5*_m")
expect("5{1}", "5*{1}")
expect("{1}5", "{1}*5")
-- Preserve function calls
expect("abs()")
expect("sin(pi)")
expect("root(x)", "root(x)")
expect("root(x,y)", "root(x,y)")
-- Lists
expect("{}")
expect("{1}")
expect("{1,2}")
expect("{1,2,3}")
expect("{(1+a)*2,2+b,3+c}")
expect("{(1+a)*2,2+b,root((1+x)*2,2*y)}")
-- Matrix
expect("[1]", "[1]")
expect("[1,2]", "[1,2]")
expect("[[1,2,3][4,5,6]]")
expect("[[1,2,3][4,5,6][7,8,9]]")
expect("[[1+1,x,y^(2+z)][abs(x),{1,2,3},\"!\"][7,8,9]]")
-- Remove parens
expect("(2^2)", "2^2")
expect("(((2^2)))", "2^2")
-- Units
expect("_km/_s")
expect("(_km/_s)", "_km/_s")
-- Syntax errors
fail("+")
fail(",")
fail(")")
fail("}")
fail(")")
fail("())")
fail("(+)")
end
function test.rpn_input()
local controller = require 'rpn.controller'
require 'views.container'
require 'views.edit'
require 'views.list'
require 'views.label'
local edit = ui.edit(nil)
local list = ui.list(nil)
local rpn = controller.new({}, edit, list)
local stack = rpn.stack
rpn:initialize()
local function expectStack(key, stack_infix)
stack.stack = {}
edit:set_text('')
for _, v in ipairs(key) do
if v == 'ENTER' then
edit:on_enter_key()
else
edit:on_char(v)
end
end
local key_str = ''
for _, k in ipairs(key) do
key_str = key_str .. ' ' .. k
end
Test.info(key_str)
if type(stack_infix) == 'string' then
if not Test.assert(#stack.stack > 0,
"Expected stack to be " .. stack_infix) then
return
end
local stack_top = stack.stack[#stack.stack]
Test.assert(stack_top.infix == stack_infix,
"Expected stack top to be '" .. stack_infix .. "' but it is '" .. stack_top.infix .. "'")
else
for idx, v in ipairs(stack_infix) do
local stack_top = stack.stack[#stack.stack - idx + 1]
Test.assert(stack_top.infix == v,
"Expected stack top to be '" .. v .. "' but it is '" .. stack_top.infix .. "'")
end
end
end
---@diagnostic disable-next-line: duplicate-set-field
expectStack({ '1', 'ENTER' }, '1')
expectStack({ '1', 'ENTER', '2', 'ENTER' }, { '2', '1' })
expectStack({ '1', '2', 'ENTER' }, '12')
-- Operators
expectStack({ '1', 'ENTER', '2', 'ENTER', '+' }, '1+2')
expectStack({ '1', 'ENTER', '2', '+' }, '1+2')
-- Remove double negation/not
expectStack({ '1', 'ENTER', sym.NEGATE }, sym.NEGATE .. '1')
expectStack({ '1', 'ENTER', sym.NEGATE, sym.NEGATE }, '1')
expectStack({ '1', 'ENTER', 'not' }, 'not 1')
expectStack({ '1', 'ENTER', 'not', 'not' }, '1')
-- Inline negation
expectStack({ '1', sym.NEGATE, 'ENTER' }, sym.NEGATE .. '1')
--expectStack({'1', sym.NEGATE, sym.NEGATE, 'ENTER'}, '1') -- Not working because of missing usub implementation!
-- Unit power suffix
expectStack({ '2', 'ENTER', '_m', 'ENTER', '2', '^', '*' }, '2*_m^2')
-- Functions
expectStack({ '1', 'ENTER', 'sin', 'ENTER' }, 'sin(1)')
expectStack({ '1', 'ENTER', '2', '+', 'sin', 'ENTER' }, 'sin(1+2)')
expectStack({ 'sin(2)', 'ENTER' }, 'sin(2)')
-- Lists
expectStack({ '{', '1', ',', '2', '}', 'ENTER' }, '{1,2}')
-- Auto removal of store operations
config.store_mode = 'replace'
expectStack({ '1', 'ENTER', 'a', sym.STORE, '2', 'ENTER', 'b', sym.STORE, '+' }, 'a+b')
expectStack({ 'a', 'ENTER', '1', ':=', '2', 'ENTER', 'b', sym.STORE, '+' }, 'a+b')
expectStack({ '1', 'ENTER', 'a', sym.STORE, 'b', 'ENTER', '2', ':=', '+' }, 'a+b')
expectStack({ 'a', 'ENTER', '1', ':=', 'b', 'ENTER', '2', ':=', '+' }, 'a+b')
-- Modify both sides
expectStack({ 'x', 'ENTER', '2', '*', '10', '=', '2', '/' }, 'x*2/2=10/2')
expectStack({ 'x', 'ENTER', '2', '*', '10', '=', '2', '/', '1', 'and' }, 'x*2/2=10/2 and 1') -- Do not logical op
end
function test.stack_to_list()
local stack = require 'rpn.stack'
if true then
return -- TODO: Implement to_list
end
local function expect(infix_list, n, stack_result)
local s = stack.new()
for _, v in ipairs(infix_list) do
s:push_infix(v)
end
--s:toList(n)
if not Test.assert(#s.stack > 0,
"Expected stack to be not empty!") then
return
end
local stack_top = s.stack[#s.stack]
Test.assert(stack_top.infix == stack_result,
"Expected stack top to be '" .. stack_result .. "' but it is '" .. stack_top.infix .. "'")
end
-- List of numbers
expect({ '1' }, 10, '{1}')
expect({ '1' }, 0, '{}')
expect({ '1' }, 1, '{1}')
expect({ '1', '2' }, 1, '{2}')
expect({ '1', '2', '3' }, 1, '{3}')
expect({ '1', '2', '3' }, 2, '{2,3}')
expect({ '1', '2', '3' }, 3, '{1,2,3}')
-- List with functions
expect({ 'abs(1)', 'root(2,3)', '3' }, 3, '{abs(1),root(2,3),3}')
-- Join lists
expect({ '{1}', '2' }, 2, '{1,2}')
expect({ '{1,2,3}', '4' }, 2, '{1,2,3,4}')
expect({ '{1,2,3}', '{4}' }, 2, '{1,2,3,4}')
expect({ '{x,y,z}', '{1,2,3}', '4' }, 3, '{x,y,z,1,2,3,4}')
expect({ '{x,y,z}', '1', '{2,3,4}' }, 3, '{x,y,z,1,2,3,4}')
end
function test.rect()
local a = ui.rect(10, 10, 10, 10)
Test.assert(not a:clone():intersects_rect(ui.rect(0, 0, 5, 5)))
Test.assert(a:clone():intersects_rect(ui.rect(a.x, a.y, a.width, a.height)))
Test.assert(a:clone():intersects_rect(ui.rect(0, 0, 15, 15)))
Test.assert(a:clone():intersects_rect(ui.rect(15, 15, 5, 5)))
end
Test.run(test)