add MatBuf::shift_up() operation

This commit is contained in:
tali 2023-04-10 13:25:29 -04:00
parent 002c0ba37c
commit 586d328718
1 changed files with 45 additions and 0 deletions

View File

@ -233,6 +233,15 @@ impl<const LEN: usize> MatBuf<LEN> {
self.rows = dst as i16;
rng
}
pub fn shift_up(&mut self) {
if self.rows as usize == LEN {
panic!("not enough available buffer space to shift up");
}
self.buf.copy_within(0..self.rows as usize, 1);
self.buf[0] = EMPTY_ROW;
self.rows += 1;
}
}
impl<const LEN: usize> Default for MatBuf<LEN> {
@ -575,4 +584,40 @@ mod test {
let mut buf: MatBuf<4> = MatBuf::default();
buf.fill_row(4, 0b1001);
}
#[test]
fn test_shift_up() {
let mat0 = mat! {
".x........";
"xxxxx.xxxx";
"x.xxxxxxxx";
".xxxxxxxxx";
};
let mat1 = mat! {
".x........";
"xxxxx.xxxx";
"x.xxxxxxxx";
".xxxxxxxxx";
"..........";
};
let mat2 = mat! {
".x........";
"xxxxx.xxxx";
"x.xxxxxxxx";
".xxxxxxxxx";
"..........";
"..........";
};
let mut buf = MatBuf::new();
buf.copy_from(mat0);
buf.shift_up();
assert_eq!(buf, mat1);
buf.shift_up();
assert_eq!(buf, mat2);
assert_eq!(buf.clear_lines(), 0..2);
assert_eq!(buf, mat0);
buf.shift_up();
assert_eq!(buf, mat1);
assert_eq!(buf.clear_lines(), 0..1);
}
}