#![cfg(any(
not(any(
feature = "parser_tests",
feature = "analyzer_tests",
feature = "codegen_tests",
feature = "interpreter_tests",
feature = "conformance_tests",
feature = "integration_tests",
)),
feature = "analyzer_tests",
))]
#[path = "common/test_utils.rs"]
mod test_utils;
fn cast_ident_to_f32(generated: &str, ident: &str) -> bool {
generated.contains(&format!("{ident} as f32"))
}
fn is_e0277_codegen_error(stderr: &str) -> bool {
stderr.contains("E0277")
&& (stderr.contains("cannot add")
|| stderr.contains("cannot subtract")
|| stderr.contains("cannot multiply")
|| stderr.contains("cannot divide")
|| stderr.contains("no implementation for"))
}
#[test]
fn test_f32_mod_i32() {
let source = r#"
pub fn wrap(value: f32, count: i32) -> f32 {
value % count
}
"#;
let output = test_utils::compile_single(source);
assert!(
cast_ident_to_f32(&output, "count"),
"f32 % i32 should cast count to f32. Got:\n{}",
output
);
let __result = test_utils::verify_rust_compiles(&output);
let rustc_ok = __result.is_ok();
let stderr = __result.err().unwrap_or_default();
if !rustc_ok && is_e0277_codegen_error(&stderr) {
panic!(
"Should compile (E0277 mixed arithmetic). stderr: {}\n\nGenerated:\n{}",
stderr, output
);
}
}
#[test]
fn test_f32_add_i32() {
let source = r#"
pub fn add(value: f32, count: i32) -> f32 {
value + count
}
"#;
let output = test_utils::compile_single(source);
assert!(
cast_ident_to_f32(&output, "count"),
"f32 + i32 should cast count to f32. Got:\n{}",
output
);
let __result = test_utils::verify_rust_compiles(&output);
let rustc_ok = __result.is_ok();
let stderr = __result.err().unwrap_or_default();
if !rustc_ok && is_e0277_codegen_error(&stderr) {
panic!(
"Should compile (E0277 mixed arithmetic). stderr: {}\n\nGenerated:\n{}",
stderr, output
);
}
}
#[test]
fn test_f32_multiply_i32() {
let source = r#"
pub fn scale(value: f32, count: i32) -> f32 {
value * count
}
"#;
let output = test_utils::compile_single(source);
assert!(
cast_ident_to_f32(&output, "count"),
"f32 * i32 should cast count to f32. Got:\n{}",
output
);
let __result = test_utils::verify_rust_compiles(&output);
let rustc_ok = __result.is_ok();
let stderr = __result.err().unwrap_or_default();
if !rustc_ok && is_e0277_codegen_error(&stderr) {
panic!(
"Should compile (E0277 mixed arithmetic). stderr: {}\n\nGenerated:\n{}",
stderr, output
);
}
}
#[test]
fn test_int_mod_int_then_cast_not_operands() {
let source = r#"
pub struct Tile {
pub sprite_index: i32,
}
pub fn uv_coord(tile: Tile, tiles_per_row: i32) -> f32 {
(tile.sprite_index % tiles_per_row) as f32 / tiles_per_row as f32
}
"#;
let output = test_utils::compile_single(source);
assert!(
output.contains("sprite_index % tiles_per_row) as f32"),
"Should cast result of int%%int: (sprite_index % tiles_per_row) as f32. Got:\n{}",
output
);
assert!(
!output.contains("sprite_index) as f32 % tiles_per_row"),
"Must NOT cast left operand of int%%int (would produce f32%%i32). Got:\n{}",
output
);
let __result = test_utils::verify_rust_compiles(&output);
let rustc_ok = __result.is_ok();
let stderr = __result.err().unwrap_or_default();
if !rustc_ok && is_e0277_codegen_error(&stderr) {
panic!(
"Should compile (E0277 mixed arithmetic). stderr: {}\n\nGenerated:\n{}",
stderr, output
);
}
}
#[test]
fn test_f32_divide_i32() {
let source = r#"
pub fn divide(value: f32, count: i32) -> f32 {
value / count
}
"#;
let output = test_utils::compile_single(source);
assert!(
cast_ident_to_f32(&output, "count"),
"f32 / i32 should cast count to f32. Got:\n{}",
output
);
let __result = test_utils::verify_rust_compiles(&output);
let rustc_ok = __result.is_ok();
let stderr = __result.err().unwrap_or_default();
if !rustc_ok && is_e0277_codegen_error(&stderr) {
panic!(
"Should compile (E0277 mixed arithmetic). stderr: {}\n\nGenerated:\n{}",
stderr, output
);
}
}
#[test]
fn test_f32_subtract_i32() {
let source = r#"
pub fn subtract(value: f32, count: i32) -> f32 {
value - count
}
"#;
let output = test_utils::compile_single(source);
assert!(
cast_ident_to_f32(&output, "count"),
"f32 - i32 should cast count to f32. Got:\n{}",
output
);
let __result = test_utils::verify_rust_compiles(&output);
let rustc_ok = __result.is_ok();
let stderr = __result.err().unwrap_or_default();
if !rustc_ok && is_e0277_codegen_error(&stderr) {
panic!(
"Should compile (E0277 mixed arithmetic). stderr: {}\n\nGenerated:\n{}",
stderr, output
);
}
}