Fixed infinite skipTo and candidate list in NextSpans.

Change-Id: I92f950146c318a5a6b0b0b097121222951f4739c
diff --git a/Changes b/Changes
index 61d9b4c..96b0ae5 100644
--- a/Changes
+++ b/Changes
@@ -1,4 +1,4 @@
-0.58.1 2018-11-12
+0.58.1 2018-11-27
     - [bugfix] Security upgrade of Jackson for CVE-2017-17485 and
       CVE-2018-7489 (diewald)
     - [bugfix] Span expansion with negation (margaretha)
@@ -10,7 +10,7 @@
     - [bugfix] Fixed skipTo in NextSpans, see de.ids_mannheim.korap.index.
       TestRepetitionIndex.testRepetitionSnippetBug3() (margaretha)
     - [bugfix] Fixed the candidate list in NextSpans, see de.ids_mannheim.
-      korap.index.TestNextIndex.testNextExpansion() (margaretha)   
+      korap.index.TestNextIndex.testNextExpansionBug() (margaretha)   
 
 0.58.0 2018-09-03
     - [feature] Implemented referencing cached collection (margaretha)
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/NextSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/NextSpans.java
index fb5b254..fc915c8 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/NextSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/NextSpans.java
@@ -178,7 +178,8 @@
             if (cs.getStart() == firstSpans.end()) {
                 addMatch(cs);
             }
-            else if (cs.getEnd() < firstSpans.end()){
+            else if (cs.getEnd() < firstSpans.end() 
+                    && cs.getStart() <firstSpans.start()){
                 i.remove();
             }
         }
@@ -268,15 +269,16 @@
                           secondSpans.end(),
                           secondSpans.doc());
             };
+            
+            if (hasMoreFirstSpan){
+                setMatchList();
+            }
+            else {
+                hasMoreSpans = false;
+                candidateList.clear();
+            }
         }
         matchPayload.clear();
-        if (hasMoreFirstSpan){
-            setMatchList();
-        }
-        else {
-            hasMoreSpans = false;
-            candidateList.clear();
-        }
         return advance();
     }
 
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestNextIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestNextIndex.java
index 93ee0ab..7a19b66 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestNextIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestNextIndex.java
@@ -4,6 +4,9 @@
 import static org.junit.Assert.assertEquals;
 
 import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.regex.Pattern;
 
 import org.apache.lucene.index.Term;
 import org.apache.lucene.search.spans.SpanOrQuery;
@@ -14,21 +17,57 @@
 import org.junit.runners.JUnit4;
 
 import de.ids_mannheim.korap.KrillIndex;
+import de.ids_mannheim.korap.TestSimple;
 import de.ids_mannheim.korap.query.SpanClassQuery;
 import de.ids_mannheim.korap.query.SpanElementQuery;
 import de.ids_mannheim.korap.query.SpanExpansionQuery;
 import de.ids_mannheim.korap.query.SpanFocusQuery;
 import de.ids_mannheim.korap.query.SpanNextQuery;
 import de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper;
+import de.ids_mannheim.korap.response.Match;
 import de.ids_mannheim.korap.response.Result;
+import de.ids_mannheim.korap.util.QueryException;
 
 @RunWith(JUnit4.class)
 public class TestNextIndex {
 
     // Todo: primary data as a non-indexed field separated.
+    
+//    @Test
+    public void fuzzyTest () throws IOException, QueryException {
+        List<String> chars = Arrays.asList("a", "b", "c", "c","d", "e");
 
+        // c c a
+        SpanTermQuery stq = new SpanTermQuery(new Term("base", "s:c"));
+        SpanTermQuery stq2 = new SpanTermQuery(new Term("base", "s:a"));
+        SpanNextQuery snq = new SpanNextQuery(stq, stq);
+        SpanNextQuery snq2 = new SpanNextQuery(snq, stq2);
+
+        Pattern resultPattern = Pattern.compile("cca");
+        TestSimple.fuzzingTest(chars, resultPattern, snq2,
+                5, 10, 8);
+    }
+    
     @Test
-    public void testNextExpansion () throws IOException {
+    public void testInfiniteSkipTo () throws IOException {
+        KrillIndex ki = new KrillIndex();
+        ki.addDoc(simpleFieldDoc("ddc"));
+        ki.addDoc(simpleFieldDoc("cc"));
+        ki.addDoc(simpleFieldDoc("abedaed"));
+        ki.commit();
+
+        //cca
+        SpanTermQuery stq = new SpanTermQuery(new Term("base", "s:c"));
+        SpanTermQuery stq2 = new SpanTermQuery(new Term("base", "s:a"));
+        SpanNextQuery snq = new SpanNextQuery(stq, stq);
+        SpanNextQuery snq2 = new SpanNextQuery(snq, stq2);
+        
+        Result kr = ki.search(snq2, (short) 10);
+        assertEquals(0, kr.getTotalResults());
+    }
+    
+    @Test
+    public void testNextExpansionBug () throws IOException {
         KrillIndex ki = new KrillIndex();
         ki.addDoc(simpleFieldDoc("ccecc"));
         ki.commit();
@@ -48,7 +87,7 @@
     }
     
     @Test
-    public void testNextExpansion2 () throws IOException {
+    public void testNextExpansionBug2 () throws IOException {
         KrillIndex ki = new KrillIndex();
         ki.addDoc(simpleFieldDoc("cccc"));
         ki.commit();
@@ -65,7 +104,24 @@
         // 1-3 1-4 1-5 2-4 2-5 3-5
         assertEquals(6, kr.getTotalResults());
     }
-
+    
+    @Test
+    public void testNextExpansionBug3 () throws IOException {
+        KrillIndex ki = new KrillIndex();
+        ki.addDoc(simpleFieldDoc("r a d m d d v b", " "));
+        ki.commit();
+        
+        SpanTermQuery stq = new SpanTermQuery(new Term("base", "s:d"));
+        SpanExpansionQuery seq = new SpanExpansionQuery(stq, 0, 4, 0, true);
+        Result kr = ki.search(seq, (short) 20);
+        
+        SpanNextQuery snq = new SpanNextQuery(seq, stq);
+        kr = ki.search(snq, (short) 10);
+        
+        // 2-5, 2-6, 4-6
+        assertEquals(3, kr.getTotalResults());
+    }
+    
     @Test
     public void indexExample1 () throws IOException {
         KrillIndex ki = new KrillIndex();
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
index 8a78b62..118dc2c 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
@@ -48,6 +48,7 @@
      * @throws IOException
      * @throws QueryException
      */
+//    @Test
     public void fuzzyTest () throws IOException, QueryException {
         List<String> chars = Arrays.asList("a", "b", "c", "d", "e");
 
@@ -59,7 +60,7 @@
 
         Pattern resultPattern = Pattern.compile("c[a-e]{0,2}a");
         TestSimple.fuzzingTest(chars, resultPattern, snq,
-                6, 20, 8,1);
+                6, 20, 8);
     }
     
     @Test