refactor: adding docs for LongestCommonPrefixTest and Parameterized Tests (#6360)

* refactor: adding docs for LongestCommonPrefixTest and Parameterized Tests

* checkstyle: fix clang formatting

---------

Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
This commit is contained in:
Oleksandr Klymenko
2025-07-11 22:39:33 +03:00
committed by GitHub
parent 3e0fd11a96
commit 048bba9499
2 changed files with 38 additions and 67 deletions

View File

@@ -2,72 +2,23 @@ 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.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class LongestCommonPrefixTest {
private final LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
@Test
public void testCommonPrefix() {
String[] input = {"flower", "flow", "flight"};
String expected = "fl";
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
@ParameterizedTest(name = "{index} => input={0}, expected=\"{1}\"")
@MethodSource("provideTestCases")
@DisplayName("Test Longest Common Prefix")
void testLongestCommonPrefix(String[] input, String expected) {
assertEquals(expected, LongestCommonPrefix.longestCommonPrefix(input));
}
@Test
public void testNoCommonPrefix() {
String[] input = {"dog", "racecar", "car"};
String expected = "";
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
}
@Test
public void testEmptyArray() {
String[] input = {};
String expected = "";
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
}
@Test
public void testNullArray() {
String[] input = null;
String expected = "";
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
}
@Test
public void testSingleString() {
String[] input = {"single"};
String expected = "single";
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
}
@Test
public void testCommonPrefixWithDifferentLengths() {
String[] input = {"ab", "a"};
String expected = "a";
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
}
@Test
public void testAllSameStrings() {
String[] input = {"test", "test", "test"};
String expected = "test";
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
}
@Test
public void testPrefixAtEnd() {
String[] input = {"abcde", "abcfgh", "abcmnop"};
String expected = "abc";
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
}
@Test
public void testMixedCase() {
String[] input = {"Flower", "flow", "flight"};
String expected = "";
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
private static Stream<Arguments> provideTestCases() {
return Stream.of(Arguments.of(new String[] {"flower", "flow", "flight"}, "fl"), Arguments.of(new String[] {"dog", "racecar", "car"}, ""), Arguments.of(new String[] {}, ""), Arguments.of(null, ""), Arguments.of(new String[] {"single"}, "single"), Arguments.of(new String[] {"ab", "a"}, "a"),
Arguments.of(new String[] {"test", "test", "test"}, "test"), Arguments.of(new String[] {"abcde", "abcfgh", "abcmnop"}, "abc"), Arguments.of(new String[] {"Flower", "flow", "flight"}, ""));
}
}