Skip to content

Commit ef1842e

Browse files
committed
test(assail): check stripped output against an independent oracle
The exhaustive test checked length, the per-byte relation and idempotence, which a scanner that blanks plain code could still pass. Add `oracle`, the comment grammar as mutually recursive rules over a char slice (code, line comment, block at depth d), sharing no code with the scanner, and assert the scanner's full output equals it for every enumerated input. A planted fault that blanks non-comment code now fails this test alone. PROOF-NEEDS names the oracle and the fault. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jma5ixtpeZyJRmfoSsC98u
1 parent 7b9f145 commit ef1842e

2 files changed

Lines changed: 84 additions & 4 deletions

File tree

‎PROOF-NEEDS.adoc‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ other comment bytes become spaces. Stripping is idempotent. Whichever
124124
comment opens first wins, and block comments nest.
125125
|`src/assail/analyzer.rs`, module `strip_proof_comments_tests`
126126
|`TESTED`
127-
|Exhaustive over bounded inputs, for the marker sets the analyzers pass:
127+
|Exhaustive over bounded inputs, against an independent recursive oracle
128+
(the comment grammar as mutually recursive rules, sharing no code with the
129+
scanner), for the marker sets the analyzers pass:
128130
`//` with `/* */`, `--` with `/- -/`, `--` with `{- -}`, `(* *)` alone, and
129131
`//` alone. Each runs every string over a 4-letter alphabet up to length 8
130132
(87,381 inputs) or a 5-letter alphabet up to length 7 (97,656 inputs). Each
@@ -135,6 +137,7 @@ are nesting, unterminated blocks and multibyte text.
135137
Planted faults, all caught:
136138

137139
* the previous two-phase function;
140+
* blanking code outside comments (caught by the oracle alone);
138141
* deleting comment text instead of blanking it;
139142
* blanking that drops a newline;
140143
* one space per character instead of per byte;

‎src/assail/analyzer.rs‎

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9009,16 +9009,93 @@ mod strip_proof_comments_tests {
90099009
assert_eq!(strip("a /* b */", ("", Some(("", "*/")))), "a /* b */");
90109010
}
90119011

9012+
/// An independent statement of what `strip_proof_comments` should give:
9013+
/// the comment grammar written as mutually recursive rules (code, line
9014+
/// comment, block comment at depth `d`) over a char slice, sharing no
9015+
/// code with the index-and-match scanner under test. Block markers are
9016+
/// checked before the line marker in code, and a close before an open
9017+
/// inside a block; every comment character but `'\n'` becomes one space
9018+
/// per UTF-8 byte.
9019+
fn oracle(s: &[char], (line, block): Markers) -> String {
9020+
fn starts(s: &[char], marker: &str) -> bool {
9021+
let m: Vec<char> = marker.chars().collect();
9022+
!m.is_empty() && s.starts_with(&m)
9023+
}
9024+
fn blank(c: char, out: &mut String) {
9025+
if c == '\n' {
9026+
out.push('\n');
9027+
} else {
9028+
out.push_str(&" ".repeat(c.len_utf8()));
9029+
}
9030+
}
9031+
fn blank_marker(marker: &str, out: &mut String) {
9032+
marker.chars().for_each(|c| blank(c, out));
9033+
}
9034+
fn code(s: &[char], m: Markers, out: &mut String) {
9035+
let (line, block) = m;
9036+
match (s, block) {
9037+
([], _) => {}
9038+
(_, Some((open, close))) if !close.is_empty() && starts(s, open) => {
9039+
blank_marker(open, out);
9040+
in_block(&s[open.chars().count()..], 1, m, out);
9041+
}
9042+
_ if starts(s, line) => line_comment(s, m, out),
9043+
([c, rest @ ..], _) => {
9044+
out.push(*c);
9045+
code(rest, m, out);
9046+
}
9047+
}
9048+
}
9049+
fn line_comment(s: &[char], m: Markers, out: &mut String) {
9050+
match s {
9051+
[] => {}
9052+
['\n', ..] => code(s, m, out),
9053+
[c, rest @ ..] => {
9054+
blank(*c, out);
9055+
line_comment(rest, m, out);
9056+
}
9057+
}
9058+
}
9059+
fn in_block(s: &[char], d: usize, m: Markers, out: &mut String) {
9060+
let Some((open, close)) = m.1 else { return };
9061+
match s {
9062+
[] => {}
9063+
_ if starts(s, close) => {
9064+
blank_marker(close, out);
9065+
let rest = &s[close.chars().count()..];
9066+
if d == 1 {
9067+
code(rest, m, out);
9068+
} else {
9069+
in_block(rest, d - 1, m, out);
9070+
}
9071+
}
9072+
_ if starts(s, open) => {
9073+
blank_marker(open, out);
9074+
in_block(&s[open.chars().count()..], d + 1, m, out);
9075+
}
9076+
[c, rest @ ..] => {
9077+
blank(*c, out);
9078+
in_block(rest, d, m, out);
9079+
}
9080+
}
9081+
}
9082+
let mut out = String::new();
9083+
code(s, (line, block), &mut out);
9084+
out
9085+
}
9086+
90129087
/// Exhaustive over every string up to the bound, for every marker set the
9013-
/// analyzers use: the result has the input's length, keeps every byte
9014-
/// outside a comment and every newline, blanks the rest with spaces, and
9015-
/// is a fixed point.
9088+
/// analyzers use: the result equals the independent `oracle`, has the
9089+
/// input's length, keeps every byte outside a comment and every newline,
9090+
/// blanks the rest with spaces, and is a fixed point.
90169091
#[test]
90179092
fn stripping_preserves_positions_and_is_idempotent_exhaustively() {
90189093
for (markers, alphabet) in CONFIGS {
90199094
let max_len = if alphabet.len() > 4 { 7 } else { 8 };
90209095
for src in all_strings(alphabet, max_len) {
90219096
let out = strip(&src, markers);
9097+
let chars: Vec<char> = src.chars().collect();
9098+
assert_eq!(out, oracle(&chars, markers), "{markers:?} {src:?}");
90229099
assert_eq!(out.len(), src.len(), "{markers:?} {src:?}");
90239100
for (a, b) in src.bytes().zip(out.bytes()) {
90249101
assert!(

0 commit comments

Comments
 (0)