|
| 1 | +//! Unit tests for the Up-collapse optimization pass. |
| 2 | +
|
| 3 | +use plotnik_bytecode::Nav; |
| 4 | + |
| 5 | +use super::collapse_up::collapse_up; |
| 6 | +use super::CompileResult; |
| 7 | +use crate::bytecode::{InstructionIR, Label, MatchIR}; |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn collapse_up_single_mode() { |
| 11 | + // Up(1) → Up(1) → exit should become Up(2) → exit |
| 12 | + let mut result = CompileResult { |
| 13 | + instructions: vec![ |
| 14 | + MatchIR::at(Label(0)).nav(Nav::Up(1)).next(Label(1)).into(), |
| 15 | + MatchIR::at(Label(1)).nav(Nav::Up(1)).next(Label(2)).into(), |
| 16 | + MatchIR::terminal(Label(2)).into(), |
| 17 | + ], |
| 18 | + def_entries: Default::default(), |
| 19 | + preamble_entry: Label(0), |
| 20 | + }; |
| 21 | + |
| 22 | + collapse_up(&mut result); |
| 23 | + |
| 24 | + // Should collapse to 2 instructions: Up(2) and terminal |
| 25 | + assert_eq!(result.instructions.len(), 2); |
| 26 | + |
| 27 | + let InstructionIR::Match(m) = &result.instructions[0] else { |
| 28 | + panic!("expected Match"); |
| 29 | + }; |
| 30 | + assert_eq!(m.nav, Nav::Up(2)); |
| 31 | + assert_eq!(m.successors, vec![Label(2)]); |
| 32 | +} |
| 33 | + |
| 34 | +#[test] |
| 35 | +fn collapse_up_chain_of_three() { |
| 36 | + // Up(1) → Up(2) → Up(3) should become Up(6) |
| 37 | + let mut result = CompileResult { |
| 38 | + instructions: vec![ |
| 39 | + MatchIR::at(Label(0)).nav(Nav::Up(1)).next(Label(1)).into(), |
| 40 | + MatchIR::at(Label(1)).nav(Nav::Up(2)).next(Label(2)).into(), |
| 41 | + MatchIR::at(Label(2)).nav(Nav::Up(3)).next(Label(3)).into(), |
| 42 | + MatchIR::terminal(Label(3)).into(), |
| 43 | + ], |
| 44 | + def_entries: Default::default(), |
| 45 | + preamble_entry: Label(0), |
| 46 | + }; |
| 47 | + |
| 48 | + collapse_up(&mut result); |
| 49 | + |
| 50 | + assert_eq!(result.instructions.len(), 2); |
| 51 | + |
| 52 | + let InstructionIR::Match(m) = &result.instructions[0] else { |
| 53 | + panic!("expected Match"); |
| 54 | + }; |
| 55 | + assert_eq!(m.nav, Nav::Up(6)); |
| 56 | +} |
| 57 | + |
| 58 | +#[test] |
| 59 | +fn collapse_up_mixed_modes_no_merge() { |
| 60 | + // Up(1) → UpSkipTrivia(1) should NOT merge (different modes) |
| 61 | + let mut result = CompileResult { |
| 62 | + instructions: vec![ |
| 63 | + MatchIR::at(Label(0)).nav(Nav::Up(1)).next(Label(1)).into(), |
| 64 | + MatchIR::at(Label(1)) |
| 65 | + .nav(Nav::UpSkipTrivia(1)) |
| 66 | + .next(Label(2)) |
| 67 | + .into(), |
| 68 | + MatchIR::terminal(Label(2)).into(), |
| 69 | + ], |
| 70 | + def_entries: Default::default(), |
| 71 | + preamble_entry: Label(0), |
| 72 | + }; |
| 73 | + |
| 74 | + collapse_up(&mut result); |
| 75 | + |
| 76 | + // Should stay 3 instructions |
| 77 | + assert_eq!(result.instructions.len(), 3); |
| 78 | +} |
| 79 | + |
| 80 | +#[test] |
| 81 | +fn collapse_up_skip_trivia_same_mode() { |
| 82 | + // UpSkipTrivia(1) → UpSkipTrivia(1) should merge |
| 83 | + let mut result = CompileResult { |
| 84 | + instructions: vec![ |
| 85 | + MatchIR::at(Label(0)) |
| 86 | + .nav(Nav::UpSkipTrivia(1)) |
| 87 | + .next(Label(1)) |
| 88 | + .into(), |
| 89 | + MatchIR::at(Label(1)) |
| 90 | + .nav(Nav::UpSkipTrivia(1)) |
| 91 | + .next(Label(2)) |
| 92 | + .into(), |
| 93 | + MatchIR::terminal(Label(2)).into(), |
| 94 | + ], |
| 95 | + def_entries: Default::default(), |
| 96 | + preamble_entry: Label(0), |
| 97 | + }; |
| 98 | + |
| 99 | + collapse_up(&mut result); |
| 100 | + |
| 101 | + assert_eq!(result.instructions.len(), 2); |
| 102 | + |
| 103 | + let InstructionIR::Match(m) = &result.instructions[0] else { |
| 104 | + panic!("expected Match"); |
| 105 | + }; |
| 106 | + assert_eq!(m.nav, Nav::UpSkipTrivia(2)); |
| 107 | +} |
| 108 | + |
| 109 | +#[test] |
| 110 | +fn collapse_up_exact_same_mode() { |
| 111 | + // UpExact(1) → UpExact(1) should merge |
| 112 | + let mut result = CompileResult { |
| 113 | + instructions: vec![ |
| 114 | + MatchIR::at(Label(0)) |
| 115 | + .nav(Nav::UpExact(1)) |
| 116 | + .next(Label(1)) |
| 117 | + .into(), |
| 118 | + MatchIR::at(Label(1)) |
| 119 | + .nav(Nav::UpExact(1)) |
| 120 | + .next(Label(2)) |
| 121 | + .into(), |
| 122 | + MatchIR::terminal(Label(2)).into(), |
| 123 | + ], |
| 124 | + def_entries: Default::default(), |
| 125 | + preamble_entry: Label(0), |
| 126 | + }; |
| 127 | + |
| 128 | + collapse_up(&mut result); |
| 129 | + |
| 130 | + assert_eq!(result.instructions.len(), 2); |
| 131 | + |
| 132 | + let InstructionIR::Match(m) = &result.instructions[0] else { |
| 133 | + panic!("expected Match"); |
| 134 | + }; |
| 135 | + assert_eq!(m.nav, Nav::UpExact(2)); |
| 136 | +} |
| 137 | + |
| 138 | +#[test] |
| 139 | +fn collapse_up_with_effects_no_merge() { |
| 140 | + // Up(1) with post_effects → Up(1) should NOT merge |
| 141 | + use crate::bytecode::EffectIR; |
| 142 | + use plotnik_bytecode::EffectOpcode; |
| 143 | + |
| 144 | + let mut result = CompileResult { |
| 145 | + instructions: vec![ |
| 146 | + MatchIR::at(Label(0)).nav(Nav::Up(1)).next(Label(1)).into(), |
| 147 | + MatchIR::at(Label(1)) |
| 148 | + .nav(Nav::Up(1)) |
| 149 | + .post_effects(vec![EffectIR::simple(EffectOpcode::Null, 0)]) |
| 150 | + .next(Label(2)) |
| 151 | + .into(), |
| 152 | + MatchIR::terminal(Label(2)).into(), |
| 153 | + ], |
| 154 | + def_entries: Default::default(), |
| 155 | + preamble_entry: Label(0), |
| 156 | + }; |
| 157 | + |
| 158 | + collapse_up(&mut result); |
| 159 | + |
| 160 | + // Should stay 3 instructions (effectful Up can't be absorbed) |
| 161 | + assert_eq!(result.instructions.len(), 3); |
| 162 | +} |
| 163 | + |
| 164 | +#[test] |
| 165 | +fn collapse_up_max_63() { |
| 166 | + // Up(60) → Up(10) should become Up(63) (capped) |
| 167 | + let mut result = CompileResult { |
| 168 | + instructions: vec![ |
| 169 | + MatchIR::at(Label(0)).nav(Nav::Up(60)).next(Label(1)).into(), |
| 170 | + MatchIR::at(Label(1)).nav(Nav::Up(10)).next(Label(2)).into(), |
| 171 | + MatchIR::terminal(Label(2)).into(), |
| 172 | + ], |
| 173 | + def_entries: Default::default(), |
| 174 | + preamble_entry: Label(0), |
| 175 | + }; |
| 176 | + |
| 177 | + collapse_up(&mut result); |
| 178 | + |
| 179 | + // Capped at 63, remaining Up(7) stays separate |
| 180 | + assert_eq!(result.instructions.len(), 2); |
| 181 | + |
| 182 | + let InstructionIR::Match(m) = &result.instructions[0] else { |
| 183 | + panic!("expected Match"); |
| 184 | + }; |
| 185 | + assert_eq!(m.nav, Nav::Up(63)); |
| 186 | +} |
| 187 | + |
| 188 | +#[test] |
| 189 | +fn collapse_up_branching_no_merge() { |
| 190 | + // Up(1) with multiple successors should NOT merge |
| 191 | + let mut result = CompileResult { |
| 192 | + instructions: vec![ |
| 193 | + MatchIR::at(Label(0)) |
| 194 | + .nav(Nav::Up(1)) |
| 195 | + .next_many(vec![Label(1), Label(2)]) |
| 196 | + .into(), |
| 197 | + MatchIR::at(Label(1)).nav(Nav::Up(1)).next(Label(3)).into(), |
| 198 | + MatchIR::at(Label(2)).nav(Nav::Up(1)).next(Label(3)).into(), |
| 199 | + MatchIR::terminal(Label(3)).into(), |
| 200 | + ], |
| 201 | + def_entries: Default::default(), |
| 202 | + preamble_entry: Label(0), |
| 203 | + }; |
| 204 | + |
| 205 | + collapse_up(&mut result); |
| 206 | + |
| 207 | + // Branching instruction can't merge, but its successors can be processed |
| 208 | + // Label(0) has 2 successors, so it stays as Up(1) |
| 209 | + let InstructionIR::Match(m) = &result.instructions[0] else { |
| 210 | + panic!("expected Match"); |
| 211 | + }; |
| 212 | + assert_eq!(m.nav, Nav::Up(1)); |
| 213 | +} |
| 214 | + |
| 215 | +#[test] |
| 216 | +fn collapse_up_no_up_unchanged() { |
| 217 | + // Non-Up instructions should pass through unchanged |
| 218 | + let mut result = CompileResult { |
| 219 | + instructions: vec![ |
| 220 | + MatchIR::at(Label(0)).nav(Nav::Down).next(Label(1)).into(), |
| 221 | + MatchIR::at(Label(1)).nav(Nav::Next).next(Label(2)).into(), |
| 222 | + MatchIR::terminal(Label(2)).into(), |
| 223 | + ], |
| 224 | + def_entries: Default::default(), |
| 225 | + preamble_entry: Label(0), |
| 226 | + }; |
| 227 | + |
| 228 | + collapse_up(&mut result); |
| 229 | + |
| 230 | + assert_eq!(result.instructions.len(), 3); |
| 231 | +} |
0 commit comments