use super::*;
#[test]
fn test_create_array_index_expression() {
let array_expr = HirExpression::Variable("arr".to_string());
let index_expr = HirExpression::Variable("i".to_string());
let array_index =
HirExpression::ArrayIndex { array: Box::new(array_expr), index: Box::new(index_expr) };
match array_index {
HirExpression::ArrayIndex { array, index } => match (*array, *index) {
(HirExpression::Variable(arr_name), HirExpression::Variable(idx_name)) => {
assert_eq!(arr_name, "arr");
assert_eq!(idx_name, "i");
}
_ => panic!("Expected Variable expressions"),
},
_ => panic!("Expected ArrayIndex expression"),
}
}
#[test]
fn test_array_index_with_literal() {
let array_expr = HirExpression::Variable("arr".to_string());
let index_expr = HirExpression::IntLiteral(0);
let array_index =
HirExpression::ArrayIndex { array: Box::new(array_expr), index: Box::new(index_expr) };
match array_index {
HirExpression::ArrayIndex { array, index } => {
assert!(matches!(*array, HirExpression::Variable(_)));
assert_eq!(*index, HirExpression::IntLiteral(0));
}
_ => panic!("Expected ArrayIndex expression"),
}
}
#[test]
fn test_nested_array_index() {
let inner_array = HirExpression::Variable("matrix".to_string());
let i_index = HirExpression::Variable("i".to_string());
let first_index =
HirExpression::ArrayIndex { array: Box::new(inner_array), index: Box::new(i_index) };
let j_index = HirExpression::Variable("j".to_string());
let nested_index =
HirExpression::ArrayIndex { array: Box::new(first_index), index: Box::new(j_index) };
match nested_index {
HirExpression::ArrayIndex { array, .. } => {
assert!(matches!(*array, HirExpression::ArrayIndex { .. }));
}
_ => panic!("Expected nested ArrayIndex"),
}
}
#[test]
fn test_array_index_with_expression() {
let array_expr = HirExpression::Variable("arr".to_string());
let index_expr = HirExpression::BinaryOp {
op: BinaryOperator::Add,
left: Box::new(HirExpression::Variable("i".to_string())),
right: Box::new(HirExpression::IntLiteral(1)),
};
let array_index =
HirExpression::ArrayIndex { array: Box::new(array_expr), index: Box::new(index_expr) };
match array_index {
HirExpression::ArrayIndex { index, .. } => {
assert!(matches!(*index, HirExpression::BinaryOp { .. }));
}
_ => panic!("Expected ArrayIndex with BinaryOp"),
}
}
#[test]
fn test_array_index_in_assignment() {
let target_expr = HirExpression::ArrayIndex {
array: Box::new(HirExpression::Variable("arr".to_string())),
index: Box::new(HirExpression::Variable("i".to_string())),
};
assert!(matches!(target_expr, HirExpression::ArrayIndex { .. }));
}
#[test]
fn test_pointer_array_index() {
let array_expr = HirExpression::Variable("ptr".to_string());
let index_expr = HirExpression::Variable("i".to_string());
let array_index =
HirExpression::ArrayIndex { array: Box::new(array_expr), index: Box::new(index_expr) };
assert!(matches!(array_index, HirExpression::ArrayIndex { .. }));
}
#[test]
fn test_array_type_declaration() {
let array_type = HirType::Array { element_type: Box::new(HirType::Int), size: Some(10) };
match array_type {
HirType::Array { element_type, size } => {
assert_eq!(*element_type, HirType::Int);
assert_eq!(size, Some(10));
}
_ => panic!("Expected Array type"),
}
}
#[test]
fn test_unsized_array_type() {
let array_type = HirType::Array { element_type: Box::new(HirType::Int), size: None };
match array_type {
HirType::Array { element_type, size } => {
assert_eq!(*element_type, HirType::Int);
assert_eq!(size, None);
}
_ => panic!("Expected Array type"),
}
}