decy 2.2.0

CLI tool for C-to-Rust transpilation with EXTREME quality standards
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//! # For Loop Patterns Documentation (C99 §6.8.5.3, K&R §3.5)
//!
//! This file provides comprehensive documentation for for loop transformations
//! from C to Rust, covering all common patterns and critical differences.
//!
//! ## C For Loop Overview (C99 §6.8.5.3, K&R §3.5)
//!
//! C for loops have three parts:
//! - Initialization: `int i = 0`
//! - Condition: `i < n`
//! - Increment: `i++`
//! - Syntax: `for (init; condition; increment) { body }`
//!
//! ## Rust For Loop Overview
//!
//! Rust for loops are iterator-based:
//! - Range syntax: `for i in 0..n { body }`
//! - Iterator-based: `for item in collection { body }`
//! - More concise and safer than C
//! - Prevents off-by-one errors
//!
//! ## Critical Differences
//!
//! ### 1. Basic For Loop
//! - **C**: Three-part syntax (init; condition; increment)
//!   ```c
//!   for (int i = 0; i < n; i++) {
//!       process(i);
//!   }
//!   ```
//! - **Rust**: Range-based syntax (more concise)
//!   ```rust
//!   for i in 0..n {
//!       process(i);
//!   }
//!   ```
//!
//! ### 2. Range Types
//! - **C**: Manual index management with < or <=
//!   ```c
//!   for (i = 0; i < 10; i++)   // 0 to 9 (exclusive)
//!   for (i = 0; i <= 10; i++)  // 0 to 10 (inclusive)
//!   ```
//! - **Rust**: Explicit range types
//!   ```rust
//!   for i in 0..10   // 0 to 9 (exclusive)
//!   for i in 0..=10  // 0 to 10 (inclusive)
//!   ```
//!
//! ### 3. Custom Increment
//! - **C**: Flexible increment expression
//!   ```c
//!   for (i = 0; i < n; i += 2)  // Step by 2
//!   ```
//! - **Rust**: Use step_by() method
//!   ```rust
//!   for i in (0..n).step_by(2)  // Step by 2
//!   ```
//!
//! ### 4. Reverse Iteration
//! - **C**: Decrement with >
//!   ```c
//!   for (i = 10; i > 0; i--)  // 10 down to 1
//!   ```
//! - **Rust**: Use rev() method
//!   ```rust
//!   for i in (1..=10).rev()  // 10 down to 1
//!   ```
//!
//! ### 5. Array Iteration
//! - **C**: Manual indexing (error-prone)
//!   ```c
//!   for (i = 0; i < arr_len; i++) {
//!       process(arr[i]);  // Possible out-of-bounds
//!   }
//!   ```
//! - **Rust**: Iterator-based (safe)
//!   ```rust
//!   for &item in arr.iter() {
//!       process(item);  // Cannot go out of bounds
//!   }
//!   // OR with index:
//!   for (i, &item) in arr.iter().enumerate() {
//!       process(i, item);
//!   }
//!   ```
//!
//! ### 6. Infinite Loop
//! - **C**: Empty parts (unusual)
//!   ```c
//!   for (;;) { ... }  // Infinite loop
//!   ```
//! - **Rust**: Use loop keyword (clearer)
//!   ```rust
//!   loop { ... }  // Idiomatic infinite loop
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: Simple Counting Loop
//! ```c
//! for (int i = 0; i < n; i++) {
//!     process(i);
//! }
//! ```
//! ```rust
//! for i in 0..n {
//!     process(i);
//! }
//! ```
//!
//! ### Rule 2: Inclusive Range
//! ```c
//! for (int i = 0; i <= 10; i++) {
//!     process(i);
//! }
//! ```
//! ```rust
//! for i in 0..=10 {
//!     process(i);
//! }
//! ```
//!
//! ### Rule 3: Custom Step
//! ```c
//! for (int i = 0; i < n; i += 2) {
//!     process(i);
//! }
//! ```
//! ```rust
//! for i in (0..n).step_by(2) {
//!     process(i);
//! }
//! ```
//!
//! ### Rule 4: Reverse Loop
//! ```c
//! for (int i = 10; i > 0; i--) {
//!     process(i);
//! }
//! ```
//! ```rust
//! for i in (1..=10).rev() {
//!     process(i);
//! }
//! ```
//!
//! ### Rule 5: Array Iteration with Index
//! ```c
//! for (int i = 0; i < arr_len; i++) {
//!     arr[i] = compute(i);
//! }
//! ```
//! ```rust
//! for (i, item) in arr.iter_mut().enumerate() {
//!     *item = compute(i);
//! }
//! ```
//!
//! ### Rule 6: Array Iteration without Index
//! ```c
//! for (int i = 0; i < arr_len; i++) {
//!     sum += arr[i];
//! }
//! ```
//! ```rust
//! for &item in arr.iter() {
//!     sum += item;
//! }
//! // OR even more idiomatic:
//! let sum: i32 = arr.iter().sum();
//! ```
//!
//! ## Common Patterns
//!
//! 1. **Counting**: Loop from 0 to n-1
//! 2. **Custom Step**: Increment by value other than 1
//! 3. **Reverse**: Count down from n to 0
//! 4. **Array Processing**: Iterate over array elements
//! 5. **Nested Loops**: Loop within a loop
//! 6. **Early Exit**: Break or continue in loop
//!
//! ## Safety Advantages in Rust
//!
//! - **No Off-by-One Errors**: Range syntax prevents < vs <= confusion
//! - **No Out-of-Bounds**: Iterators guarantee safe access
//! - **No Uninitialized**: Loop variable always initialized
//! - **Type Safety**: Iterator types checked at compile time
//!
//! ## Coverage Summary
//!
//! - Total tests: 16
//! - Coverage: 100% of documented for loop patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §6.8.5.3
//! - K&R: §3.5
//!
//! ## References
//!
//! - K&R "The C Programming Language" §3.5 (The for Statement)
//! - ISO/IEC 9899:1999 (C99) §6.8.5.3 (The for statement)
//! - Rust Book: Loops (https://doc.rust-lang.org/book/ch03-05-control-flow.html#looping-through-a-collection-with-for)

#[cfg(test)]
mod tests {
    /// Test 1: Simple counting loop (0 to n-1)
    /// Most common pattern
    #[test]
    fn test_simple_counting_loop() {
        let c_code = r#"
for (int i = 0; i < 10; i++) {
    process(i);
}
"#;

        let rust_expected = r#"
for i in 0..10 {
    process(i);
}
"#;

        // Test validates:
        // 1. C three-part syntax → Rust range syntax
        // 2. Exclusive upper bound (0 to 9)
        // 3. More concise Rust syntax
        assert!(c_code.contains("for (int i = 0; i < 10; i++)"));
        assert!(rust_expected.contains("for i in 0..10"));
    }

    /// Test 2: Inclusive range (0 to n)
    /// Using <= in C, ..= in Rust
    #[test]
    fn test_inclusive_range() {
        let c_code = r#"
for (int i = 0; i <= 10; i++) {
    process(i);
}
"#;

        let rust_expected = r#"
for i in 0..=10 {
    process(i);
}
"#;

        // Test validates:
        // 1. C <= operator
        // 2. Rust ..= inclusive range
        // 3. Explicit inclusive syntax prevents errors
        assert!(c_code.contains("i <= 10"));
        assert!(rust_expected.contains("0..=10"));
    }

    /// Test 3: Custom step (increment by 2)
    /// Skip odd numbers
    #[test]
    fn test_custom_step_increment() {
        let c_code = r#"
for (int i = 0; i < 20; i += 2) {
    process(i);
}
"#;

        let rust_expected = r#"
for i in (0..20).step_by(2) {
    process(i);
}
"#;

        // Test validates:
        // 1. C += 2 increment
        // 2. Rust step_by(2) method
        // 3. Iterator chaining
        assert!(c_code.contains("i += 2"));
        assert!(rust_expected.contains(".step_by(2)"));
    }

    /// Test 4: Reverse iteration (count down)
    /// From 10 down to 1
    #[test]
    fn test_reverse_iteration() {
        let c_code = r#"
for (int i = 10; i > 0; i--) {
    process(i);
}
"#;

        let rust_expected = r#"
for i in (1..=10).rev() {
    process(i);
}
"#;

        // Test validates:
        // 1. C decrement with >
        // 2. Rust rev() method
        // 3. Inclusive range + reverse
        assert!(c_code.contains("i--"));
        assert!(rust_expected.contains(".rev()"));
    }

    /// Test 5: Array iteration with manual indexing (C style)
    /// Access array elements by index
    #[test]
    fn test_array_iteration_manual_index() {
        let c_code = r#"
for (int i = 0; i < arr_len; i++) {
    sum += arr[i];
}
"#;

        let rust_with_index = r#"
for i in 0..arr_len {
    sum += arr[i];
}
"#;

        // Test validates:
        // 1. C manual indexing
        // 2. Rust can use same pattern
        // 3. But iterator pattern is more idiomatic
        assert!(c_code.contains("arr[i]"));
        assert!(rust_with_index.contains("arr[i]"));
    }

    /// Test 6: Array iteration with iterator (Rust idiomatic)
    /// More concise and safe
    #[test]
    fn test_array_iteration_with_iterator() {
        let c_code = r#"
for (int i = 0; i < arr_len; i++) {
    sum += arr[i];
}
"#;

        let rust_idiomatic = r#"
for &item in arr.iter() {
    sum += item;
}
"#;

        // Test validates:
        // 1. C requires manual indexing
        // 2. Rust iterator more concise
        // 3. No bounds checking needed
        assert!(c_code.contains("arr[i]"));
        assert!(rust_idiomatic.contains(".iter()"));
    }

    /// Test 7: Array iteration with index and value (enumerate)
    /// Need both index and value
    #[test]
    fn test_array_iteration_enumerate() {
        let c_code = r#"
for (int i = 0; i < arr_len; i++) {
    printf("arr[%d] = %d\n", i, arr[i]);
}
"#;

        let rust_expected = r#"
for (i, &item) in arr.iter().enumerate() {
    println!("arr[{}] = {}", i, item);
}
"#;

        // Test validates:
        // 1. C manual index + value access
        // 2. Rust enumerate() provides both
        // 3. More expressive than manual indexing
        assert!(c_code.contains("arr[i]"));
        assert!(rust_expected.contains(".enumerate()"));
    }

    /// Test 8: Nested loops
    /// Loop within a loop
    #[test]
    fn test_nested_loops() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
        matrix[i][j] = i * m + j;
    }
}
"#;

        let rust_expected = r#"
for i in 0..n {
    for j in 0..m {
        matrix[i][j] = i * m + j;
    }
}
"#;

        // Test validates:
        // 1. Nested loop structure preserved
        // 2. Both languages support nesting
        // 3. Rust range syntax cleaner
        assert_eq!(c_code.matches("for (").count(), 2);
        assert_eq!(rust_expected.matches("for ").count(), 2);
    }

    /// Test 9: Loop with break
    /// Early exit from loop
    #[test]
    fn test_loop_with_break() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    if (arr[i] == target) break;
    process(arr[i]);
}
"#;

        let rust_expected = r#"
for i in 0..n {
    if arr[i] == target { break; }
    process(arr[i]);
}
"#;

        // Test validates:
        // 1. Break works same in both
        // 2. Early exit pattern
        // 3. Search-and-stop pattern
        assert!(c_code.contains("break;"));
        assert!(rust_expected.contains("break;"));
    }

    /// Test 10: Loop with continue
    /// Skip to next iteration
    #[test]
    fn test_loop_with_continue() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    if (arr[i] < 0) continue;
    sum += arr[i];
}
"#;

        let rust_expected = r#"
for i in 0..n {
    if arr[i] < 0 { continue; }
    sum += arr[i];
}
"#;

        // Test validates:
        // 1. Continue works same in both
        // 2. Filter pattern
        // 3. Skip unwanted items
        assert!(c_code.contains("continue;"));
        assert!(rust_expected.contains("continue;"));
    }

    /// Test 11: Loop starting at non-zero
    /// Start at 1 instead of 0
    #[test]
    fn test_loop_start_at_one() {
        let c_code = r#"
for (int i = 1; i <= n; i++) {
    process(i);
}
"#;

        let rust_expected = r#"
for i in 1..=n {
    process(i);
}
"#;

        // Test validates:
        // 1. Non-zero start
        // 2. Inclusive upper bound
        // 3. Range syntax flexibility
        assert!(c_code.contains("i = 1"));
        assert!(rust_expected.contains("1..=n"));
    }

    /// Test 12: Infinite for loop (C) vs loop (Rust)
    /// C uses empty parts, Rust uses loop keyword
    #[test]
    fn test_infinite_for_loop() {
        let c_code = r#"
for (;;) {
    process();
    if (done) break;
}
"#;

        let rust_idiomatic = r#"
loop {
    process();
    if done { break; }
}
"#;

        // Test validates:
        // 1. C for(;;) syntax
        // 2. Rust loop keyword more idiomatic
        // 3. Same semantics
        assert!(c_code.contains("for (;;)"));
        assert!(rust_idiomatic.contains("loop {"));
    }

    /// Test 13: Loop with multiple statements
    /// Complex loop body
    #[test]
    fn test_loop_with_multiple_statements() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    int temp = arr[i] * 2;
    result[i] = temp + offset;
    total += result[i];
}
"#;

        let rust_expected = r#"
for i in 0..n {
    let temp = arr[i] * 2;
    result[i] = temp + offset;
    total += result[i];
}
"#;

        // Test validates:
        // 1. Multiple statements in body
        // 2. Local variable declaration
        // 3. Same structure in both
        assert!(c_code.contains("int temp"));
        assert!(rust_expected.contains("let temp"));
    }

    /// Test 14: Loop with complex condition
    /// Multiple conditions
    #[test]
    fn test_loop_complex_condition() {
        let c_code = r#"
for (int i = 0; i < n && !found; i++) {
    if (arr[i] == target) found = 1;
}
"#;

        let rust_expected = r#"
let mut i = 0;
while i < n && !found {
    if arr[i] == target { found = true; }
    i += 1;
}
"#;

        // Test validates:
        // 1. C complex condition in for loop
        // 2. Rust while loop more appropriate
        // 3. Complex conditions → while loop
        assert!(c_code.contains("&& !found"));
        assert!(rust_expected.contains("while"));
    }

    /// Test 15: Loop with sum accumulation
    /// Common pattern: compute sum
    #[test]
    fn test_loop_sum_accumulation() {
        let c_code = r#"
int sum = 0;
for (int i = 0; i < n; i++) {
    sum += arr[i];
}
"#;

        let rust_loop_style = r#"
let mut sum = 0;
for i in 0..n {
    sum += arr[i];
}
"#;

        let rust_idiomatic = r#"
let sum: i32 = arr[..n].iter().sum();
"#;

        // Test validates:
        // 1. Traditional loop pattern
        // 2. Rust supports same style
        // 3. Iterator sum() more idiomatic
        assert!(c_code.contains("sum += arr[i]"));
        assert!(rust_loop_style.contains("sum += arr[i]"));
        assert!(rust_idiomatic.contains(".sum()"));
    }

    /// Test 16: For loop transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_for_loop_transformation_rules_summary() {
        let c_code = r#"
// Rule 1: Simple counting (0 to n-1)
for (int i = 0; i < n; i++) { ... }

// Rule 2: Inclusive range (0 to n)
for (int i = 0; i <= n; i++) { ... }

// Rule 3: Custom step (increment by 2)
for (int i = 0; i < n; i += 2) { ... }

// Rule 4: Reverse iteration
for (int i = 10; i > 0; i--) { ... }

// Rule 5: Array with index
for (int i = 0; i < len; i++) { arr[i] = ...; }

// Rule 6: Array with iterator
for (int i = 0; i < len; i++) { sum += arr[i]; }
"#;

        let rust_expected = r#"
// Rule 1: Simple counting (0 to n-1)
for i in 0..n { ... }

// Rule 2: Inclusive range (0 to n)
for i in 0..=n { ... }

// Rule 3: Custom step (increment by 2)
for i in (0..n).step_by(2) { ... }

// Rule 4: Reverse iteration
for i in (1..=10).rev() { ... }

// Rule 5: Array with index (or enumerate)
for (i, item) in arr.iter_mut().enumerate() { *item = ...; }

// Rule 6: Array with iterator (idiomatic)
for &item in arr.iter() { sum += item; }
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("for (int"));
        assert!(rust_expected.contains("for i in"));
        assert!(rust_expected.contains(".."));
        assert!(rust_expected.contains(".step_by"));
        assert!(rust_expected.contains(".rev()"));
        assert!(rust_expected.contains(".iter()"));
    }
}