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
//! DECY-131: Integer in boolean context tests.
//!
//! In C, any non-zero value is truthy in boolean context (&&, ||, if).
//! In Rust, we need explicit conversion: `x != 0` for i32 → bool.
//!
//! C: if (x && y) where x, y are int
//! Expected Rust: if x != 0 && y != 0
use decy_core::transpile;
/// Test that struct fields in && context get boolean conversion.
///
/// C: if (f.active && f.ready) where active, ready are int
/// Expected: if f.active != 0 && f.ready != 0
#[test]
fn test_int_fields_in_logical_and_context() {
let c_code = r#"
struct Flags {
int active;
int ready;
};
int check(struct Flags f) {
if (f.active && f.ready) {
return 1;
}
return 0;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Should convert int to bool for && operator
// Either explicit != 0 or generate bool type
assert!(
result.contains("!= 0") || result.contains(": bool"),
"Should convert int to bool in && context\nGenerated:\n{}",
result
);
}
/// Test that int variables in if condition get boolean conversion.
///
/// C: if (x) where x is int
/// Expected: if x != 0
#[test]
fn test_int_variable_in_if_condition() {
let c_code = r#"
int is_nonzero(int x) {
if (x) {
return 1;
}
return 0;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Should convert int to bool for if condition
assert!(
result.contains("!= 0") || result.contains("x != 0") || result.contains("x as bool"),
"Should convert int to bool in if condition\nGenerated:\n{}",
result
);
}
/// Test logical NOT on integer.
///
/// C: if (!error) where error is int
/// Expected: if error == 0
#[test]
fn test_logical_not_on_int() {
let c_code = r#"
int check_no_error(int error) {
if (!error) {
return 1;
}
return 0;
}
"#;
let result = transpile(c_code).expect("Transpilation should succeed");
println!("Generated Rust code:\n{}", result);
// Logical NOT on int should become == 0
assert!(
result.contains("== 0") || result.contains("error == 0"),
"Should convert !int to int == 0\nGenerated:\n{}",
result
);
}