Testing Z Algorithm String Pattern Matching in williamfiset/Algorithms
This test suite validates the Z Algorithm implementation for string pattern matching in Java. It comprehensively tests various string patterns and edge cases to ensure correct Z-array computation for string matching applications.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
williamfiset/algorithms
src/test/java/com/williamfiset/algorithms/strings/ZAlgorithmTest.java
package com.williamfiset.algorithms.strings;
import static com.google.common.truth.Truth.assertThat;
import org.junit.jupiter.api.*;
public class ZAlgorithmTest {
private ZAlgorithm underTest;
@BeforeEach
public void setup() {
underTest = new ZAlgorithm();
}
@Test
public void shouldReturnEmptyArrayOnNullOrEmptyInput() {
assertThat(underTest.calculateZ(null)).isEmpty();
assertThat(underTest.calculateZ("")).isEmpty();
}
@Test
public void textContainsASingleCharacterRepeated() {
assertThat(underTest.calculateZ("aaaaaaa")).isEqualTo(new int[] {7, 6, 5, 4, 3, 2, 1});
assertThat(underTest.calculateZ("bbbbbbbb")).isEqualTo(new int[] {8, 7, 6, 5, 4, 3, 2, 1});
}
@Test
public void textContainsAllDistinctCharacters() {
assertThat(underTest.calculateZ("abcdefgh")).isEqualTo(new int[] {8, 0, 0, 0, 0, 0, 0, 0});
}
@Test
public void textContainsRepeatedPattern() {
assertThat(underTest.calculateZ("abababab")).isEqualTo(new int[] {8, 0, 6, 0, 4, 0, 2, 0});
assertThat(underTest.calculateZ("ababababa")).isEqualTo(new int[] {9, 0, 7, 0, 5, 0, 3, 0, 1});
assertThat(underTest.calculateZ("abcabcabca"))
.isEqualTo(new int[] {10, 0, 0, 7, 0, 0, 4, 0, 0, 1});
}
}