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:
parent
e3ad3761fd
commit
49d1c84cb7
@ -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
|
||||||
*
|
*
|
||||||
|
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user