Skip to content

Commit

Permalink
tests: Update IR tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkajetanp committed Aug 12, 2024
1 parent 88f0cc5 commit be9ba48
Showing 1 changed file with 57 additions and 48 deletions.
105 changes: 57 additions & 48 deletions src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,54 +371,63 @@ impl fmt::Display for Identifier {
mod tests {
use super::*;

// #[test]
// fn ir_program() {
// let mut ctx = IrCtx::new();

// let ast_program = ast::Program {
// body: ast::Function {
// name: "main".to_owned(),
// return_type: "Int".to_owned(),
// body: todo!(), // ast::Statement::Return(ast::Expression::Unary(
// // ast::UnaryOperator::Negation,
// // Box::new(ast::Expression::Constant(5)),
// },
// };

// let expected = Program {
// body: Function::generate(ast_program.body.clone(), &mut ctx),
// };
// let mut ctx = IrCtx::new();
// let actual = Program::generate(ast_program, &mut ctx);
// assert_eq!(actual, expected);
// }

// #[test]
// fn ir_function() {
// let mut ctx = IrCtx::new();

// let stmt = ast::Statement::Return(ast::Expression::Unary(
// ast::UnaryOperator::Negation,
// Box::new(ast::Expression::Constant(5)),
// ));

// let ast_fn = ast::Function {
// name: "main".to_owned(),
// return_type: "Int".to_owned(),
// body: stmt.clone(),
// };

// let expected = Function {
// name: "main".to_owned(),
// return_type: "Int".to_owned(),
// instructions: Instruction::generate_from_statement(stmt, &mut ctx),
// };

// let mut ctx = IrCtx::new();
// let actual = Function::generate(ast_fn, &mut ctx);

// assert_eq!(actual, expected);
// }
#[test]
fn ir_program() {
let mut ctx = IrCtx::new();

let ast_program = ast::Program {
body: ast::Function {
name: ast::Identifier::new("main"),
return_type: "Int".to_owned(),
body: vec![ast::BlockItem::Stmt(ast::Statement::Return(
ast::Expression::Unary(
ast::UnaryOperator::Negation,
Box::new(ast::Expression::Constant(5)),
),
))],
},
};

let expected = Program {
body: Function::generate(ast_program.body.clone(), &mut ctx),
};
let mut ctx = IrCtx::new();
let actual = Program::generate(ast_program, &mut ctx);
assert_eq!(actual, expected);
}

#[test]
fn ir_function() {
let mut ctx = IrCtx::new();

let stmt = ast::Statement::Return(ast::Expression::Unary(
ast::UnaryOperator::Negation,
Box::new(ast::Expression::Constant(5)),
));

let ast_fn = ast::Function {
name: ast::Identifier::new("main"),
return_type: "Int".to_owned(),
body: vec![ast::BlockItem::Stmt(stmt.clone())],
};

let expected = Function {
name: Identifier::new("main"),
return_type: "Int".to_owned(),
instructions: vec![
Instruction::generate_from_statement(stmt, &mut ctx),
vec![Instruction::Return(Val::Constant(0))],
]
.into_iter()
.flatten()
.collect(),
};

let mut ctx = IrCtx::new();
let actual = Function::generate(ast_fn, &mut ctx);

assert_eq!(actual, expected);
}

#[test]
fn ir_instruction_unary() {
Expand Down

0 comments on commit be9ba48

Please sign in to comment.