From ac451a0eeac46c576c19e40509d5342424db3a0a Mon Sep 17 00:00:00 2001 From: alxkm Date: Sun, 20 Jul 2025 23:58:44 +0200 Subject: [PATCH] testing: improve StackArrayTest --- .../datastructures/stacks/StackArrayTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayTest.java b/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayTest.java index 74de7ad6435a..392cdf2329fc 100644 --- a/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayTest.java +++ b/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayTest.java @@ -126,4 +126,62 @@ void testToString() { 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 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()); + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy