From c7e50992ca9284e659eaf99e299d8057acf7c09d Mon Sep 17 00:00:00 2001 From: alxkm Date: Mon, 21 Jul 2025 22:25:35 +0200 Subject: [PATCH 1/2] testing: improve test coverage StackOfLinkedListTest --- .../stacks/StackOfLinkedListTest.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java index 58af66bc38f4..1025f3995d57 100644 --- a/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java @@ -118,4 +118,104 @@ public void testSequentialPushAndPop() { } assertTrue(stack.isEmpty(), "Stack should be empty after popping all elements"); } + + @Test + public void testPushZeroAndNegativeValues() { + stack.push(0); + stack.push(-1); + stack.push(-1); + + assertEquals(-1, stack.pop(), "Should handle negative values correctly"); + assertEquals(-1, stack.pop(), "Should handle negative values correctly"); + assertEquals(0, stack.pop(), "Should handle zero value correctly"); + } + + @Test + public void testPushDuplicateValues() { + stack.push(1); + stack.push(1); + stack.push(1); + + assertEquals(3, stack.getSize(), "Should allow duplicate values"); + assertEquals(1, stack.pop()); + assertEquals(1, stack.pop()); + assertEquals(1, stack.pop()); + } + + @Test + public void testPushAfterEmptyingStack() { + stack.push(1); + stack.push(2); + stack.pop(); + stack.pop(); + + assertTrue(stack.isEmpty(), "Stack should be empty"); + + stack.push(10); + assertEquals(10, stack.peek(), "Should work correctly after emptying and refilling"); + assertEquals(1, stack.getSize(), "Size should be correct after refilling"); + } + + @Test + public void testPeekDoesNotModifyStack() { + stack.push(1); + + int firstPeek = stack.peek(); + int secondPeek = stack.peek(); + int thirdPeek = stack.peek(); + + assertEquals(firstPeek, secondPeek, "Multiple peeks should return same value"); + assertEquals(secondPeek, thirdPeek, "Multiple peeks should return same value"); + assertEquals(1, stack.getSize(), "Peek should not modify stack size"); + assertEquals(1, stack.pop(), "Element should still be poppable after peeking"); + } + + @Test + public void testAlternatingPushAndPop() { + stack.push(1); + assertEquals(1, stack.pop()); + + stack.push(2); + stack.push(3); + assertEquals(3, stack.pop()); + + stack.push(4); + assertEquals(4, stack.pop()); + assertEquals(2, stack.pop()); + + assertTrue(stack.isEmpty(), "Stack should be empty after alternating operations"); + } + + @Test + public void testToStringWithSingleElement() { + stack.push(42); + assertEquals("42", stack.toString(), "String representation with single element should not have arrows"); + } + + @Test + public void testStackIntegrity() { + // Test that internal state remains consistent + for (int i = 0; i < 10; i++) { + stack.push(i); + assertEquals(i + 1, stack.getSize(), "Size should be consistent during pushes"); + assertEquals(i, stack.peek(), "Peek should return last pushed value"); + } + + for (int i = 9; i >= 0; i--) { + assertEquals(i, stack.peek(), "Peek should return correct value before pop"); + assertEquals(i, stack.pop(), "Pop should return values in LIFO order"); + assertEquals(i, stack.getSize(), "Size should be consistent during pops"); + } + } + + @Test + public void testMixedDataTypes() { + // If your stack supports Object types, test with different data types + + stack.push(Integer.valueOf(1)); + stack.push(Integer.valueOf(2)); + + assertEquals(Integer.valueOf(2), stack.pop()); + assertEquals(Integer.valueOf(1), stack.pop()); + } } From 244897490d7ace14ede680fa33235e225b1048de Mon Sep 17 00:00:00 2001 From: alxkm Date: Mon, 21 Jul 2025 22:41:47 +0200 Subject: [PATCH 2/2] testing: fix spotbugs --- .../datastructures/stacks/StackOfLinkedListTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java index 1025f3995d57..2dfe4c242e1c 100644 --- a/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java @@ -212,8 +212,8 @@ public void testStackIntegrity() { public void testMixedDataTypes() { // If your stack supports Object types, test with different data types - stack.push(Integer.valueOf(1)); - stack.push(Integer.valueOf(2)); + stack.push(1); + stack.push(2); assertEquals(Integer.valueOf(2), stack.pop()); assertEquals(Integer.valueOf(1), stack.pop()); 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