|
| 1 | +package com.thealgorithms.strings; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +class SuffixArrayTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + void testEmptyString() { |
| 11 | + int[] result = SuffixArray.buildSuffixArray(""); |
| 12 | + assertArrayEquals(new int[] {}, result, "Empty string should return empty suffix array"); |
| 13 | + } |
| 14 | + |
| 15 | + @Test |
| 16 | + void testSingleCharacter() { |
| 17 | + int[] result = SuffixArray.buildSuffixArray("a"); |
| 18 | + assertArrayEquals(new int[] {0}, result, "Single char string should return [0]"); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + void testDistinctCharacters() { |
| 23 | + int[] result = SuffixArray.buildSuffixArray("abc"); |
| 24 | + assertArrayEquals(new int[] {0, 1, 2}, result, "Suffixes already in order for distinct chars"); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void testBananaExample() { |
| 29 | + int[] result = SuffixArray.buildSuffixArray("banana"); |
| 30 | + assertArrayEquals(new int[] {5, 3, 1, 0, 4, 2}, result, "Suffix array of 'banana' should be [5,3,1,0,4,2]"); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void testStringWithDuplicates() { |
| 35 | + int[] result = SuffixArray.buildSuffixArray("aaaa"); |
| 36 | + assertArrayEquals(new int[] {3, 2, 1, 0}, result, "Suffix array should be descending indices for 'aaaa'"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void testRandomString() { |
| 41 | + int[] result = SuffixArray.buildSuffixArray("mississippi"); |
| 42 | + assertArrayEquals(new int[] {10, 7, 4, 1, 0, 9, 8, 6, 3, 5, 2}, result, "Suffix array for 'mississippi' should match expected" |
| 43 | + ); |
| 44 | + } |
| 45 | +} |
0 commit comments