Files
lucisv/crates/vlog_formatter/tests/deep_nesting.rs
T
Guoliang ChenandClaude Mythos 5.2 71d13b5fbe
ci / checks (push) Successful in 1m0s
ci / corpus (push) Successful in 18s
ci / linux (push) Successful in 6m4s
release / build-and-publish (push) Successful in 5m31s
Release 0.66.2: every source file names its author
Every tracked file that can carry a comment (880 of them) now opens with
"Author: Guoliang Chen, a Senior Digital Design Engineer" and closes with
"Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love,
luciochen.com", in the file's own comment syntax. No behaviour change: the
diff outside the version bump and changelogs is comment lines only.

- tools/stamp_author.py: writes the lines; idempotent, `--check` lists
  what is missing. Headers go under a shebang and under Markdown front
  matter.
- Left alone: test fixtures, cases and snapshots (byte-exact inputs), JSON
  and JSONL, logs, images, lockfiles, docs/site/dist (regenerated) and the
  LICENSE files (already carry the copyright line).
- tools/linux_check.sh: --help reads its header from line 3, now that the
  author line sits under the shebang.
- tools/gates.sh green on this tree: 5 545 tests, corpus 814/6 as before.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-24 12:11:09 -08:00

110 lines
4.5 KiB
Rust

// Author: Guoliang Chen, a Senior Digital Design Engineer
//! The formatter's recursive walk against the parser's depth limit.
//!
//! `vlog_parser` bounds AST depth (`MAX_NESTING_DEPTH`), and every recursive
//! path in `format.rs`/`printer.rs` descends the AST it is handed, so these
//! tests pin the two ends of that argument: the deepest tree the parser can
//! produce still formats (and converges), and anything deeper never reaches
//! the formatter at all.
use vlog_formatter::{format_source, Config, FormatError};
use vlog_parser::parser::{MAX_CHAIN_LENGTH, MAX_NESTING_DEPTH, PARSE_STACK_SIZE};
/// Formatting the deepest legal tree peaks around 9.7 MiB of stack in an
/// unoptimized build, far more than a default test thread's 2 MiB. The size is
/// the one every production parse site is given, so these tests fail if the
/// guarantee is ever cut below what the two limits need.
fn format_deep(source: String) -> Result<String, FormatError> {
std::thread::Builder::new()
.stack_size(PARSE_STACK_SIZE)
.spawn(move || format_source(&source, &Config::default()))
.expect("spawn formatter thread")
.join()
.expect("formatter thread crashed")
}
/// `n` nested `begin` blocks; the module and the `initial` add two levels.
fn nested_begins(n: usize) -> String {
format!(
"module m;\ninitial\n{}x <= 1;\n{}\nendmodule\n",
"begin\n".repeat(n),
"end\n".repeat(n)
)
}
/// `n` nested parenthesized expressions.
fn nested_parens(n: usize) -> String {
format!("module m;\nassign x = {}1{};\nendmodule\n", "(".repeat(n), ")".repeat(n))
}
#[test]
fn deepest_legal_statement_nesting_formats() {
// Two levels for `module` + `initial`, one for the innermost statement.
let out = format_deep(nested_begins(MAX_NESTING_DEPTH as usize - 5))
.expect("the deepest tree the parser accepts must format");
assert!(out.ends_with("end\nendmodule\n"), "{}", &out[out.len().saturating_sub(40)..]);
// `format_source` verifies equivalence and convergence internally, so a
// second pass returning the same text is the idempotency check.
let again = format_deep(out.clone()).expect("reformat");
assert_eq!(out, again, "formatting deep nesting is a fixed point");
}
#[test]
fn deepest_legal_expression_nesting_formats() {
let out = format_deep(nested_parens(MAX_NESTING_DEPTH as usize / 2 - 2))
.expect("the deepest expression the parser accepts must format");
let again = format_deep(out.clone()).expect("reformat");
assert_eq!(out, again, "formatting deep expressions is a fixed point");
}
/// `a` followed by `n` postfix segments — a chain the parser builds with a
/// loop, so it costs no parser recursion but one AST layer per segment.
fn member_chain(n: usize) -> String {
format!("module m;\nassign x = a{};\nendmodule\n", ".b".repeat(n))
}
#[test]
fn longest_legal_chain_formats() {
let out = format_deep(member_chain(MAX_CHAIN_LENGTH as usize))
.expect("the longest chain the parser accepts must format");
let again = format_deep(out.clone()).expect("reformat");
assert_eq!(out, again, "formatting a maximal chain is a fixed point");
}
/// The stack worst case: a maximal chain inside statement nesting. The two
/// peak in different phases — nesting while parsing, chains while formatting —
/// so this is the input `PARSE_STACK_SIZE` is measured against.
#[test]
fn a_maximal_chain_inside_deep_nesting_formats() {
let depth = MAX_NESTING_DEPTH as usize / 2 - 4;
let src = format!(
"module m;\ninitial\n{}x <= a{};\n{}\nendmodule\n",
"begin\n".repeat(depth),
".b".repeat(MAX_CHAIN_LENGTH as usize),
"end\n".repeat(depth)
);
let out = format_deep(src).expect("a maximal chain inside deep nesting must format");
let again = format_deep(out.clone()).expect("reformat");
assert_eq!(out, again, "the combined worst case is a fixed point");
}
#[test]
fn nesting_past_the_limit_never_reaches_the_formatter() {
let cases = [
(nested_begins(400), "nesting too deep"),
(nested_parens(400), "nesting too deep"),
(member_chain(MAX_CHAIN_LENGTH as usize + 1), "expression chain too long"),
];
for (src, expected) in cases {
match format_deep(src) {
Err(FormatError::Source(e)) => {
assert!(e.to_string().contains(expected), "unexpected error: {e}")
}
other => panic!("expected a parse error, got {other:?}"),
}
}
}
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com