depyler-core 3.23.0

Core transpilation engine for the Depyler Python-to-Rust transpiler
Documentation
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
// Generated by: DEPYLER stdlib validation Phase 1
// Module: math - Python math module validation
// Status: RED phase - Tests written first, implementation pending

use depyler_core::transpile_python_to_rust;

// DEPYLER-STDLIB-MATH-001: Basic trigonometric functions
#[test]
fn test_math_sin() {
    let python = r#"
import math

def calculate_sin(x: float) -> float:
    return math.sin(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");

    // Verify the Rust code compiles
    assert!(result.contains("fn calculate_sin"));
    assert!(result.contains("x.sin()") || result.contains("f64::sin"));

    // Should compile and run correctly
    let output = std::process::Command::new("rustc")
        .arg("--crate-type")
        .arg("lib")
        .arg("--deny")
        .arg("warnings")
        .arg("-")
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .and_then(|mut child| {
            use std::io::Write;
            child.stdin.as_mut().unwrap().write_all(result.as_bytes())?;
            child.wait_with_output()
        });

    assert!(output.is_ok(), "Generated Rust code should compile");
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_cos() {
    let python = r#"
import math

def calculate_cos(x: float) -> float:
    return math.cos(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.cos()") || result.contains("f64::cos"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_tan() {
    let python = r#"
import math

def calculate_tan(x: float) -> float:
    return math.tan(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.tan()") || result.contains("f64::tan"));
}

// DEPYLER-STDLIB-MATH-002: Square root and power functions
#[test]
fn test_math_sqrt() {
    let python = r#"
import math

def calculate_sqrt(x: float) -> float:
    return math.sqrt(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.sqrt()") || result.contains("f64::sqrt"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_pow() {
    let python = r#"
import math

def calculate_pow(x: float, y: float) -> float:
    return math.pow(x, y)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("powf") || result.contains("powi"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_exp() {
    let python = r#"
import math

def calculate_exp(x: float) -> float:
    return math.exp(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.exp()") || result.contains("f64::exp"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_log() {
    let python = r#"
import math

def calculate_log(x: float) -> float:
    return math.log(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.ln()") || result.contains("f64::ln"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_log10() {
    let python = r#"
import math

def calculate_log10(x: float) -> float:
    return math.log10(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.log10()") || result.contains("f64::log10"));
}

// DEPYLER-STDLIB-MATH-003: Ceiling and floor functions
#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_ceil() {
    let python = r#"
import math

def calculate_ceil(x: float) -> int:
    return math.ceil(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.ceil()") || result.contains("f64::ceil"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_floor() {
    let python = r#"
import math

def calculate_floor(x: float) -> int:
    return math.floor(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.floor()") || result.contains("f64::floor"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_trunc() {
    let python = r#"
import math

def calculate_trunc(x: float) -> int:
    return math.trunc(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.trunc()") || result.contains("f64::trunc"));
}

// DEPYLER-STDLIB-MATH-004: Mathematical constants
#[test]
fn test_math_pi() {
    let python = r#"
import math

def get_pi() -> float:
    return math.pi
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("std::f64::consts::PI") || result.contains("PI"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_e() {
    let python = r#"
import math

def get_e() -> float:
    return math.e
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("std::f64::consts::E") || result.contains("::E"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_tau() {
    let python = r#"
import math

def get_tau() -> float:
    return math.tau
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("std::f64::consts::TAU") || result.contains("TAU"));
}

// DEPYLER-STDLIB-MATH-005: Hyperbolic functions
#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_sinh() {
    let python = r#"
import math

def calculate_sinh(x: float) -> float:
    return math.sinh(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.sinh()") || result.contains("f64::sinh"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_cosh() {
    let python = r#"
import math

def calculate_cosh(x: float) -> float:
    return math.cosh(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.cosh()") || result.contains("f64::cosh"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_tanh() {
    let python = r#"
import math

def calculate_tanh(x: float) -> float:
    return math.tanh(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.tanh()") || result.contains("f64::tanh"));
}

// DEPYLER-STDLIB-MATH-006: Absolute value and sign
#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_fabs() {
    let python = r#"
import math

def calculate_fabs(x: float) -> float:
    return math.fabs(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.abs()") || result.contains("f64::abs"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_copysign() {
    let python = r#"
import math

def calculate_copysign(x: float, y: float) -> float:
    return math.copysign(x, y)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("copysign"));
}

// DEPYLER-STDLIB-MATH-007: Factorial and combinations
#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_factorial() {
    let python = r#"
import math

def calculate_factorial(n: int) -> int:
    return math.factorial(n)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("factorial"));
}

// DEPYLER-STDLIB-MATH-008: Degrees and radians conversion
#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_degrees() {
    let python = r#"
import math

def convert_to_degrees(x: float) -> float:
    return math.degrees(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("to_degrees()") || result.contains("degrees"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_radians() {
    let python = r#"
import math

def convert_to_radians(x: float) -> float:
    return math.radians(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("to_radians()") || result.contains("radians"));
}

// DEPYLER-STDLIB-MATH-009: GCD and LCM
#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_gcd() {
    let python = r#"
import math

def calculate_gcd(a: int, b: int) -> int:
    return math.gcd(a, b)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("gcd") || result.contains("num::integer"));
}

// DEPYLER-STDLIB-MATH-010: Special values
#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_inf() {
    let python = r#"
import math

def get_inf() -> float:
    return math.inf
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("f64::INFINITY") || result.contains("INFINITY"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_nan() {
    let python = r#"
import math

def get_nan() -> float:
    return math.nan
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("f64::NAN") || result.contains("::NAN"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_isnan() {
    let python = r#"
import math

def check_nan(x: float) -> bool:
    return math.isnan(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.is_nan()") || result.contains("f64::is_nan"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_isinf() {
    let python = r#"
import math

def check_inf(x: float) -> bool:
    return math.isinf(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.is_infinite()") || result.contains("f64::is_infinite"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_isfinite() {
    let python = r#"
import math

def check_finite(x: float) -> bool:
    return math.isfinite(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.is_finite()") || result.contains("f64::is_finite"));
}

// DEPYLER-STDLIB-MATH-011: Complex usage patterns
#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_combined_operations() {
    let python = r#"
import math

def calculate_distance(x1: float, y1: float, x2: float, y2: float) -> float:
    dx = x2 - x1
    dy = y2 - y1
    return math.sqrt(dx * dx + dy * dy)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("sqrt"));

    // Verify compilation
    let output = std::process::Command::new("rustc")
        .arg("--crate-type")
        .arg("lib")
        .arg("--deny")
        .arg("warnings")
        .arg("-")
        .stdin(std::process::Stdio::piped())
        .spawn()
        .and_then(|mut child| {
            use std::io::Write;
            child.stdin.as_mut().unwrap().write_all(result.as_bytes()).ok();
            child.wait_with_output()
        });

    if let Ok(output) = output {
        assert!(output.status.success(), "Generated Rust code should compile without errors");
    }
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_circle_area() {
    let python = r#"
import math

def circle_area(radius: float) -> float:
    return math.pi * radius * radius
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("PI"));
}

// DEPYLER-STDLIB-MATH-012: Inverse trigonometric functions
#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_asin() {
    let python = r#"
import math

def calculate_asin(x: float) -> float:
    return math.asin(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.asin()") || result.contains("f64::asin"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_acos() {
    let python = r#"
import math

def calculate_acos(x: float) -> float:
    return math.acos(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.acos()") || result.contains("f64::acos"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_atan() {
    let python = r#"
import math

def calculate_atan(x: float) -> float:
    return math.atan(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("x.atan()") || result.contains("f64::atan"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_atan2() {
    let python = r#"
import math

def calculate_atan2(y: float, x: float) -> float:
    return math.atan2(y, x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("atan2"));
}

// DEPYLER-STDLIB-MATH-013: Additional functions
#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_ldexp() {
    let python = r#"
import math

def calculate_ldexp(x: float, i: int) -> float:
    return math.ldexp(x, i)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("ldexp"));
}

#[test]
#[ignore = "DEPYLER-STDLIB-MATH: Not implemented yet - RED phase"]
fn test_math_frexp() {
    let python = r#"
import math

def calculate_frexp(x: float) -> tuple[float, int]:
    return math.frexp(x)
"#;

    let result = transpile_python_to_rust(python).expect("Transpilation failed");
    assert!(result.contains("frexp"));
}

// Total: 33 comprehensive tests for math module
// Coverage: Basic trig, inverse trig, hyperbolic, power, log, ceiling/floor,
//           constants, special values, type checking, and complex patterns