A nasty pop2+keep bug
This commit is contained in:
parent
2dfd53ba16
commit
34ceb1b392
1 changed files with 21 additions and 2 deletions
|
@ -66,7 +66,7 @@ impl Stack for PopStack {
|
||||||
if self._idx < 2 {
|
if self._idx < 2 {
|
||||||
return Err(StackError::StackUnderflow);
|
return Err(StackError::StackUnderflow);
|
||||||
}
|
}
|
||||||
let val = self.get2(self._idx - 1)?;
|
let val = self.get2(self._idx - 2)?;
|
||||||
self._idx -= 2;
|
self._idx -= 2;
|
||||||
Ok(val)
|
Ok(val)
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ mod test_ps {
|
||||||
use crate::stack::arrs::ArrayStack;
|
use crate::stack::arrs::ArrayStack;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_dummy_pop() {
|
fn test_dummy_pop1() {
|
||||||
// Set up the underlying stack...
|
// Set up the underlying stack...
|
||||||
let underlying: Rc<RefCell<dyn Stack>> =
|
let underlying: Rc<RefCell<dyn Stack>> =
|
||||||
Rc::new(RefCell::new(ArrayStack::of1(&[0xFF, 0xFE])));
|
Rc::new(RefCell::new(ArrayStack::of1(&[0xFF, 0xFE])));
|
||||||
|
@ -96,6 +96,25 @@ mod test_ps {
|
||||||
assert_eq!(underlying.borrow_mut().pop1().unwrap(), 0xFF);
|
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]
|
#[test]
|
||||||
fn test_pop_pushthrough() {
|
fn test_pop_pushthrough() {
|
||||||
// Set up the underlying stack...
|
// Set up the underlying stack...
|
||||||
|
|
Loading…
Reference in a new issue