forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingLinkedListTest.java
More file actions
57 lines (47 loc) · 1.33 KB
/
StackUsingLinkedListTest.java
File metadata and controls
57 lines (47 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.thealgorithms.stacks;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class StackUsingLinkedListTest {
private StackUsingLinkedList<Integer> stack;
@BeforeEach
public void setUp() {
stack = new StackUsingLinkedList<>();
}
@Test
public void testPushAndPeek() {
stack.push(10);
stack.push(20);
assertEquals(20, stack.peek());
}
@Test
public void testPop() {
stack.push(5);
stack.push(15);
int popped = stack.pop();
assertEquals(15, popped);
assertEquals(5, stack.peek());
}
@Test
public void testIsEmpty() {
assertTrue(stack.isEmpty());
stack.push(1);
assertFalse(stack.isEmpty());
}
@Test
public void testSize() {
assertEquals(0, stack.size());
stack.push(1);
stack.push(2);
assertEquals(2, stack.size());
}
@Test
public void testPopOnEmptyStack() {
assertThrows(RuntimeException.class, () -> stack.pop());
}
@Test
public void testPeekOnEmptyStackThrowsException() {
RuntimeException exception = assertThrows(RuntimeException.class, () -> stack.peek());
assertEquals("Stack is empty", exception.getMessage());
}
}