mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +08:00
Add KMP String Search Algorithm (#3200)
This commit is contained in:
66
src/test/java/com/thealgorithms/searches/KMPSearchTest.java
Normal file
66
src/test/java/com/thealgorithms/searches/KMPSearchTest.java
Normal file
@ -0,0 +1,66 @@
|
||||
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class KMPSearchTest {
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void KMPSearchTestLast() {
|
||||
String txt = "ABABDABACDABABCABAB";
|
||||
String pat = "ABABCABAB";
|
||||
KMPSearch kmpSearch = new KMPSearch();
|
||||
int value = kmpSearch.KMPSearch(pat, txt);
|
||||
System.out.println(value);
|
||||
assertEquals(value, 10);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void KMPSearchTestFront() {
|
||||
String txt = "AAAAABAAABA";
|
||||
String pat = "AAAA";
|
||||
KMPSearch kmpSearch = new KMPSearch();
|
||||
int value = kmpSearch.KMPSearch(pat, txt);
|
||||
System.out.println(value);
|
||||
assertEquals(value, 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void KMPSearchTestMiddle() {
|
||||
String txt = "AAACAAAAAC";
|
||||
String pat = "AAAA";
|
||||
KMPSearch kmpSearch = new KMPSearch();
|
||||
int value = kmpSearch.KMPSearch(pat, txt);
|
||||
System.out.println(value);
|
||||
assertEquals(value, 4);
|
||||
|
||||
}
|
||||
@Test
|
||||
// valid test case
|
||||
public void KMPSearchTestNotFound() {
|
||||
String txt = "AAABAAAA";
|
||||
String pat = "AAAA";
|
||||
KMPSearch kmpSearch = new KMPSearch();
|
||||
int value = kmpSearch.KMPSearch(pat, txt);
|
||||
System.out.println(value);
|
||||
assertEquals(value, 4);
|
||||
|
||||
}
|
||||
@Test
|
||||
// not valid test case
|
||||
public void KMPSearchTest4() {
|
||||
String txt = "AABAAA";
|
||||
String pat = "AAAA";
|
||||
KMPSearch kmpSearch = new KMPSearch();
|
||||
int value = kmpSearch.KMPSearch(pat, txt);
|
||||
System.out.println(value);
|
||||
assertEquals(value, -1);
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user