use crate::{HirExpression, HirType};
#[test]
fn test_slice_index_creation() {
let slice_index = HirExpression::SliceIndex {
slice: Box::new(HirExpression::Variable("arr".to_string())),
index: Box::new(HirExpression::IntLiteral(5)),
element_type: HirType::Int,
};
if let HirExpression::SliceIndex { slice, index, element_type } = slice_index {
assert!(matches!(*slice, HirExpression::Variable(_)));
assert!(matches!(*index, HirExpression::IntLiteral(5)));
assert_eq!(element_type, HirType::Int);
} else {
panic!("Expected SliceIndex variant");
}
}
#[test]
fn test_slice_index_debug_format() {
let slice_index = HirExpression::SliceIndex {
slice: Box::new(HirExpression::Variable("arr".to_string())),
index: Box::new(HirExpression::Variable("i".to_string())),
element_type: HirType::Int,
};
let debug_str = format!("{:?}", slice_index);
assert!(debug_str.contains("SliceIndex"), "Debug output should mention SliceIndex");
assert!(
debug_str.contains("arr") || debug_str.contains("Variable"),
"Debug output should show slice variable"
);
}
#[test]
fn test_slice_index_distinguishes_from_array_index() {
let slice_index = HirExpression::SliceIndex {
slice: Box::new(HirExpression::Variable("arr".to_string())),
index: Box::new(HirExpression::IntLiteral(0)),
element_type: HirType::Int,
};
let array_index = HirExpression::ArrayIndex {
array: Box::new(HirExpression::Variable("arr".to_string())),
index: Box::new(HirExpression::IntLiteral(0)),
};
assert!(
!matches!(slice_index, HirExpression::ArrayIndex { .. }),
"SliceIndex should not be ArrayIndex"
);
assert!(
!matches!(array_index, HirExpression::SliceIndex { .. }),
"ArrayIndex should not be SliceIndex"
);
}
#[test]
fn test_slice_index_with_expression_index() {
let slice_index = HirExpression::SliceIndex {
slice: Box::new(HirExpression::Variable("arr".to_string())),
index: Box::new(HirExpression::BinaryOp {
op: crate::BinaryOperator::Add,
left: Box::new(HirExpression::Variable("i".to_string())),
right: Box::new(HirExpression::IntLiteral(1)),
}),
element_type: HirType::Int,
};
if let HirExpression::SliceIndex { index, .. } = slice_index {
assert!(matches!(*index, HirExpression::BinaryOp { .. }), "Index should be BinaryOp");
} else {
panic!("Expected SliceIndex");
}
}
#[test]
fn test_slice_index_element_type_preserved() {
let int_slice = HirExpression::SliceIndex {
slice: Box::new(HirExpression::Variable("int_arr".to_string())),
index: Box::new(HirExpression::IntLiteral(0)),
element_type: HirType::Int,
};
let float_slice = HirExpression::SliceIndex {
slice: Box::new(HirExpression::Variable("float_arr".to_string())),
index: Box::new(HirExpression::IntLiteral(0)),
element_type: HirType::Float,
};
if let HirExpression::SliceIndex { element_type, .. } = int_slice {
assert_eq!(element_type, HirType::Int);
}
if let HirExpression::SliceIndex { element_type, .. } = float_slice {
assert_eq!(element_type, HirType::Float);
}
}