Skip to content

Commit

Permalink
Add tests for optimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sellig6792 committed Dec 27, 2022
1 parent d49c490 commit d0f01cd
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ mod patterns;
pub use instruction_types::InstructionType;
pub use instructions::{Instruction, InstructionTrait};
pub use parser::Parser;
pub use patterns::pattern_structs;
pub use patterns::{Pattern, PatternType};
8 changes: 5 additions & 3 deletions src/ast/patterns.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
mod set_to_zero;

use crate::ast::{Instruction, InstructionTrait};
use crate::ast::InstructionTrait;
use crate::optimization::optimized_instructions::OptimizedInstruction;

pub mod pattern_structs {
pub use super::set_to_zero::SetToZero;
}

pub trait Pattern<T>
where
T: InstructionTrait<T>,
Expand Down Expand Up @@ -32,5 +36,3 @@ impl PatternType {
self.get_pattern().replace(instructions)
}
}


5 changes: 4 additions & 1 deletion src/ast/patterns/set_to_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ where
// Remove the instructions that were replaced
for instruction in &mut instructions {
if self.match_pattern(&vec![instruction.clone()]) {
let new_instruction = T::new(InstructionType::Pattern(PatternType::SetToZero(SetToZero {})), None);
let new_instruction = T::new(
InstructionType::Pattern(PatternType::SetToZero(SetToZero {})),
None,
);
*instruction = new_instruction;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/evaluation/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::instructions::Instruction;
use crate::ast::Instruction;

#[test]
fn test_move_right() {
Expand Down
163 changes: 161 additions & 2 deletions src/optimization/optimizer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ast::{Instruction, InstructionTrait, InstructionType, Pattern, PatternType};
use crate::ast::{Instruction, InstructionTrait, InstructionType, PatternType};

use crate::optimization::optimized_instructions::OptimizedInstruction;

Expand Down Expand Up @@ -130,7 +130,8 @@ impl Optimizer {
}

fn recognize_patterns(&self, optimized_instructions: &mut Vec<OptimizedInstruction>) -> () {
let mut new_optimized_instructions: Vec<OptimizedInstruction> = optimized_instructions.clone();
let mut new_optimized_instructions: Vec<OptimizedInstruction> =
optimized_instructions.clone();

let patterns = PatternType::iter();

Expand All @@ -141,3 +142,161 @@ impl Optimizer {
*optimized_instructions = new_optimized_instructions;
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::ast::pattern_structs;
use crate::ast::{InstructionType, PatternType};

#[test]
fn test_merge_instructions() {
let instructions = vec![
Instruction::new(InstructionType::Increment, None),
Instruction::new(InstructionType::Increment, None),
Instruction::new(InstructionType::Increment, None),
Instruction::new(InstructionType::MoveLeft, None),
Instruction::new(InstructionType::MoveLeft, None),
Instruction::new(InstructionType::Decrement, None),
];
let mut optimizer = Optimizer::new(instructions);
let optimized_instructions = optimizer.optimize();

assert_eq!(optimized_instructions.len(), 3);
assert_eq!(optimized_instructions[0].get_amount(), 3);
assert_eq!(optimized_instructions[1].get_amount(), 2);
assert_eq!(optimized_instructions[2].get_amount(), 1);
}

#[test]
fn test_cancel_opposed_instructions_same_amount() {
let instructions = vec![
Instruction::new(InstructionType::Increment, None),
Instruction::new(InstructionType::Decrement, None),
];
let mut optimizer = Optimizer::new(instructions);
let optimized_instructions = optimizer.optimize();

assert_eq!(optimized_instructions.len(), 0);
}

#[test]
fn test_cancel_opposed_instructions_left_bigger() {
let instructions = vec![
Instruction::new(InstructionType::Increment, None),
Instruction::new(InstructionType::Increment, None),
Instruction::new(InstructionType::Decrement, None),
];
let mut optimizer = Optimizer::new(instructions);
let optimized_instructions = optimizer.optimize();

assert_eq!(optimized_instructions.len(), 1);
assert_eq!(
optimized_instructions[0].get_instruction_type(),
InstructionType::Increment
);
assert_eq!(optimized_instructions[0].get_amount(), 1);
}

#[test]
fn test_cancel_opposed_instructions_right_bigger() {
let instructions = vec![
Instruction::new(InstructionType::Increment, None),
Instruction::new(InstructionType::Decrement, None),
Instruction::new(InstructionType::Decrement, None),
];
let mut optimizer = Optimizer::new(instructions);
let optimized_instructions = optimizer.optimize();

assert_eq!(optimized_instructions.len(), 1);
assert_eq!(
optimized_instructions[0].get_instruction_type(),
InstructionType::Decrement
);
assert_eq!(optimized_instructions[0].get_amount(), 1);
}

#[test]
fn test_recognize_pattern_set_to_zero_with_decrement() {
for i in (1..5).step_by(2) {
let mut loop_content = vec![];
for _ in 0..i {
loop_content.push(Instruction::new(InstructionType::Decrement, None));
}

let instructions = vec![Instruction::new(InstructionType::Loop, Some(loop_content))];

let mut optimizer = Optimizer::new(instructions);
let optimized_instructions = optimizer.optimize();

assert_eq!(optimized_instructions.len(), 1);
assert_eq!(
optimized_instructions[0].get_instruction_type(),
InstructionType::Pattern(PatternType::SetToZero(pattern_structs::SetToZero {}))
);
}
}

#[test]
fn test_recognize_pattern_set_to_zero_with_increment() {
for i in (1..5).step_by(2) {
let mut loop_content = vec![];
for _ in 0..i {
loop_content.push(Instruction::new(InstructionType::Increment, None));
}

let instructions = vec![Instruction::new(InstructionType::Loop, Some(loop_content))];

let mut optimizer = Optimizer::new(instructions);
let optimized_instructions = optimizer.optimize();

assert_eq!(optimized_instructions.len(), 1);
assert_eq!(
optimized_instructions[0].get_instruction_type(),
InstructionType::Pattern(PatternType::SetToZero(pattern_structs::SetToZero {}))
);
}
}

#[test]
fn test_do_not_recognize_pattern_set_to_zero_with_decrement() {
for i in (2..5).step_by(2) {
let mut loop_content = vec![];
for _ in 0..i {
loop_content.push(Instruction::new(InstructionType::Decrement, None));
}

let instructions = vec![Instruction::new(InstructionType::Loop, Some(loop_content))];

let mut optimizer = Optimizer::new(instructions);
let optimized_instructions = optimizer.optimize();

assert_eq!(optimized_instructions.len(), 1);
assert_eq!(
optimized_instructions[0].get_instruction_type(),
InstructionType::Loop
);
}
}

#[test]
fn test_do_not_recognize_pattern_set_to_zero_with_increment() {
for i in (2..5).step_by(2) {
let mut loop_content = vec![];
for _ in 0..i {
loop_content.push(Instruction::new(InstructionType::Increment, None));
}

let instructions = vec![Instruction::new(InstructionType::Loop, Some(loop_content))];

let mut optimizer = Optimizer::new(instructions);
let optimized_instructions = optimizer.optimize();

assert_eq!(optimized_instructions.len(), 1);
assert_eq!(
optimized_instructions[0].get_instruction_type(),
InstructionType::Loop
);
}
}
}

0 comments on commit d0f01cd

Please sign in to comment.