Skip to content

Commit 1da26b4

Browse files
test: add unit tests for StackUsingLinkedList
1 parent ce4a905 commit 1da26b4

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.thealgorithms.stacks;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
public class StackUsingLinkedListTest {
9+
private StackUsingLinkedList<Integer> stack;
10+
11+
@BeforeEach
12+
public void setUp() {
13+
stack = new StackUsingLinkedList<>();
14+
}
15+
16+
@Test
17+
public void testPushAndPeek() {
18+
stack.push(10);
19+
stack.push(20);
20+
21+
assertEquals(20, stack.peek());
22+
}
23+
24+
@Test
25+
public void testPop() {
26+
stack.push(5);
27+
stack.push(15);
28+
int popped = stack.pop();
29+
assertEquals(15, popped);
30+
assertEquals(5, stack.peek());
31+
}
32+
33+
@Test
34+
public void testIsEmpty() {
35+
assertTrue(stack.isEmpty());
36+
stack.push(1);
37+
assertFalse(stack.isEmpty());
38+
}
39+
40+
@Test
41+
public void testSize() {
42+
assertEquals(0, stack.size());
43+
stack.push(1);
44+
stack.push(2);
45+
assertEquals(2, stack.size());
46+
}
47+
48+
@Test
49+
public void testPopOnEmptyStack() {
50+
assertThrows(RuntimeException.class, () -> stack.pop());
51+
}
52+
}

0 commit comments

Comments
 (0)