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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! String Safety Integration Test
//!
//! **RED PHASE**: Comprehensive test for C string operations → Safe Rust
//!
//! This test validates that unsafe C string operations (strcpy, strcat, strlen)
//! are transpiled to safe Rust alternatives (String, str methods).
//!
//! **Pattern**: EXTREME TDD - Test-First Development
//! **Reference**: ISO C99 §7.21 (String handling) + K&R Chapter 5
//!
//! **Safety Goal**: Zero unsafe blocks for string operations
//! **Validation**: Transpiled Rust code must compile and run safely
use decy_core::transpile;
// ============================================================================
// RED PHASE: Failing Tests for String Operations
// ============================================================================
#[test]
fn test_strlen_transpilation() {
// C code using strlen
let c_code = r#"
#include <string.h>
int get_length(const char* str) {
return strlen(str);
}
int main() {
const char* message = "Hello, World!";
int len = get_length(message);
return len;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
// Should NOT contain unsafe blocks for strlen
assert!(
!result.contains("unsafe"),
"String length should be safe (use .len())"
);
// Should use Rust str::len() or String::len()
assert!(
result.contains(".len()"),
"Should use safe Rust .len() method"
);
// Should compile as valid Rust
assert!(result.contains("fn get_length"), "Should generate function");
assert!(result.contains("fn main"), "Should generate main");
}
#[test]
fn test_strcpy_transpilation_to_string_copy() {
// C code using strcpy (UNSAFE in C!)
let c_code = r#"
#include <string.h>
void copy_string(char* dest, const char* src) {
strcpy(dest, src);
}
int main() {
char buffer[100];
copy_string(buffer, "Safe in Rust!");
return 0;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
// Should avoid raw strcpy - use String or clone
// Unsafe count should be minimal (ideally 0)
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 2,
"strcpy should minimize unsafe (found {})",
unsafe_count
);
// Should generate valid Rust function
assert!(
result.contains("fn copy_string"),
"Should generate function"
);
}
#[test]
fn test_strcat_transpilation_to_string_concatenation() {
// C code using strcat (UNSAFE in C!)
let c_code = r#"
#include <string.h>
void append_string(char* dest, const char* src) {
strcat(dest, src);
}
int main() {
char buffer[200];
strcpy(buffer, "Hello, ");
append_string(buffer, "Rust!");
return 0;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
// strcat should ideally use String::push_str or format!
// Check for minimal unsafe
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 3,
"strcat should minimize unsafe (found {})",
unsafe_count
);
assert!(
result.contains("fn append_string"),
"Should generate function"
);
}
#[test]
fn test_string_literal_transpilation() {
// C string literals should become &str or String
let c_code = r#"
int main() {
const char* greeting = "Hello, World!";
const char* farewell = "Goodbye!";
return 0;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
// String literals should be safe
assert!(
!result.contains("unsafe") || result.matches("unsafe").count() <= 1,
"String literals should be mostly safe"
);
// Should contain the string values
assert!(
result.contains("Hello, World!"),
"Should preserve string literal"
);
assert!(
result.contains("Goodbye!"),
"Should preserve string literal"
);
}
#[test]
fn test_strcmp_transpilation_to_eq() {
// strcmp should become == comparison
let c_code = r#"
#include <string.h>
int are_equal(const char* s1, const char* s2) {
return strcmp(s1, s2) == 0;
}
int main() {
const char* a = "test";
const char* b = "test";
int equal = are_equal(a, b);
return equal;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
// strcmp should ideally become == (safe comparison)
assert!(result.contains("fn are_equal"), "Should generate function");
// Should minimize unsafe
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 2,
"String comparison should minimize unsafe (found {})",
unsafe_count
);
}
// ============================================================================
// Edge Cases and Safety Validation
// ============================================================================
#[test]
fn test_null_string_handling() {
// NULL pointer check pattern in C
let c_code = r#"
#include <string.h>
int check_null(const char* str) {
if (str == 0) {
return 0;
}
return strlen(str);
}
int main() {
const char* valid_str = "test";
int len = check_null(valid_str);
return len;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
// NULL check should be handled
assert!(result.contains("fn check_null"), "Should generate function");
// Should handle null checks safely (no crashes)
// May use if statements or Option type
assert!(result.contains("fn main"), "Should generate main");
}
#[test]
fn test_empty_string_handling() {
// Empty strings should work correctly
let c_code = r#"
#include <string.h>
int main() {
const char* empty = "";
int len = strlen(empty);
return len;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
// Empty string should be safe
assert!(!result.is_empty(), "Should generate code");
assert!(result.contains("fn main"), "Should generate main");
}
#[test]
fn test_unsafe_block_count_target() {
// CRITICAL: Validate unsafe minimization goal
let c_code = r#"
#include <string.h>
int main() {
const char* str1 = "Hello";
const char* str2 = "World";
int len1 = strlen(str1);
int len2 = strlen(str2);
return len1 + len2;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
// Count unsafe blocks
let unsafe_count = result.matches("unsafe").count();
let lines_of_code = result.lines().count();
// Target: <5 unsafe per 1000 LOC
let unsafe_per_1000 = if lines_of_code > 0 {
(unsafe_count as f64 / lines_of_code as f64) * 1000.0
} else {
0.0
};
assert!(
unsafe_per_1000 < 5.0,
"Unsafe blocks per 1000 LOC should be <5 (got {:.2})",
unsafe_per_1000
);
}
// ============================================================================
// Compilation Validation
// ============================================================================
#[test]
fn test_transpiled_rust_compiles() {
// The transpiled Rust code should compile
let c_code = r#"
#include <string.h>
int main() {
const char* message = "Decy transpiler";
int length = strlen(message);
return length;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
// Basic syntax checks (actual compilation test would use rustc)
assert!(!result.is_empty(), "Should generate non-empty code");
assert!(result.contains("fn main"), "Should have main function");
// Should not have obvious syntax errors
assert!(
!result.contains("}}}}"),
"Should not have excessive closing braces"
);
assert!(
!result.contains(";;;;"),
"Should not have excessive semicolons"
);
}
#[test]
fn test_string_safety_documentation() {
// Transpiled code should have safety documentation
let c_code = r#"
#include <string.h>
char* get_string() {
static char buffer[100] = "Static string";
return buffer;
}
int main() {
char* str = get_string();
int len = strlen(str);
return len;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
// If unsafe blocks exist, they should have SAFETY comments
if result.contains("unsafe") {
// Should document why unsafe is needed
// (This is aspirational - may not be implemented yet)
assert!(result.contains("fn get_string"), "Should generate function");
}
}