From 34ceb1b392bf46fd470af3f4300c1f5d4676d79f Mon Sep 17 00:00:00 2001 From: Reid 'arrdem' McKenzie Date: Sun, 25 Dec 2022 22:57:07 -0700 Subject: [PATCH] A nasty pop2+keep bug --- src/stack/pop.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/stack/pop.rs b/src/stack/pop.rs index 23881bc..b78d383 100644 --- a/src/stack/pop.rs +++ b/src/stack/pop.rs @@ -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> = 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> = + 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> = + 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...