mod common;
fn survives(parser: &tabnas::Tabnas, source: &str) -> bool {
match tabnas_c::parse_with(parser, source) {
Ok(value) => {
let _ = value.to_json().to_string();
true
}
Err(_) => true,
}
}
#[test]
fn empty_and_tiny_inputs() {
let parser = tabnas_c::make();
for source in [
"", " ", "\n", "\t", ";", "i", "{", "}", "(", "/*", "//", "\"", "'",
] {
assert!(survives(&parser, source), "{source:?} did not return");
}
}
#[test]
fn top_level_call_returns() {
let parser = tabnas_c::make();
let value = tabnas_c::parse_with(&parser, "f(1);").expect("a top-level call must return");
let json = value.to_json();
assert_eq!(json["children"][0]["viaPath"], "legacy");
}
#[test]
fn nested_abstract_parameters_return() {
let parser = tabnas_c::make_with(&tabnas_c::COptions::new().with_extended(true));
let depth = 48;
let source = format!(
"void f({}int{});",
"void (*)(".repeat(depth),
")".repeat(depth)
);
assert!(survives(&parser, &source));
}
#[test]
fn unterminated_constructs() {
let parser = tabnas_c::make_with(&tabnas_c::COptions::new().with_extended(true));
for source in [
"/* never closed",
"\"never closed",
"'a",
"int f(void) {",
"int f(void) { if (a) {",
"struct s {",
"#define A(",
"#include <stdio",
"#if 1",
"int x = (1 + 2",
"int a[",
"enum e {",
"__attribute__((",
"int x = \\",
] {
assert!(survives(&parser, source), "{source:?} did not return");
}
}
#[test]
fn control_characters_and_odd_unicode() {
let parser = tabnas_c::make_with(&tabnas_c::COptions::new().with_extended(true));
for source in [
"int\u{0}x;",
"int x;\u{7}",
"int \u{feff}x;",
"int \u{a0}x;",
"int x = \"\u{1f600}\";",
"/* \u{1f600} */ int x;",
"int \u{4e2d}\u{6587};",
"// \u{202e}reversed\n int x;",
"int x = '\u{1f600}';",
"\u{feff}int x;",
] {
assert!(survives(&parser, source), "{source:?} did not return");
}
}
#[test]
fn multibyte_tails_do_not_panic() {
let parser = tabnas_c::make_with(&tabnas_c::COptions::new().with_extended(true));
let source = "int x = \"caf\u{e9} \u{1f600} \u{4e2d}\u{6587}\"; /* \u{e9}\u{1f600} */";
for (end, _) in source.char_indices() {
let head = &source[..end];
assert!(survives(&parser, head), "{head:?} did not return");
}
assert!(survives(&parser, source));
}
#[test]
fn very_long_input_returns() {
let parser = tabnas_c::make();
let mut flat = String::new();
for index in 0..20_000 {
use std::fmt::Write;
writeln!(flat, "int v{index};").expect("a String write cannot fail");
}
assert!(survives(&parser, &flat), "a long flat unit did not return");
let long_name = format!("int {};", "a".repeat(200_000));
assert!(survives(&parser, &long_name), "a long name did not return");
let long_string = format!("const char *s = \"{}\";", "x".repeat(200_000));
assert!(
survives(&parser, &long_string),
"a long literal did not return"
);
}
const DEEPEST_ADMITTED: usize = tabnas_c::REALIZE_DEPTH_CAP - 4;
fn nest(depth: usize) -> String {
format!(
"void f(void) {{ {}{} }}",
"{".repeat(depth),
"}".repeat(depth)
)
}
#[test]
fn nesting_at_the_cap() {
for extended in [false, true] {
let parser = tabnas_c::make_with(&tabnas_c::COptions::new().with_extended(extended));
let value = tabnas_c::parse_with(&parser, &nest(DEEPEST_ADMITTED))
.unwrap_or_else(|error| panic!("{DEEPEST_ADMITTED} levels: {}", error.code));
assert_eq!(value.to_json()["kind"], "translation_unit");
}
}
#[test]
fn nesting_past_the_cap() {
for extended in [false, true] {
let parser = tabnas_c::make_with(&tabnas_c::COptions::new().with_extended(extended));
for depth in [
DEEPEST_ADMITTED + 1,
tabnas_c::REALIZE_DEPTH_CAP * 4,
20_000,
] {
let error = tabnas_c::parse_with(&parser, &nest(depth))
.err()
.unwrap_or_else(|| panic!("{depth} levels should not have realized"));
assert_eq!(
error.code, "cancel",
"{depth} levels reported the wrong code"
);
}
}
}
#[test]
fn expression_cycle() {
let parser = tabnas_c::make();
let error =
tabnas_c::parse_with(&parser, "int a = b ? c : d;").expect_err("the tree holds itself");
assert_eq!(error.code, "cancel");
let value = tabnas_c::parse_with(&parser, "int a = (b ? c : d);").expect("parentheses parse");
assert_eq!(value.to_json()["kind"], "translation_unit");
}
#[test]
fn other_deep_shapes_return() {
let parser = tabnas_c::make_with(&tabnas_c::COptions::new().with_extended(true));
for source in [
format!("int x = {}1{};", "(".repeat(5_000), ")".repeat(5_000)),
format!("int x = {}1;", "-".repeat(5_000)),
format!("int x{};", "[1]".repeat(5_000)),
format!("int {}p;", "*".repeat(5_000)),
format!("void f(void) {{ {} ; }}", "if (a) ".repeat(2_000)),
format!(
"{}int x;{}",
"#if 1\n".repeat(2_000),
"#endif\n".repeat(2_000)
),
] {
assert!(
survives(&parser, &source),
"a deep shape did not return: {:?}",
&source[..40.min(source.len())]
);
}
}
#[test]
fn every_recursive_construct_stops_at_the_cap() {
let parser = tabnas_c::make_with(&tabnas_c::COptions::new().with_extended(true));
let deep = |open: &str, core: &str, close: &str| {
format!("{}{core}{}", open.repeat(2_000), close.repeat(2_000))
};
let body = |statements: String| format!("void f(void) {{ {statements} }}");
for source in [
body(deep("if (a) ", " ;", "")),
body(deep("if (a) ; else ", " ;", "")),
body(deep("", "if (a) ;", " else if (a) ;")),
body(deep("while (a) ", " ;", "")),
body(deep("for (;;) ", " ;", "")),
body(deep("do ", " ;", " while (a);")),
body(deep("switch (a) ", " ;", "")),
body(deep("l: ", " ;", "")),
body(format!("switch (a) {{ {} }}", deep("case 1: ", " ;", ""))),
body(deep("if (a) {", " ;", "}")),
format!("{};", deep("struct s { ", "int x;", "} y;")),
format!("int {};", deep("(", "x", ")")),
format!("int {};", deep("(*", "x", ")(void)")),
format!("void f({});", deep("void (*)(", "int", ")")),
body(format!("int x = {};", deep("{", "1", "}"))),
body(format!("x = {};", deep("(", "1", ")"))),
body(format!("x = {};", deep("-", "1", ""))),
body(format!("x = {};", deep("(int)", "1", ""))),
body(format!("{};", deep("a = ", "1", ""))),
body(format!("{};", deep("f(", "1", ")"))),
body(format!("x = {};", deep("", "1", " + 1"))),
body(format!("{};", deep("", "x", ".m"))),
] {
let head = &source[..40.min(source.len())];
let error = tabnas_c::parse_with(&parser, &source)
.err()
.unwrap_or_else(|| panic!("{head:?} should not have realized"));
assert_eq!(error.code, "cancel", "{head:?} reported the wrong code");
}
}