mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
Add tests for HorspoolSearch (#4165)
This commit is contained in:
@ -100,6 +100,10 @@ public class HorspoolSearch {
|
||||
shiftValues = calcShiftValues(pattern); // build the bad symbol table
|
||||
comparisons = 0; // reset comparisons
|
||||
|
||||
if (pattern.length() == 0) { // return failure, if pattern empty
|
||||
return -1;
|
||||
}
|
||||
|
||||
int textIndex = pattern.length() - 1; // align pattern with text start and get index of the last character
|
||||
|
||||
// while pattern is not out of text bounds
|
||||
|
@ -0,0 +1,88 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class HorspoolSearchTest {
|
||||
|
||||
@Test
|
||||
void testFindFirstMatch() {
|
||||
int index = HorspoolSearch.findFirst("World", "Hello World");
|
||||
assertEquals(6, index);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindFirstNotMatch() {
|
||||
int index = HorspoolSearch.findFirst("hell", "Hello World");
|
||||
assertEquals(-1, index);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindFirstPatternLongerText() {
|
||||
int index = HorspoolSearch.findFirst("Hello World!!!", "Hello World");
|
||||
assertEquals(-1, index);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindFirstPatternEmpty() {
|
||||
int index = HorspoolSearch.findFirst("", "Hello World");
|
||||
assertEquals(-1, index);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindFirstTextEmpty() {
|
||||
int index = HorspoolSearch.findFirst("Hello", "");
|
||||
assertEquals(-1, index);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindFirstPatternAndTextEmpty() {
|
||||
int index = HorspoolSearch.findFirst("", "");
|
||||
assertEquals(-1, index);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindFirstSpecialCharacter() {
|
||||
int index = HorspoolSearch.findFirst("$3**", "Hello $3**$ World");
|
||||
assertEquals(6, index);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindFirstInsensitiveMatch() {
|
||||
int index = HorspoolSearch.findFirstInsensitive("hello", "Hello World");
|
||||
assertEquals(0, index);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindFirstInsensitiveNotMatch() {
|
||||
int index = HorspoolSearch.findFirstInsensitive("helo", "Hello World");
|
||||
assertEquals(-1, index);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetLastComparisons() {
|
||||
HorspoolSearch.findFirst("World", "Hello World");
|
||||
int lastSearchNumber = HorspoolSearch.getLastComparisons();
|
||||
assertEquals(7, lastSearchNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetLastComparisonsNotMatch() {
|
||||
HorspoolSearch.findFirst("Word", "Hello World");
|
||||
int lastSearchNumber = HorspoolSearch.getLastComparisons();
|
||||
assertEquals(3, lastSearchNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindFirstPatternNull() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> HorspoolSearch.findFirst(null, "Hello World"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindFirstTextNull() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> HorspoolSearch.findFirst("Hello", null));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user