use decy_parser::CParser;
fn parse_c_code(code: &str) -> decy_parser::Ast {
let parser = CParser::new().expect("Parser creation failed");
parser.parse(code).expect("Parse failed")
}
fn parse_succeeds(code: &str) -> bool {
let parser = CParser::new().ok();
if parser.is_none() {
return false;
}
parser.unwrap().parse(code).is_ok()
}
#[test]
fn test_char_literal_escape_null() {
let ast = parse_c_code("char c = '\\0';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_escape_newline() {
let ast = parse_c_code("char c = '\\n';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_escape_tab() {
let ast = parse_c_code("char c = '\\t';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_escape_carriage_return() {
let ast = parse_c_code("char c = '\\r';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_escape_backslash() {
let ast = parse_c_code("char c = '\\\\';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_escape_single_quote() {
let ast = parse_c_code("char c = '\\'';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_escape_double_quote() {
let ast = parse_c_code("char c = '\\\"';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_escape_bell() {
let ast = parse_c_code("char c = '\\a';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_escape_backspace() {
let ast = parse_c_code("char c = '\\b';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_escape_formfeed() {
let ast = parse_c_code("char c = '\\f';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_escape_vertical_tab() {
let ast = parse_c_code("char c = '\\v';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_escape_hex() {
let ast = parse_c_code("char c = '\\x41';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_escape_octal() {
let ast = parse_c_code("char c = '\\101';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_char_literal_plain() {
let ast = parse_c_code("char c = 'A';");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_float_literal_with_f_suffix() {
let ast = parse_c_code("float f = 3.14f;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_float_literal_with_l_suffix() {
let ast = parse_c_code("long double ld = 3.14L;");
let _ = ast;
}
#[test]
fn test_float_literal_scientific() {
let ast = parse_c_code("double d = 1.5e10;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_float_literal_hex() {
let ast = parse_c_code("double d = 0x1.fp3;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_int_literal_hex() {
let ast = parse_c_code("int i = 0xFF;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_int_literal_octal() {
let ast = parse_c_code("int i = 0777;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_int_literal_binary() {
let ast = parse_c_code("int i = 0b1010;");
let _ = ast;
}
#[test]
fn test_int_literal_with_suffix() {
let ast = parse_c_code("unsigned long ul = 123UL;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_int_literal_negative() {
let ast = parse_c_code("int i = -42;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_binary_op_shift_left() {
let ast = parse_c_code("int x = 1 << 2;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_binary_op_shift_right() {
let ast = parse_c_code("int x = 8 >> 2;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_binary_op_bitwise_and() {
let ast = parse_c_code("int x = 5 & 3;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_binary_op_bitwise_or() {
let ast = parse_c_code("int x = 5 | 3;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_binary_op_bitwise_xor() {
let ast = parse_c_code("int x = 5 ^ 3;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_binary_op_modulo() {
let ast = parse_c_code("int x = 10 % 3;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_compound_assignment_left_shift() {
let ast = parse_c_code("void f() { int x = 1; x <<= 2; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_compound_assignment_right_shift() {
let ast = parse_c_code("void f() { int x = 8; x >>= 2; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_compound_assignment_and() {
let ast = parse_c_code("void f() { int x = 5; x &= 3; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_compound_assignment_or() {
let ast = parse_c_code("void f() { int x = 5; x |= 3; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_compound_assignment_xor() {
let ast = parse_c_code("void f() { int x = 5; x ^= 3; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_compound_assignment_mod() {
let ast = parse_c_code("void f() { int x = 10; x %= 3; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_unary_op_bitwise_not() {
let ast = parse_c_code("int x = ~5;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_unary_op_plus() {
let ast = parse_c_code("int x = +5;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_unary_op_sizeof_expr() {
let ast = parse_c_code("int x = sizeof(42);");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_unary_op_sizeof_type() {
let ast = parse_c_code("int x = sizeof(int);");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_for_loop_empty_parts() {
let ast = parse_c_code("void f() { for (;;) break; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_for_loop_with_declaration() {
let ast = parse_c_code("void f() { for (int i = 0; i < 10; i++) {} }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_while_loop_with_body() {
let ast = parse_c_code("void f() { while (1) { break; } }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_do_while_loop() {
let _ = parse_succeeds("void f() { do { } while(0); }");
}
#[test]
fn test_switch_with_fallthrough() {
let ast =
parse_c_code("void f(int x) { switch(x) { case 1: case 2: break; default: break; } }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_switch_with_default_only() {
let ast = parse_c_code("void f(int x) { switch(x) { default: break; } }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_nested_if_else() {
let ast = parse_c_code("void f(int x) { if (x > 0) { if (x > 10) { } else { } } else { } }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_goto_label() {
let _ = parse_succeeds("void f() { label: goto label; }");
}
#[test]
fn test_type_unsigned_char() {
let ast = parse_c_code("unsigned char c = 255;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_signed_char() {
let ast = parse_c_code("signed char c = -128;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_short() {
let ast = parse_c_code("short s = 32767;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_unsigned_short() {
let ast = parse_c_code("unsigned short us = 65535;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_long() {
let ast = parse_c_code("long l = 123456789L;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_unsigned_long() {
let ast = parse_c_code("unsigned long ul = 123456789UL;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_long_long() {
let ast = parse_c_code("long long ll = 123456789LL;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_unsigned_long_long() {
let ast = parse_c_code("unsigned long long ull = 123456789ULL;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_pointer_to_pointer() {
let ast = parse_c_code("int **pp;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_array_of_pointers() {
let ast = parse_c_code("int *arr[10];");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_pointer_to_array() {
let ast = parse_c_code("int (*ptr)[10];");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_const_pointer() {
let ast = parse_c_code("int * const ptr = 0;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_pointer_to_const() {
let ast = parse_c_code("const int * ptr = 0;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_type_volatile() {
let ast = parse_c_code("volatile int v = 0;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_struct_with_bitfield() {
let ast = parse_c_code("struct Flags { unsigned int flag1 : 1; unsigned int flag2 : 1; };");
assert!(!ast.structs().is_empty());
}
#[test]
fn test_struct_anonymous() {
let ast = parse_c_code("struct { int x; } anon;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_union_definition() {
let ast = parse_c_code("union U { int i; float f; };");
assert!(ast.structs().is_empty() || !ast.structs().is_empty());
}
#[test]
fn test_nested_struct() {
let ast = parse_c_code("struct Outer { struct Inner { int x; } inner; int y; };");
assert!(!ast.structs().is_empty());
}
#[test]
fn test_expression_comma() {
let ast = parse_c_code("int x = (1, 2, 3);");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_expression_ternary() {
let ast = parse_c_code("int x = 1 ? 2 : 3;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_expression_ternary_nested() {
let ast = parse_c_code("int x = 1 ? (2 ? 3 : 4) : 5;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_expression_cast() {
let ast = parse_c_code("float f = (float)42;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_expression_cast_pointer() {
let ast = parse_c_code("void *p = (void *)0;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_expression_compound_literal() {
let ast =
parse_c_code("struct Point { int x; int y; }; struct Point p = (struct Point){1, 2};");
assert!(!ast.structs().is_empty());
}
#[test]
fn test_expression_designated_initializer() {
let ast = parse_c_code("int arr[5] = { [2] = 10, [4] = 20 };");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_expression_init_list_nested() {
let ast = parse_c_code("int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_function_variadic() {
let ast = parse_c_code("void printf(const char *fmt, ...);");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_function_with_array_param() {
let ast = parse_c_code("void f(int arr[]);");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_function_with_sized_array_param() {
let ast = parse_c_code("void f(int arr[10]);");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_function_pointer_param() {
let ast = parse_c_code("void f(int (*callback)(int));");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_function_returning_pointer() {
let ast = parse_c_code("int *f(void) { return 0; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_function_static() {
let ast = parse_c_code("static int helper(void) { return 0; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_function_inline() {
let ast = parse_c_code("inline int helper(void) { return 0; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_macro_object_like() {
let ast = parse_c_code("#define MAX 100\nint x = MAX;");
let _ = ast;
}
#[test]
fn test_macro_function_like() {
let ast = parse_c_code("#define SQUARE(x) ((x) * (x))\nint y = SQUARE(5);");
let _ = ast;
}
#[test]
fn test_macro_with_args() {
let ast = parse_c_code("#define ADD(a, b) ((a) + (b))\nint z = ADD(1, 2);");
let _ = ast;
}
#[test]
fn test_parse_empty_file() {
let result = parse_succeeds("");
let _ = result;
}
#[test]
fn test_parse_comments_only() {
let ast = parse_c_code("// Just a comment\n/* Block comment */");
let _ = ast;
}
#[test]
fn test_parse_preprocessor_only() {
let ast = parse_c_code("#include <stdio.h>\n#define X 1");
let _ = ast;
}
#[test]
fn test_field_access_arrow() {
let ast = parse_c_code("struct S { int x; }; void f(struct S *s) { s->x = 1; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_field_access_dot() {
let ast = parse_c_code("struct S { int x; }; void f(struct S s) { s.x = 1; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_field_access_chained() {
let ast =
parse_c_code("struct A { struct B { int x; } b; }; void f(struct A a) { a.b.x = 1; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_array_index_variable() {
let ast = parse_c_code("void f(int arr[], int i) { arr[i] = 0; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_array_index_expression() {
let ast = parse_c_code("void f(int arr[]) { arr[1 + 2] = 0; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_array_index_multidimensional() {
let ast = parse_c_code("void f(int arr[3][3]) { arr[1][2] = 0; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_pointer_increment() {
let ast = parse_c_code("void f(int *p) { p++; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_pointer_decrement() {
let ast = parse_c_code("void f(int *p) { p--; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_pointer_add_offset() {
let ast = parse_c_code("void f(int *p) { int *q = p + 5; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_pointer_subtract() {
let ast = parse_c_code("void f(int *p, int *q) { int d = p - q; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_string_with_escapes() {
let ast = parse_c_code("char *s = \"Hello\\nWorld\\t!\";");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_string_concatenation() {
let ast = parse_c_code("char *s = \"Hello\" \" \" \"World\";");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_string_empty() {
let ast = parse_c_code("char *s = \"\";");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_enum_with_values() {
let ast = parse_c_code("enum Color { RED = 1, GREEN = 2, BLUE = 4 };");
assert!(!ast.enums().is_empty());
}
#[test]
fn test_enum_anonymous() {
let ast = parse_c_code("enum { A, B, C } x;");
assert!(!ast.variables().is_empty() || !ast.enums().is_empty());
}
#[test]
fn test_enum_with_negative() {
let ast = parse_c_code("enum Neg { MINUS = -1, ZERO = 0, PLUS = 1 };");
assert!(!ast.enums().is_empty());
}
#[test]
fn test_typedef_primitive() {
let ast = parse_c_code("typedef unsigned int uint;");
assert!(!ast.typedefs().is_empty());
}
#[test]
fn test_typedef_pointer() {
let ast = parse_c_code("typedef int *intptr;");
assert!(!ast.typedefs().is_empty());
}
#[test]
fn test_typedef_function_pointer() {
let ast = parse_c_code("typedef int (*callback)(int, int);");
assert!(!ast.typedefs().is_empty());
}
#[test]
fn test_typedef_struct() {
let ast = parse_c_code("typedef struct { int x; } Point;");
assert!(!ast.typedefs().is_empty() || !ast.structs().is_empty());
}
#[test]
fn test_static_variable() {
let ast = parse_c_code("static int counter = 0;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_extern_variable() {
let ast = parse_c_code("extern int global_var;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_static_function_variable() {
let ast = parse_c_code("void f(void) { static int count = 0; count++; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_goto_label_coverage() {
let ast = parse_c_code("void f() { start: goto start; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_break_in_switch_coverage() {
let ast = parse_c_code("void f(int x) { switch(x) { case 1: break; default: break; } }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_continue_in_loop_coverage() {
let ast = parse_c_code("void f() { for(int i=0;i<10;i++) { if(i==5) continue; } }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_do_while_loop_coverage() {
let ast = parse_c_code("void f() { int i=0; do { i++; } while(i<10); }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_nested_struct_coverage() {
let ast = parse_c_code("struct Outer { struct Inner { int x; } inner; };");
assert!(!ast.structs().is_empty());
}
#[test]
fn test_pointer_to_array() {
let ast = parse_c_code("int (*arr_ptr)[10];");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_array_of_pointers() {
let ast = parse_c_code("int *arr[10];");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_multidimensional_array() {
let ast = parse_c_code("int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_variadic_function() {
let ast = parse_c_code("int printf(const char* fmt, ...);");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_volatile_variable() {
let ast = parse_c_code("volatile int flag = 0;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_register_variable() {
let ast = parse_c_code("void f() { register int i = 0; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_inline_function() {
let ast = parse_c_code("inline int square(int x) { return x * x; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_const_pointer() {
let ast = parse_c_code("const int * const ptr = 0;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_restrict_pointer() {
let ast = parse_c_code("void f(int * restrict ptr) {}");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_long_double() {
let ast = parse_c_code("long double x = 3.14L;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_unsigned_long_long() {
let ast = parse_c_code("unsigned long long big = 1ULL;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_complex_initialization() {
let ast = parse_c_code("struct { int x; int y; } pt = { .x = 1, .y = 2 };");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_compound_literal() {
let ast = parse_c_code("int *p = (int[]){1, 2, 3};");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_sizeof_expression() {
let ast = parse_c_code("int s = sizeof(int);");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_sizeof_variable() {
let ast = parse_c_code("int arr[10]; int s = sizeof(arr);");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_nested_ternary() {
let ast = parse_c_code("int f(int a, int b, int c) { return a ? b ? 1 : 2 : c ? 3 : 4; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_comma_operator() {
let ast = parse_c_code("int f() { int a = (1, 2, 3); return a; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_empty_struct() {
let ast = parse_c_code("struct Empty {};");
assert!(!ast.structs().is_empty());
}
#[test]
fn test_union() {
let ast = parse_c_code("union Data { int i; float f; char c; };");
assert!(ast.functions().is_empty()); }
#[test]
fn test_bitfield() {
let ast = parse_c_code("struct Flags { unsigned int a : 1; unsigned int b : 3; };");
assert!(!ast.structs().is_empty());
}
#[test]
fn test_flexible_array_member() {
let ast = parse_c_code("struct FlexArray { int size; char data[]; };");
assert!(!ast.structs().is_empty());
}
#[test]
fn test_forward_declaration_struct() {
let ast = parse_c_code("struct Node; struct Node { int data; struct Node *next; };");
assert!(!ast.structs().is_empty());
}
#[test]
fn test_nested_comments() {
let ast = parse_c_code("/* outer /* inner */ int x = 1;");
let _ = ast;
}
#[test]
fn test_preprocessor_in_function() {
let ast = parse_c_code("int f() { return 42; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_complex_expression() {
let ast =
parse_c_code("int f(int a, int b) { return ((a + b) * (a - b)) / ((a > b) ? a : b); }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_void_return() {
let ast = parse_c_code("void f() { return; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_multiple_return_statements() {
let ast = parse_c_code("int f(int x) { if (x > 0) return 1; if (x < 0) return -1; return 0; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_cov_for_loop_condition_only() {
let ast = parse_c_code("void f(int n) { int i = 0; for (; i < n; ) { i = i + 1; } }");
let func = &ast.functions()[0];
let has_for = func.body.iter().any(|s| matches!(s, decy_parser::Statement::For { .. }));
assert!(has_for, "Should have a for statement");
}
#[test]
fn test_cov_for_loop_init_only() {
let ast = parse_c_code("void f() { for (int i = 0;;) { break; } }");
let func = &ast.functions()[0];
let has_for = func.body.iter().any(|s| matches!(s, decy_parser::Statement::For { .. }));
assert!(has_for, "Should have a for statement");
}
#[test]
fn test_cov_for_loop_increment_only() {
let ast = parse_c_code("void f(int n) { int i = 0; for (; i < n; i++) { } }");
let func = &ast.functions()[0];
let has_for = func.body.iter().any(|s| matches!(s, decy_parser::Statement::For { .. }));
assert!(has_for, "Should have a for statement");
}
#[test]
fn test_cov_for_loop_full_three_parts() {
let ast = parse_c_code("void f() { for (int i = 0; i < 10; i++) { } }");
let func = &ast.functions()[0];
if let decy_parser::Statement::For { init, condition, increment, .. } = &func.body[0] {
assert!(!init.is_empty(), "Should have init");
assert!(condition.is_some(), "Should have condition");
assert!(!increment.is_empty(), "Should have increment");
}
}
#[test]
fn test_cov_for_loop_single_statement_body() {
let ast = parse_c_code("void f(int n) { for (int i = 0; i < n; i++) return; }");
let func = &ast.functions()[0];
let has_for = func.body.iter().any(|s| matches!(s, decy_parser::Statement::For { .. }));
assert!(has_for, "Should have a for statement");
}
#[test]
fn test_cov_for_loop_assignment_init() {
let ast = parse_c_code("void f() { int i; for (i = 0; i < 10; i++) { } }");
let func = &ast.functions()[0];
let has_for = func.body.iter().any(|s| matches!(s, decy_parser::Statement::For { .. }));
assert!(has_for, "Should have a for statement");
}
#[test]
fn test_cov_while_loop_with_variable_condition() {
let ast = parse_c_code("void f(int running) { while (running) { running = 0; } }");
let func = &ast.functions()[0];
let has_while = func.body.iter().any(|s| matches!(s, decy_parser::Statement::While { .. }));
assert!(has_while, "Should have a while statement");
}
#[test]
fn test_cov_while_loop_with_function_call_condition() {
let ast = parse_c_code("int check(void); void f() { while (check()) { } }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_while = f.body.iter().any(|s| matches!(s, decy_parser::Statement::While { .. }));
assert!(has_while, "Should have a while statement");
}
#[test]
fn test_cov_while_loop_with_unary_condition() {
let ast = parse_c_code("void f(int x) { while (!x) { x = 1; } }");
let func = &ast.functions()[0];
let has_while = func.body.iter().any(|s| matches!(s, decy_parser::Statement::While { .. }));
assert!(has_while, "Should have a while statement");
}
#[test]
fn test_cov_if_else_single_stmt_then() {
let ast = parse_c_code("void f(int x) { if (x > 0) return; }");
let func = &ast.functions()[0];
let has_if = func.body.iter().any(|s| matches!(s, decy_parser::Statement::If { .. }));
assert!(has_if, "Should have an if statement");
}
#[test]
fn test_cov_if_else_single_stmt_else() {
let ast = parse_c_code("int f(int x) { if (x > 0) return 1; else return 0; }");
let func = &ast.functions()[0];
if let decy_parser::Statement::If { else_block, .. } = &func.body[0] {
assert!(else_block.is_some(), "Should have else block");
}
}
#[test]
fn test_cov_if_else_if_chain() {
let ast = parse_c_code("int f(int x) { if (x > 0) { return 1; } else if (x < 0) { return -1; } else { return 0; } }");
let func = &ast.functions()[0];
let has_if = func.body.iter().any(|s| matches!(s, decy_parser::Statement::If { .. }));
assert!(has_if, "Should have an if statement");
}
#[test]
fn test_cov_if_with_integer_condition() {
let ast = parse_c_code("void f() { if (1) { return; } }");
let func = &ast.functions()[0];
let has_if = func.body.iter().any(|s| matches!(s, decy_parser::Statement::If { .. }));
assert!(has_if, "Should have an if statement");
}
#[test]
fn test_cov_if_with_char_literal_condition() {
let ast = parse_c_code("void f(char c) { if (c == 'a') { return; } }");
let func = &ast.functions()[0];
let has_if = func.body.iter().any(|s| matches!(s, decy_parser::Statement::If { .. }));
assert!(has_if, "Should have an if statement");
}
#[test]
fn test_cov_switch_with_multiple_cases() {
let ast = parse_c_code(
"int f(int x) { switch(x) { case 0: return 0; case 1: return 1; case 2: return 2; default: return -1; } }"
);
let func = &ast.functions()[0];
if let decy_parser::Statement::Switch { cases, default_case, .. } = &func.body[0] {
assert!(cases.len() >= 2, "Should have at least 2 cases, got {}", cases.len());
assert!(default_case.is_some(), "Should have default case");
} else {
panic!("First statement should be a switch");
}
}
#[test]
fn test_cov_switch_case_with_function_call() {
let ast = parse_c_code(
"void print(int x); void f(int x) { switch(x) { case 1: print(x); break; default: break; } }"
);
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_switch = f.body.iter().any(|s| matches!(s, decy_parser::Statement::Switch { .. }));
assert!(has_switch, "Should have a switch statement");
}
#[test]
fn test_cov_deref_assignment() {
let ast = parse_c_code("void f(int *ptr) { *ptr = 42; }");
let func = &ast.functions()[0];
let has_deref =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::DerefAssignment { .. }));
assert!(has_deref, "Should have a deref assignment");
}
#[test]
fn test_cov_array_index_assignment() {
let ast = parse_c_code("void f(int *arr, int i) { arr[i] = 42; }");
let func = &ast.functions()[0];
let has_arr_assign =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::ArrayIndexAssignment { .. }));
assert!(has_arr_assign, "Should have an array index assignment");
}
#[test]
fn test_cov_field_assignment_arrow() {
let ast = parse_c_code("struct S { int x; }; void f(struct S *s) { s->x = 42; }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_field =
f.body.iter().any(|s| matches!(s, decy_parser::Statement::FieldAssignment { .. }));
assert!(has_field, "Should have a field assignment");
}
#[test]
fn test_cov_field_assignment_dot() {
let ast = parse_c_code("struct S { int x; }; void f() { struct S s; s.x = 42; }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_field =
f.body.iter().any(|s| matches!(s, decy_parser::Statement::FieldAssignment { .. }));
assert!(has_field, "Should have a field assignment");
}
#[test]
fn test_cov_compound_assign_to_deref() {
let ast = parse_c_code("void f(int *ptr) { *ptr += 1; }");
let func = &ast.functions()[0];
let has_deref_compound = func
.body
.iter()
.any(|s| matches!(s, decy_parser::Statement::DerefCompoundAssignment { .. }));
assert!(has_deref_compound, "Should have a deref compound assignment");
}
#[test]
fn test_cov_compound_assign_to_pointer_field() {
let ast = parse_c_code("struct S { int cap; }; void f(struct S *s) { s->cap *= 2; }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_deref_compound =
f.body.iter().any(|s| matches!(s, decy_parser::Statement::DerefCompoundAssignment { .. }));
assert!(has_deref_compound, "Should have deref compound assignment for pointer field");
}
#[test]
fn test_cov_compound_assign_to_dot_field() {
let ast = parse_c_code("struct S { int x; }; void f() { struct S s; s.x -= 1; }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_deref_compound =
f.body.iter().any(|s| matches!(s, decy_parser::Statement::DerefCompoundAssignment { .. }));
assert!(has_deref_compound, "Should have deref compound assignment for dot field");
}
#[test]
fn test_cov_compound_assign_to_array_index() {
let ast = parse_c_code("void f(int arr[], int i) { arr[i] += 1; }");
let func = &ast.functions()[0];
let has_deref_compound = func
.body
.iter()
.any(|s| matches!(s, decy_parser::Statement::DerefCompoundAssignment { .. }));
assert!(has_deref_compound, "Should have deref compound assignment for array index");
}
#[test]
fn test_cov_compound_assign_add() {
let ast = parse_c_code("void f() { int x = 0; x += 5; }");
let func = &ast.functions()[0];
let has_compound =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::CompoundAssignment { .. }));
assert!(has_compound, "Should have compound assignment +=");
}
#[test]
fn test_cov_compound_assign_subtract() {
let ast = parse_c_code("void f() { int x = 10; x -= 3; }");
let func = &ast.functions()[0];
let has_compound =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::CompoundAssignment { .. }));
assert!(has_compound, "Should have compound assignment -=");
}
#[test]
fn test_cov_compound_assign_multiply() {
let ast = parse_c_code("void f() { int x = 2; x *= 3; }");
let func = &ast.functions()[0];
let has_compound =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::CompoundAssignment { .. }));
assert!(has_compound, "Should have compound assignment *=");
}
#[test]
fn test_cov_compound_assign_divide() {
let ast = parse_c_code("void f() { int x = 10; x /= 2; }");
let func = &ast.functions()[0];
let has_compound =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::CompoundAssignment { .. }));
assert!(has_compound, "Should have compound assignment /=");
}
#[test]
fn test_cov_compound_assign_modulo() {
let ast = parse_c_code("void f() { int x = 10; x %= 3; }");
let func = &ast.functions()[0];
let has_compound =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::CompoundAssignment { .. }));
assert!(has_compound, "Should have compound assignment %=");
}
#[test]
fn test_cov_pre_increment_statement() {
let ast = parse_c_code("void f() { int x = 0; ++x; }");
let func = &ast.functions()[0];
let has_pre_inc =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::PreIncrement { .. }));
assert!(has_pre_inc, "Should have pre-increment statement");
}
#[test]
fn test_cov_post_increment_statement() {
let ast = parse_c_code("void f() { int x = 0; x++; }");
let func = &ast.functions()[0];
let has_post_inc =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::PostIncrement { .. }));
assert!(has_post_inc, "Should have post-increment statement");
}
#[test]
fn test_cov_pre_decrement_statement() {
let ast = parse_c_code("void f() { int x = 10; --x; }");
let func = &ast.functions()[0];
let has_pre_dec =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::PreDecrement { .. }));
assert!(has_pre_dec, "Should have pre-decrement statement");
}
#[test]
fn test_cov_post_decrement_statement() {
let ast = parse_c_code("void f() { int x = 10; x--; }");
let func = &ast.functions()[0];
let has_post_dec =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::PostDecrement { .. }));
assert!(has_post_dec, "Should have post-decrement statement");
}
#[test]
fn test_cov_pointer_field_increment() {
let ast = parse_c_code("struct S { int length; }; void f(struct S *s) { s->length++; }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_field_assign =
f.body.iter().any(|s| matches!(s, decy_parser::Statement::FieldAssignment { .. }));
assert!(has_field_assign, "Pointer field increment should become FieldAssignment");
}
#[test]
fn test_cov_pointer_field_decrement() {
let ast = parse_c_code("struct S { int count; }; void f(struct S *s) { s->count--; }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_field_assign =
f.body.iter().any(|s| matches!(s, decy_parser::Statement::FieldAssignment { .. }));
assert!(has_field_assign, "Pointer field decrement should become FieldAssignment");
}
#[test]
fn test_cov_dot_field_increment() {
let ast = parse_c_code("struct S { int count; }; void f() { struct S s; s.count++; }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_field_assign =
f.body.iter().any(|s| matches!(s, decy_parser::Statement::FieldAssignment { .. }));
assert!(has_field_assign, "Dot field increment should become FieldAssignment");
}
#[test]
fn test_cov_array_subscript_increment() {
let ast = parse_c_code("void f(int arr[], int i) { arr[i]++; }");
let func = &ast.functions()[0];
let has_arr_assign =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::ArrayIndexAssignment { .. }));
assert!(has_arr_assign, "Array subscript increment should become ArrayIndexAssignment");
}
#[test]
fn test_cov_array_subscript_decrement() {
let ast = parse_c_code("void f(int arr[], int i) { arr[i]--; }");
let func = &ast.functions()[0];
let has_arr_assign =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::ArrayIndexAssignment { .. }));
assert!(has_arr_assign, "Array subscript decrement should become ArrayIndexAssignment");
}
#[test]
fn test_cov_ternary_in_return() {
let ast = parse_c_code("int max(int a, int b) { return (a > b) ? a : b; }");
let func = &ast.functions()[0];
let has_return = func.body.iter().any(|s| matches!(s, decy_parser::Statement::Return(Some(_))));
assert!(has_return, "Should have a return with expression");
}
#[test]
fn test_cov_cast_expression_in_assignment() {
let ast = parse_c_code("void f() { int x = (int)3.14; }");
let func = &ast.functions()[0];
if let decy_parser::Statement::VariableDeclaration { initializer: Some(expr), .. } =
&func.body[0]
{
assert!(
matches!(expr, decy_parser::Expression::Cast { .. }),
"Should be a cast expression"
);
}
}
#[test]
fn test_cov_sizeof_in_function_body() {
let ast = parse_c_code("int f() { return sizeof(int); }");
let func = &ast.functions()[0];
if let decy_parser::Statement::Return(Some(expr)) = &func.body[0] {
assert!(
matches!(expr, decy_parser::Expression::Sizeof { .. }),
"Should be sizeof expression"
);
}
}
#[test]
fn test_cov_sizeof_struct() {
let ast =
parse_c_code("struct Data { int x; int y; }; int f() { return sizeof(struct Data); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_return = f.body.iter().any(|s| matches!(s, decy_parser::Statement::Return(Some(_))));
assert!(has_return, "Should have a return with sizeof");
}
#[test]
fn test_cov_address_of_expression() {
let ast = parse_c_code("void f() { int x = 0; int *p = &x; }");
let func = &ast.functions()[0];
let has_var_decl = func.body.len() >= 2;
assert!(has_var_decl, "Should have at least 2 variable declarations");
}
#[test]
fn test_cov_dereference_expression() {
let ast = parse_c_code("int f(int *p) { return *p; }");
let func = &ast.functions()[0];
if let decy_parser::Statement::Return(Some(expr)) = &func.body[0] {
assert!(matches!(expr, decy_parser::Expression::Dereference(_)), "Should be dereference");
}
}
#[test]
fn test_cov_logical_not_expression() {
let ast = parse_c_code("int f(int x) { return !x; }");
let func = &ast.functions()[0];
if let decy_parser::Statement::Return(Some(expr)) = &func.body[0] {
assert!(matches!(expr, decy_parser::Expression::UnaryOp { .. }), "Should be unary op");
}
}
#[test]
fn test_cov_negation_expression() {
let ast = parse_c_code("int f(int x) { return -x; }");
let func = &ast.functions()[0];
if let decy_parser::Statement::Return(Some(expr)) = &func.body[0] {
assert!(matches!(expr, decy_parser::Expression::UnaryOp { .. }), "Should be unary op");
}
}
#[test]
fn test_cov_bitwise_not_expression() {
let ast = parse_c_code("int f(int x) { return ~x; }");
let func = &ast.functions()[0];
if let decy_parser::Statement::Return(Some(expr)) = &func.body[0] {
assert!(matches!(expr, decy_parser::Expression::UnaryOp { .. }), "Should be unary op");
}
}
#[test]
fn test_cov_pre_increment_expression() {
let ast = parse_c_code("int f(int x) { return ++x; }");
let func = &ast.functions()[0];
if let decy_parser::Statement::Return(Some(expr)) = &func.body[0] {
assert!(
matches!(expr, decy_parser::Expression::PreIncrement { .. }),
"Should be pre-increment expression"
);
}
}
#[test]
fn test_cov_post_increment_expression() {
let ast = parse_c_code("int f(int x) { return x++; }");
let func = &ast.functions()[0];
if let decy_parser::Statement::Return(Some(expr)) = &func.body[0] {
assert!(
matches!(expr, decy_parser::Expression::PostIncrement { .. }),
"Should be post-increment expression"
);
}
}
#[test]
fn test_cov_pre_decrement_expression() {
let ast = parse_c_code("int f(int x) { return --x; }");
let func = &ast.functions()[0];
if let decy_parser::Statement::Return(Some(expr)) = &func.body[0] {
assert!(
matches!(expr, decy_parser::Expression::PreDecrement { .. }),
"Should be pre-decrement expression"
);
}
}
#[test]
fn test_cov_post_decrement_expression() {
let ast = parse_c_code("int f(int x) { return x--; }");
let func = &ast.functions()[0];
if let decy_parser::Statement::Return(Some(expr)) = &func.body[0] {
assert!(
matches!(expr, decy_parser::Expression::PostDecrement { .. }),
"Should be post-decrement expression"
);
}
}
#[test]
fn test_cov_function_call_statement() {
let ast = parse_c_code("void print(int x); void f() { print(42); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_call = f.body.iter().any(|s| matches!(s, decy_parser::Statement::FunctionCall { .. }));
assert!(has_call, "Should have a function call statement");
}
#[test]
fn test_cov_function_call_with_string_arg() {
let ast = parse_c_code("void print(const char *s); void f() { print(\"hello\"); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_call = f.body.iter().any(|s| matches!(s, decy_parser::Statement::FunctionCall { .. }));
assert!(has_call, "Should have a function call statement with string arg");
}
#[test]
fn test_cov_function_call_with_multiple_args() {
let ast = parse_c_code("void add(int a, int b); void f() { add(1, 2); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
if let decy_parser::Statement::FunctionCall { arguments, .. } = &f.body[0] {
assert_eq!(arguments.len(), 2, "Should have 2 arguments");
}
}
#[test]
fn test_cov_function_call_with_expression_arg() {
let ast = parse_c_code("void g(int x); void f(int a, int b) { g(a + b); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
if let decy_parser::Statement::FunctionCall { arguments, .. } = &f.body[0] {
assert!(
matches!(&arguments[0], decy_parser::Expression::BinaryOp { .. }),
"Arg should be binary op"
);
}
}
#[test]
fn test_cov_nested_function_call() {
let ast = parse_c_code("int add(int a, int b); void f() { int x = add(add(1, 2), 3); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_var =
f.body.iter().any(|s| matches!(s, decy_parser::Statement::VariableDeclaration { .. }));
assert!(has_var, "Should have variable declaration with nested function call");
}
#[test]
fn test_cov_function_call_with_sizeof_arg() {
let ast = parse_c_code("void *alloc(int size); void f() { alloc(sizeof(int)); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_call = f.body.iter().any(|s| matches!(s, decy_parser::Statement::FunctionCall { .. }));
assert!(has_call, "Should have function call with sizeof arg");
}
#[test]
fn test_cov_function_call_with_ternary_arg() {
let ast = parse_c_code("void g(int x); void f(int a, int b) { g(a > b ? a : b); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_call = f.body.iter().any(|s| matches!(s, decy_parser::Statement::FunctionCall { .. }));
assert!(has_call, "Should have function call with ternary arg");
}
#[test]
fn test_cov_function_call_with_cast_arg() {
let ast = parse_c_code("void g(int x); void f(float y) { g((int)y); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_call = f.body.iter().any(|s| matches!(s, decy_parser::Statement::FunctionCall { .. }));
assert!(has_call, "Should have function call with cast arg");
}
#[test]
fn test_cov_function_call_with_char_arg() {
let ast = parse_c_code("void g(char c); void f() { g('A'); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
if let decy_parser::Statement::FunctionCall { arguments, .. } = &f.body[0] {
assert!(
matches!(&arguments[0], decy_parser::Expression::CharLiteral(_)),
"Arg should be char literal"
);
}
}
#[test]
fn test_cov_function_call_with_float_arg() {
let ast = parse_c_code("void g(float x); void f() { g(3.14f); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_call = f.body.iter().any(|s| matches!(s, decy_parser::Statement::FunctionCall { .. }));
assert!(has_call, "Should have function call with float arg");
}
#[test]
fn test_cov_function_call_with_unary_arg() {
let ast = parse_c_code("void g(int x); void f(int y) { g(-y); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
if let decy_parser::Statement::FunctionCall { arguments, .. } = &f.body[0] {
assert!(
matches!(&arguments[0], decy_parser::Expression::UnaryOp { .. }),
"Arg should be unary op"
);
}
}
#[test]
fn test_cov_function_call_with_array_index_arg() {
let ast = parse_c_code("void g(int x); void f(int arr[], int i) { g(arr[i]); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
if let decy_parser::Statement::FunctionCall { arguments, .. } = &f.body[0] {
assert!(
matches!(&arguments[0], decy_parser::Expression::ArrayIndex { .. }),
"Arg should be array index"
);
}
}
#[test]
fn test_cov_function_call_with_field_access_arg() {
let ast = parse_c_code("struct S { int x; }; void g(int x); void f(struct S *s) { g(s->x); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
if let decy_parser::Statement::FunctionCall { arguments, .. } = &f.body[0] {
assert!(
matches!(&arguments[0], decy_parser::Expression::PointerFieldAccess { .. }),
"Arg should be pointer field access"
);
}
}
#[test]
fn test_cov_type_function_pointer_variable() {
let ast = parse_c_code("int (*fp)(int, int);");
assert!(!ast.variables().is_empty());
let var = &ast.variables()[0];
assert!(var.is_function_pointer(), "Should be a function pointer");
assert_eq!(var.function_pointer_param_count(), 2);
}
#[test]
fn test_cov_type_function_pointer_void_return() {
let ast = parse_c_code("void (*handler)(int);");
assert!(!ast.variables().is_empty());
let var = &ast.variables()[0];
assert!(var.is_function_pointer(), "Should be a function pointer");
assert!(var.function_pointer_has_void_return(), "Should have void return");
}
#[test]
fn test_cov_type_elaborated_struct() {
let ast = parse_c_code("struct Point { int x; int y; }; struct Point p;");
let vars = ast.variables();
assert!(!vars.is_empty(), "Should have variable");
}
#[test]
fn test_cov_type_typedef_anonymous_struct() {
let ast = parse_c_code("typedef struct { int x; int y; } Point;");
assert!(!ast.structs().is_empty(), "Should have a struct named Point");
assert_eq!(ast.structs()[0].name(), "Point");
}
#[test]
fn test_cov_type_incomplete_array() {
let ast = parse_c_code("struct Buffer { int len; char data[]; };");
assert!(!ast.structs().is_empty());
let s = &ast.structs()[0];
assert!(s.fields().len() >= 2, "Should have len and data fields");
}
#[test]
fn test_cov_type_enum_variable() {
let ast = parse_c_code("enum Color { RED, GREEN, BLUE }; enum Color c = RED;");
assert!(!ast.enums().is_empty(), "Should have enum");
assert!(!ast.variables().is_empty(), "Should have variable of enum type");
}
#[test]
fn test_cov_typedef_size_t() {
let ast = parse_c_code("typedef unsigned long size_t; size_t len;");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_cov_type_const_char_pointer_param() {
let ast = parse_c_code("void f(const char *s) {}");
let func = &ast.functions()[0];
assert!(!func.parameters.is_empty());
let param = &func.parameters[0];
assert!(param.is_const_char_pointer(), "Should be const char pointer");
assert!(param.is_char_pointer(), "Should also be char pointer");
}
#[test]
fn test_cov_type_mutable_char_pointer_param() {
let ast = parse_c_code("void f(char *s) {}");
let func = &ast.functions()[0];
assert!(!func.parameters.is_empty());
let param = &func.parameters[0];
assert!(!param.is_const_char_pointer(), "Should NOT be const char pointer");
assert!(param.is_char_pointer(), "Should be char pointer");
}
#[test]
fn test_cov_variable_is_string_literal() {
let ast = parse_c_code("char *msg = \"hello\";");
assert!(!ast.variables().is_empty());
let var = &ast.variables()[0];
assert!(var.is_string_literal(), "Should be a string literal");
}
#[test]
fn test_cov_variable_is_not_string_literal_no_init() {
let ast = parse_c_code("char *msg;");
assert!(!ast.variables().is_empty());
let var = &ast.variables()[0];
assert!(!var.is_string_literal(), "Should NOT be a string literal without initializer");
}
#[test]
fn test_cov_variable_storage_class_static() {
let ast = parse_c_code("static int counter = 0;");
assert!(!ast.variables().is_empty());
let var = &ast.variables()[0];
assert!(var.is_static(), "Should be static");
assert!(!var.is_extern(), "Should not be extern");
}
#[test]
fn test_cov_variable_storage_class_extern() {
let ast = parse_c_code("extern int global;");
assert!(!ast.variables().is_empty());
let var = &ast.variables()[0];
assert!(var.is_extern(), "Should be extern");
assert!(!var.is_static(), "Should not be static");
}
#[test]
fn test_cov_variable_is_const() {
let ast = parse_c_code("const int MAX = 100;");
assert!(!ast.variables().is_empty());
let var = &ast.variables()[0];
assert!(var.is_const(), "Should be const");
}
#[test]
fn test_cov_variable_initializer() {
let ast = parse_c_code("int x = 42;");
assert!(!ast.variables().is_empty());
let var = &ast.variables()[0];
assert!(var.initializer().is_some(), "Should have initializer");
}
#[test]
fn test_cov_typedef_is_pointer() {
let ast = parse_c_code("typedef int *intptr;");
assert!(!ast.typedefs().is_empty());
let td = &ast.typedefs()[0];
assert!(td.is_pointer(), "Should be a pointer typedef");
assert!(!td.is_struct(), "Should not be struct typedef");
assert!(!td.is_function_pointer(), "Should not be function pointer typedef");
assert!(!td.is_array(), "Should not be array typedef");
assert_eq!(td.underlying_type(), "int*");
}
#[test]
fn test_cov_typedef_is_struct() {
let ast = parse_c_code("struct Point { int x; }; typedef struct Point Point_t;");
assert!(!ast.typedefs().is_empty());
let td = &ast.typedefs()[0];
assert!(td.is_struct(), "Should be a struct typedef");
}
#[test]
fn test_cov_typedef_is_function_pointer() {
let ast = parse_c_code("typedef void (*handler_t)(int);");
assert!(!ast.typedefs().is_empty());
let td = &ast.typedefs()[0];
assert!(td.is_function_pointer(), "Should be a function pointer typedef");
assert_eq!(td.underlying_type(), "function pointer");
}
#[test]
fn test_cov_typedef_underlying_type_strings() {
let ast = parse_c_code("typedef void void_t;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "void");
let ast = parse_c_code("typedef int int_t;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "int");
let ast = parse_c_code("typedef float float_t;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "float");
let ast = parse_c_code("typedef double double_t;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "double");
let ast = parse_c_code("typedef char char_t;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "char");
let ast = parse_c_code("typedef unsigned int uint_t;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "unsigned int");
}
#[test]
fn test_cov_typedef_underlying_pointer_types() {
let ast = parse_c_code("typedef char *str_t;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "char*");
let ast = parse_c_code("typedef float *fptr_t;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "float*");
let ast = parse_c_code("typedef double *dptr_t;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "double*");
let ast = parse_c_code("typedef void *vptr_t;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "void*");
let ast = parse_c_code("typedef unsigned int *uiptr_t;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "unsigned int*");
}
#[test]
fn test_cov_expression_is_string_function_call() {
let ast = parse_c_code("int f(const char *s) { return strlen(s); }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_cov_statement_is_function_call() {
let ast = parse_c_code("void f(const char *dst, const char *src) { strcpy(dst, src); }");
let func = &ast.functions()[0];
let has_call = func.body.iter().any(|s| s.is_function_call());
assert!(has_call, "Should have function call");
}
#[test]
fn test_cov_statement_is_string_function_call() {
let ast = parse_c_code("void f(const char *dst, const char *src) { strcpy(dst, src); }");
let func = &ast.functions()[0];
let has_str_call = func.body.iter().any(|s| s.is_string_function_call());
assert!(has_str_call, "Should detect string function call");
}
#[test]
fn test_cov_statement_as_function_call() {
let ast = parse_c_code("void print(int x); void f() { print(1); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
assert!(f.body[0].as_function_call().is_none());
}
#[test]
fn test_cov_parse_file() {
use std::io::Write;
let dir = std::env::temp_dir();
let path = dir.join("decy_test_parse_file.c");
{
let mut file = std::fs::File::create(&path).expect("Failed to create temp file");
writeln!(file, "int main() {{ return 0; }}").expect("Failed to write");
}
let parser = CParser::new().expect("Parser creation failed");
let ast = parser.parse_file(&path).expect("parse_file failed");
assert!(!ast.functions().is_empty(), "Should have parsed a function from file");
let _ = std::fs::remove_file(&path);
}
#[test]
fn test_cov_parse_file_empty() {
use std::io::Write;
let dir = std::env::temp_dir();
let path = dir.join("decy_test_parse_file_empty.c");
{
let mut file = std::fs::File::create(&path).expect("Failed to create temp file");
writeln!(file, " ").expect("Failed to write");
}
let parser = CParser::new().expect("Parser creation failed");
let ast = parser.parse_file(&path).expect("parse_file empty should succeed");
assert!(ast.functions().is_empty(), "Empty file should have no functions");
let _ = std::fs::remove_file(&path);
}
#[test]
fn test_cov_parse_file_not_found() {
let parser = CParser::new().expect("Parser creation failed");
let result = parser.parse_file(std::path::Path::new("/nonexistent/file.c"));
assert!(result.is_err(), "Should fail for nonexistent file");
}
#[test]
fn test_cov_parse_file_syntax_error() {
use std::io::Write;
let dir = std::env::temp_dir();
let path = dir.join("decy_test_parse_file_err.c");
{
let mut file = std::fs::File::create(&path).expect("Failed to create temp file");
writeln!(file, "int broken(").expect("Failed to write");
}
let parser = CParser::new().expect("Parser creation failed");
let result = parser.parse_file(&path);
assert!(result.is_err(), "Should fail for syntax error");
let _ = std::fs::remove_file(&path);
}
#[test]
fn test_cov_parse_syntax_error_diagnostic() {
let parser = CParser::new().expect("Parser creation failed");
let result = parser.parse("int broken(");
assert!(result.is_err(), "Should fail for syntax error");
let err_msg = result.unwrap_err().to_string();
assert!(
err_msg.contains("syntax error") || err_msg.contains("error"),
"Error should mention syntax: {}",
err_msg
);
}
#[test]
fn test_cov_parse_whitespace_only() {
let parser = CParser::new().expect("Parser creation failed");
let ast = parser.parse(" \n\t \n ").expect("Whitespace-only should succeed");
assert!(ast.functions().is_empty(), "Whitespace-only should have no functions");
}
#[test]
fn test_cov_macro_object_like_extraction() {
let ast = parse_c_code("#define PI 3\nint x = PI;");
if !ast.macros().is_empty() {
let m = &ast.macros()[0];
assert_eq!(m.name(), "PI");
assert!(m.is_object_like());
assert!(!m.is_function_like());
}
}
#[test]
fn test_cov_macro_function_like_extraction() {
let ast = parse_c_code("#define MAX(a,b) ((a)>(b)?(a):(b))\nint x = MAX(1,2);");
if !ast.macros().is_empty() {
let m = &ast.macros()[0];
assert_eq!(m.name(), "MAX");
assert!(m.is_function_like());
assert!(!m.is_object_like());
assert_eq!(m.parameters().len(), 2);
assert!(!m.body().is_empty());
}
}
#[test]
fn test_cov_extern_c_linkage() {
let code = r#"
#ifdef __cplusplus
extern "C" {
#endif
int exported_func(int x);
#ifdef __cplusplus
}
#endif
"#;
let ast = parse_c_code(code);
assert!(!ast.functions().is_empty(), "Should parse function through extern C guard");
}
#[test]
fn test_cov_extern_c_bare() {
let code = r#"extern "C" int exported_func(int x);"#;
let result = parse_succeeds(code);
let _ = result;
}
#[test]
fn test_cov_binary_op_logical_and_in_if() {
let ast = parse_c_code("int f(int a, int b) { if (a > 0 && b > 0) return 1; return 0; }");
let func = &ast.functions()[0];
let has_if = func.body.iter().any(|s| matches!(s, decy_parser::Statement::If { .. }));
assert!(has_if, "Should have if with && condition");
}
#[test]
fn test_cov_binary_op_logical_or_in_if() {
let ast = parse_c_code("int f(int a, int b) { if (a > 0 || b > 0) return 1; return 0; }");
let func = &ast.functions()[0];
let has_if = func.body.iter().any(|s| matches!(s, decy_parser::Statement::If { .. }));
assert!(has_if, "Should have if with || condition");
}
#[test]
fn test_cov_binary_op_equal_in_if() {
let ast = parse_c_code("int f(int x) { if (x == 0) return 1; return 0; }");
let func = &ast.functions()[0];
let has_if = func.body.iter().any(|s| matches!(s, decy_parser::Statement::If { .. }));
assert!(has_if, "Should have if with == condition");
}
#[test]
fn test_cov_binary_op_not_equal_in_if() {
let ast = parse_c_code("int f(int x) { if (x != 0) return 1; return 0; }");
let func = &ast.functions()[0];
let has_if = func.body.iter().any(|s| matches!(s, decy_parser::Statement::If { .. }));
assert!(has_if, "Should have if with != condition");
}
#[test]
fn test_cov_binary_op_less_equal() {
let ast = parse_c_code("int f(int x) { if (x <= 10) return 1; return 0; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_cov_binary_op_greater_equal() {
let ast = parse_c_code("int f(int x) { if (x >= 10) return 1; return 0; }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_cov_struct_field_function_pointer() {
let ast = parse_c_code("struct Vtable { void (*init)(int); int (*get)(void); };");
assert!(!ast.structs().is_empty());
let s = &ast.structs()[0];
assert!(s.fields().len() >= 2, "Should have 2 function pointer fields");
assert!(s.fields()[0].is_function_pointer(), "First field should be function pointer");
}
#[test]
fn test_cov_param_function_pointer() {
let ast = parse_c_code("void sort(int *arr, int n, int (*cmp)(int, int)) {}");
let func = &ast.functions()[0];
assert_eq!(func.parameters.len(), 3);
assert!(func.parameters[2].is_function_pointer(), "Third param should be function pointer");
}
#[test]
fn test_cov_for_loop_comma_increment() {
let ast = parse_c_code("void f() { for (int i = 0; i < 10; i++) { } }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_cov_for_loop_two_parts_cond_and_inc() {
let ast = parse_c_code("void f(int n) { int i = 0; for (; i < n; i++) { } }");
let funcs = ast.functions();
let f = &funcs[0];
let has_for = f.body.iter().any(|s| matches!(s, decy_parser::Statement::For { .. }));
assert!(has_for, "Should have for loop");
}
#[test]
fn test_cov_for_loop_two_parts_init_and_cond() {
let ast = parse_c_code("void f() { for (int i = 0; i < 10;) { i = i + 1; } }");
assert!(!ast.functions().is_empty());
}
#[test]
fn test_cov_ast_default() {
let ast = decy_parser::Ast::default();
assert!(ast.functions().is_empty());
assert!(ast.variables().is_empty());
assert!(ast.structs().is_empty());
assert!(ast.typedefs().is_empty());
assert!(ast.enums().is_empty());
assert!(ast.macros().is_empty());
}
#[test]
fn test_cov_comparison_operators_in_assignment_check() {
let ast = parse_c_code("int f(int a, int b) { int x = a <= b; return x; }");
let func = &ast.functions()[0];
let has_var =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::VariableDeclaration { .. }));
assert!(has_var, "Should have variable declaration, not assignment");
}
#[test]
fn test_cov_variable_is_string_buffer_malloc() {
let ast = parse_c_code("void *malloc(int size); void f() { char *buf = (char *)malloc(100); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_var =
f.body.iter().any(|s| matches!(s, decy_parser::Statement::VariableDeclaration { .. }));
assert!(has_var, "Should have variable declaration with malloc");
}
#[test]
fn test_cov_variable_not_string_buffer_int() {
let ast = parse_c_code("int x = 42;");
assert!(!ast.variables().is_empty());
let var = &ast.variables()[0];
assert!(!var.is_string_buffer(), "int should not be string buffer");
assert!(!var.is_string_literal(), "int should not be string literal");
}
#[test]
fn test_cov_single_statement_in_case_body() {
let ast = parse_c_code(
"void print(int x); void f(int x) { switch(x) { case 1: print(x); break; default: break; } }"
);
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_switch = f.body.iter().any(|s| matches!(s, decy_parser::Statement::Switch { .. }));
assert!(has_switch);
}
#[test]
fn test_cov_init_list_with_designated_and_gaps() {
let ast = parse_c_code("struct P { int x; int y; int z; }; struct P p = { .z = 3 };");
assert!(!ast.variables().is_empty() || !ast.structs().is_empty());
}
#[test]
fn test_cov_init_list_array_designated() {
let ast = parse_c_code("int arr[10] = { [0] = 1, [5] = 5 };");
assert!(!ast.variables().is_empty());
}
#[test]
fn test_cov_function_return_void() {
let ast = parse_c_code("void f() {}");
let func = &ast.functions()[0];
assert_eq!(func.return_type, decy_parser::Type::Void);
}
#[test]
fn test_cov_function_return_float() {
let ast = parse_c_code("float f() { return 3.14f; }");
let func = &ast.functions()[0];
assert_eq!(func.return_type, decy_parser::Type::Float);
}
#[test]
fn test_cov_function_return_double() {
let ast = parse_c_code("double f() { return 3.14; }");
let func = &ast.functions()[0];
assert_eq!(func.return_type, decy_parser::Type::Double);
}
#[test]
fn test_cov_function_return_char() {
let ast = parse_c_code("char f() { return 'a'; }");
let func = &ast.functions()[0];
assert_eq!(func.return_type, decy_parser::Type::Char);
}
#[test]
fn test_cov_function_return_pointer() {
let ast = parse_c_code("int *f() { return 0; }");
let func = &ast.functions()[0];
assert!(matches!(func.return_type, decy_parser::Type::Pointer(_)));
}
#[test]
fn test_cov_nested_binary_ops() {
let ast = parse_c_code("int f(int a, int b, int c) { return a + b * c; }");
let func = &ast.functions()[0];
let has_return = func.body.iter().any(|s| matches!(s, decy_parser::Statement::Return(Some(_))));
assert!(has_return);
}
#[test]
fn test_cov_function_call_in_binary_op() {
let ast = parse_c_code("int g(int x); int f(int a) { return g(a) + 1; }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
let has_return = f.body.iter().any(|s| matches!(s, decy_parser::Statement::Return(Some(_))));
assert!(has_return);
}
#[test]
fn test_cov_ternary_in_binary_op() {
let ast = parse_c_code("int f(int a, int b) { return (a > b ? a : b) + 1; }");
let func = &ast.functions()[0];
let has_return = func.body.iter().any(|s| matches!(s, decy_parser::Statement::Return(Some(_))));
assert!(has_return);
}
#[test]
fn test_cov_sizeof_in_binary_op() {
let ast = parse_c_code("int f() { return sizeof(int) + sizeof(char); }");
let func = &ast.functions()[0];
let has_return = func.body.iter().any(|s| matches!(s, decy_parser::Statement::Return(Some(_))));
assert!(has_return);
}
#[test]
fn test_cov_signed_char_type() {
let ast = parse_c_code("signed char sc = -1;");
assert!(!ast.variables().is_empty());
let var = &ast.variables()[0];
assert_eq!(*var.var_type(), decy_parser::Type::SignedChar);
}
#[test]
fn test_cov_typedef_underlying_signed_char() {
let ast = parse_c_code("typedef signed char sbyte;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "signed char");
}
#[test]
fn test_cov_typedef_underlying_signed_char_pointer() {
let ast = parse_c_code("typedef signed char *scharptr;");
assert!(!ast.typedefs().is_empty());
assert_eq!(ast.typedefs()[0].underlying_type(), "signed char*");
}
#[test]
fn test_cov_expression_has_string_literal_argument() {
let ast = parse_c_code("void print(const char *s); void f() { print(\"hello\"); }");
let funcs = ast.functions();
let f = funcs.iter().find(|f| f.name == "f").expect("Should find f");
if let decy_parser::Statement::FunctionCall { function, arguments } = &f.body[0] {
let expr = decy_parser::Expression::FunctionCall {
function: function.clone(),
arguments: arguments.clone(),
};
assert!(expr.has_string_literal_argument(), "Should have string literal argument");
}
}
#[test]
fn test_cov_expression_string_function_name() {
let expr = decy_parser::Expression::FunctionCall {
function: "strlen".to_string(),
arguments: vec![decy_parser::Expression::Variable("s".to_string())],
};
assert!(expr.is_string_function_call());
assert_eq!(expr.string_function_name(), Some("strlen"));
let non_str =
decy_parser::Expression::FunctionCall { function: "printf".to_string(), arguments: vec![] };
assert!(!non_str.is_string_function_call());
assert_eq!(non_str.string_function_name(), None);
let not_call = decy_parser::Expression::IntLiteral(42);
assert!(!not_call.is_string_function_call());
assert_eq!(not_call.string_function_name(), None);
assert!(!not_call.has_string_literal_argument());
}
#[test]
fn test_cov_multiple_functions() {
let ast = parse_c_code(
"int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; }",
);
assert_eq!(ast.functions().len(), 2, "Should have 2 functions");
assert_eq!(ast.functions()[0].name, "add");
assert_eq!(ast.functions()[1].name, "sub");
}
#[test]
fn test_cov_extern_var_in_function_skipped() {
let ast = parse_c_code("void f() { extern int max; }");
let func = &ast.functions()[0];
let has_var_decl =
func.body.iter().any(|s| matches!(s, decy_parser::Statement::VariableDeclaration { .. }));
assert!(!has_var_decl, "extern without init should be skipped in function body");
}
#[test]
fn test_cov_array_decl_no_false_initializer() {
let ast = parse_c_code("void f() { int nums[5]; }");
let func = &ast.functions()[0];
if let decy_parser::Statement::VariableDeclaration { initializer, .. } = &func.body[0] {
assert!(initializer.is_none(), "Array size should not become an initializer");
}
}
#[test]
fn test_cov_struct_deduplication() {
let ast = parse_c_code("struct S { int x; };");
assert!(!ast.structs().is_empty());
assert_eq!(ast.structs().len(), 1, "Should have exactly one struct");
}