diff --git a/problem-bank/judges.json b/problem-bank/judges.json index 0d06fdf3a..079c9a66b 100644 --- a/problem-bank/judges.json +++ b/problem-bank/judges.json @@ -5492,6 +5492,41 @@ "1" ], "expected": 1 + }, + { + "label": "minus before a group", + "input": [ + "-(2+3)" + ], + "expected": -5 + }, + { + "label": "minus inside a group", + "input": [ + "1-(-2)" + ], + "expected": 3 + }, + { + "label": "minus on the first number only", + "input": [ + "-2+3" + ], + "expected": 1 + }, + { + "label": "space after a leading minus", + "input": [ + "- 7" + ], + "expected": -7 + }, + { + "label": "minus before a two-digit number", + "input": [ + "-12" + ], + "expected": -12 } ], "paramNames": [ diff --git a/tests/browser/compiler-explorer.test.js b/tests/browser/compiler-explorer.test.js index 6f43ea725..baeeedf5c 100644 --- a/tests/browser/compiler-explorer.test.js +++ b/tests/browser/compiler-explorer.test.js @@ -6,6 +6,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { promisify } from "node:util"; +import { evaluate as calculate } from "../../web/calculator.js"; import { ALL_LANGUAGES, COMPILED_LANGUAGES, @@ -364,6 +365,100 @@ test("jump-game cases reject each hop-by-hop shortcut", () => { } }); +test("basic-calculator cases reject parsers without a unary minus", () => { + // One recursive-descent parser for the wrong ones, which differ only in when + // a "-" that starts a term is read as a sign, and in what that sign negates. + // web/calculator.js has no such switch, so it only checks the values below. + const digit = (c) => c >= "0" && c <= "9"; + const evaluate = ( + s, + { sign = () => false, rest = false, spaces = false, one = false } = {}, + ) => { + const text = spaces ? s : s.replaceAll(" ", ""); + let at = 0; + const skip = () => { + while (text[at] === " ") at += 1; + }; + const term = () => { + skip(); + if (text[at] === "-" && sign(text[at + 1])) { + at += 1; + if (one && digit(text[at])) return -Number(text[at++]); + return rest ? -expression() : -term(); + } + if (text[at] === "(") { + at += 1; + const value = expression(); + skip(); + if (text[at] !== ")") throw new Error(`no ")" at ${at}`); + at += 1; + return value; + } + const start = at; + while (digit(text[at])) at += 1; + if (at === start) throw new Error(`no number at ${start}`); + return Number(text.slice(start, at)); + }; + const expression = () => { + let value = term(); + skip(); + while (text[at] === "+" || text[at] === "-") { + const op = text[at]; + at += 1; + value = op === "+" ? value + term() : value - term(); + skip(); + } + return value; + }; + const value = expression(); + if (at !== text.length) throw new Error(`unread input at ${at}`); + return value; + }; + for (const [name, parse] of [ + ["the binary-minus-only parser", (s) => evaluate(s)], + [ + "the parser that prepends 0 for a leading minus", + (s) => evaluate(s.trim().startsWith("-") ? `0${s.trim()}` : s), + ], + [ + "the parser whose minus negates the rest of the expression", + (s) => evaluate(s, { sign: () => true, rest: true }), + ], + [ + "the parser whose unary minus only precedes a number", + (s) => evaluate(s, { sign: digit }), + ], + [ + "the parser whose unary minus must touch its operand", + (s) => evaluate(s, { sign: (c) => c === "(" || digit(c), spaces: true }), + ], + [ + "the parser whose unary minus reads one digit", + (s) => evaluate(s, { sign: () => true, one: true }), + ], + ]) { + assert.ok( + judges["basic-calculator"].cases.some(({ input: [s], expected }) => { + try { + return parse(s) !== expected; + } catch { + return true; + } + }), + `the built-in cases must reject ${name}`, + ); + } + // The task workspace's calculator checks every expected value, and refuses + // an unclosed group or input left unread. It also reads `*`, functions and a + // plus sign of its own, which the contract leaves out, so each input is held + // to the contract's characters first. + for (const { input, expected, label } of judges["basic-calculator"].cases) { + assert.match(input[0], /^[\d ()+-]+$/, label); + assert.doesNotMatch(input[0], /(^|[(+-]) *\+/, label); + assert.equal(calculate(input[0]), expected, label); + } +}); + test("harness generator covers every function problem without network access", () => { for (const [id, spec] of Object.entries(judges)) { if (spec.kind !== "function") continue; diff --git a/tests/golden/task-posed.json b/tests/golden/task-posed.json index 5f9703983..9cd577256 100644 --- a/tests/golden/task-posed.json +++ b/tests/golden/task-posed.json @@ -3,7 +3,7 @@ "add-binary": "7bf1c0a6e13777e1a063aebc49f88841039999d45ec25decf032d0e83ff99b53", "add-two-numbers": "63c7d5769d1b808781d419f0453f76db5ba8b21805141b0c90695880eb17aa8a", "average-of-levels-in-binary-tree": "bb86918dfe897be35b53445421a4cbb84d3ea78082acd3a35056bd551056f590", - "basic-calculator": "61e6eea9d1ef217bc23542488ef516dd617c6b3d4d76bc2eae93a2070074a7d9", + "basic-calculator": "5a58fefbd2b1a90ec0559276df63d0566d31c9001d31f777c32aa3801b1841e5", "best-time-to-buy-and-sell-stock": "c1c521053accac35bade7e43ebfb008b39b1fe62a2c64c2ebd3da2970c9fb3ea", "best-time-to-buy-and-sell-stock-ii": "945ce2a74693dcc19b4fa5c9ed75210f04c5487b72b9328887e934a20fadca94", "best-time-to-buy-and-sell-stock-iii": "45592b40e06696a75258c37a57c9b988ebd6930843ab5c24bd55d536cfb9b247", diff --git a/tests/web/contract.rs b/tests/web/contract.rs index 60f14932b..3b3e134d4 100644 --- a/tests/web/contract.rs +++ b/tests/web/contract.rs @@ -631,6 +631,11 @@ fn static_problem_bank_and_judges_cover_each_problem() { "negative division truncates toward zero", ), ("basic-calculator", "nested subtraction"), + ("basic-calculator", "minus before a group"), + ("basic-calculator", "minus inside a group"), + ("basic-calculator", "minus on the first number only"), + ("basic-calculator", "space after a leading minus"), + ("basic-calculator", "minus before a two-digit number"), ("linked-list-cycle", "tail points to itself"), ("add-two-numbers", "carry extends result"), ("merge-two-sorted-lists", "negative values"), diff --git a/web/judges/budget-sheet-formulas.json b/web/judges/budget-sheet-formulas.json index a287c894a..9bf9d17a6 100644 --- a/web/judges/budget-sheet-formulas.json +++ b/web/judges/budget-sheet-formulas.json @@ -51,6 +51,41 @@ "1" ], "expected": 1 + }, + { + "label": "minus before a group", + "input": [ + "-(2+3)" + ], + "expected": -5 + }, + { + "label": "minus inside a group", + "input": [ + "1-(-2)" + ], + "expected": 3 + }, + { + "label": "minus on the first number only", + "input": [ + "-2+3" + ], + "expected": 1 + }, + { + "label": "space after a leading minus", + "input": [ + "- 7" + ], + "expected": -7 + }, + { + "label": "minus before a two-digit number", + "input": [ + "-12" + ], + "expected": -12 } ], "paramNames": [