|
| 1 | +/** |
| 2 | + * Mario (Less) 评测检查 |
| 3 | + * |
| 4 | + * 迁移自 CS50 problems/mario/less/__init__.py |
| 5 | + */ |
| 6 | +import { Checks, Check, exists, include, run, Failure } from "bootcs"; |
| 7 | +import { compile } from "bootcs/c"; |
| 8 | +import * as fs from "fs"; |
| 9 | + |
| 10 | +// 期望输出 |
| 11 | +const PYRAMIDS: Record<number, string> = { |
| 12 | + 1: "#\n", |
| 13 | + 2: " #\n##\n", |
| 14 | + 8: " #\n ##\n ###\n ####\n #####\n ######\n #######\n########\n", |
| 15 | +}; |
| 16 | + |
| 17 | +function checkPyramid(output: string, expected: string): void { |
| 18 | + if (output === expected) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const outputLines = output.split("\n").filter((line) => line !== ""); |
| 23 | + const expectedLines = expected.split("\n").filter((line) => line !== ""); |
| 24 | + |
| 25 | + let help: string | undefined; |
| 26 | + if (outputLines.length === expectedLines.length) { |
| 27 | + if ( |
| 28 | + outputLines.every( |
| 29 | + (ol, i) => ol.trimEnd() === expectedLines[i] |
| 30 | + ) |
| 31 | + ) { |
| 32 | + help = "did you add too much trailing whitespace to the end of your pyramid?"; |
| 33 | + } else if ( |
| 34 | + outputLines.every((ol, i) => ol.slice(1) === expectedLines[i]) |
| 35 | + ) { |
| 36 | + help = "are you printing an additional character at the beginning of each line?"; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + throw new Failure(`Expected pyramid does not match output`, { help }); |
| 41 | +} |
| 42 | + |
| 43 | +export default class MarioLessChecks extends Checks { |
| 44 | + @Check({ description: "mario.c exists" }) |
| 45 | + async exists() { |
| 46 | + exists("mario.c"); |
| 47 | + } |
| 48 | + |
| 49 | + @Check({ description: "mario.c compiles", dependency: "exists" }) |
| 50 | + async compiles() { |
| 51 | + await compile("mario.c", { lcs50: true }); |
| 52 | + } |
| 53 | + |
| 54 | + @Check({ description: "rejects a height of -1", dependency: "compiles" }) |
| 55 | + async test_reject_negative() { |
| 56 | + await run("./mario").stdin("-1").reject(); |
| 57 | + } |
| 58 | + |
| 59 | + @Check({ description: "rejects a height of 0", dependency: "compiles" }) |
| 60 | + async test0() { |
| 61 | + await run("./mario").stdin("0").reject(); |
| 62 | + } |
| 63 | + |
| 64 | + @Check({ description: "handles a height of 1 correctly", dependency: "compiles" }) |
| 65 | + async test1() { |
| 66 | + const result = await run("./mario").stdin("1").stdout(); |
| 67 | + checkPyramid(result, PYRAMIDS[1]); |
| 68 | + } |
| 69 | + |
| 70 | + @Check({ description: "handles a height of 2 correctly", dependency: "compiles" }) |
| 71 | + async test2() { |
| 72 | + const result = await run("./mario").stdin("2").stdout(); |
| 73 | + checkPyramid(result, PYRAMIDS[2]); |
| 74 | + } |
| 75 | + |
| 76 | + @Check({ description: "handles a height of 8 correctly", dependency: "compiles" }) |
| 77 | + async test8() { |
| 78 | + const result = await run("./mario").stdin("8").stdout(); |
| 79 | + checkPyramid(result, PYRAMIDS[8]); |
| 80 | + } |
| 81 | + |
| 82 | + @Check({ |
| 83 | + description: 'rejects a height of -1, then accepts height of 2', |
| 84 | + dependency: "compiles", |
| 85 | + }) |
| 86 | + async test9() { |
| 87 | + const result = await run("./mario").stdin("-1").reject().stdin("2").stdout(); |
| 88 | + checkPyramid(result, PYRAMIDS[2]); |
| 89 | + } |
| 90 | + |
| 91 | + @Check({ description: 'rejects a non-numeric height of "foo"', dependency: "compiles" }) |
| 92 | + async test_reject_foo() { |
| 93 | + await run("./mario").stdin("foo").reject(); |
| 94 | + } |
| 95 | + |
| 96 | + @Check({ description: 'rejects a non-numeric height of ""', dependency: "compiles" }) |
| 97 | + async test_reject_empty() { |
| 98 | + await run("./mario").stdin("").reject(); |
| 99 | + } |
| 100 | +} |
0 commit comments