A nasty pop2+keep bug

This commit is contained in:
Reid 'arrdem' McKenzie 2022-12-25 22:57:07 -07:00
parent 2dfd53ba16
commit 34ceb1b392

View file

@ -66,7 +66,7 @@ impl Stack for PopStack {
if self._idx < 2 {
return Err(StackError::StackUnderflow);
}
let val = self.get2(self._idx - 1)?;
let val = self.get2(self._idx - 2)?;
self._idx -= 2;
Ok(val)
}
@ -78,7 +78,7 @@ mod test_ps {
use crate::stack::arrs::ArrayStack;
#[test]
fn test_dummy_pop() {
fn test_dummy_pop1() {
// Set up the underlying stack...
let underlying: Rc<RefCell<dyn Stack>> =
Rc::new(RefCell::new(ArrayStack::of1(&[0xFF, 0xFE])));
@ -96,6 +96,25 @@ mod test_ps {
assert_eq!(underlying.borrow_mut().pop1().unwrap(), 0xFF);
}
#[test]
fn test_dummy_pop2() {
// Set up the underlying stack...
let underlying: Rc<RefCell<dyn Stack>> =
Rc::new(RefCell::new(ArrayStack::of1(&[0xFF, 0xFE])));
// Set up the wrapper, which assumes it has exclusive access to the underlying stack
let wrapper: Rc<RefCell<dyn Stack>> =
Rc::new(RefCell::new(PopStack::new(underlying.clone())));
// We should be able to pop an appropriate short
assert_eq!(wrapper.borrow_mut().pop2().unwrap(), 0xFFFE);
assert_eq!(wrapper.borrow_mut().idx(), 0);
// And the underlying stack should be unaffected
assert_eq!(underlying.borrow_mut().pop1().unwrap(), 0xFE);
assert_eq!(underlying.borrow_mut().pop1().unwrap(), 0xFF);
}
#[test]
fn test_pop_pushthrough() {
// Set up the underlying stack...