Skip to content

Commit 3408977

Browse files
authored
test: refactor PalindromeTest (#5365)
1 parent a6fcbf5 commit 3408977

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package com.thealgorithms.strings;
22

3+
import java.util.stream.Stream;
34
import org.junit.jupiter.api.Assertions;
4-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.MethodSource;
57

68
public class PalindromeTest {
9+
private static Stream<TestData> provideTestCases() {
10+
return Stream.of(new TestData(null, true), new TestData("", true), new TestData("aba", true), new TestData("123321", true), new TestData("kayak", true), new TestData("abb", false), new TestData("abc", false), new TestData("abc123", false), new TestData("kayaks", false));
11+
}
712

8-
@Test
9-
public void palindrome() {
10-
11-
String[] palindromes = {null, "", "aba", "123321", "kayak"};
12-
for (String s : palindromes) {
13-
Assertions.assertTrue(Palindrome.isPalindrome(s) && Palindrome.isPalindromeRecursion(s) && Palindrome.isPalindromeTwoPointer(s));
14-
}
13+
@ParameterizedTest
14+
@MethodSource("provideTestCases")
15+
void testPalindrome(TestData testData) {
16+
Assertions.assertEquals(testData.expected, Palindrome.isPalindrome(testData.input) && Palindrome.isPalindromeRecursion(testData.input) && Palindrome.isPalindromeTwoPointer(testData.input));
17+
}
1518

16-
String[] notPalindromes = {"abb", "abc", "abc123", "kayaks"};
17-
for (String s : notPalindromes) {
18-
Assertions.assertFalse(Palindrome.isPalindrome(s) || Palindrome.isPalindromeRecursion(s) || Palindrome.isPalindromeTwoPointer(s));
19-
}
19+
private record TestData(String input, boolean expected) {
2020
}
2121
}

0 commit comments

Comments
 (0)