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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! Integration test for pointer arithmetic → slice indexing transformation (DECY-070).
//!
//! Tests the complete pipeline: C parsing → HIR → ownership inference → transformation → codegen.
//! Verifies that pointer arithmetic on array-derived pointers generates safe slice indexing
//! with 0 unsafe blocks.
use decy_core::transpile;
/// Test stack array with pointer arithmetic.
///
/// C code with stack array and pointer arithmetic should generate
/// safe Rust code with slice indexing, not unsafe pointer arithmetic.
///
/// C: int arr[10];
/// int* p = arr;
/// return *(p + 1);
///
/// Expected Rust: let arr = [0; 10];
/// let p = &arr[..];
/// return p[1]; // OR arr[1]
#[test]
fn test_stack_array_pointer_arithmetic_to_slice_index() {
let c_code = r#"
int get_next() {
int arr[10];
int* p = arr;
return *(p + 1);
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Verify NO unsafe blocks
assert!(
!result.contains("unsafe"),
"Pointer arithmetic should NOT generate unsafe blocks.\nGenerated code:\n{}",
result
);
// Verify no unsafe pointer methods
assert!(
!result.contains("wrapping_add"),
"Should not use wrapping_add (unsafe pointer arithmetic).\nGenerated code:\n{}",
result
);
assert!(
!result.contains("offset"),
"Should not use offset/offset_from (unsafe pointer arithmetic).\nGenerated code:\n{}",
result
);
// Verify slice indexing pattern (either arr[1] or p[1])
// The transformation should convert *(p + 1) to safe indexing
assert!(
result.contains("[") && result.contains("]"),
"Should use safe slice indexing syntax.\nGenerated code:\n{}",
result
);
}
/// Test pointer from array initialization with pointer arithmetic.
///
/// C: int arr[10];
/// int* ptr = arr;
/// return *(ptr + 5);
///
/// Expected Rust: let arr = [0; 10];
/// let ptr = &arr[..];
/// return ptr[5]; // OR arr[5]
#[test]
fn test_array_init_pointer_arithmetic_to_slice_index() {
let c_code = r#"
int get_element() {
int arr[10];
int* ptr = arr;
return *(ptr + 5);
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Verify NO unsafe blocks
assert!(
!result.contains("unsafe"),
"Array pointer arithmetic should NOT generate unsafe blocks.\nGenerated code:\n{}",
result
);
// Verify no unsafe pointer methods
assert!(
!result.contains("wrapping_add"),
"Should not use wrapping_add.\nGenerated code:\n{}",
result
);
}
/// Test pointer arithmetic with subtraction.
///
/// C: int arr[10];
/// return *(arr - 2); // Note: simplified to avoid compound expression
///
/// Expected Rust: Safe slice indexing with bounds checking
#[test]
fn test_pointer_subtraction_to_slice_index() {
let c_code = r#"
int get_prev() {
int arr[10];
return *(arr - 2);
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Verify NO unsafe blocks
assert!(
!result.contains("unsafe"),
"Pointer subtraction should NOT generate unsafe blocks.\nGenerated code:\n{}",
result
);
assert!(
!result.contains("wrapping_sub"),
"Should not use wrapping_sub.\nGenerated code:\n{}",
result
);
}
/// Test multiple pointer arithmetic operations.
///
/// C: int arr[10];
/// int a = *(arr + 1);
/// int b = *(arr + 5);
/// return a + b;
///
/// Expected Rust: Safe slice indexing for both operations
#[test]
fn test_multiple_pointer_arithmetic_operations() {
let c_code = r#"
int sum_elements() {
int arr[10];
int a = *(arr + 1);
int b = *(arr + 5);
return a + b;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Verify NO unsafe blocks
assert!(
!result.contains("unsafe"),
"Multiple pointer arithmetic should NOT generate unsafe blocks.\nGenerated code:\n{}",
result
);
// Count unsafe operations (should be 0)
let unsafe_count = result.matches("unsafe").count();
assert_eq!(
unsafe_count, 0,
"Should have 0 unsafe blocks, found {}.\nGenerated code:\n{}",
unsafe_count, result
);
}
/// Test pointer arithmetic with variable offset.
///
/// C: int arr[10];
/// int offset = 3;
/// return *(arr + offset);
///
/// Expected Rust: return arr[offset as usize]; // Safe runtime bounds check
#[test]
fn test_pointer_arithmetic_with_variable_offset() {
let c_code = r#"
int get_at_offset() {
int arr[10];
int offset = 3;
return *(arr + offset);
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Verify NO unsafe blocks
assert!(
!result.contains("unsafe"),
"Variable offset pointer arithmetic should NOT generate unsafe blocks.\nGenerated code:\n{}",
result
);
}
/// Verify unsafe block count metric for DECY mission: <5 unsafe per 1000 LOC
#[test]
fn test_pointer_arithmetic_unsafe_count_metric() {
// Generate multiple functions with pointer arithmetic
let c_code = r#"
int test1() {
int arr[10];
return *(arr + 1);
}
int test2() {
int arr[20];
return *(arr + 5);
}
int test3() {
int arr[15];
int* p = arr;
return *(p + 3);
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Count unsafe blocks
let unsafe_count = result.matches("unsafe").count();
let loc = result.lines().count();
let unsafe_per_1000 = (unsafe_count as f64 / loc as f64) * 1000.0;
println!("Unsafe count: {}", unsafe_count);
println!("Lines of code: {}", loc);
println!("Unsafe per 1000 LOC: {:.2}", unsafe_per_1000);
// CRITICAL: Verify <5 unsafe per 1000 LOC (target: 0 for pointer arithmetic)
assert!(
unsafe_per_1000 < 5.0,
"Must achieve <5 unsafe blocks per 1000 LOC. Got {:.2} unsafe per 1000 LOC",
unsafe_per_1000
);
// STRETCH GOAL: 0 unsafe for pointer arithmetic
assert_eq!(
unsafe_count, 0,
"Pointer arithmetic transformation should achieve 0 unsafe blocks. Found {}",
unsafe_count
);
}