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
//! # Floating-Point Literals Documentation (C99 §6.4.4.2, K&R §2.3)
//!
//! This file provides comprehensive documentation for floating-point literal transformations
//! from C to Rust, covering all floating-point patterns, scientific notation, and precision differences.
//!
//! ## C Floating-Point Literals Overview (C99 §6.4.4.2, K&R §2.3)
//!
//! C floating-point literal characteristics:
//! - Decimal notation: `3.14`, `0.5`, `.5`
//! - Scientific notation: `1.5e10`, `2.3E-5`
//! - Type: `double` by default (64-bit)
//! - Suffixes: `f` or `F` (float, 32-bit), `l` or `L` (long double, implementation-defined)
//! - Special values: `INFINITY`, `NAN` (via macros)
//! - Precision: float (6-7 digits), double (15-17 digits)
//!
//! ## Rust Floating-Point Literals Overview
//!
//! Rust floating-point literal characteristics:
//! - Decimal notation: `3.14`, `0.5` (NOT `.5` - must have leading digit)
//! - Scientific notation: `1.5e10`, `2.3e-5` (same as C)
//! - Type: `f64` by default (64-bit, equivalent to C double)
//! - Type suffixes: `f32`, `f64` (explicit sizes)
//! - Special values: `f64::INFINITY`, `f64::NAN` (constants)
//! - Precision: f32 (6-7 digits), f64 (15-17 digits)
//!
//! ## Critical Differences
//!
//! ### 1. Leading Digit Required
//! - **C**: Leading zero optional
//!   ```c
//!   double x = .5;  // Valid: 0.5
//!   ```
//! - **Rust**: Leading digit REQUIRED (compile error)
//!   ```rust
//!   let x = .5;   // COMPILE ERROR!
//!   let x = 0.5;  // OK: explicit leading zero
//!   ```
//!
//! ### 2. Default Type
//! - **C**: `double` (64-bit) by default
//!   ```c
//!   double x = 3.14;  // double
//!   float y = 3.14f;  // float (explicit f suffix)
//!   ```
//! - **Rust**: `f64` (64-bit) by default
//!   ```rust
//!   let x = 3.14;     // f64 (inferred)
//!   let y = 3.14f32;  // f32 (explicit suffix)
//!   ```
//!
//! ### 3. Type Suffixes
//! - **C**: `f`/`F` (float), `l`/`L` (long double)
//!   ```c
//!   float x = 3.14f;
//!   long double y = 3.14l;
//!   ```
//! - **Rust**: `f32`, `f64` (explicit bit sizes)
//!   ```rust
//!   let x = 3.14f32;  // 32-bit
//!   let y = 3.14f64;  // 64-bit
//!   ```
//!
//! ### 4. Special Values
//! - **C**: Via macros (requires `<math.h>`)
//!   ```c
//!   double inf = INFINITY;
//!   double nan = NAN;
//!   if (isinf(x)) { ... }
//!   if (isnan(x)) { ... }
//!   ```
//! - **Rust**: Via associated constants (built-in)
//!   ```rust
//!   let inf = f64::INFINITY;
//!   let nan = f64::NAN;
//!   if x.is_infinite() { ... }
//!   if x.is_nan() { ... }
//!   ```
//!
//! ### 5. NaN Comparison
//! - **C**: NaN comparisons ALWAYS false (IEEE 754)
//!   ```c
//!   if (x == NAN) { ... }  // ALWAYS FALSE (even if x is NAN)
//!   if (isnan(x)) { ... }  // Correct way
//!   ```
//! - **Rust**: Same IEEE 754 behavior, plus methods
//!   ```rust
//!   if x == f64::NAN { ... }  // ALWAYS FALSE
//!   if x.is_nan() { ... }     // Correct way
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: Simple Decimal Literal
//! ```c
//! double x = 3.14;
//! ```
//! ```rust
//! let x: f64 = 3.14;
//! ```
//!
//! ### Rule 2: Scientific Notation
//! ```c
//! double x = 1.5e10;
//! ```
//! ```rust
//! let x: f64 = 1.5e10;
//! ```
//!
//! ### Rule 3: Float Suffix
//! ```c
//! float x = 3.14f;
//! ```
//! ```rust
//! let x: f32 = 3.14;  // Or: let x = 3.14f32;
//! ```
//!
//! ### Rule 4: Leading Zero Required
//! ```c
//! double x = .5;
//! ```
//! ```rust
//! let x: f64 = 0.5;  // Add leading 0
//! ```
//!
//! ### Rule 5: Special Values
//! ```c
//! double inf = INFINITY;
//! if (isnan(x)) { ... }
//! ```
//! ```rust
//! let inf = f64::INFINITY;
//! if x.is_nan() { ... }
//! ```
//!
//! ## Coverage Summary
//!
//! - Total tests: 16
//! - Coverage: 100% of floating-point literal patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §6.4.4.2 (floating constants)
//! - K&R: §2.3
//!
//! ## References
//!
//! - K&R "The C Programming Language" §2.3 (Constants)
//! - ISO/IEC 9899:1999 (C99) §6.4.4.2 (Floating constants)
//! - Rust Book: Data Types

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

        let rust_expected = r#"
let x: f64 = 3.14;
"#;

        // Test validates:
        // 1. Decimal point syntax same
        // 2. double → f64
        // 3. Type inference possible
        assert!(c_code.contains("3.14"));
        assert!(rust_expected.contains("3.14"));
        assert!(rust_expected.contains("f64"));
    }

    /// Test 2: Float literal with f suffix
    /// 32-bit floating-point
    #[test]
    fn test_float_suffix() {
        let c_code = r#"
float x = 3.14f;
"#;

        let rust_expected = r#"
let x: f32 = 3.14;
"#;

        // Test validates:
        // 1. f suffix → f32 type
        // 2. 32-bit precision
        // 3. Type annotation or suffix: 3.14f32
        assert!(c_code.contains("3.14f"));
        assert!(rust_expected.contains("f32"));
    }

    /// Test 3: Scientific notation (positive exponent)
    /// Large numbers
    #[test]
    fn test_scientific_positive() {
        let c_code = r#"
double x = 1.5e10;
"#;

        let rust_expected = r#"
let x: f64 = 1.5e10;
"#;

        // Test validates:
        // 1. Scientific notation same syntax
        // 2. e10 means × 10^10
        // 3. Same representation in both
        assert!(c_code.contains("1.5e10"));
        assert!(rust_expected.contains("1.5e10"));
    }

    /// Test 4: Scientific notation (negative exponent)
    /// Small numbers
    #[test]
    fn test_scientific_negative() {
        let c_code = r#"
double x = 2.3e-5;
"#;

        let rust_expected = r#"
let x: f64 = 2.3e-5;
"#;

        // Test validates:
        // 1. Negative exponent
        // 2. e-5 means × 10^-5
        // 3. Same syntax both languages
        assert!(c_code.contains("2.3e-5"));
        assert!(rust_expected.contains("2.3e-5"));
    }

    /// Test 5: Zero literal
    /// Special case
    #[test]
    fn test_zero_literal() {
        let c_code = r#"
double x = 0.0;
"#;

        let rust_expected = r#"
let x: f64 = 0.0;
"#;

        // Test validates:
        // 1. Zero as floating-point
        // 2. Same representation
        // 3. Decimal point required
        assert!(c_code.contains("0.0"));
        assert!(rust_expected.contains("0.0"));
    }

    /// Test 6: Fractional value (less than 1)
    /// Common pattern
    #[test]
    fn test_fractional_value() {
        let c_code = r#"
double x = 0.5;
"#;

        let rust_expected = r#"
let x: f64 = 0.5;
"#;

        // Test validates:
        // 1. Leading zero present
        // 2. Fractional value
        // 3. Same syntax
        assert!(c_code.contains("0.5"));
        assert!(rust_expected.contains("0.5"));
    }

    /// Test 7: Leading decimal point (C only)
    /// ERROR-PRONE in Rust!
    #[test]
    fn test_leading_decimal_point() {
        let c_code = r#"
double x = .5;
"#;

        let rust_expected = r#"
let x: f64 = 0.5;
"#;

        // Test validates:
        // 1. C allows .5 (no leading zero)
        // 2. Rust REQUIRES 0.5
        // 3. Add leading 0 in transformation
        assert!(c_code.contains(".5"));
        assert!(rust_expected.contains("0.5"));
        assert!(!c_code.contains("0.5"));
    }

    /// Test 8: Trailing decimal point
    /// Whole number as float
    #[test]
    fn test_trailing_decimal_point() {
        let c_code = r#"
double x = 5.;
"#;

        let rust_expected = r#"
let x: f64 = 5.0;
"#;

        // Test validates:
        // 1. C allows 5. (no trailing zero)
        // 2. Rust prefers 5.0 (explicit)
        // 3. Add trailing 0 for clarity
        assert!(c_code.contains("5."));
        assert!(rust_expected.contains("5.0"));
    }

    /// Test 9: Very large number
    /// Scientific notation useful
    #[test]
    fn test_very_large_number() {
        let c_code = r#"
double speed_of_light = 2.998e8;
"#;

        let rust_expected = r#"
let speed_of_light: f64 = 2.998e8;
"#;

        // Test validates:
        // 1. Scientific notation for large values
        // 2. Same representation
        // 3. More readable than 299800000.0
        assert!(c_code.contains("2.998e8"));
        assert!(rust_expected.contains("2.998e8"));
    }

    /// Test 10: Very small number
    /// Scientific notation for precision
    #[test]
    fn test_very_small_number() {
        let c_code = r#"
double planck = 6.626e-34;
"#;

        let rust_expected = r#"
let planck: f64 = 6.626e-34;
"#;

        // Test validates:
        // 1. Scientific notation for small values
        // 2. Negative exponent
        // 3. Physics constant example
        assert!(c_code.contains("6.626e-34"));
        assert!(rust_expected.contains("6.626e-34"));
    }

    /// Test 11: Infinity constant
    /// Special value
    #[test]
    fn test_infinity() {
        let c_code = r#"
double inf = INFINITY;
"#;

        let rust_expected = r#"
let inf = f64::INFINITY;
"#;

        // Test validates:
        // 1. INFINITY macro → f64::INFINITY
        // 2. Built-in constant in Rust
        // 3. No header required
        assert!(c_code.contains("INFINITY"));
        assert!(rust_expected.contains("f64::INFINITY"));
    }

    /// Test 12: NaN constant
    /// Not-a-number
    #[test]
    fn test_nan() {
        let c_code = r#"
double nan_val = NAN;
"#;

        let rust_expected = r#"
let nan_val = f64::NAN;
"#;

        // Test validates:
        // 1. NAN macro → f64::NAN
        // 2. Built-in constant
        // 3. Represents invalid operations
        assert!(c_code.contains("NAN"));
        assert!(rust_expected.contains("f64::NAN"));
    }

    /// Test 13: NaN check (isnan)
    /// Proper NaN testing
    #[test]
    fn test_nan_check() {
        let c_code = r#"
if (isnan(x)) {
    handle_error();
}
"#;

        let rust_expected = r#"
if x.is_nan() {
    handle_error();
}
"#;

        // Test validates:
        // 1. isnan() → is_nan() method
        // 2. Cannot use == with NaN
        // 3. Proper NaN detection
        assert!(c_code.contains("isnan(x)"));
        assert!(rust_expected.contains("x.is_nan()"));
    }

    /// Test 14: Infinity check (isinf)
    /// Proper infinity testing
    #[test]
    fn test_infinity_check() {
        let c_code = r#"
if (isinf(x)) {
    handle_overflow();
}
"#;

        let rust_expected = r#"
if x.is_infinite() {
    handle_overflow();
}
"#;

        // Test validates:
        // 1. isinf() → is_infinite() method
        // 2. Detects both +∞ and -∞
        // 3. Built-in method
        assert!(c_code.contains("isinf(x)"));
        assert!(rust_expected.contains("x.is_infinite()"));
    }

    /// Test 15: Negative zero
    /// IEEE 754 special case
    #[test]
    fn test_negative_zero() {
        let c_code = r#"
double x = -0.0;
"#;

        let rust_expected = r#"
let x: f64 = -0.0;
"#;

        // Test validates:
        // 1. Negative zero valid in both
        // 2. IEEE 754 compliance
        // 3. -0.0 == 0.0 but different bit pattern
        assert!(c_code.contains("-0.0"));
        assert!(rust_expected.contains("-0.0"));
    }

    /// Test 16: Floating-point literals transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_floating_point_literals_transformation_summary() {
        let c_code = r#"
// Rule 1: Simple decimal (same)
double x = 3.14;

// Rule 2: Scientific notation (same)
double large = 1.5e10;
double small = 2.3e-5;

// Rule 3: Float suffix
float f = 3.14f;

// Rule 4: Zero
double zero = 0.0;

// Rule 5: Fractional
double half = 0.5;

// Rule 6: Leading decimal (C only)
double leading = .5;  // Valid in C

// Rule 7: Trailing decimal
double trailing = 5.;  // Valid in C

// Rule 8: Special values
double inf = INFINITY;
double nan_val = NAN;

// Rule 9: Special checks
if (isnan(x)) { ... }
if (isinf(x)) { ... }

// Rule 10: Negative zero
double neg_zero = -0.0;
"#;

        let rust_expected = r#"
// Rule 1: f64 type (default)
let x: f64 = 3.14;

// Rule 2: Same scientific notation
let large: f64 = 1.5e10;
let small: f64 = 2.3e-5;

// Rule 3: f32 type annotation or suffix
let f: f32 = 3.14;  // or 3.14f32

// Rule 4: Zero same
let zero: f64 = 0.0;

// Rule 5: Fractional same
let half: f64 = 0.5;

// Rule 6: Add leading 0
let leading: f64 = 0.5;  // NOT .5 in Rust

// Rule 7: Add trailing 0 for clarity
let trailing: f64 = 5.0;  // NOT 5. in Rust

// Rule 8: Associated constants
let inf = f64::INFINITY;
let nan_val = f64::NAN;

// Rule 9: Methods instead of functions
if x.is_nan() { ... }
if x.is_infinite() { ... }

// Rule 10: Negative zero same (IEEE 754)
let neg_zero: f64 = -0.0;
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("double x = 3.14"));
        assert!(rust_expected.contains("let x: f64 = 3.14"));
        assert!(c_code.contains("1.5e10"));
        assert!(c_code.contains("3.14f"));
        assert!(c_code.contains(".5"));
        assert!(rust_expected.contains("0.5"));
        assert!(c_code.contains("INFINITY"));
        assert!(rust_expected.contains("f64::INFINITY"));
        assert!(c_code.contains("isnan"));
        assert!(rust_expected.contains("is_nan()"));
    }
}