Skip to content

Commit 8a0e2b4

Browse files
authored
adding test for Suffix Array
1 parent 76f7a42 commit 8a0e2b4

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)