Skip to content

Commit 16d16da

Browse files
committed
Add tests for the stack
1 parent bdeea80 commit 16d16da

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ where
100100
data: &'a mut [T],
101101
}
102102

103-
#[derive(Debug)]
103+
#[derive(Debug, PartialEq)]
104104
enum StackError {
105105
Overflow,
106106
Underflow,

src/tests.rs

+60
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,66 @@
22
mod tests {
33
use crate::*;
44

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+
565
#[test]
666
fn initialization() {
767
default_fixed_sized_environment!(_environment);

0 commit comments

Comments
 (0)