forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStackArrayTest.java
More file actions
187 lines (154 loc) · 5.06 KB
/
StackArrayTest.java
File metadata and controls
187 lines (154 loc) · 5.06 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.thealgorithms.datastructures.stacks;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class StackArrayTest {
private Stack<Integer> stack;
@BeforeEach
void setUp() {
stack = new StackArray<>(5); // Initialize a stack with capacity of 5
}
@Test
void testPushAndPop() {
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
Assertions.assertEquals(5, stack.pop());
Assertions.assertEquals(4, stack.pop());
Assertions.assertEquals(3, stack.pop());
Assertions.assertEquals(2, stack.pop());
Assertions.assertEquals(1, stack.pop());
}
@Test
void testPeek() {
stack.push(10);
stack.push(20);
stack.push(30);
Assertions.assertEquals(30, stack.peek());
Assertions.assertEquals(3, stack.size());
stack.pop();
Assertions.assertEquals(20, stack.peek());
}
@Test
void testIsEmpty() {
Assertions.assertTrue(stack.isEmpty());
stack.push(42);
Assertions.assertFalse(stack.isEmpty());
stack.pop();
Assertions.assertTrue(stack.isEmpty());
}
@Test
void testResizeOnPush() {
StackArray<Integer> smallStack = new StackArray<>(2);
smallStack.push(1);
smallStack.push(2);
Assertions.assertTrue(smallStack.isFull());
smallStack.push(3);
Assertions.assertFalse(smallStack.isFull());
Assertions.assertEquals(3, smallStack.size());
Assertions.assertEquals(3, smallStack.pop());
Assertions.assertEquals(2, smallStack.pop());
Assertions.assertEquals(1, smallStack.pop());
}
@Test
void testResizeOnPop() {
StackArray<Integer> stack = new StackArray<>(4);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.pop();
stack.pop();
stack.pop();
Assertions.assertEquals(1, stack.size());
stack.pop();
Assertions.assertTrue(stack.isEmpty());
}
@Test
void testMakeEmpty() {
stack.push(1);
stack.push(2);
stack.push(3);
stack.makeEmpty();
Assertions.assertTrue(stack.isEmpty());
Assertions.assertThrows(IllegalStateException.class, stack::pop);
}
@Test
void testPopEmptyStackThrowsException() {
Assertions.assertThrows(IllegalStateException.class, stack::pop);
}
@Test
void testPeekEmptyStackThrowsException() {
Assertions.assertThrows(IllegalStateException.class, stack::peek);
}
@Test
void testConstructorWithInvalidSizeThrowsException() {
Assertions.assertThrows(IllegalArgumentException.class, () -> new StackArray<>(0));
Assertions.assertThrows(IllegalArgumentException.class, () -> new StackArray<>(-5));
}
@Test
void testDefaultConstructor() {
StackArray<Integer> defaultStack = new StackArray<>();
Assertions.assertEquals(0, defaultStack.size());
defaultStack.push(1);
Assertions.assertEquals(1, defaultStack.size());
}
@Test
void testToString() {
stack.push(1);
stack.push(2);
stack.push(3);
Assertions.assertEquals("StackArray [1, 2, 3]", stack.toString());
}
@Test
void testSingleElementOperations() {
// Test operations with a single element
stack.push(2);
Assertions.assertEquals(1, stack.size());
Assertions.assertFalse(stack.isEmpty());
Assertions.assertEquals(2, stack.peek());
Assertions.assertEquals(2, stack.pop());
Assertions.assertTrue(stack.isEmpty());
}
@Test
void testAlternatingPushPop() {
// Test alternating push and pop operations
stack.push(1);
Assertions.assertEquals(1, stack.pop());
stack.push(2);
stack.push(3);
Assertions.assertEquals(3, stack.pop());
stack.push(4);
Assertions.assertEquals(4, stack.pop());
Assertions.assertEquals(2, stack.pop());
Assertions.assertTrue(stack.isEmpty());
}
@Test
void testPushNullElements() {
// Test pushing null values
stack.push(null);
Assertions.assertEquals(1, stack.size());
Assertions.assertNull(stack.peek());
Assertions.assertNull(stack.pop());
// Mix null and non-null values
stack.push(1);
stack.push(null);
stack.push(2);
Assertions.assertEquals(2, stack.pop());
Assertions.assertNull(stack.pop());
Assertions.assertEquals(1, stack.pop());
}
@Test
void testWithDifferentDataTypes() {
// Test with String type
StackArray<String> stringStack = new StackArray<>(3);
stringStack.push("first");
stringStack.push("second");
stringStack.push("third");
Assertions.assertEquals("third", stringStack.pop());
Assertions.assertEquals("second", stringStack.peek());
Assertions.assertEquals(2, stringStack.size());
}
}