|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
4 | 4 |
|
5 |
| -import org.junit.jupiter.api.Test; |
| 5 | +import java.util.stream.Stream; |
| 6 | +import org.junit.jupiter.api.DisplayName; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.Arguments; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
6 | 10 |
|
7 | 11 | public class LongestCommonPrefixTest {
|
8 | 12 |
|
9 |
| - private final LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix(); |
10 |
| - |
11 |
| - @Test |
12 |
| - public void testCommonPrefix() { |
13 |
| - String[] input = {"flower", "flow", "flight"}; |
14 |
| - String expected = "fl"; |
15 |
| - assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input)); |
16 |
| - } |
17 |
| - |
18 |
| - @Test |
19 |
| - public void testNoCommonPrefix() { |
20 |
| - String[] input = {"dog", "racecar", "car"}; |
21 |
| - String expected = ""; |
22 |
| - assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input)); |
23 |
| - } |
24 |
| - |
25 |
| - @Test |
26 |
| - public void testEmptyArray() { |
27 |
| - String[] input = {}; |
28 |
| - String expected = ""; |
29 |
| - assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input)); |
30 |
| - } |
31 |
| - |
32 |
| - @Test |
33 |
| - public void testNullArray() { |
34 |
| - String[] input = null; |
35 |
| - String expected = ""; |
36 |
| - assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input)); |
37 |
| - } |
38 |
| - |
39 |
| - @Test |
40 |
| - public void testSingleString() { |
41 |
| - String[] input = {"single"}; |
42 |
| - String expected = "single"; |
43 |
| - assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input)); |
44 |
| - } |
45 |
| - |
46 |
| - @Test |
47 |
| - public void testCommonPrefixWithDifferentLengths() { |
48 |
| - String[] input = {"ab", "a"}; |
49 |
| - String expected = "a"; |
50 |
| - assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input)); |
51 |
| - } |
52 |
| - |
53 |
| - @Test |
54 |
| - public void testAllSameStrings() { |
55 |
| - String[] input = {"test", "test", "test"}; |
56 |
| - String expected = "test"; |
57 |
| - assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input)); |
58 |
| - } |
59 |
| - |
60 |
| - @Test |
61 |
| - public void testPrefixAtEnd() { |
62 |
| - String[] input = {"abcde", "abcfgh", "abcmnop"}; |
63 |
| - String expected = "abc"; |
64 |
| - assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input)); |
| 13 | + @ParameterizedTest(name = "{index} => input={0}, expected=\"{1}\"") |
| 14 | + @MethodSource("provideTestCases") |
| 15 | + @DisplayName("Test Longest Common Prefix") |
| 16 | + void testLongestCommonPrefix(String[] input, String expected) { |
| 17 | + assertEquals(expected, LongestCommonPrefix.longestCommonPrefix(input)); |
65 | 18 | }
|
66 | 19 |
|
67 |
| - @Test |
68 |
| - public void testMixedCase() { |
69 |
| - String[] input = {"Flower", "flow", "flight"}; |
70 |
| - String expected = ""; |
71 |
| - assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input)); |
| 20 | + private static Stream<Arguments> provideTestCases() { |
| 21 | + 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"), |
| 22 | + Arguments.of(new String[] {"test", "test", "test"}, "test"), Arguments.of(new String[] {"abcde", "abcfgh", "abcmnop"}, "abc"), Arguments.of(new String[] {"Flower", "flow", "flight"}, "")); |
72 | 23 | }
|
73 | 24 | }
|
0 commit comments