From 49d1c84cb7740613fcee5d6846843c3a8e123e6e Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Tue, 27 Aug 2024 13:00:11 +0200 Subject: [PATCH] refactor: `ReverseString`, test improvements (#5406) * refactor: ReverseString * refactor: refactor testing into two methods * checkstyle: fix formatting * checkstyle: fix formatting --------- Co-authored-by: alxkm --- .../thealgorithms/strings/ReverseString.java | 5 --- .../strings/ReverseStringTest.java | 37 +++++++++---------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/ReverseString.java b/src/main/java/com/thealgorithms/strings/ReverseString.java index b47a77e9..46a0494f 100644 --- a/src/main/java/com/thealgorithms/strings/ReverseString.java +++ b/src/main/java/com/thealgorithms/strings/ReverseString.java @@ -7,11 +7,6 @@ public final class ReverseString { private ReverseString() { } - public static void main(String[] args) { - assert reverse("abc123").equals("321cba"); - assert reverse2("abc123").equals("321cba"); - } - /** * easiest way to reverses the string str and returns it * diff --git a/src/test/java/com/thealgorithms/strings/ReverseStringTest.java b/src/test/java/com/thealgorithms/strings/ReverseStringTest.java index e73f7afc..501f7029 100644 --- a/src/test/java/com/thealgorithms/strings/ReverseStringTest.java +++ b/src/test/java/com/thealgorithms/strings/ReverseStringTest.java @@ -2,30 +2,27 @@ package com.thealgorithms.strings; import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; public class ReverseStringTest { - @Test - public void testReverseString() { - String input1 = "Hello World"; - String input2 = "helloworld"; - String input3 = "123456789"; - String input4 = ""; + private static Stream testCases() { + return Stream.of(Arguments.of("Hello World", "dlroW olleH"), Arguments.of("helloworld", "dlrowolleh"), Arguments.of("123456789", "987654321"), Arguments.of("", ""), Arguments.of("A", "A"), Arguments.of("ab", "ba"), + Arguments.of(" leading and trailing spaces ", " secaps gniliart dna gnidael "), Arguments.of("!@#$%^&*()", ")(*&^%$#@!"), Arguments.of("MixOf123AndText!", "!txeTdnA321fOxiM")); + } - String expectedOutput1 = "dlroW olleH"; - String expectedOutput2 = "dlrowolleh"; - String expectedOutput3 = "987654321"; - String expectedOutput4 = ""; + @ParameterizedTest + @MethodSource("testCases") + public void testReverseString(String input, String expectedOutput) { + assertEquals(expectedOutput, ReverseString.reverse(input)); + } - assertEquals(ReverseString.reverse(input1), expectedOutput1); - assertEquals(ReverseString.reverse(input2), expectedOutput2); - assertEquals(ReverseString.reverse(input3), expectedOutput3); - assertEquals(ReverseString.reverse(input4), expectedOutput4); - - assertEquals(ReverseString.reverse2(input1), expectedOutput1); - assertEquals(ReverseString.reverse2(input2), expectedOutput2); - assertEquals(ReverseString.reverse2(input3), expectedOutput3); - assertEquals(ReverseString.reverse2(input4), expectedOutput4); + @ParameterizedTest + @MethodSource("testCases") + public void testReverseString2(String input, String expectedOutput) { + assertEquals(expectedOutput, ReverseString.reverse2(input)); } }