Skip to content

Commit 5561bc4

Browse files
author
alxkm
committed
test: added LinkedListStackTest
1 parent 74b05ef commit 5561bc4

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.thealgorithms.datastructures.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.util.NoSuchElementException;
11+
12+
public class LinkedListStackTest {
13+
14+
private LinkedListStack stack;
15+
16+
@BeforeEach
17+
public void setUp() {
18+
stack = new LinkedListStack();
19+
}
20+
21+
@Test
22+
public void testPushAndPeek() {
23+
stack.push(1);
24+
stack.push(2);
25+
stack.push(3);
26+
27+
assertEquals(3, stack.peek());
28+
assertEquals(3, stack.getSize());
29+
}
30+
31+
@Test
32+
public void testPop() {
33+
stack.push(1);
34+
stack.push(2);
35+
stack.push(3);
36+
37+
assertEquals(3, stack.pop());
38+
assertEquals(2, stack.pop());
39+
assertEquals(1, stack.pop());
40+
assertTrue(stack.isEmpty());
41+
}
42+
43+
@Test
44+
public void testPopEmptyStack() {
45+
org.junit.jupiter.api.Assertions.assertThrows(NoSuchElementException.class, () -> stack.pop());
46+
}
47+
48+
@Test
49+
public void testPeekEmptyStack() {
50+
org.junit.jupiter.api.Assertions.assertThrows(NoSuchElementException.class, () -> stack.peek());
51+
}
52+
53+
@Test
54+
public void testIsEmpty() {
55+
assertTrue(stack.isEmpty());
56+
57+
stack.push(1);
58+
assertFalse(stack.isEmpty());
59+
60+
stack.pop();
61+
assertTrue(stack.isEmpty());
62+
}
63+
64+
@Test
65+
public void testToString() {
66+
stack.push(1);
67+
stack.push(2);
68+
stack.push(3);
69+
70+
assertEquals("3->2->1", stack.toString());
71+
}
72+
}

0 commit comments

Comments
 (0)