refactor: ReverseString, test improvements (#5406)

* refactor: ReverseString

* refactor: refactor testing into two methods

* checkstyle: fix formatting

* checkstyle: fix formatting

---------

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko 2024-08-27 13:00:11 +02:00 committed by GitHub
parent e3ad3761fd
commit 49d1c84cb7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 25 deletions

View File

@ -7,11 +7,6 @@ public final class ReverseString {
private 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 * easiest way to reverses the string str and returns it
* *

View File

@ -2,30 +2,27 @@ package com.thealgorithms.strings;
import static org.junit.jupiter.api.Assertions.assertEquals; 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 { public class ReverseStringTest {
@Test private static Stream<Arguments> testCases() {
public void testReverseString() { 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"),
String input1 = "Hello World"; Arguments.of(" leading and trailing spaces ", " secaps gniliart dna gnidael "), Arguments.of("!@#$%^&*()", ")(*&^%$#@!"), Arguments.of("MixOf123AndText!", "!txeTdnA321fOxiM"));
String input2 = "helloworld"; }
String input3 = "123456789";
String input4 = "";
String expectedOutput1 = "dlroW olleH"; @ParameterizedTest
String expectedOutput2 = "dlrowolleh"; @MethodSource("testCases")
String expectedOutput3 = "987654321"; public void testReverseString(String input, String expectedOutput) {
String expectedOutput4 = ""; assertEquals(expectedOutput, ReverseString.reverse(input));
}
assertEquals(ReverseString.reverse(input1), expectedOutput1); @ParameterizedTest
assertEquals(ReverseString.reverse(input2), expectedOutput2); @MethodSource("testCases")
assertEquals(ReverseString.reverse(input3), expectedOutput3); public void testReverseString2(String input, String expectedOutput) {
assertEquals(ReverseString.reverse(input4), expectedOutput4); assertEquals(expectedOutput, ReverseString.reverse2(input));
assertEquals(ReverseString.reverse2(input1), expectedOutput1);
assertEquals(ReverseString.reverse2(input2), expectedOutput2);
assertEquals(ReverseString.reverse2(input3), expectedOutput3);
assertEquals(ReverseString.reverse2(input4), expectedOutput4);
} }
} }