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