Skip to content

Commit

Permalink
Make test helper more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
samestep committed Feb 2, 2024
1 parent a8e86d4 commit 501808d
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from "fs/promises";
import path from "path";
import url from "url";
import { expect, test } from "vitest";
import { autodiff } from "../index.js";
import * as wasmad from "../index.js";

const dir = path.dirname(url.fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -45,21 +45,23 @@ type Binop = {
) => [number, number];
};

const binop = async (filename: string): Promise<Binop> => {
const autodiff = async <T extends WebAssembly.Exports>(
filename: string,
): Promise<T> => {
let binary;
const mod = binaryen.parseText(await slurp(filename));
try {
mod.setFeatures(binaryen.Features.Multivalue);
autodiff(mod);
wasmad.autodiff(mod);
binary = mod.emitBinary();
} finally {
mod.dispose();
}
return await compile<Binop>(binary);
return await compile<T>(binary);
};

test("subtraction", async () => {
const { fwd, bwd } = await binop("sub.wat");
const { fwd, bwd } = await autodiff<Binop>("sub.wat");
const a = 5;
const b = 3;
let da = 0;
Expand All @@ -72,7 +74,7 @@ test("subtraction", async () => {
});

test("division", async () => {
const { fwd, bwd } = await binop("div.wat");
const { fwd, bwd } = await autodiff<Binop>("div.wat");
const a = 5;
const b = 3;
let da = 0;
Expand Down

0 comments on commit 501808d

Please sign in to comment.