Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions problem-bank/judges.json
Original file line number Diff line number Diff line change
Expand Up @@ -5492,6 +5492,41 @@
"1"
],
"expected": 1
},
{
"label": "minus before a group",
"input": [
"-(2+3)"
Comment thread
SkyDrive2 marked this conversation as resolved.
Resolved
],
"expected": -5
},
{
"label": "minus inside a group",
"input": [
"1-(-2)"
],
"expected": 3
},
{
"label": "minus on the first number only",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The new -2+3 case only tests unary minus at the start of an expression. A parser that rejects unary minus immediately after a binary operator still passes these cases; add 1+-2 with expected value -1 to cover that position.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At problem-bank/judges.json, line 5444:

<comment>The new `-2+3` case only tests unary minus at the start of an expression. A parser that rejects unary minus immediately after a binary operator still passes these cases; add `1+-2` with expected value `-1` to cover that position.</comment>

<file context>
@@ -5434,11 +5434,25 @@
         "expected": 3
+      },
+      {
+        "label": "minus on the first number only",
+        "input": [
+          "-2+3"
</file context>

"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": [
Expand Down
95 changes: 95 additions & 0 deletions tests/browser/compiler-explorer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Comment thread
SkyDrive2 marked this conversation as resolved.
Resolved
}
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",
Comment thread
SkyDrive2 marked this conversation as resolved.
Resolved
(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}`,
);
}
Comment thread
SkyDrive2 marked this conversation as resolved.
Resolved
// 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);
Comment thread
SkyDrive2 marked this conversation as resolved.
Resolved
}
});

test("harness generator covers every function problem without network access", () => {
for (const [id, spec] of Object.entries(judges)) {
if (spec.kind !== "function") continue;
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/task-posed.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions tests/web/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
35 changes: 35 additions & 0 deletions web/judges/budget-sheet-formulas.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
Loading