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 (92 loc) · 2.68 KB
/
StackTest.java
File metadata and controls
119 lines (92 loc) · 2.68 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 src.test.java.com.dataStructures;
import org.junit.Assert;
import org.junit.Test;
import src.main.java.com.dataStructures.Stack;
import java.util.EmptyStackException;
public class StackTest {
@Test
public void testEmpty() {
Stack<Integer> myStack = new Stack<>();
boolean isEmpty = myStack.empty();
Assert.assertTrue(isEmpty);
myStack.push(10);
isEmpty = myStack.empty();
Assert.assertFalse(isEmpty);
}
@Test(expected = EmptyStackException.class)
public void testPeekWithoutElements() {
Stack<Integer> myStack = new Stack<>();
myStack.peek();
}
@Test
public void testPeekWithElements() {
Stack<Integer> myStack = new Stack<>();
myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.push(40);
Assert.assertEquals(40, myStack.peek());
}
@Test(expected = EmptyStackException.class)
public void testPopWithoutElements() {
Stack<Integer> myStack = new Stack<>();
myStack.pop();
}
@Test
public void testPopWithElements() {
Stack<Integer> myStack = new Stack<>();
myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.push(40);
myStack.push(50);
Assert.assertEquals(50, myStack.pop());
}
@Test
public 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);
Assert.assertEquals(10, myStack.size());
}
@Test
public 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);
Assert.assertEquals(11, myStack.size());
}
@Test
public void testSearchWithObjectUnavailable() {
Stack<Integer> myStack = new Stack<>();
myStack.push(10);
myStack.push(20);
myStack.push(30);
Assert.assertEquals(-1,myStack.search(50));
}
@Test
public void testSearchWithObjectAvailable() {
Stack<Integer> myStack = new Stack<>();
myStack.push(10);
myStack.push(20);
myStack.push(30);
Assert.assertEquals(3,myStack.search(10));
}
}