use super::super::super::HighLevelEmitter;
#[test]
fn switch_fold_preserves_non_temp_inter_case_statement() {
let mut statements = vec![
"if loc0 == 0 {".to_string(),
" do0();".to_string(),
"}".to_string(),
"loc5 = side_effect();".to_string(),
"if loc0 == 1 {".to_string(),
" do1();".to_string(),
"}".to_string(),
"if loc0 == 2 {".to_string(),
" do2();".to_string(),
"}".to_string(),
];
HighLevelEmitter::rewrite_switch_statements(&mut statements);
assert!(
statements
.iter()
.any(|s| s.trim() == "loc5 = side_effect();"),
"non-temp inter-case statement must survive: {statements:?}"
);
}
#[test]
fn switch_fold_preserves_side_effecting_temp_between_cases() {
let mut statements = vec![
"if loc0 == 0 {".to_string(),
" do0();".to_string(),
" return;".to_string(),
"}".to_string(),
"let t7 = Foo(arg);".to_string(),
"if loc0 == 1 {".to_string(),
" do1();".to_string(),
" return;".to_string(),
"}".to_string(),
"if loc0 == 2 {".to_string(),
" do2();".to_string(),
" return;".to_string(),
"}".to_string(),
];
HighLevelEmitter::rewrite_switch_statements(&mut statements);
assert!(
statements.iter().any(|s| s.trim() == "let t7 = Foo(arg);"),
"side-effecting temp must survive: {statements:?}"
);
}
#[test]
fn switch_fold_still_applies_to_consecutive_cases() {
let mut statements = vec![
"if loc0 == 0 {".to_string(),
" do0();".to_string(),
"}".to_string(),
"if loc0 == 1 {".to_string(),
" do1();".to_string(),
"}".to_string(),
"if loc0 == 2 {".to_string(),
" do2();".to_string(),
"}".to_string(),
];
HighLevelEmitter::rewrite_switch_statements(&mut statements);
assert!(
statements.iter().any(|s| s.trim().starts_with("switch ")),
"consecutive standalone-if cases should still fold to a switch: {statements:?}"
);
}