forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackTest.java
More file actions
119 lines (95 loc) · 2.72 KB
/
StackTest.java
File metadata and controls
119 lines (95 loc) · 2.72 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.dataStructures;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.EmptyStackException;
class StackTest {
@Test
void testEmpty() {
Stack<Integer> myStack = new Stack<>();
boolean isEmpty = myStack.empty();
Assertions.assertTrue(isEmpty);
myStack.push(10);
isEmpty = myStack.empty();
Assertions.assertFalse(isEmpty);
}
@Test
void testPeekWithoutElements() {
Assertions.assertThrows(EmptyStackException.class, () -> {
Stack<Integer> myStack = new Stack<>();
myStack.peek();
});
}
@Test
void testPeekWithElements() {
Stack<Integer> myStack = new Stack<>();
myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.push(40);
Assertions.assertEquals(40, (int) myStack.peek());
}
@Test
void testPopWithoutElements() {
Assertions.assertThrows(EmptyStackException.class, () -> {
Stack<Integer> myStack = new Stack<>();
myStack.pop();
});
}
@Test
void testPopWithElements() {
Stack<Integer> myStack = new Stack<>();
myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.push(40);
myStack.push(50);
Assertions.assertEquals(50, (int) myStack.pop());
}
@Test
void testPushWithinInitialCapacity() {
Stack<Integer> myStack = new Stack<>();
myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.push(40);
myStack.push(50);
myStack.push(60);
myStack.push(70);
myStack.push(80);
myStack.push(90);
myStack.push(100);
Assertions.assertEquals(10, myStack.size());
}
@Test
void testPushOutsideInitialCapacity() {
Stack<Integer> myStack = new Stack<>();
myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.push(40);
myStack.push(50);
myStack.push(60);
myStack.push(70);
myStack.push(80);
myStack.push(90);
myStack.push(100);
myStack.push(110);
Assertions.assertEquals(11, myStack.size());
}
@Test
void testSearchWithObjectUnavailable() {
Stack<Integer> myStack = new Stack<>();
myStack.push(10);
myStack.push(20);
myStack.push(30);
Assertions.assertEquals(-1, myStack.search(50));
}
@Test
void testSearchWithObjectAvailable() {
Stack<Integer> myStack = new Stack<>();
myStack.push(10);
myStack.push(20);
myStack.push(30);
Assertions.assertEquals(3, myStack.search(10));
}
}