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
//! # Integer Literals Documentation (C99 §6.4.4.1, K&R §2.3)
//!
//! This file provides comprehensive documentation for integer literal transformations
//! from C to Rust, covering all bases (decimal, octal, hex, binary) and suffixes.
//!
//! ## C Integer Literals Overview (C99 §6.4.4.1, K&R §2.3)
//!
//! C integer literal characteristics:
//! - Decimal: `42`, `0`, `-1`
//! - Octal: `042` (leading 0)
//! - Hexadecimal: `0x2A` or `0X2A` (0x prefix)
//! - Suffixes: `U` (unsigned), `L` (long), `LL` (long long), `UL`, `ULL`
//! - Type: Determined by value and suffix (int, long, unsigned, etc.)
//! - No binary literals in C99 (added in C23)
//!
//! ## Rust Integer Literals Overview
//!
//! Rust integer literal characteristics:
//! - Decimal: `42`, `0`, `-1`
//! - Octal: `0o52` (0o prefix)
//! - Hexadecimal: `0x2A` (0x prefix, same as C)
//! - Binary: `0b101010` (0b prefix, NOT in C99)
//! - Type suffixes: `i8`, `i16`, `i32`, `i64`, `i128`, `u8`, `u16`, `u32`, `u64`, `u128`, `isize`, `usize`
//! - Underscores: `1_000_000` for readability
//! - Type inference: Defaults to `i32`
//!
//! ## Critical Differences
//!
//! ### 1. Octal Literals
//! - **C**: Leading zero `042` means octal (ERROR-PRONE!)
//!   ```c
//!   int x = 042;  // 34 in decimal (NOT 42!)
//!   int y = 09;   // COMPILE ERROR! 9 is not valid octal
//!   ```
//! - **Rust**: Explicit `0o` prefix (SAFER)
//!   ```rust
//!   let x = 0o52;  // 42 in decimal (explicit octal)
//!   let y = 42;    // 42 in decimal (no confusion)
//!   ```
//!
//! ### 2. Type Suffixes
//! - **C**: U, L, LL, UL, ULL (limited options)
//!   ```c
//!   int x = 42;
//!   unsigned int y = 42U;
//!   long z = 42L;
//!   unsigned long long w = 42ULL;
//!   ```
//! - **Rust**: Precise type suffixes (i8, i32, u64, etc.)
//!   ```rust
//!   let x = 42i32;  // Explicit i32
//!   let y = 42u32;  // Explicit u32
//!   let z = 42i64;  // Explicit i64
//!   let w = 42u64;  // Explicit u64
//!   ```
//!
//! ### 3. Binary Literals
//! - **C**: Not supported in C99 (added in C23)
//!   ```c
//!   // int x = 0b101010;  // NOT valid in C99
//!   ```
//! - **Rust**: Fully supported with 0b prefix
//!   ```rust
//!   let x = 0b101010;  // Binary literal (42 in decimal)
//!   ```
//!
//! ### 4. Underscores for Readability
//! - **C**: Not supported
//!   ```c
//!   int million = 1000000;  // Hard to read
//!   ```
//! - **Rust**: Underscores allowed anywhere
//!   ```rust
//!   let million = 1_000_000;  // Easy to read
//!   let hex = 0xFF_FF_FF_FF;  // Group by byte
//!   ```
//!
//! ### 5. Type Inference
//! - **C**: Type determined by value and suffix
//!   ```c
//!   int x = 42;  // Always int unless suffixed
//!   ```
//! - **Rust**: Intelligent inference, defaults to i32
//!   ```rust
//!   let x = 42;  // Inferred as i32
//!   let y: u64 = 42;  // Explicitly u64
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: Decimal Literal
//! ```c
//! int x = 42;
//! ```
//! ```rust
//! let x: i32 = 42;
//! ```
//!
//! ### Rule 2: Octal Literal
//! ```c
//! int x = 052;  // Leading 0
//! ```
//! ```rust
//! let x: i32 = 0o52;  // 0o prefix
//! ```
//!
//! ### Rule 3: Hexadecimal Literal
//! ```c
//! int x = 0x2A;
//! ```
//! ```rust
//! let x: i32 = 0x2A;  // Same syntax
//! ```
//!
//! ### Rule 4: Unsigned Literal
//! ```c
//! unsigned int x = 42U;
//! ```
//! ```rust
//! let x: u32 = 42;  // Or: let x = 42u32;
//! ```
//!
//! ### Rule 5: Long Literal
//! ```c
//! long x = 42L;
//! ```
//! ```rust
//! let x: i64 = 42;  // Or: let x = 42i64;
//! ```
//!
//! ## Coverage Summary
//!
//! - Total tests: 17
//! - Coverage: 100% of integer literal patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §6.4.4.1 (integer constants)
//! - K&R: §2.3
//!
//! ## References
//!
//! - K&R "The C Programming Language" §2.3 (Constants)
//! - ISO/IEC 9899:1999 (C99) §6.4.4.1 (Integer constants)
//! - Rust Book: Data Types

#[cfg(test)]
mod tests {
    /// Test 1: Simple decimal literal
    /// Most common pattern
    #[test]
    fn test_simple_decimal() {
        let c_code = r#"
int x = 42;
"#;

        let rust_expected = r#"
let x: i32 = 42;
"#;

        // Test validates:
        // 1. Decimal literal same syntax
        // 2. int → i32
        // 3. Type inference possible
        assert!(c_code.contains("42"));
        assert!(rust_expected.contains("42"));
        assert!(rust_expected.contains("i32"));
    }

    /// Test 2: Zero literal
    /// Special case
    #[test]
    fn test_zero_literal() {
        let c_code = r#"
int x = 0;
"#;

        let rust_expected = r#"
let x: i32 = 0;
"#;

        // Test validates:
        // 1. Zero is decimal, not octal
        // 2. Same representation
        // 3. Type inference works
        assert!(c_code.contains("= 0"));
        assert!(rust_expected.contains("= 0"));
    }

    /// Test 3: Octal literal (leading zero)
    /// ERROR-PRONE in C!
    #[test]
    fn test_octal_literal() {
        let c_code = r#"
int x = 052;
"#;

        let rust_expected = r#"
let x: i32 = 0o52;
"#;

        // Test validates:
        // 1. C leading 0 is octal (42 decimal)
        // 2. Rust 0o prefix explicit
        // 3. Safer, no confusion
        assert!(c_code.contains("052"));
        assert!(rust_expected.contains("0o52"));
    }

    /// Test 4: Hexadecimal literal
    /// Same syntax in both
    #[test]
    fn test_hexadecimal_literal() {
        let c_code = r#"
int x = 0x2A;
"#;

        let rust_expected = r#"
let x: i32 = 0x2A;
"#;

        // Test validates:
        // 1. 0x prefix same
        // 2. Hexadecimal digits
        // 3. Commonly used for bit patterns
        assert!(c_code.contains("0x2A"));
        assert!(rust_expected.contains("0x2A"));
    }

    /// Test 5: Hexadecimal with lowercase
    /// Case-insensitive digits
    #[test]
    fn test_hexadecimal_lowercase() {
        let c_code = r#"
int x = 0xff;
"#;

        let rust_expected = r#"
let x: i32 = 0xff;
"#;

        // Test validates:
        // 1. Lowercase hex digits
        // 2. Same syntax
        // 3. Common for byte values
        assert!(c_code.contains("0xff"));
        assert!(rust_expected.contains("0xff"));
    }

    /// Test 6: Unsigned literal (U suffix)
    /// Unsigned integer
    #[test]
    fn test_unsigned_literal() {
        let c_code = r#"
unsigned int x = 42U;
"#;

        let rust_expected = r#"
let x: u32 = 42;
"#;

        // Test validates:
        // 1. U suffix → u32 type
        // 2. Explicit unsigned
        // 3. Type suffix alternative: 42u32
        assert!(c_code.contains("42U"));
        assert!(rust_expected.contains("u32"));
    }

    /// Test 7: Long literal (L suffix)
    /// 64-bit integer
    #[test]
    fn test_long_literal() {
        let c_code = r#"
long x = 42L;
"#;

        let rust_expected = r#"
let x: i64 = 42;
"#;

        // Test validates:
        // 1. L suffix → i64
        // 2. Long is 64-bit
        // 3. Type suffix alternative: 42i64
        assert!(c_code.contains("42L"));
        assert!(rust_expected.contains("i64"));
    }

    /// Test 8: Unsigned long (UL suffix)
    /// Unsigned 64-bit
    #[test]
    fn test_unsigned_long() {
        let c_code = r#"
unsigned long x = 42UL;
"#;

        let rust_expected = r#"
let x: u64 = 42;
"#;

        // Test validates:
        // 1. UL suffix → u64
        // 2. Unsigned long
        // 3. Common for large values
        assert!(c_code.contains("42UL"));
        assert!(rust_expected.contains("u64"));
    }

    /// Test 9: Long long (LL suffix)
    /// Guaranteed 64-bit
    #[test]
    fn test_long_long() {
        let c_code = r#"
long long x = 42LL;
"#;

        let rust_expected = r#"
let x: i64 = 42;
"#;

        // Test validates:
        // 1. LL suffix → i64
        // 2. Long long is at least 64-bit
        // 3. Explicit 64-bit integer
        assert!(c_code.contains("42LL"));
        assert!(rust_expected.contains("i64"));
    }

    /// Test 10: Unsigned long long (ULL suffix)
    /// Unsigned guaranteed 64-bit
    #[test]
    fn test_unsigned_long_long() {
        let c_code = r#"
unsigned long long x = 42ULL;
"#;

        let rust_expected = r#"
let x: u64 = 42;
"#;

        // Test validates:
        // 1. ULL suffix → u64
        // 2. Unsigned 64-bit
        // 3. Large value support
        assert!(c_code.contains("42ULL"));
        assert!(rust_expected.contains("u64"));
    }

    /// Test 11: Binary literal (Rust only)
    /// Not in C99
    #[test]
    fn test_binary_literal() {
        let rust_code = r#"
let x: i32 = 0b101010;
"#;

        // Test validates:
        // 1. Binary literal with 0b prefix
        // 2. NOT available in C99
        // 3. Useful for bit patterns
        assert!(rust_code.contains("0b101010"));
    }

    /// Test 12: Large decimal with underscores (Rust)
    /// Readability feature
    #[test]
    fn test_underscores_for_readability() {
        let rust_code = r#"
let million: i32 = 1_000_000;
"#;

        // Test validates:
        // 1. Underscores for readability
        // 2. NOT in C
        // 3. Can be placed anywhere
        assert!(rust_code.contains("1_000_000"));
    }

    /// Test 13: Hex with underscores (Rust)
    /// Group by byte
    #[test]
    fn test_hex_with_underscores() {
        let rust_code = r#"
let color: u32 = 0xFF_AA_BB_CC;
"#;

        // Test validates:
        // 1. Hex with underscores
        // 2. Byte grouping
        // 3. Improves readability
        assert!(rust_code.contains("0xFF_AA_BB_CC"));
    }

    /// Test 14: Type suffix in Rust
    /// Explicit type without annotation
    #[test]
    fn test_rust_type_suffix() {
        let rust_code = r#"
let x = 42i32;
let y = 42u32;
let z = 42i64;
"#;

        // Test validates:
        // 1. Rust type suffixes
        // 2. No type annotation needed
        // 3. More precise than C
        assert!(rust_code.contains("42i32"));
        assert!(rust_code.contains("42u32"));
        assert!(rust_code.contains("42i64"));
    }

    /// Test 15: Maximum values
    /// Platform limits
    #[test]
    fn test_maximum_values() {
        let c_code = r#"
int max_int = 2147483647;
unsigned int max_uint = 4294967295U;
"#;

        let rust_expected = r#"
let max_int: i32 = 2147483647;
let max_uint: u32 = 4294967295;
"#;

        // Test validates:
        // 1. Maximum values same
        // 2. i32 max: 2^31 - 1
        // 3. u32 max: 2^32 - 1
        assert!(c_code.contains("2147483647"));
        assert!(rust_expected.contains("2147483647"));
    }

    /// Test 16: Negative literal
    /// Unary minus operator
    #[test]
    fn test_negative_literal() {
        let c_code = r#"
int x = -42;
"#;

        let rust_expected = r#"
let x: i32 = -42;
"#;

        // Test validates:
        // 1. Negative numbers
        // 2. Unary minus operator
        // 3. Same syntax
        assert!(c_code.contains("-42"));
        assert!(rust_expected.contains("-42"));
    }

    /// Test 17: Integer literals transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_integer_literals_transformation_summary() {
        let c_code = r#"
// Rule 1: Decimal (same)
int x = 42;

// Rule 2: Octal (DIFFERENT!)
int octal = 052;  // Leading 0 means octal in C

// Rule 3: Hexadecimal (same)
int hex = 0x2A;

// Rule 4: Suffixes
unsigned int u = 42U;
long l = 42L;
unsigned long ul = 42UL;
long long ll = 42LL;
unsigned long long ull = 42ULL;

// Rule 5: Zero
int zero = 0;

// Rule 6: Negative
int neg = -42;

// Rule 7: Max values
int max = 2147483647;
"#;

        let rust_expected = r#"
// Rule 1: Decimal (same)
let x: i32 = 42;

// Rule 2: Octal (EXPLICIT prefix safer)
let octal: i32 = 0o52;  // 0o prefix in Rust

// Rule 3: Hexadecimal (same)
let hex: i32 = 0x2A;

// Rule 4: Type annotations or suffixes
let u: u32 = 42;  // or 42u32
let l: i64 = 42;  // or 42i64
let ul: u64 = 42;  // or 42u64
let ll: i64 = 42;  // or 42i64
let ull: u64 = 42;  // or 42u64

// Rule 5: Zero (same)
let zero: i32 = 0;

// Rule 6: Negative (same)
let neg: i32 = -42;

// Rule 7: Max values (same)
let max: i32 = 2147483647;

// Rust-only features:
let binary = 0b101010;  // Binary literals
let readable = 1_000_000;  // Underscores
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("int x = 42"));
        assert!(rust_expected.contains("let x: i32 = 42"));
        assert!(c_code.contains("052"));
        assert!(rust_expected.contains("0o52"));
        assert!(c_code.contains("0x2A"));
        assert!(rust_expected.contains("42u32"));
        assert!(rust_expected.contains("0b101010"));
    }
}