use super::*;
fn setup_stack(values: &[Word]) -> Stack {
let mut stack = Stack::default();
for &value in values {
stack.push(value).unwrap();
}
stack
}
#[test]
fn test_reserve_zeroed_success() {
let mut stack = setup_stack(&[3]); assert!(stack.reserve_zeroed().is_ok());
assert_eq!(stack.0, vec![0, 0, 0]);
stack.push(2).unwrap(); assert!(stack.reserve_zeroed().is_ok());
assert_eq!(stack.0, vec![0, 0, 0, 0, 0]);
}
#[test]
fn test_reserve_zeroed_empty_stack() {
let mut stack = Stack::default();
assert!(matches!(
stack.reserve_zeroed().unwrap_err(),
StackError::Empty
));
}
#[test]
fn test_reserve_zeroed_negative_length() {
let mut stack = setup_stack(&[-1]);
assert!(matches!(
stack.reserve_zeroed().unwrap_err(),
StackError::IndexOutOfBounds
));
}
#[test]
fn test_reserve_zeroed_exceeds_size_limit() {
let mut stack = setup_stack(&[Word::MAX]);
assert!(matches!(
stack.reserve_zeroed().unwrap_err(),
StackError::IndexOutOfBounds
));
}
#[test]
fn test_load_success() {
let mut stack = setup_stack(&[42, 84, 126]);
stack.push(1).unwrap(); assert!(stack.load().is_ok());
assert_eq!(stack.0, vec![42, 84, 126, 84]);
assert_eq!(stack.pop().unwrap(), 84); }
#[test]
fn test_load_empty_stack() {
let mut stack = Stack::default();
assert!(matches!(stack.load().unwrap_err(), StackError::Empty));
}
#[test]
fn test_load_negative_index() {
let mut stack = setup_stack(&[-1]);
assert!(matches!(
stack.load().unwrap_err(),
StackError::IndexOutOfBounds
));
}
#[test]
fn test_load_out_of_bounds() {
let mut stack = setup_stack(&[5]);
stack.0 = vec![1, 2, 3]; assert!(matches!(
stack.load().unwrap_err(),
StackError::IndexOutOfBounds
));
}
#[test]
fn test_store_success() {
let mut stack = setup_stack(&[1, 2, 3]);
stack.push(1).unwrap(); stack.push(99).unwrap(); assert!(stack.store().is_ok());
assert_eq!(stack.0, vec![1, 99, 3]);
}
#[test]
fn test_store_empty_stack() {
let mut stack = Stack::default();
assert!(matches!(stack.store().unwrap_err(), StackError::Empty));
}
#[test]
fn test_store_single_value_stack() {
let mut stack = setup_stack(&[42]);
assert!(matches!(stack.store().unwrap_err(), StackError::Empty));
}
#[test]
fn test_store_negative_index() {
let mut stack = setup_stack(&[99, -1]); assert!(matches!(
stack.store().unwrap_err(),
StackError::IndexOutOfBounds
));
}
#[test]
fn test_store_out_of_bounds() {
let mut stack = setup_stack(&[1, 2, 3]);
stack.push(5).unwrap(); stack.push(99).unwrap(); assert!(matches!(
stack.store().unwrap_err(),
StackError::IndexOutOfBounds
));
}