removeOverlaps() {
return ignoreOverlaps();
}
diff --git a/src/main/java/org/ahocorasick/util/ListElementRemoval.java b/src/main/java/org/ahocorasick/util/ListElementRemoval.java
deleted file mode 100644
index e229633..0000000
--- a/src/main/java/org/ahocorasick/util/ListElementRemoval.java
+++ /dev/null
@@ -1,51 +0,0 @@
-package org.ahocorasick.util;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * Helps removes elements from a list in a efficient way.
- *
- * Removing elements from an ArrayList in a naive way can lead to O(n^3)
- * running time. If the algorithm first creates a list of all the elements
- * to remove, then we for each element in this list (assume n elements) we look
- * for the element in the original list (against n elements) and when found we need
- * to remove the element and move the elements to the right (of the removed element)
- * to the left by one, the size of this operation is at worst n hence O(n^3).
- *
- * This instead makes a new list and copies over only elements we want to keep,
- * we then clear the original list and then add all of the elements to the original
- * list. This gives us (for ArrayList) a running time of O(n).
- *
- * The performance of this has not been thoroughly tested for linked list.
- *
- * This can be completely removed in java 8 as the List#removeIf() method can be used instead
- * as this already is optimised for each list implementation.
- *
- */
-public class ListElementRemoval {
-
- public interface RemoveElementPredicate {
- boolean remove( T t );
- }
-
- /**
- * Removes all elements from the list matching the given predicate.
- *
- * @param list the list from which to remove
- * @param predicate to test for removal
- * @param type of list
- */
- public static void removeIf(final List list, final RemoveElementPredicate predicate) {
- final List newList = new ArrayList<>(list.size());
-
- for(final T element : list) {
- if (!predicate.remove(element)) {
- newList.add(element);
- }
- }
-
- list.clear();
- list.addAll(newList);
- }
-}
diff --git a/src/test/java/org/ahocorasick/interval/IntervalTest.java b/src/test/java/org/ahocorasick/interval/IntervalTest.java
index 4a3598e..328b902 100644
--- a/src/test/java/org/ahocorasick/interval/IntervalTest.java
+++ b/src/test/java/org/ahocorasick/interval/IntervalTest.java
@@ -6,52 +6,62 @@ import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
-import static junit.framework.Assert.*;
+import static org.junit.Assert.*;
public class IntervalTest {
@Test
- public void construct() {
- Interval i = new Interval(1, 3);
+ public void test_construct() {
+ final Interval i = new Interval(1, 3);
assertEquals(1, i.getStart());
assertEquals(3, i.getEnd());
}
@Test
- public void size() {
+ public void test_size() {
assertEquals(3, new Interval(0, 2).size());
}
@Test
- public void intervaloverlaps() {
+ public void test_intervaloverlaps() {
assertTrue(new Interval(1, 3).overlapsWith(new Interval(2, 4)));
}
@Test
- public void intervalDoesNotOverlap() {
+ public void test_intervalDoesNotOverlap() {
assertFalse(new Interval(1, 13).overlapsWith(new Interval(27, 42)));
}
@Test
- public void pointOverlaps() {
+ public void test_pointOverlaps() {
assertTrue(new Interval(1, 3).overlapsWith(2));
}
@Test
- public void pointDoesNotOverlap() {
+ public void test_pointDoesNotOverlap() {
assertFalse(new Interval(1, 13).overlapsWith(42));
}
@Test
- public void comparable() {
- Set intervals = new TreeSet<>();
+ public void test_comparable() {
+ final Set intervals = new TreeSet<>();
intervals.add(new Interval(4, 6));
intervals.add(new Interval(2, 7));
intervals.add(new Interval(3, 4));
- Iterator it = intervals.iterator();
+ final Iterator it = intervals.iterator();
assertEquals(2, it.next().getStart());
assertEquals(3, it.next().getStart());
assertEquals(4, it.next().getStart());
}
+ @Test
+ public void test_checkToString() {
+ assertEquals("4:6", new Interval(4, 6).toString());
+ }
+
+ @Test
+ public void test_compareToNegativeTest() {
+ assertEquals(-1, new Interval(4, 6).compareTo(new Object()));
+ }
+
}
diff --git a/src/test/java/org/ahocorasick/trie/PayloadTrieTest.java b/src/test/java/org/ahocorasick/trie/PayloadTrieTest.java
index 556fa8e..a69e462 100644
--- a/src/test/java/org/ahocorasick/trie/PayloadTrieTest.java
+++ b/src/test/java/org/ahocorasick/trie/PayloadTrieTest.java
@@ -5,15 +5,14 @@ import org.ahocorasick.trie.handler.PayloadEmitHandler;
import org.ahocorasick.trie.handler.StatefulPayloadEmitHandler;
import org.junit.Test;
-import java.util.ArrayList;
+import java.util.LinkedList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import static java.util.Arrays.asList;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
public class PayloadTrieTest {
@@ -216,7 +215,7 @@ public class PayloadTrieTest {
public void ushersTestByCallback() {
PayloadTrie trie = PayloadTrie.builder().addKeywords(PRONOUNS_WITH_PAYLOADS).build();
- final List> emits = new ArrayList<>();
+ final List> emits = new LinkedList<>();
PayloadEmitHandler emitHandler = emit -> {
emits.add(emit);
return true;
@@ -448,6 +447,42 @@ public class PayloadTrieTest {
assertEquals(textSize / interval, emits.size());
}
+ @Test
+ public void test_containsMatchWithCaseInsensitive() {
+ PayloadTrie trie = PayloadTrie.builder().caseInsensitive().addKeyword("foo", "bar").build();
+
+ assertTrue(trie.containsMatch("FOOBAR"));
+ assertFalse(trie.containsMatch("FO!?AR"));
+ }
+
+ // @see https://github.com/robert-bor/aho-corasick/issues/85
+ @Test
+ public void test_wholeWords() {
+ PayloadTrie trie = PayloadTrie.builder().addKeyword("foo", "bar").onlyWholeWords().build();
+ // access via PayloadTrie.parseText(CharSequence)
+ Collection> result1 = trie.parseText("foobar");
+ // access via PayloadTrie.parseText(CharSequence, PayloadEmitHandler)
+ Collection> result2 = new LinkedList<>();
+ trie.parseText("foobar", result2::add);
+
+ assertTrue(result1.isEmpty());
+ assertEquals(result1, result2);
+ }
+
+ // @see https://github.com/robert-bor/aho-corasick/issues/85
+ @Test
+ public void test_wholeWordsWhiteSpaceSeparated() {
+ PayloadTrie trie = PayloadTrie.builder().addKeyword("foo", "bar").onlyWholeWordsWhiteSpaceSeparated().build();
+ // access via PayloadTrie.parseText(CharSequence)
+ Collection> result1 = trie.parseText("foo#bar");
+ // access via PayloadTrie.parseText(CharSequence, PayloadEmitHandler)
+ Collection> result2 = new LinkedList<>();
+ trie.parseText("foo#bar", result2::add);
+
+ assertTrue(result1.isEmpty());
+ assertEquals(result1, result2);
+ }
+
/**
* Generates a random sequence of ASCII numbers.
*
diff --git a/src/test/java/org/ahocorasick/trie/StateTest.java b/src/test/java/org/ahocorasick/trie/StateTest.java
index 0834d52..02840d9 100644
--- a/src/test/java/org/ahocorasick/trie/StateTest.java
+++ b/src/test/java/org/ahocorasick/trie/StateTest.java
@@ -2,12 +2,15 @@ package org.ahocorasick.trie;
import org.junit.Test;
-import static junit.framework.Assert.assertEquals;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.junit.Assert.*;
public class StateTest {
@Test
- public void constructSequenceOfCharacters() {
+ public void test_constructSequenceOfCharacters() {
final State rootState = new State();
rootState
.addState('a')
@@ -19,5 +22,50 @@ public class StateTest {
assertEquals(2, currentState.getDepth());
currentState = currentState.nextState('c');
assertEquals(3, currentState.getDepth());
+ currentState = currentState.nextState('F');
+ assertNull(currentState);
}
+
+ @Test
+ public void test_getStates() {
+ final State rootState = new State();
+ rootState.addState("foo");
+ final State currentState = rootState.nextState('f');
+ final Collection states = rootState.getStates();
+
+ assertEquals(1, states.size());
+ assertEquals(currentState, states.iterator().next());
+ }
+
+ @Test
+ public void test_getTransitions() {
+ final State rootState = new State();
+ rootState.addState("foo");
+ final State currentState = rootState.nextState('f');
+ final Collection transitions = rootState.getTransitions();
+
+ assertEquals(1, transitions.size());
+ assertEquals(Character.valueOf('f'), transitions.iterator().next());
+ }
+
+ @Test
+ public void test_failure() {
+ final State failureState = new State();
+ final State rootState = new State();
+ rootState.setFailure(failureState);
+
+ assertEquals(failureState, rootState.failure());
+ }
+
+ @Test
+ public void test_checkEmits() {
+ final State rootState = new State();
+ rootState.addState('a')
+ .addEmit(Collections.singleton("tag"));
+ final Collection actual = rootState.nextState('a').emit();
+
+ assertEquals(1, actual.size());
+ assertEquals("tag", actual.iterator().next());
+ }
+
}
diff --git a/src/test/java/org/ahocorasick/util/ListElementRemovalTest.java b/src/test/java/org/ahocorasick/util/ListElementRemovalTest.java
deleted file mode 100644
index 6fc508a..0000000
--- a/src/test/java/org/ahocorasick/util/ListElementRemovalTest.java
+++ /dev/null
@@ -1,52 +0,0 @@
-package org.ahocorasick.util;
-
-import org.ahocorasick.util.ListElementRemoval.RemoveElementPredicate;
-import org.junit.Test;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static java.util.Arrays.asList;
-import static org.junit.Assert.assertEquals;
-
-/**
- * Responsible for testing that elements can be removed efficiently.
- */
-public class ListElementRemovalTest {
-
- @Test
- public void test_RemoveNone() {
- final List list = createList();
- RemoveElementPredicate matchNothing = t -> false;
-
- ListElementRemoval.removeIf( list, matchNothing );
-
- assertEquals( 3, list.size() );
- }
-
- @Test
- public void test_RemoveAll() {
- final List list = createList();
- RemoveElementPredicate matchNothing = t -> true;
-
- ListElementRemoval.removeIf( list, matchNothing );
-
- assertEquals( 0, list.size() );
- }
-
- @Test
- public void test_RemoveSome() {
- final List list = createList();
- RemoveElementPredicate matchNothing =
- t -> "a".equals( t ) || "c".equals( t );
-
- ListElementRemoval.removeIf( list, matchNothing );
-
- assertEquals( 1, list.size() );
- assertEquals( "b", list.get( 0 ) );
- }
-
- private List createList() {
- return new ArrayList<>( asList( "a", "b", "c" ) );
- }
-}