1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! DECY-119: Struct pointer field assignment tests.
//!
//! When assigning &x to a struct field of type *mut T, cast properly.
//!
//! C: node.next = &other;
//! Expected Rust: node.next = &mut other as *mut T;
use decy_core::transpile;
/// Test that struct field assignment with address-of casts to raw pointer.
///
/// C: n2.next = &n3; where next is *mut Node
/// Expected: n2.next = &mut n3 as *mut Node;
#[test]
fn test_struct_field_address_of_casts_to_ptr() {
let c_code = r#"
struct Node {
int value;
struct Node *next;
};
int main() {
struct Node n1;
struct Node n2;
n1.value = 10;
n2.value = 20;
n1.next = &n2;
return n1.value;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Should cast &n2 to raw pointer
assert!(
result.contains("as *mut Node") || result.contains("as *mut"),
"Should cast &x to *mut T\nGenerated:\n{}",
result
);
// Should NOT have bare &n2 assigned to pointer field
assert!(
!result.contains(".next = &n2;"),
"Should NOT assign bare &x to pointer field\nGenerated:\n{}",
result
);
}
/// Test that null pointer assignment still works.
#[test]
fn test_struct_field_null_still_works() {
let c_code = r#"
struct Node {
int value;
struct Node *next;
};
int main() {
struct Node n;
n.value = 10;
n.next = 0;
return n.value;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Should use null_mut() for 0 assignment to pointer
assert!(
result.contains("std::ptr::null_mut()"),
"Should use null_mut() for 0 assignment\nGenerated:\n{}",
result
);
}