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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//! Integration tests for array parameter → slice signature transformation (DECY-072).
//!
//! Tests the complete pipeline: C parsing → ownership inference → signature transformation → codegen.
//! Verifies that C array parameters are transformed to safe Rust slice parameters.
use decy_core::transpile;
/// Test basic array parameter transformation: (int* arr, int len) → &[i32]
///
/// C: void process(int* arr, int len) { }
/// Rust: fn process(arr: &[i32]) { }
#[test]
// DECY-072 GREEN: Test now active
fn test_transform_array_parameter_to_slice() {
let c_code = r#"
void process(int* arr, int len) {
// Empty function for signature testing
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Should transform to slice parameter
assert!(
result.contains("arr: &[i32]"),
"Should transform (int* arr, int len) to arr: &[i32]\nGenerated:\n{}",
result
);
// Length parameter should be removed (slice includes length)
assert!(
!result.contains("len: i32") && !result.contains("len: usize"),
"Should remove redundant length parameter\nGenerated:\n{}",
result
);
// Should NOT use unsafe
assert!(
!result.contains("unsafe"),
"Should generate safe code\nGenerated:\n{}",
result
);
}
/// Test mutable array parameter: (int* arr, int len) with mutations → &mut [i32]
///
/// C: void fill(int* arr, int len) { arr[0] = 1; }
/// Rust: fn fill(arr: &mut [i32]) { arr[0] = 1; }
#[test]
// DECY-072 GREEN: Test now active
fn test_transform_mutable_array_parameter() {
let c_code = r#"
void fill(int* arr, int len) {
arr[0] = 1;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Should transform to mutable slice parameter
assert!(
result.contains("arr: &mut [i32]"),
"Should transform to arr: &mut [i32] for mutable access\nGenerated:\n{}",
result
);
// Should NOT use unsafe
assert!(
!result.contains("unsafe"),
"Should generate safe code\nGenerated:\n{}",
result
);
}
/// Test array parameter with length usage in body: len → arr.len()
///
/// C: int sum(int* arr, int len) { for(int i=0; i<len; i++) { } }
/// Rust: fn sum(arr: &[i32]) -> i32 { for i in 0..arr.len() { } }
#[test]
// DECY-072 GREEN: Test now active
fn test_transform_length_usage_in_body() {
let c_code = r#"
int sum(int* arr, int len) {
int total = 0;
for (int i = 0; i < len; i++) {
total += arr[i];
}
return total;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Should transform to slice parameter
assert!(
result.contains("arr: &[i32]"),
"Should use slice parameter\nGenerated:\n{}",
result
);
// Length usage should be transformed to arr.len()
assert!(
result.contains("arr.len()"),
"Should use arr.len() instead of len parameter\nGenerated:\n{}",
result
);
// Should NOT use unsafe
assert!(
!result.contains("unsafe"),
"Should generate safe code\nGenerated:\n{}",
result
);
}
/// Test char array parameter: (char* buf, int size) → &[u8]
///
/// C: void process_buffer(char* buf, int size) { }
/// Rust: fn process_buffer(buf: &[u8]) { }
#[test]
// DECY-072 GREEN: Test now active
fn test_transform_char_array_parameter() {
let c_code = r#"
void process_buffer(char* buf, int size) {
buf[0] = 65;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Should transform to mutable u8 slice
assert!(
result.contains("buf: &mut [u8]"),
"Should transform char* to &mut [u8]\nGenerated:\n{}",
result
);
// Should NOT use unsafe
assert!(
!result.contains("unsafe"),
"Should generate safe code\nGenerated:\n{}",
result
);
}
/// Test multiple array parameters
///
/// C: void merge(int* arr1, int len1, int* arr2, int len2) { }
/// Rust: fn merge(arr1: &[i32], arr2: &[i32]) { }
#[test]
// DECY-072 GREEN: Test now active
fn test_transform_multiple_array_parameters() {
let c_code = r#"
void merge(int* arr1, int len1, int* arr2, int len2) {
// Empty for signature testing
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Should transform both array parameters
assert!(
result.contains("arr1: &[i32]"),
"Should transform arr1 to slice\nGenerated:\n{}",
result
);
assert!(
result.contains("arr2: &[i32]"),
"Should transform arr2 to slice\nGenerated:\n{}",
result
);
// Both length parameters should be removed
assert!(
!result.contains("len1") && !result.contains("len2"),
"Should remove both length parameters\nGenerated:\n{}",
result
);
// Should NOT use unsafe
assert!(
!result.contains("unsafe"),
"Should generate safe code\nGenerated:\n{}",
result
);
}
/// Test array parameter with return value
///
/// C: int first_element(int* arr, int len) { return arr[0]; }
/// Rust: fn first_element(arr: &[i32]) -> i32 { arr[0] }
#[test]
// DECY-072 GREEN: Test now active
fn test_transform_array_parameter_with_return() {
let c_code = r#"
int first_element(int* arr, int len) {
return arr[0];
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Should transform to slice parameter
assert!(
result.contains("arr: &[i32]"),
"Should use slice parameter\nGenerated:\n{}",
result
);
// Should use safe array indexing (with or without usize cast)
// DECY-150: Accept parenthesized indexing pattern arr[(0) as usize]
assert!(
result.contains("arr[0]")
|| result.contains("arr[0 as usize]")
|| result.contains("arr[(0)"),
"Should use safe indexing\nGenerated:\n{}",
result
);
// Should NOT use unsafe
assert!(
!result.contains("unsafe"),
"Should generate safe code\nGenerated:\n{}",
result
);
}
/// Test that non-array pointers are transformed to references (DECY-111)
///
/// C: void process(int* ptr) { }
/// Rust: fn process(ptr: &mut i32) { } (pointer becomes reference)
///
/// DECY-111: Pointer params are now transformed to references for safe Rust code.
/// Non-array pointers become &T or &mut T based on modification analysis.
#[test]
fn test_non_array_pointer_becomes_reference() {
let c_code = r#"
void process(int* ptr) {
// Single pointer without length - not an array
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Should NOT transform to slice (no length parameter)
assert!(
!result.contains(": &[i32]"),
"Should NOT transform single pointer to slice\nGenerated:\n{}",
result
);
// DECY-111/DECY-180: Should transform pointer to reference (not raw pointer)
// Generated code may include lifetime annotations: &'a i32 or &'a mut i32
let has_reference = result.contains("&i32")
|| result.contains("&mut i32")
|| result.contains("&'a i32")
|| result.contains("&'a mut i32");
assert!(
has_reference,
"Should transform to reference\nGenerated:\n{}",
result
);
}
/// Test unsafe block count metric
#[test]
// DECY-072 GREEN: Test now active
fn test_array_parameter_transformation_unsafe_count() {
let c_code = r#"
void process1(int* arr, int len) { arr[0] = 1; }
void process2(char* buf, int size) { buf[0] = 'A'; }
int sum(int* arr, int len) {
int total = 0;
for (int i = 0; i < len; i++) {
total += arr[i];
}
return total;
}
"#;
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 array params)
assert!(
unsafe_per_1000 < 5.0,
"Must achieve <5 unsafe blocks per 1000 LOC. Got {:.2}",
unsafe_per_1000
);
// STRETCH GOAL: 0 unsafe for array parameters
assert_eq!(
unsafe_count, 0,
"Array parameter transformation should achieve 0 unsafe blocks. Found {}",
unsafe_count
);
}