Skip to content

Commit

Permalink
fix: fixing bug in focus_head method of Stack and adding focus_tail
Browse files Browse the repository at this point in the history
  • Loading branch information
sminez committed Oct 23, 2024
1 parent f34bee1 commit 3d6c4d4
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/pure/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,30 @@ impl<T> Stack<T> {
swap(&mut head, &mut self.focus);
self.down.push_front(head);

for item in take(&mut self.up).into_iter().rev() {
for item in take(&mut self.up).into_iter() {
self.down.push_front(item);
}

self
}

/// Move focus to the element in the head position
pub fn focus_tail(&mut self) -> &mut Self {
let mut tail = match self.down.pop_back() {
None => return self, // focus is already tail
Some(t) => t,
};

swap(&mut tail, &mut self.focus);
self.up.push_front(tail);

for item in take(&mut self.down).into_iter() {
self.up.push_front(item);
}

self
}

/// Insert the given element in place of the current focus, pushing
/// the current focus down the [Stack].
pub fn insert(&mut self, t: T) -> &mut Self {
Expand Down Expand Up @@ -697,9 +714,9 @@ mod tests {
assert_eq!(s, expected);
}

#[test_case(stack!([1, 2], 3, [4, 5]), stack!(1, [2, 3, 4, 5]); "items up and down")]
#[test_case(stack!([1, 2], 3), stack!(1, [2, 3]); "items up")]
#[test_case(stack!(3, [4, 5]), stack!(3, [4, 5]); "items down")]
#[test_case(stack!([1, 2, 3], 4, [5, 6, 7]), stack!(1, [2, 3, 4, 5, 6, 7]); "items up and down")]
#[test_case(stack!([1, 2, 3], 4), stack!(1, [2, 3, 4]); "items up")]
#[test_case(stack!(3, [4, 5, 6]), stack!(3, [4, 5, 6]); "items down")]
#[test_case(stack!(3), stack!(3); "focus only")]
#[test]
fn focus_head(mut s: Stack<u8>, expected: Stack<u8>) {
Expand All @@ -708,6 +725,18 @@ mod tests {
assert_eq!(s, expected);
}

#[test_case(stack!([1, 2, 3], 4, [5, 6, 7]), stack!([1, 2, 3, 4, 5, 6], 7); "items up and down")]
#[test_case(stack!([1, 2, 3], 4), stack!([1, 2, 3], 4); "items up")]
#[test_case(stack!(3, [4, 5, 6]), stack!([3, 4, 5], 6); "items down")]
#[test_case(stack!(3), stack!(3); "focus only")]
#[test]
fn focus_tail(mut s: Stack<u8>, expected: Stack<u8>) {
s.focus_tail();

assert_eq!(s, expected);
}


#[test_case(stack!([1, 2], 3, [4, 5, 6]), |&e| e == 3, stack!([1, 2], 3, [4, 5, 6]); "current focus")]
#[test_case(stack!([1, 2], 3, [4, 5, 6]), |&e| e > 4, stack!([1, 2, 3, 4], 5, [6]); "in tail")]
#[test_case(stack!([1, 2], 3, [4, 5, 6]), |&e| e < 3 && e > 1, stack!([1], 2, [3, 4, 5, 6]); "in head")]
Expand Down

0 comments on commit 3d6c4d4

Please sign in to comment.