Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-y committed Sep 4, 2024
1 parent c39c640 commit bccccac
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/__tests__/__snapshots__/compiler.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`E2E Compiler Pipeline > Compiler can do tco 1`] = `
"(module
(type $0 (func (param i32 i32 i32) (result i32)))
(memory $0 1 150)
(export "buffer" (memory $0))
(export "fib" (func $fib#15065))
(func $fib#15065 (type $0) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(if (result i32)
(local.get $0)
(then
(return_call $fib#15065
(i32.sub
(local.get $0)
(i32.const 1)
)
(local.get $2)
(i32.add
(local.get $1)
(local.get $2)
)
)
)
(else
(local.get $1)
)
)
)
)
"
`;
10 changes: 8 additions & 2 deletions src/__tests__/compiler.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { e2eVoidText, gcVoidText } from "./fixtures/e2e-file.js";
import { e2eVoidText, gcVoidText, tcoText } from "./fixtures/e2e-file.js";
import { compile } from "../compiler.js";
import { describe, test } from "vitest";
import { describe, expect, test } from "vitest";
import assert from "node:assert";
import { getWasmFn, getWasmInstance } from "../lib/wasm.js";

Expand Down Expand Up @@ -35,4 +35,10 @@ describe("E2E Compiler Pipeline", () => {
t.expect(test5(), "test 5 returns correct value").toEqual(21);
t.expect(test6(), "test 6 returns correct value").toEqual(-1);
});

test("Compiler can do tco", async (t) => {
const mod = await compile(tcoText);
mod.optimize();
t.expect(mod.emitText()).toMatchSnapshot();
});
});
11 changes: 11 additions & 0 deletions src/__tests__/fixtures/e2e-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ pub fn test6()
let vec = Bitly { x: 52, y: 2, z: 21 }
get_num_from_vec_sub_obj(vec)
`;

export const tcoText = `
use std::all
// Tail call fib
pub fn fib(n: i32, a: i32, b: i32) -> i32
if n == 0 then:
a
else:
fib(n - 1, b, a + b)
`;

0 comments on commit bccccac

Please sign in to comment.