Skip to content

Commit c12f08b

Browse files
committed
Add tests for deadcode elimination
1 parent 062c09f commit c12f08b

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

src/stdlib/mexpr/deadcode.mc

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
include "mexpr/ast-builder.mc"
22
include "mexpr/builtin.mc"
33
include "mexpr/eq.mc"
4+
include "mexpr/pprint.mc"
45
include "mexpr/side-effect.mc"
56
include "mexpr/symbolize.mc"
67
include "name.mc"
@@ -327,20 +328,86 @@ lang MExprDeadcodeElimination =
327328
removeLets nmap t
328329
end
329330

330-
lang TestLang = MExprDeadcodeElimination + MExprEq
331+
lang TestLang = MExprDeadcodeElimination + MExprEq + MExprPrettyPrint
331332
end
332333

333334
mexpr
334335

335336
use TestLang in
336337

338+
let pprintExprs = lam l. lam r.
339+
join ["LHS:\n", expr2str l, "\n\nRHS:\n", expr2str r]
340+
in
341+
342+
let e =
343+
bind_
344+
(ulet_ "x" (int_ 2))
345+
(var_ "x")
346+
in
347+
utest deadcodeElimination e with e using eqExpr else pprintExprs in
348+
337349
let e =
338350
bindall_
339351
[ ulet_ "x" (int_ 2)
340352
, ulet_ "y" (int_ 3) ]
341353
(var_ "y")
342354
in
343355
let expected = bind_ (ulet_ "y" (int_ 3)) (var_ "y") in
344-
utest deadcodeElimination e with expected using eqExpr in
356+
utest deadcodeElimination e with expected using eqExpr else pprintExprs in
357+
358+
let e =
359+
bind_
360+
(ureclets_ [
361+
("x", (ulam_ "a" (app_ (var_ "y") (var_ "a")))),
362+
("y", (ulam_ "b" (int_ 0)))
363+
])
364+
(var_ "x")
365+
in
366+
utest deadcodeElimination e with e using eqExpr else pprintExprs in
367+
368+
let e =
369+
bind_
370+
(ureclets_ [
371+
("x", (ulam_ "a" (app_ (var_ "y") (var_ "a")))),
372+
("y", (ulam_ "b" (app_ (var_ "x") (var_ "b")))),
373+
("z", (ulam_ "c" (int_ 0)))
374+
])
375+
(var_ "y")
376+
in
377+
let expected =
378+
bind_
379+
(ureclets_
380+
[ ("x", (ulam_ "a" (app_ (var_ "y") (var_ "a"))))
381+
, ("y", (ulam_ "b" (app_ (var_ "x") (var_ "b")))) ])
382+
(var_ "y")
383+
in
384+
utest deadcodeElimination e with expected using eqExpr else pprintExprs in
385+
386+
let e =
387+
bindall_
388+
[ ulet_ "z" (int_ 0)
389+
, ureclets_
390+
[ ("x", (ulam_ "a" (app_ (var_ "y") (var_ "a"))))
391+
, ("y", (ulam_ "b" (app_ (var_ "x") (var_ "b")))) ] ]
392+
(var_ "z")
393+
in
394+
let expected = bind_ (ulet_ "z" (int_ 0)) (var_ "z") in
395+
utest deadcodeElimination e with expected using eqExpr else pprintExprs in
396+
397+
let e =
398+
bindall_
399+
[ ext_ "abs_int" true (tyarrow_ tyint_ tyint_)
400+
, ulet_ "" (app_ (var_ "abs_int") (int_ -3)) ]
401+
(int_ 0)
402+
in
403+
utest deadcodeElimination e with e using eqExpr else pprintExprs in
404+
405+
let e =
406+
bindall_
407+
[ ext_ "abs_int" false (tyarrow_ tyint_ tyint_)
408+
, ulet_ "" (app_ (var_ "abs_int") (int_ -3)) ]
409+
(int_ 0)
410+
in
411+
utest deadcodeElimination e with int_ 0 using eqExpr else pprintExprs in
345412

346413
()

0 commit comments

Comments
 (0)