Skip to content

Commit ec7b654

Browse files
authored
testing: improve StackArrayTest (#6424)
testing: improve StackArrayTest
1 parent 6992716 commit ec7b654

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/test/java/com/thealgorithms/datastructures/stacks/StackArrayTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,62 @@ void testToString() {
126126
stack.push(3);
127127
Assertions.assertEquals("StackArray [1, 2, 3]", stack.toString());
128128
}
129+
130+
@Test
131+
void testSingleElementOperations() {
132+
// Test operations with a single element
133+
stack.push(2);
134+
Assertions.assertEquals(1, stack.size());
135+
Assertions.assertFalse(stack.isEmpty());
136+
Assertions.assertEquals(2, stack.peek());
137+
Assertions.assertEquals(2, stack.pop());
138+
Assertions.assertTrue(stack.isEmpty());
139+
}
140+
141+
@Test
142+
void testAlternatingPushPop() {
143+
// Test alternating push and pop operations
144+
stack.push(1);
145+
Assertions.assertEquals(1, stack.pop());
146+
147+
stack.push(2);
148+
stack.push(3);
149+
Assertions.assertEquals(3, stack.pop());
150+
151+
stack.push(4);
152+
Assertions.assertEquals(4, stack.pop());
153+
Assertions.assertEquals(2, stack.pop());
154+
Assertions.assertTrue(stack.isEmpty());
155+
}
156+
157+
@Test
158+
void testPushNullElements() {
159+
// Test pushing null values
160+
stack.push(null);
161+
Assertions.assertEquals(1, stack.size());
162+
Assertions.assertNull(stack.peek());
163+
Assertions.assertNull(stack.pop());
164+
165+
// Mix null and non-null values
166+
stack.push(1);
167+
stack.push(null);
168+
stack.push(2);
169+
170+
Assertions.assertEquals(2, stack.pop());
171+
Assertions.assertNull(stack.pop());
172+
Assertions.assertEquals(1, stack.pop());
173+
}
174+
175+
@Test
176+
void testWithDifferentDataTypes() {
177+
// Test with String type
178+
StackArray<String> stringStack = new StackArray<>(3);
179+
stringStack.push("first");
180+
stringStack.push("second");
181+
stringStack.push("third");
182+
183+
Assertions.assertEquals("third", stringStack.pop());
184+
Assertions.assertEquals("second", stringStack.peek());
185+
Assertions.assertEquals(2, stringStack.size());
186+
}
129187
}

0 commit comments

Comments
 (0)