Skip to content

Commit 766600c

Browse files
author
alxkm
committed
refactor: ReverseString
1 parent c5b73ec commit 766600c

File tree

2 files changed

+7
-27
lines changed

2 files changed

+7
-27
lines changed

src/main/java/com/thealgorithms/strings/ReverseString.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ public final class ReverseString {
77
private ReverseString() {
88
}
99

10-
public static void main(String[] args) {
11-
assert reverse("abc123").equals("321cba");
12-
assert reverse2("abc123").equals("321cba");
13-
}
14-
1510
/**
1611
* easiest way to reverses the string str and returns it
1712
*

src/test/java/com/thealgorithms/strings/ReverseStringTest.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,15 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
public class ReverseStringTest {
89

9-
@Test
10-
public void testReverseString() {
11-
String input1 = "Hello World";
12-
String input2 = "helloworld";
13-
String input3 = "123456789";
14-
String input4 = "";
15-
16-
String expectedOutput1 = "dlroW olleH";
17-
String expectedOutput2 = "dlrowolleh";
18-
String expectedOutput3 = "987654321";
19-
String expectedOutput4 = "";
20-
21-
assertEquals(ReverseString.reverse(input1), expectedOutput1);
22-
assertEquals(ReverseString.reverse(input2), expectedOutput2);
23-
assertEquals(ReverseString.reverse(input3), expectedOutput3);
24-
assertEquals(ReverseString.reverse(input4), expectedOutput4);
25-
26-
assertEquals(ReverseString.reverse2(input1), expectedOutput1);
27-
assertEquals(ReverseString.reverse2(input2), expectedOutput2);
28-
assertEquals(ReverseString.reverse2(input3), expectedOutput3);
29-
assertEquals(ReverseString.reverse2(input4), expectedOutput4);
10+
@ParameterizedTest
11+
@CsvSource({"'Hello World', 'dlroW olleH'", "'helloworld', 'dlrowolleh'", "'123456789', '987654321'", "'', ''", "'A', 'A'", "'ab', 'ba'", "' leading and trailing spaces ', ' secaps gniliart dna gnidael '", "'!@#$%^&*()', ')(*&^%$#@!'", "'MixOf123AndText!', '!txeTdnA321fOxiM'"})
12+
public void testReverseString(String input, String expectedOutput) {
13+
assertEquals(expectedOutput, ReverseString.reverse(input));
14+
assertEquals(expectedOutput, ReverseString.reverse2(input));
3015
}
3116
}

0 commit comments

Comments
 (0)