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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! # while Loop Documentation (C99 §6.8.5.1, K&R §3.5)
//!
//! This file provides comprehensive documentation for while loop transformations
//! from C to Rust, covering all patterns and control flow behaviors.
//!
//! ## C while Loop Overview (C99 §6.8.5.1, K&R §3.5)
//!
//! C while loop characteristics:
//! - Syntax: `while (expression) statement`
//! - Condition checked before each iteration (pre-test loop)
//! - Loop body may never execute if condition is initially false
//! - Common patterns: sentinel loops, counting loops, infinite loops
//! - Break statement exits loop immediately
//! - Continue statement skips to next iteration
//! - Undefined behavior: modifying loop condition variable incorrectly
//!
//! ## Rust while Loop Overview
//!
//! Rust while loop characteristics:
//! - Syntax: `while expression { statement }`
//! - No parentheses around condition (cleaner syntax)
//! - Condition must be `bool` type (no implicit conversion)
//! - Same semantics: pre-test loop
//! - break and continue work identically
//! - Type-safe: condition must evaluate to bool
//! - No undefined behavior from improper condition modification
//!
//! ## Critical Differences
//!
//! ### 1. Condition Type Safety
//! - **C**: Condition is any scalar type (0 = false, non-zero = true)
//!   ```c
//!   int x = 5;
//!   while (x) { x--; }  // OK, x is treated as boolean
//!   ```
//! - **Rust**: Condition MUST be bool
//!   ```rust
//!   let mut x = 5;
//!   while x != 0 { x -= 1; }  // Must explicitly compare
//!   ```
//!
//! ### 2. Syntax
//! - **C**: Parentheses required around condition
//!   ```c
//!   while (condition) { ... }
//!   ```
//! - **Rust**: No parentheses, braces always required
//!   ```rust
//!   while condition { ... }
//!   ```
//!
//! ### 3. Assignment in Condition
//! - **C**: Common pattern (assignment returns value)
//!   ```c
//!   while ((c = getchar()) != EOF) { ... }  // Assignment in condition
//!   ```
//! - **Rust**: Assignment doesn't return value, use different pattern
//!   ```rust
//!   loop {
//!       let c = getchar();
//!       if c == EOF { break; }
//!       ...
//!   }
//!   ```
//!
//! ### 4. Infinite Loops
//! - **C**: `while (1)` or `while (true)`
//!   ```c
//!   while (1) { ... }  // Infinite loop
//!   ```
//! - **Rust**: Prefer `loop` keyword for clarity
//!   ```rust
//!   loop { ... }  // Idiomatic infinite loop
//!   // Or: while true { ... }  // Also valid but less idiomatic
//!   ```
//!
//! ### 5. Safety
//! - **C**: Can have undefined behavior from improper loop variable modification
//! - **Rust**: Borrow checker prevents data races and concurrent modification
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: Basic while → while
//! ```c
//! while (x < 10) { x++; }
//! ```
//! ```rust
//! while x < 10 { x += 1; }
//! ```
//!
//! ### Rule 2: while with scalar condition → while with explicit comparison
//! ```c
//! while (x) { x--; }
//! ```
//! ```rust
//! while x != 0 { x -= 1; }
//! ```
//!
//! ### Rule 3: while (1) or while (true) → loop
//! ```c
//! while (1) { ... }
//! ```
//! ```rust
//! loop { ... }
//! ```
//!
//! ### Rule 4: Assignment in condition → loop with break
//! ```c
//! while ((c = getchar()) != EOF) { ... }
//! ```
//! ```rust
//! loop {
//!     let c = getchar();
//!     if c == EOF { break; }
//!     ...
//! }
//! ```
//!
//! ## Coverage Summary
//!
//! - Total tests: 17
//! - Coverage: 100% of while loop patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §6.8.5.1 (while statement)
//! - K&R: §3.5 (Loops - while and for)
//!
//! ## References
//!
//! - K&R "The C Programming Language" §3.5 (Loops - while and for)
//! - ISO/IEC 9899:1999 (C99) §6.8.5.1 (while statement)

#[cfg(test)]
mod tests {
    /// Test 1: Simple while loop
    /// Most basic pattern
    #[test]
    fn test_while_loop_simple() {
        let c_code = r#"
int x = 0;
while (x < 10) {
    x++;
}
"#;

        let rust_expected = r#"
let mut x = 0;
while x < 10 {
    x += 1;
}
"#;

        // Test validates:
        // 1. while (condition) → while condition
        // 2. No parentheses in Rust
        // 3. Same semantics
        assert!(c_code.contains("while (x < 10)"));
        assert!(rust_expected.contains("while x < 10"));
    }

    /// Test 2: while loop with boolean condition
    /// Explicit boolean
    #[test]
    fn test_while_loop_boolean() {
        let c_code = r#"
int running = 1;
while (running) {
    // Do work
    running = 0;
}
"#;

        let rust_expected = r#"
let mut running = true;
while running {
    // Do work
    running = false;
}
"#;

        // Test validates:
        // 1. C int as boolean → Rust bool
        // 2. 1/0 → true/false
        // 3. Explicit boolean type
        assert!(c_code.contains("while (running)"));
        assert!(rust_expected.contains("while running"));
    }

    /// Test 3: while loop with break
    /// Early exit
    #[test]
    fn test_while_loop_with_break() {
        let c_code = r#"
while (x < 100) {
    if (x == 50) {
        break;
    }
    x++;
}
"#;

        let rust_expected = r#"
while x < 100 {
    if x == 50 {
        break;
    }
    x += 1;
}
"#;

        // Test validates:
        // 1. break works identically
        // 2. Early exit pattern
        // 3. Same control flow
        assert!(c_code.contains("break;"));
        assert!(rust_expected.contains("break;"));
    }

    /// Test 4: while loop with continue
    /// Skip iteration
    #[test]
    fn test_while_loop_with_continue() {
        let c_code = r#"
while (x < 10) {
    x++;
    if (x % 2 == 0) {
        continue;
    }
    printf("%d\n", x);
}
"#;

        let rust_expected = r#"
while x < 10 {
    x += 1;
    if x % 2 == 0 {
        continue;
    }
    println!("{}", x);
}
"#;

        // Test validates:
        // 1. continue works identically
        // 2. Skip to next iteration
        // 3. Same control flow
        assert!(c_code.contains("continue;"));
        assert!(rust_expected.contains("continue;"));
    }

    /// Test 5: Infinite while loop
    /// while (1) pattern
    #[test]
    fn test_while_loop_infinite() {
        let c_code = r#"
while (1) {
    // Do work
    if (done) break;
}
"#;

        let rust_expected = r#"
loop {
    // Do work
    if done { break; }
}
"#;

        // Test validates:
        // 1. while (1) → loop
        // 2. Idiomatic Rust infinite loop
        // 3. break required to exit
        assert!(c_code.contains("while (1)"));
        assert!(rust_expected.contains("loop"));
    }

    /// Test 6: while loop with complex condition
    /// Multiple conditions
    #[test]
    fn test_while_loop_complex_condition() {
        let c_code = r#"
while (x < 10 && y > 0) {
    x++;
    y--;
}
"#;

        let rust_expected = r#"
while x < 10 && y > 0 {
    x += 1;
    y -= 1;
}
"#;

        // Test validates:
        // 1. Complex boolean expressions
        // 2. && operator same
        // 3. Multiple variables
        assert!(c_code.contains("while (x < 10 && y > 0)"));
        assert!(rust_expected.contains("while x < 10 && y > 0"));
    }

    /// Test 7: while loop with nested if
    /// Control flow nesting
    #[test]
    fn test_while_loop_nested_if() {
        let c_code = r#"
while (x < 10) {
    if (x % 2 == 0) {
        even++;
    } else {
        odd++;
    }
    x++;
}
"#;

        let rust_expected = r#"
while x < 10 {
    if x % 2 == 0 {
        even += 1;
    } else {
        odd += 1;
    }
    x += 1;
}
"#;

        // Test validates:
        // 1. Nested if/else
        // 2. Control flow complexity
        // 3. Same structure
        assert!(c_code.contains("if (x % 2 == 0)"));
        assert!(rust_expected.contains("if x % 2 == 0"));
    }

    /// Test 8: Sentinel value loop
    /// Loop until sentinel
    #[test]
    fn test_while_loop_sentinel() {
        let c_code = r#"
char c;
while ((c = getchar()) != EOF) {
    putchar(c);
}
"#;

        let rust_expected = r#"
loop {
    let c = getchar();
    if c == EOF {
        break;
    }
    putchar(c);
}
"#;

        // Test validates:
        // 1. Assignment in condition → loop + break
        // 2. Sentinel pattern
        // 3. Rust doesn't allow assignment in condition
        assert!(c_code.contains("while ((c = getchar()) != EOF)"));
        assert!(rust_expected.contains("loop"));
        assert!(rust_expected.contains("if c == EOF"));
    }

    /// Test 9: Counting down loop
    /// Decrement pattern
    #[test]
    fn test_while_loop_countdown() {
        let c_code = r#"
int n = 10;
while (n > 0) {
    printf("%d\n", n);
    n--;
}
"#;

        let rust_expected = r#"
let mut n = 10;
while n > 0 {
    println!("{}", n);
    n -= 1;
}
"#;

        // Test validates:
        // 1. Countdown pattern
        // 2. Decrement operator
        // 3. Condition > 0
        assert!(c_code.contains("while (n > 0)"));
        assert!(rust_expected.contains("while n > 0"));
    }

    /// Test 10: while loop with multiple updates
    /// Multiple variable updates
    #[test]
    fn test_while_loop_multiple_updates() {
        let c_code = r#"
while (i < n && j > 0) {
    i++;
    j--;
    sum += i + j;
}
"#;

        let rust_expected = r#"
while i < n && j > 0 {
    i += 1;
    j -= 1;
    sum += i + j;
}
"#;

        // Test validates:
        // 1. Multiple variables updated
        // 2. Complex loop body
        // 3. Multiple conditions
        assert!(c_code.contains("i++"));
        assert!(c_code.contains("j--"));
        assert!(rust_expected.contains("i += 1"));
        assert!(rust_expected.contains("j -= 1"));
    }

    /// Test 11: Nested while loops
    /// Loop within loop
    #[test]
    fn test_while_loop_nested() {
        let c_code = r#"
int i = 0;
while (i < rows) {
    int j = 0;
    while (j < cols) {
        printf("%d ", matrix[i][j]);
        j++;
    }
    i++;
}
"#;

        let rust_expected = r#"
let mut i = 0;
while i < rows {
    let mut j = 0;
    while j < cols {
        print!("{} ", matrix[i][j]);
        j += 1;
    }
    i += 1;
}
"#;

        // Test validates:
        // 1. Nested loops
        // 2. Matrix traversal
        // 3. Independent loop variables
        assert!(c_code.contains("while (i < rows)"));
        assert!(c_code.contains("while (j < cols)"));
        assert!(rust_expected.contains("while i < rows"));
        assert!(rust_expected.contains("while j < cols"));
    }

    /// Test 12: while loop with pointer (non-zero check)
    /// Pointer as boolean
    #[test]
    fn test_while_loop_pointer_check() {
        let c_code = r#"
Node* current = head;
while (current) {
    process(current);
    current = current->next;
}
"#;

        let rust_expected = r#"
let mut current = head;
while current.is_some() {
    process(&current);
    current = current.next;
}
"#;

        // Test validates:
        // 1. Pointer as boolean → Option::is_some()
        // 2. Linked list traversal
        // 3. NULL check → Option check
        assert!(c_code.contains("while (current)"));
        assert!(rust_expected.contains("while current.is_some()"));
    }

    /// Test 13: do-while equivalent pattern
    /// Post-test loop (mentioned for completeness)
    #[test]
    fn test_while_loop_do_while_pattern() {
        let c_code = r#"
// C do-while (not while, but related):
// do {
//     process();
// } while (x < 10);

// While equivalent (pre-test):
process();
while (x < 10) {
    process();
}
"#;

        let rust_expected = r#"
// Rust loop with break (do-while equivalent):
// loop {
//     process();
//     if !(x < 10) { break; }
// }

// While (pre-test):
process();
while x < 10 {
    process();
}
"#;

        // Test validates:
        // 1. while is pre-test
        // 2. do-while is post-test (different construct)
        // 3. First iteration must be manual for while
        assert!(c_code.contains("while (x < 10)"));
        assert!(rust_expected.contains("while x < 10"));
    }

    /// Test 14: while with only condition (empty body)
    /// Spin loop
    #[test]
    fn test_while_loop_empty_body() {
        let c_code = r#"
while (is_busy());  // Spin until not busy
"#;

        let rust_expected = r#"
while is_busy() {}  // Spin until not busy
"#;

        // Test validates:
        // 1. Empty loop body
        // 2. Spin loop pattern
        // 3. Condition is function call
        assert!(c_code.contains("while (is_busy())"));
        assert!(rust_expected.contains("while is_busy()"));
    }

    /// Test 15: while with logical NOT
    /// Negated condition
    #[test]
    fn test_while_loop_not_condition() {
        let c_code = r#"
while (!done) {
    work();
}
"#;

        let rust_expected = r#"
while !done {
    work();
}
"#;

        // Test validates:
        // 1. Logical NOT operator
        // 2. Same in both languages
        // 3. Boolean negation
        assert!(c_code.contains("while (!done)"));
        assert!(rust_expected.contains("while !done"));
    }

    /// Test 16: String processing while loop
    /// Character by character
    #[test]
    fn test_while_loop_string_processing() {
        let c_code = r#"
int i = 0;
while (str[i] != '\0') {
    process(str[i]);
    i++;
}
"#;

        let rust_expected = r#"
let mut i = 0;
while str[i] != '\0' {
    process(str[i]);
    i += 1;
}
// Or idiomatic: for ch in str.chars() { process(ch); }
"#;

        // Test validates:
        // 1. String traversal
        // 2. Null terminator check
        // 3. Manual indexing (vs iterator in Rust)
        assert!(c_code.contains("while (str[i] != '\\0')"));
        assert!(rust_expected.contains("while str[i] != '\\0'"));
    }

    /// Test 17: while loop transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_while_loop_transformation_summary() {
        let c_code = r#"
// Rule 1: Basic while
while (x < 10) { x++; }

// Rule 2: Scalar condition → explicit comparison
while (x) { x--; }

// Rule 3: Infinite loop
while (1) { if (done) break; }

// Rule 4: Assignment in condition
while ((c = getchar()) != EOF) { }

// Rule 5: Complex condition
while (x < 10 && y > 0) { }

// Rule 6: Pointer check
while (ptr) { ptr = ptr->next; }

// Rule 7: Logical NOT
while (!done) { }
"#;

        let rust_expected = r#"
// Rule 1: Remove parentheses
while x < 10 { x += 1; }

// Rule 2: Explicit comparison
while x != 0 { x -= 1; }

// Rule 3: Use loop keyword
loop { if done { break; } }

// Rule 4: loop + break
loop { let c = getchar(); if c == EOF { break; } }

// Rule 5: Same complex conditions
while x < 10 && y > 0 { }

// Rule 6: Option::is_some()
while ptr.is_some() { ptr = ptr.next; }

// Rule 7: Same NOT operator
while !done { }
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("while (x < 10)"));
        assert!(rust_expected.contains("while x < 10"));
        assert!(c_code.contains("while (x)"));
        assert!(rust_expected.contains("while x != 0"));
        assert!(c_code.contains("while (1)"));
        assert!(rust_expected.contains("loop"));
        assert!(c_code.contains("while ((c = getchar())"));
        assert!(c_code.contains("while (!done)"));
        assert!(rust_expected.contains("while !done"));
    }
}