45 lines
2.4 KiB
TypeScript
45 lines
2.4 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { tmpdir } from "node:os";
|
|
import { lcbTask, type LcbRow } from "../src/benchmarks/livecodebench";
|
|
|
|
const path = new URL("../data/datasets/livecodebench.json", import.meta.url).pathname;
|
|
const rows: LcbRow[] = existsSync(path) ? JSON.parse(readFileSync(path, "utf8")) : [];
|
|
|
|
async function grade(id: string, solution: string | null) {
|
|
const task = lcbTask(rows.find((r) => r.id === id)!);
|
|
const w = mkdtempSync(join(tmpdir(), "lcb-"));
|
|
writeFileSync(join(w, "solution.py"), solution ?? task.files!["solution.py"]);
|
|
writeFileSync(join(w, ".grade.py"), task.grade);
|
|
const p = Bun.spawn(["python3", ".grade.py"], { cwd: w, stdout: "pipe", stderr: "pipe" });
|
|
return p.exited;
|
|
}
|
|
|
|
// LiveCodeBench ships no reference solutions, so these are hand-written for one stdin and one functional problem.
|
|
test.skipIf(!rows.length)("stdin problem: a correct program passes, a wrong or missing one fails", async () => {
|
|
const good = "x = int(input())\nprint(sum(i * j for i in range(1, 10) for j in range(1, 10)) - sum(i * j for i in range(1, 10) for j in range(1, 10) if i * j == x) * 0 - x)\n";
|
|
const exact = "x = int(input())\nprint(sum(i * j for i in range(1, 10) for j in range(1, 10) if i * j != x))\n";
|
|
expect(await grade("abc387_b", exact)).toBe(0);
|
|
expect(await grade("abc387_b", good)).not.toBe(0);
|
|
expect(await grade("abc387_b", null)).not.toBe(0);
|
|
expect(await grade("abc387_b", "while True: pass\n")).not.toBe(0);
|
|
});
|
|
|
|
test.skipIf(!rows.length)("functional problem: a correct class passes, a wrong or missing one fails", async () => {
|
|
const good = `from typing import List
|
|
class Solution:
|
|
def sumOfGoodNumbers(self, nums: List[int], k: int) -> int:
|
|
n = len(nums)
|
|
return sum(v for i, v in enumerate(nums) if (i - k < 0 or v > nums[i - k]) and (i + k >= n or v > nums[i + k]))
|
|
`;
|
|
expect(await grade("3723", good)).toBe(0);
|
|
expect(await grade("3723", good.replace("v > nums[i + k]", "v >= nums[i + k]"))).not.toBe(0);
|
|
expect(await grade("3723", null)).not.toBe(0);
|
|
});
|
|
|
|
test.skipIf(!rows.length)("every problem has tests and an untouched workspace fails all of them", async () => {
|
|
expect(rows.length).toBe(175);
|
|
for (const r of rows) expect(r.tests.length).toBeGreaterThan(0);
|
|
for (const r of rows.filter((_, i) => i % 25 === 0)) expect(await grade(r.id, null)).not.toBe(0);
|
|
});
|