Skip to content

Commit

Permalink
~Migrated tests to deno
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Sep 11, 2023
1 parent e2dd165 commit 7b2c976
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 145 deletions.
3 changes: 1 addition & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"lock": false,
"nodeModulesDir": true,
"test": {
"include": ["source/"],
"exclude": ["source/bnf/"]
"include": ["tests/*"]
},
"tasks": {
"start": "deno run --allow-read --allow-write cli.ts"
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
"scripts": {
"build": "run-s build:*",
"build:syntax": "npx bnf-compile ./source/bnf/",
"test": "run-s test:*",
"test:types": "tsc --noEmit",
"test:mocha": "npx mocha"
"test": "deno test"
},
"bin": {
"salient": "bin/cli.js"
Expand Down
86 changes: 0 additions & 86 deletions tests/wasm/hello-world.test.js

This file was deleted.

84 changes: 84 additions & 0 deletions tests/wasm/hello-world.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/// <reference lib="deno.ns" />
import { assert, fail, assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
import { Module, Instruction, Type } from "../../source/wasm/index.ts";

const decoder = new TextDecoder();

const goalText = "Hello, World!" + Math.floor(Math.random() * 9);

Deno.test(`Wasm module test: should print "${goalText}"`, async () => {
let mod = new Module();
const mem = mod.addMemory(1);
mod.exportMemory("memory", mem);

const type0 = mod.makeType([Type.Intrinsic.i32], [Type.Intrinsic.i32]);
const type1 = mod.makeType([Type.Intrinsic.i32, Type.Intrinsic.i32, Type.Intrinsic.i32, Type.Intrinsic.i32], [Type.Intrinsic.i32]);

const fd_write = mod.importFunction("wasi_snapshot_preview1", "fd_write", type1);

mod.setData(0, goalText);
// The WASI iovec struct, which consists of a pointer to
// the data and the length of the data.
// This starts at address 100, to leave some room after the string data.
mod.setData(16, "\x00\x00\x00\x00\x0e\x00\x00\x00"); // Set pointer to 0 and length to 14

const main = mod.makeFunction([], []);
main.code.push(Instruction.const.i32(1)); // File descriptor for stdout
main.code.push(Instruction.const.i32(16)); // iovec array
main.code.push(Instruction.const.i32(1)); // number of iovec structs
main.code.push(Instruction.const.i32(0)); // address to store number of bytes written (ignoring it here)
main.code.push(Instruction.call(fd_write));
main.code.push(Instruction.drop());

const extra = mod.makeFunction([Type.Intrinsic.i32], [Type.Intrinsic.i32]);
extra.code.push(Instruction.local.get(0));
extra.code.push(Instruction.return());

mod.exportFunction("_start", main.ref);

let stdout = "";

let memory: WebAssembly.Memory;

const imports = {
wasi_snapshot_preview1: {
fd_write: (fd: number, iovs: number, iovs_len: number, n_written: number) => {
const memoryArray = new Int32Array(memory.buffer);
const byteArray = new Uint8Array(memory.buffer);
for (let iovIdx = 0; iovIdx < iovs_len; iovIdx++) {
const bufPtr = memoryArray.at(iovs/4 + iovIdx*2) || 0;
const bufLen = memoryArray.at(iovs/4 + iovIdx*2 + 1) || 0;
const data = decoder.decode(byteArray.slice(bufPtr, bufPtr + bufLen));
stdout += data;
}
return 0; // Return 0 to indicate success
}
}
};

// Load the wasm module
const wasmModule = new WebAssembly.Module(mod.toBinary());

try {
// Instantiate the wasm module
const instance = await WebAssembly.instantiate(wasmModule, imports);

const exports = instance.exports;
memory = exports.memory as WebAssembly.Memory;

// Call the _start function
if (typeof exports._start === "function") {
(exports._start as Function)();
} else {
fail(`Expected _start to be a function`);
}

// Check stdout
assertEquals(stdout, goalText);

} catch (err) {
// If there's an error, the test will fail
fail(`Failed to run wasm module: ${err}`);
}

});
54 changes: 0 additions & 54 deletions tests/wasm/type.test.js

This file was deleted.

49 changes: 49 additions & 0 deletions tests/wasm/type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// <reference lib="deno.ns" />
import { assertEquals, assertThrows } from "https://deno.land/[email protected]/assert/mod.ts";
import { EncodeSignedLEB, EncodeUnsignedLEB } from "../../source/wasm/type.ts";

function toHex(arr: number[]): string {
return arr.map(x => x.toString(16).padStart(2, "0")).join("");
}

Deno.test("EncodeSignedLEB: zero encoding", () => {
assertEquals(toHex(EncodeSignedLEB(0)), "00");
});

Deno.test("EncodeSignedLEB: small positive integer", () => {
assertEquals(toHex(EncodeSignedLEB(8)), "08");
assertEquals(toHex(EncodeSignedLEB(10)), "0a");
assertEquals(toHex(EncodeSignedLEB(100)), "e400");
assertEquals(toHex(EncodeSignedLEB(123456)), "c0c407");
assertEquals(toHex(EncodeSignedLEB(2141192192)), "808080fd07");
});

Deno.test("EncodeSignedLEB: small negative integer", () => {
assertEquals(toHex(EncodeSignedLEB(-100)), "9c7f");
});

Deno.test("EncodeSignedLEB: should throw an error for non-integers", () => {
assertThrows(() => EncodeSignedLEB(12.34));
});

Deno.test("EncodeUnsignedLEB: zero encoding", () => {
assertEquals(toHex(EncodeUnsignedLEB(0)), "00");
});

Deno.test("EncodeUnsignedLEB: small integer", () => {
assertEquals(toHex(EncodeUnsignedLEB(1)), "01");
assertEquals(toHex(EncodeUnsignedLEB(8)), "08");
assertEquals(toHex(EncodeUnsignedLEB(127)), "7f");
assertEquals(toHex(EncodeUnsignedLEB(128)), "8001");
assertEquals(toHex(EncodeUnsignedLEB(255)), "ff01");
assertEquals(toHex(EncodeUnsignedLEB(256)), "8002");
assertEquals(toHex(EncodeUnsignedLEB(624485)), "e58e26");
});

Deno.test("EncodeUnsignedLEB: should throw an error for non-integers", () => {
assertThrows(() => EncodeUnsignedLEB(12.34));
});

Deno.test("EncodeUnsignedLEB: should throw an error for negative integers", () => {
assertThrows(() => EncodeUnsignedLEB(-12));
});

0 comments on commit 7b2c976

Please sign in to comment.