|
2 | 2 | mod tests {
|
3 | 3 | use crate::*;
|
4 | 4 |
|
| 5 | + mod stack { |
| 6 | + use crate::*; |
| 7 | + |
| 8 | + #[test] |
| 9 | + fn empty() { |
| 10 | + let mut buffer: [Cell; 0] = Default::default(); |
| 11 | + let mut stack = Stack::new(&mut buffer); |
| 12 | + assert_eq!(stack.len(), 0); |
| 13 | + assert_eq!(stack.push(0).err().unwrap(), StackError::Overflow); |
| 14 | + assert_eq!(stack.pop().err().unwrap(), StackError::Underflow); |
| 15 | + } |
| 16 | + |
| 17 | + #[test] |
| 18 | + fn one_item_stack() { |
| 19 | + let mut buffer: [Cell; 1] = Default::default(); |
| 20 | + let mut stack = Stack::new(&mut buffer); |
| 21 | + assert_eq!(stack.len(), 0); |
| 22 | + stack.push(1).unwrap(); |
| 23 | + assert_eq!(stack.len(), 1); |
| 24 | + assert_eq!(stack.push(2).err().unwrap(), StackError::Overflow); |
| 25 | + assert_eq!(stack.pop().unwrap(), 1); |
| 26 | + assert_eq!(stack.len(), 0); |
| 27 | + } |
| 28 | + |
| 29 | + #[test] |
| 30 | + fn sanity() { |
| 31 | + let mut buffer = [0, 0, 0, 0, 0]; |
| 32 | + let mut stack = Stack::new(&mut buffer); |
| 33 | + |
| 34 | + stack.push(1).unwrap(); |
| 35 | + stack.push(2).unwrap(); |
| 36 | + stack.push(3).unwrap(); |
| 37 | + stack.push(4).unwrap(); |
| 38 | + stack.push(5).unwrap(); |
| 39 | + assert_eq!(stack.len(), 5); |
| 40 | + assert_eq!(*stack.last().unwrap(), 5); |
| 41 | + assert_eq!(stack.push(5).err().unwrap(), StackError::Overflow); |
| 42 | + |
| 43 | + assert_eq!(stack.pop().unwrap(), 5); |
| 44 | + assert_eq!(stack.pop().unwrap(), 4); |
| 45 | + assert_eq!(stack.pop().unwrap(), 3); |
| 46 | + assert_eq!(stack.pop().unwrap(), 2); |
| 47 | + assert_eq!(stack.pop().unwrap(), 1); |
| 48 | + assert_eq!(stack.len(), 0); |
| 49 | + assert_eq!(stack.pop().err().unwrap(), StackError::Underflow); |
| 50 | + |
| 51 | + stack.push(1).unwrap(); |
| 52 | + stack.push(2).unwrap(); |
| 53 | + stack.push(3).unwrap(); |
| 54 | + stack.push(4).unwrap(); |
| 55 | + stack.push(5).unwrap(); |
| 56 | + assert_eq!(stack.len(), 5); |
| 57 | + assert_eq!(*stack.last().unwrap(), 5); |
| 58 | + assert_eq!(stack.push(5).err().unwrap(), StackError::Overflow); |
| 59 | + stack.clear(); |
| 60 | + assert_eq!(stack.len(), 0); |
| 61 | + assert_eq!(stack.pop().err().unwrap(), StackError::Underflow); |
| 62 | + } |
| 63 | + } |
| 64 | + |
5 | 65 | #[test]
|
6 | 66 | fn initialization() {
|
7 | 67 | default_fixed_sized_environment!(_environment);
|
|
0 commit comments