From d7eed5b30a606e94ca51b8012c2a121c5426e0bb Mon Sep 17 00:00:00 2001 From: VickySource Date: Wed, 20 Aug 2025 20:09:06 +0530 Subject: [PATCH] Migrate ReverseStackUsingRecursion tests into ReverseStackTest (#6474) --- .devcontainer/Dockerfile | 25 -------- .devcontainer/devcontainer.json | 47 --------------- .../datastructures/stacks/ReverseStack.java | 19 +++--- .../others/ReverseStackUsingRecursion.java | 44 -------------- .../ReverseStackUsingRecursionTest.java | 58 ------------------- 5 files changed, 11 insertions(+), 182 deletions(-) delete mode 100644 .devcontainer/Dockerfile delete mode 100644 .devcontainer/devcontainer.json delete mode 100644 src/main/java/com/thealgorithms/others/ReverseStackUsingRecursion.java delete mode 100644 src/test/java/com/thealgorithms/others/ReverseStackUsingRecursionTest.java diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile deleted file mode 100644 index bcea8e797ffb..000000000000 --- a/.devcontainer/Dockerfile +++ /dev/null @@ -1,25 +0,0 @@ -# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.238.0/containers/java/.devcontainer/base.Dockerfile - -# [Choice] Java version (use -bullseye variants on local arm64/Apple Silicon): 11, 17, 11-bullseye, 17-bullseye, 11-buster, 17-buster -ARG VARIANT="21-bullseye" -FROM mcr.microsoft.com/vscode/devcontainers/java:1.1.0-${VARIANT} - -# [Option] Install Maven -ARG INSTALL_MAVEN="false" -ARG MAVEN_VERSION="" -# [Option] Install Gradle -ARG INSTALL_GRADLE="false" -ARG GRADLE_VERSION="" -RUN if [ "${INSTALL_MAVEN}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/sdkman/bin/sdkman-init.sh && sdk install maven \"${MAVEN_VERSION}\""; fi \ - && if [ "${INSTALL_GRADLE}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/sdkman/bin/sdkman-init.sh && sdk install gradle \"${GRADLE_VERSION}\""; fi - -# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10 -ARG NODE_VERSION="none" -RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi - -# [Optional] Uncomment this section to install additional OS packages. -# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ -# && apt-get -y install --no-install-recommends - -# [Optional] Uncomment this line to install global node packages. -# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json deleted file mode 100644 index fdc7cdbd25f9..000000000000 --- a/.devcontainer/devcontainer.json +++ /dev/null @@ -1,47 +0,0 @@ -// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: -// https://github.com/microsoft/vscode-dev-containers/tree/v0.238.0/containers/java -{ - "name": "Java", - "build": { - "dockerfile": "Dockerfile", - "args": { - // Update the VARIANT arg to pick a Java version: 11, 17 - // Append -bullseye or -buster to pin to an OS version. - // Use the -bullseye variants on local arm64/Apple Silicon. - "VARIANT": "21-bullseye", - // Options - "INSTALL_MAVEN": "true", - "INSTALL_GRADLE": "true", - "NODE_VERSION": "lts/*" - } - }, - - // Configure tool-specific properties. - "customizations": { - // Configure properties specific to VS Code. - "vscode": { - // Set *default* container specific settings.json values on container create. - "settings": { - }, - - // Add the IDs of extensions you want installed when the container is created. - "extensions": [ - "vscjava.vscode-java-pack", - "GitHub.copilot", - ] - } - }, - - // Use 'forwardPorts' to make a list of ports inside the container available locally. - // "forwardPorts": [], - - // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": "java -version", - - // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. - "remoteUser": "vscode", - "features": { - "git": "os-provided", - "github-cli": "latest" - } -} diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java b/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java index e9de14b53302..9b69b14d878d 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java @@ -37,16 +37,19 @@ private ReverseStack() { * * @param stack the stack to reverse; should not be null */ - public static void reverseStack(Stack stack) { - if (stack.isEmpty()) { - return; - } - - int element = stack.pop(); - reverseStack(stack); - insertAtBottom(stack, element); + public static void reverseStack(Stack stack) { + if (stack == null) { + throw new IllegalArgumentException("Stack cannot be null"); + } + if (stack.isEmpty()) { + return; } + int element = stack.pop(); + reverseStack(stack); + insertAtBottom(stack, element); +} + /** * Inserts the specified element at the bottom of the stack. * diff --git a/src/main/java/com/thealgorithms/others/ReverseStackUsingRecursion.java b/src/main/java/com/thealgorithms/others/ReverseStackUsingRecursion.java deleted file mode 100644 index de36673512a0..000000000000 --- a/src/main/java/com/thealgorithms/others/ReverseStackUsingRecursion.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.thealgorithms.others; - -import java.util.Stack; - -/** - * Class that provides methods to reverse a stack using recursion. - */ -public final class ReverseStackUsingRecursion { - private ReverseStackUsingRecursion() { - } - - /** - * Reverses the elements of the given stack using recursion. - * - * @param stack the stack to be reversed - * @throws IllegalArgumentException if the stack is null - */ - public static void reverse(Stack stack) { - if (stack == null) { - throw new IllegalArgumentException("Stack cannot be null"); - } - if (!stack.isEmpty()) { - int topElement = stack.pop(); - reverse(stack); - insertAtBottom(stack, topElement); - } - } - - /** - * Inserts an element at the bottom of the given stack. - * - * @param stack the stack where the element will be inserted - * @param element the element to be inserted at the bottom - */ - private static void insertAtBottom(Stack stack, int element) { - if (stack.isEmpty()) { - stack.push(element); - } else { - int topElement = stack.pop(); - insertAtBottom(stack, element); - stack.push(topElement); - } - } -} diff --git a/src/test/java/com/thealgorithms/others/ReverseStackUsingRecursionTest.java b/src/test/java/com/thealgorithms/others/ReverseStackUsingRecursionTest.java deleted file mode 100644 index 23b99ae87d35..000000000000 --- a/src/test/java/com/thealgorithms/others/ReverseStackUsingRecursionTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.thealgorithms.others; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.Stack; -import org.junit.jupiter.api.Test; - -public class ReverseStackUsingRecursionTest { - - @Test - void testReverseWithMultipleElements() { - Stack stack = new Stack<>(); - for (int i = 0; i < 5; i++) { - stack.push(i); - } - - ReverseStackUsingRecursion.reverse(stack); - - for (int i = 0; i < 5; i++) { - assertEquals(i, stack.pop()); - } - assertTrue(stack.isEmpty()); - } - - @Test - void testReverseWithSingleElement() { - Stack stack = new Stack<>(); - stack.push(1); - - ReverseStackUsingRecursion.reverse(stack); - - assertEquals(1, stack.pop()); - assertTrue(stack.isEmpty()); - } - - @Test - void testReverseWithEmptyStack() { - Stack stack = new Stack<>(); - - ReverseStackUsingRecursion.reverse(stack); - - assertTrue(stack.isEmpty()); - } - - @Test - void testReverseWithNullStack() { - Stack stack = null; - - Exception exception = assertThrows(IllegalArgumentException.class, () -> ReverseStackUsingRecursion.reverse(stack)); - - String expectedMessage = "Stack cannot be null"; - String actualMessage = exception.getMessage(); - - assertTrue(actualMessage.contains(expectedMessage)); - } -}