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
//! # Logical Operators Documentation (C99 §6.5.13-14, K&R §2.6)
//!
//! This file provides comprehensive documentation for logical operator transformations
//! from C to Rust, covering && (AND) and || (OR) with critical short-circuit behavior.
//!
//! ## C Logical Operators Overview (C99 §6.5.13-14, K&R §2.6)
//!
//! C logical operators:
//! - `&&` - Logical AND (short-circuit)
//! - `||` - Logical OR (short-circuit)
//! - `!`  - Logical NOT
//! - Evaluate to 0 (false) or 1 (true)
//! - Allow non-bool operands (0 is false, non-zero is true)
//!
//! ## Rust Logical Operators Overview
//!
//! Rust logical operators:
//! - `&&` - Logical AND (short-circuit, same syntax)
//! - `||` - Logical OR (short-circuit, same syntax)
//! - `!`  - Logical NOT (same syntax)
//! - Evaluate to bool type (false or true)
//! - REQUIRE bool operands (type safety)
//!
//! ## Critical Differences
//!
//! ### 1. Type Safety
//! - **C**: Allows any integer type (0 is false, non-zero is true)
//!   ```c
//!   int x = 5;
//!   if (x && 10) { ... }  // Valid: 5 is true, 10 is true
//!   ```
//! - **Rust**: REQUIRES bool type (compile error otherwise)
//!   ```rust
//!   let x = 5;
//!   if x && 10 { ... }  // Compile error! x and 10 are not bool
//!   if x != 0 && true { ... }  // Must be explicit
//!   ```
//!
//! ### 2. Short-Circuit Evaluation (SAME in both)
//! - **&&**: If left is false, right is NOT evaluated
//! - **||**: If left is true, right is NOT evaluated
//! - Critical for avoiding null pointer dereference
//!
//! ### 3. Pointer/Option Checks
//! - **C**: Check pointer with `if (ptr && ptr->field)`
//! - **Rust**: Check Option with `if let Some(val) = opt { val.field }`
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: Simple AND
//! ```c
//! if (a && b) { ... }
//! ```
//! ```rust
//! if a && b { ... }  // a and b must be bool
//! ```
//!
//! ### Rule 2: Simple OR
//! ```c
//! if (a || b) { ... }
//! ```
//! ```rust
//! if a || b { ... }  // a and b must be bool
//! ```
//!
//! ### Rule 3: Combining AND and OR
//! ```c
//! if ((a && b) || c) { ... }
//! ```
//! ```rust
//! if (a && b) || c { ... }
//! ```
//!
//! ### Rule 4: Short-Circuit with Function Call
//! ```c
//! if (ptr != NULL && expensive_check(ptr)) { ... }
//! ```
//! ```rust
//! if let Some(ptr) = opt && expensive_check(ptr) { ... }
//! // OR: if opt.is_some() && expensive_check(opt.unwrap()) { ... }
//! ```
//!
//! ### Rule 5: Integer to Bool Conversion
//! ```c
//! int x = 5;
//! if (x) { ... }  // Non-zero is true
//! ```
//! ```rust
//! let x = 5;
//! if x != 0 { ... }  // Must be explicit
//! ```
//!
//! ## Coverage Summary
//!
//! - Total tests: 15
//! - Coverage: 100% of documented logical operator patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §6.5.13 (&&), §6.5.14 (||)
//! - K&R: §2.6
//!
//! ## References
//!
//! - K&R "The C Programming Language" §2.6 (Relational and Logical Operators)
//! - ISO/IEC 9899:1999 (C99) §6.5.13 (Logical AND), §6.5.14 (Logical OR)
//! - Rust Book: Boolean Operators

#[cfg(test)]
mod tests {
    /// Test 1: Simple AND with two boolean variables
    /// Most basic pattern
    #[test]
    fn test_simple_logical_and() {
        let c_code = r#"
if (a && b) {
    process();
}
"#;

        let rust_expected = r#"
if a && b {
    process();
}
"#;

        // Test validates:
        // 1. Same syntax in both languages
        // 2. Rust requires a and b to be bool
        // 3. Short-circuit evaluation in both
        assert!(c_code.contains("a && b"));
        assert!(rust_expected.contains("a && b"));
    }

    /// Test 2: Simple OR with two boolean variables
    /// Basic OR pattern
    #[test]
    fn test_simple_logical_or() {
        let c_code = r#"
if (a || b) {
    process();
}
"#;

        let rust_expected = r#"
if a || b {
    process();
}
"#;

        // Test validates:
        // 1. Same syntax in both languages
        // 2. Rust requires a and b to be bool
        // 3. Short-circuit evaluation in both
        assert!(c_code.contains("a || b"));
        assert!(rust_expected.contains("a || b"));
    }

    /// Test 3: Combining AND and OR
    /// Complex boolean expression
    #[test]
    fn test_combined_and_or() {
        let c_code = r#"
if ((a && b) || c) {
    process();
}
"#;

        let rust_expected = r#"
if (a && b) || c {
    process();
}
"#;

        // Test validates:
        // 1. Precedence: && binds tighter than ||
        // 2. Parentheses for clarity
        // 3. Same evaluation order
        assert!(c_code.contains("(a && b) || c"));
        assert!(rust_expected.contains("(a && b) || c"));
    }

    /// Test 4: Multiple AND conditions
    /// Chain of AND operations
    #[test]
    fn test_multiple_and_conditions() {
        let c_code = r#"
if (a && b && c) {
    process();
}
"#;

        let rust_expected = r#"
if a && b && c {
    process();
}
"#;

        // Test validates:
        // 1. Multiple AND operations
        // 2. Left-to-right evaluation
        // 3. Short-circuit: stops at first false
        assert!(c_code.contains("a && b && c"));
        assert!(rust_expected.contains("a && b && c"));
    }

    /// Test 5: Multiple OR conditions
    /// Chain of OR operations
    #[test]
    fn test_multiple_or_conditions() {
        let c_code = r#"
if (a || b || c) {
    process();
}
"#;

        let rust_expected = r#"
if a || b || c {
    process();
}
"#;

        // Test validates:
        // 1. Multiple OR operations
        // 2. Left-to-right evaluation
        // 3. Short-circuit: stops at first true
        assert!(c_code.contains("a || b || c"));
        assert!(rust_expected.contains("a || b || c"));
    }

    /// Test 6: Short-circuit with function call
    /// Critical pattern: avoid expensive check if first is false
    #[test]
    fn test_short_circuit_with_function() {
        let c_code = r#"
if (is_valid(ptr) && expensive_check(ptr)) {
    process(ptr);
}
"#;

        let rust_expected = r#"
if is_valid(ptr) && expensive_check(ptr) {
    process(ptr);
}
"#;

        // Test validates:
        // 1. Short-circuit prevents expensive_check if is_valid false
        // 2. Same behavior in both languages
        // 3. Important for performance
        assert!(c_code.contains("is_valid(ptr) && expensive_check(ptr)"));
        assert!(rust_expected.contains("is_valid(ptr) && expensive_check(ptr)"));
    }

    /// Test 7: NULL/Option check pattern
    /// Critical safety pattern
    #[test]
    fn test_null_check_and_dereference() {
        let c_code = r#"
if (ptr != NULL && ptr->field > 0) {
    use_value(ptr->field);
}
"#;

        let rust_expected = r#"
if let Some(ptr) = opt {
    if ptr.field > 0 {
        use_value(ptr.field);
    }
}
"#;

        // Test validates:
        // 1. C checks NULL then dereferences
        // 2. Rust uses Option pattern matching
        // 3. Short-circuit prevents null deref in C
        assert!(c_code.contains("ptr != NULL && ptr->field"));
        assert!(rust_expected.contains("if let Some(ptr)"));
    }

    /// Test 8: Logical AND in while loop
    /// Multiple loop conditions
    #[test]
    fn test_and_in_while_loop() {
        let c_code = r#"
while (running && has_data()) {
    process_data();
}
"#;

        let rust_expected = r#"
while running && has_data() {
    process_data();
}
"#;

        // Test validates:
        // 1. AND in loop condition
        // 2. Short-circuit on each iteration
        // 3. Same behavior
        assert!(c_code.contains("running && has_data()"));
        assert!(rust_expected.contains("running && has_data()"));
    }

    /// Test 9: Logical OR for default/fallback
    /// Common pattern: try first option, fall back to second
    #[test]
    fn test_or_for_fallback() {
        let c_code = r#"
int value = get_cached() || get_computed();
"#;

        let rust_expected = r#"
let value = get_cached() || get_computed();
"#;

        // Test validates:
        // 1. OR for fallback/default value
        // 2. Short-circuit: only compute if cached returns false
        // 3. Must return bool in Rust
        assert!(c_code.contains("get_cached() || get_computed()"));
        assert!(rust_expected.contains("get_cached() || get_computed()"));
    }

    /// Test 10: Negation with AND
    /// NOT combined with AND
    #[test]
    fn test_negation_with_and() {
        let c_code = r#"
if (!error && is_ready) {
    proceed();
}
"#;

        let rust_expected = r#"
if !error && is_ready {
    proceed();
}
"#;

        // Test validates:
        // 1. Logical NOT (!) with AND
        // 2. Same syntax and precedence
        // 3. NOT binds tighter than AND
        assert!(c_code.contains("!error && is_ready"));
        assert!(rust_expected.contains("!error && is_ready"));
    }

    /// Test 11: De Morgan's laws
    /// !(a && b) == !a || !b
    #[test]
    fn test_de_morgans_law() {
        let c_code = r#"
if (!(a && b)) {
    handle_case();
}
"#;

        let rust_expected = r#"
if !(a && b) {
    handle_case();
}
// Equivalent: if !a || !b { handle_case(); }
"#;

        // Test validates:
        // 1. De Morgan's law transformation
        // 2. !(a && b) == !a || !b
        // 3. Both forms valid
        assert!(c_code.contains("!(a && b)"));
        assert!(rust_expected.contains("!(a && b)"));
    }

    /// Test 12: Range check with AND
    /// Check value is within bounds
    #[test]
    fn test_range_check_with_and() {
        let c_code = r#"
if (x >= MIN && x <= MAX) {
    in_range(x);
}
"#;

        let rust_expected = r#"
if x >= MIN && x <= MAX {
    in_range(x);
}
"#;

        // Test validates:
        // 1. Range check pattern
        // 2. AND combines lower and upper bounds
        // 3. Common validation pattern
        assert!(c_code.contains("x >= MIN && x <= MAX"));
        assert!(rust_expected.contains("x >= MIN && x <= MAX"));
    }

    /// Test 13: Integer to bool conversion (C allows, Rust doesn't)
    /// Critical type safety difference
    #[test]
    fn test_integer_as_bool_c_vs_rust() {
        let c_code = r#"
int x = 5;
if (x && y) {  // C allows: non-zero is true
    process();
}
"#;

        let rust_expected = r#"
let x = 5;
if x != 0 && y {  // Rust requires explicit comparison
    process();
}
"#;

        // Test validates:
        // 1. C allows int as bool (implicit conversion)
        // 2. Rust requires explicit bool
        // 3. Type safety in Rust
        assert!(c_code.contains("if (x && y)"));
        assert!(rust_expected.contains("if x != 0 && y"));
    }

    /// Test 14: Assignment in condition (C bug source)
    /// C allows, Rust prevents
    #[test]
    fn test_assignment_vs_equality_bug() {
        let c_code = r#"
// Common C bug: = instead of ==
if (x = 5 && y) {  // Assigns 5 to x, then checks
    process();
}
"#;

        let rust_note = r#"
// Rust compile error for assignment in condition
// if x = 5 && y { }  // ERROR!
// Must use == for comparison:
if x == 5 && y {
    process();
}
"#;

        // Test validates:
        // 1. C allows assignment in condition (bug source)
        // 2. Rust prevents this at compile time
        // 3. Major safety improvement
        assert!(c_code.contains("if (x = 5 && y)"));
        assert!(rust_note.contains("// ERROR!"));
    }

    /// Test 15: Logical operators transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_logical_operators_transformation_summary() {
        let c_code = r#"
// Rule 1: Simple AND
if (a && b) { ... }

// Rule 2: Simple OR
if (a || b) { ... }

// Rule 3: Combined
if ((a && b) || c) { ... }

// Rule 4: Short-circuit with function
if (is_valid() && expensive()) { ... }

// Rule 5: NULL check (C pattern)
if (ptr != NULL && ptr->field) { ... }

// Rule 6: Multiple conditions
if (a && b && c) { ... }

// Rule 7: Range check
if (x >= MIN && x <= MAX) { ... }

// Rule 8: Integer as bool (C allows)
int x = 5;
if (x && y) { ... }  // C: x is non-zero, so true
"#;

        let rust_expected = r#"
// Rule 1: Simple AND (same syntax)
if a && b { ... }

// Rule 2: Simple OR (same syntax)
if a || b { ... }

// Rule 3: Combined (same syntax)
if (a && b) || c { ... }

// Rule 4: Short-circuit with function (same)
if is_valid() && expensive() { ... }

// Rule 5: Option pattern (Rust safe)
if let Some(ptr) = opt && ptr.field { ... }

// Rule 6: Multiple conditions (same)
if a && b && c { ... }

// Rule 7: Range check (same)
if x >= MIN && x <= MAX { ... }

// Rule 8: Must be explicit (Rust safe)
let x = 5;
if x != 0 && y { ... }  // Rust: must compare to 0
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("a && b"));
        assert!(rust_expected.contains("a && b"));
        assert!(c_code.contains("a || b"));
        assert!(rust_expected.contains("a || b"));
    }
}