Add TestUnorderedDistanceIndex
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java
index a1ff477..44ce601 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java
@@ -289,5 +289,23 @@
 	    assertEquals(9, kr.getMatch(0).endPos);	    
     }
     
+    /** Same tokens */
+    @Test
+    public void testCase7() throws IOException{    	
+    	ki = new KorapIndex();    	
+	    ki.addDoc(createFieldDoc1());
+	    ki.commit();
+	    	    
+	    SpanQuery firstClause = createQuery("s:c", "s:c", 1, 2,true);
+	    kr = ki.search(firstClause, (short) 10); 
+    
+	    assertEquals(3, kr.totalResults());
+        assertEquals(0, kr.getMatch(0).startPos);
+	    assertEquals(3, kr.getMatch(0).endPos);
+	    assertEquals(2, kr.getMatch(1).startPos);
+	    assertEquals(4, kr.getMatch(1).endPos);
+	    assertEquals(3, kr.getMatch(2).startPos);
+	    assertEquals(6, kr.getMatch(2).endPos);
+    }
     
 }
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceIndex.java
index 90b1d8a..c6d6277 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceIndex.java
@@ -54,7 +54,7 @@
 	
 	private FieldDocument createFieldDoc2() {
     	FieldDocument fd = new FieldDocument();
-        fd.addString("ID", "doc-0");
+        fd.addString("ID", "doc-2");
         fd.addTV("base",
             "text",             
             "[(0-1)s:b|_1#0-1|<>:p#0-2$<i>1]" +
@@ -68,7 +68,7 @@
 	
 	private FieldDocument createFieldDoc3() {
     	FieldDocument fd = new FieldDocument();
-        fd.addString("ID", "doc-0");
+        fd.addString("ID", "doc-3");
         fd.addTV("base",
             "text",             
             "[(0-1)s:b|_1#0-1|<>:s#0-2$<i>1]" +
@@ -88,7 +88,7 @@
         		new SpanTermQuery(new Term("base",y)), 
         		minDistance, 
         		maxDistance, 
-        		true,
+        		isOrdered,
         		true);
 	}
 	
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestUnorderedDistanceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestUnorderedDistanceIndex.java
index 217e67f..72c735c 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestUnorderedDistanceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestUnorderedDistanceIndex.java
@@ -187,15 +187,7 @@
 	    assertEquals(6,kr.getMatch(0).getEndPos());
 	    assertEquals(3,kr.getMatch(1).getStartPos());
 	    assertEquals(6,kr.getMatch(1).getEndPos());
-	    
-//	    System.out.print(kr.getTotalResults()+"\n");
-//			for (int i=0; i< kr.getTotalResults(); i++){
-//				System.out.println(
-//					kr.match(i).getLocalDocID()+" "+
-//					kr.match(i).startPos + " " +
-//					kr.match(i).endPos
-//			    );
-//			}
+
 	}
 	
 	 /** ElementQueries */    
@@ -245,8 +237,8 @@
      * 	WARNING: 
      * 	This kind of query is not appropriate for an unordered distance span query.
      * 	Instead, it must be an ordered distance span query. Such an unordered distance 
-     * 	span query yields "redundant results" because matches are found for each child 
-     * 	span.
+     * 	span query yields "redundant results" because matches are searched for each 
+     * 	child span.
      * */
     @Test
     public void testCase6() throws IOException{    	
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestUnorderedElementDistanceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestUnorderedElementDistanceIndex.java
new file mode 100644
index 0000000..75244eb
--- /dev/null
+++ b/src/test/java/de/ids_mannheim/korap/index/TestUnorderedElementDistanceIndex.java
@@ -0,0 +1,213 @@
+package de.ids_mannheim.korap.index;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.spans.SpanQuery;
+import org.apache.lucene.search.spans.SpanTermQuery;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import de.ids_mannheim.korap.KorapIndex;
+import de.ids_mannheim.korap.KorapResult;
+import de.ids_mannheim.korap.query.SpanDistanceQuery;
+import de.ids_mannheim.korap.query.SpanElementQuery;
+import de.ids_mannheim.korap.query.SpanNextQuery;
+
+@RunWith(JUnit4.class)
+public class TestUnorderedElementDistanceIndex {
+	
+	private KorapIndex ki;
+	private KorapResult kr;
+	
+	private FieldDocument createFieldDoc0() {
+    	FieldDocument fd = new FieldDocument();
+        fd.addString("ID", "doc-0");
+        fd.addTV("base",
+            "text",             
+            "[(0-1)s:b|s:c|_1#0-1|<>:s#0-2$<i>1]" +
+            "[(1-2)s:b|_2#1-2]" +             
+            "[(2-3)s:b|_3#2-3|<>:s#2-3$<i>3]" +
+            "[(3-4)s:c|_4#3-4|<>:s#3-4$<i>4]" + 
+            "[(4-5)s:b|_5#4-5|<>:s#4-5$<i>5]" +             
+            "[(5-6)s:b|_6#5-6]" +
+            "[(6-7)s:c|_7#6-7|<>:s#6-7$<i>7]");
+        return fd;
+	}
+	
+	private FieldDocument createFieldDoc1() {
+    	FieldDocument fd = new FieldDocument();
+        fd.addString("ID", "doc-1");
+        fd.addTV("base",
+            "text",             
+            "[(0-1)s:b|_1#0-1|<>:s#0-2$<i>1]" +
+            "[(1-2)s:c|_2#1-2|<>:s#1-2$<i>4]" +             
+            "[(2-3)s:e|_3#2-3]" +
+            "[(3-4)s:c|_4#3-4]" + 
+            "[(4-5)s:b|_5#4-5|<>:s#4-5$<i>7]" +             
+            "[(5-6)s:e|_6#5-6]" +
+    		"[(6-7)s:e|_7#6-7]");	
+        return fd;
+	}
+	
+	private FieldDocument createFieldDoc2() {
+    	FieldDocument fd = new FieldDocument();
+        fd.addString("ID", "doc-2");
+        fd.addTV("base",
+            "text",             
+            "[(0-1)s:e|_1#0-1|<>:p#0-2$<i>1]" +
+            "[(1-2)s:c|_2#1-2|<>:p#1-2$<i>2]" +             
+            "[(2-3)s:d|_3#2-3|<>:p#2-3$<i>3]" +
+            "[(3-4)s:b|_4#3-4|<>:p#3-4$<i>4]" + 
+            "[(4-5)s:d|_5#4-5|<>:p#4-5$<i>5]" +             
+            "[(5-6)s:c|_6#5-6|<>:p#5-6$<i>6]" +
+    		"[(6-7)s:e|_7#6-7|<>:p#6-7$<i>7]" +
+        	"[(7-8)s:b|_8#7-8|<>:p#7-8$<i>8]");	
+        return fd;
+	}
+	
+	private FieldDocument createFieldDoc3() {
+    	FieldDocument fd = new FieldDocument();
+        fd.addString("ID", "doc-3");
+        fd.addTV("base",
+            "text",             
+            "[(0-1)s:a|_1#0-1|<>:s#0-2$<i>1]" +
+            "[(1-2)s:d|_2#1-2|<>:s#1-2$<i>3]" +             
+            "[(2-3)s:e|_3#2-3]");	
+        return fd;
+	}
+	
+	private FieldDocument createFieldDoc4() {
+    	FieldDocument fd = new FieldDocument();
+        fd.addString("ID", "doc-4");
+        fd.addTV("base",
+            "text",             
+            "[(0-1)s:c|_1#0-1|<>:s#0-2$<i>2]" +
+            "[(1-2)s:e|_2#1-2]" +             
+            "[(2-3)s:b|_3#2-3|<>:s#2-3$<i>3]" +
+            "[(3-4)s:e|_4#3-4|<>:s#3-4$<i>4]");
+        return fd;
+	}
+	
+	public SpanQuery createQuery(String elementType, String x, String y, 
+			int minDistance, int maxDistance, boolean isOrdered){        
+		return new SpanDistanceQuery(
+        		new SpanElementQuery("base", elementType), 
+        		new SpanTermQuery(new Term("base",x)), 
+        		new SpanTermQuery(new Term("base",y)), 
+        		minDistance, 
+        		maxDistance, 
+        		isOrdered,
+        		true);
+	}
+	
+	/** Only terms within an element are matched. 
+	 * */
+	@Test
+	public void testCase1() throws IOException{
+		//System.out.println("testCase1");
+		ki = new KorapIndex();
+        ki.addDoc(createFieldDoc0());
+        ki.commit();
+        
+        SpanQuery sq;        
+        sq = createQuery("s", "s:b", "s:c", 0, 1,false);        
+        kr = ki.search(sq, (short) 10);
+		
+        assertEquals(5,kr.getTotalResults());
+        assertEquals(0, kr.getMatch(0).startPos);
+        assertEquals(1, kr.getMatch(0).endPos);
+        assertEquals(0, kr.getMatch(1).startPos);
+        assertEquals(3, kr.getMatch(1).endPos);
+        assertEquals(2, kr.getMatch(2).startPos);
+        assertEquals(4, kr.getMatch(2).endPos);
+        assertEquals(3, kr.getMatch(3).startPos);
+        assertEquals(5, kr.getMatch(3).endPos);
+        assertEquals(4, kr.getMatch(4).startPos);
+        assertEquals(7, kr.getMatch(4).endPos);        
+
+	}
+	
+	/** Ensure same doc.
+	 * 	In the beginning, first and second spans are already too far from each other
+	 * 	(one-list-empty case, both-list-empty case). 
+	 * */
+	@Test
+	public void testCase2() throws IOException{
+		//System.out.println("testCase2");
+		ki = new KorapIndex();
+        ki.addDoc(createFieldDoc0());
+        ki.addDoc(createFieldDoc1());
+        ki.addDoc(createFieldDoc2());
+        ki.commit();
+        
+        SpanQuery sq;        
+        sq = createQuery("p", "s:b", "s:e", 0, 2,false);        
+        kr = ki.search(sq, (short) 10);
+        
+        assertEquals(1,kr.getTotalResults());
+        assertEquals(6, kr.getMatch(0).startPos);
+        assertEquals(8, kr.getMatch(0).endPos);   
+    }
+	
+	/** Multiple occurrences in an element. 	 
+	 * */
+	@Test
+	public void testCase3() throws IOException{
+		//System.out.println("testCase3");
+		ki = new KorapIndex();
+        ki.addDoc(createFieldDoc1());
+        ki.commit();
+        
+        SpanQuery sq;        
+        sq = createQuery("s", "s:c", "s:e", 1, 2,false);        
+        kr = ki.search(sq, (short) 10);
+	
+        assertEquals(4,kr.getTotalResults());
+        assertEquals(1, kr.getMatch(0).startPos);
+        assertEquals(6, kr.getMatch(0).endPos);
+        assertEquals(1, kr.getMatch(1).startPos);
+        assertEquals(7, kr.getMatch(1).endPos);
+        assertEquals(3, kr.getMatch(2).startPos);
+        assertEquals(6, kr.getMatch(2).endPos);
+        assertEquals(3, kr.getMatch(3).startPos);
+        assertEquals(7, kr.getMatch(3).endPos);
+	}
+	
+	/** Multiple documents  
+	 * 	Skip to */
+	@Test
+	public void testCase4() throws IOException{
+		//System.out.println("testCase4");
+		ki = new KorapIndex();		
+        ki.addDoc(createFieldDoc1());
+        ki.addDoc(createFieldDoc0());        
+        ki.addDoc(createFieldDoc3());
+        ki.addDoc(createFieldDoc4());
+        ki.commit();
+        
+        SpanQuery sq, edq;
+        edq = createQuery("s", "s:b", "s:c", 1, 1,false);
+		
+        sq = new SpanNextQuery(edq, 
+        		new SpanTermQuery(new Term("base", "s:e")));
+        
+        kr = ki.search(sq, (short) 10);
+        
+        assertEquals(4,kr.getTotalResults());
+        assertEquals(0, kr.getMatch(0).startPos);
+        assertEquals(3, kr.getMatch(0).endPos);
+        assertEquals(1, kr.getMatch(1).startPos);
+        assertEquals(6, kr.getMatch(1).endPos);
+        assertEquals(3, kr.getMatch(2).startPos);
+        assertEquals(6, kr.getMatch(2).endPos);
+        assertEquals(3,kr.getMatch(3).getLocalDocID());
+        assertEquals(0, kr.getMatch(3).startPos);
+        assertEquals(4, kr.getMatch(3).endPos);       
+
+    }
+	
+}