Pārlūkot izejas kodu

IndexMap: tests

Ranides Atterwim 9 gadi atpakaļ
vecāks
revīzija
8ada6b42ad

+ 54 - 211
assira.drafts/src/test/java/net/ranides/assira/collections/map/impl/CIndexTreeMapTest.java

@@ -6,7 +6,9 @@
  */
 package net.ranides.assira.collections.map.impl;
 
+import java.util.Objects;
 import net.ranides.assira.collections.map.IndexMap;
+import net.ranides.assira.collections.map.IndexQuery;
 import net.ranides.assira.generic.CompareUtils;
 import org.junit.Test;
 import static org.junit.Assert.*;
@@ -26,138 +28,43 @@ public class CIndexTreeMapTest {
         db.index("surname", (a,b) -> CompareUtils.cmp(a.surname, b.surname));
         db.index("age", (a,b) -> CompareUtils.cmp(a.age, b.age));
         
-        db.put(new Person("Tom", "A", 17));
-        db.put(new Person("Tom", "B", 23));
-        db.put(new Person("Joe", "A", 20));
-        db.put(new Person("Helene", "C", 19));
-        db.put(new Person("Andrew", "S", 23));
-        db.put(new Person("Helene", "C", 23));
-        db.put(new Person("George", "T", 17));
-        db.put(new Person("Peter", "A", 17));
+        putData(db);
         
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[{Helene C 19}, {Helene C 23}]", db.find()
             .eq("name", new Person("Helene", "", 0))
-            .sort()
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[{Tom B 23}, {Helene C 23}, {Andrew S 23}]", db.find()
             .eq("age", new Person("", "", 23))
-            .sort()
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[{Helene C 23}]", db.find()
             .eq("name", new Person("Helene", "", 0))
             .eq("age", new Person("", "", 23))
-            .sort()
-            .list()
-        );
-        System.out.printf("RESULT = %s%n", db.find()
-            .eq("age", new Person("", "", 17))
-            .sort()
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
-            .eq("surname", new Person("", "A", 3))
+        assertQueryEquals("[{Peter A 17}, {Tom A 17}, {George T 17}]", db.find()
             .eq("age", new Person("", "", 17))
-            .sort()    
-            .list()
-        );
-        System.out.printf("RESULT = %s%n", db.find()
-            .gt("age", new Person("", "", 17))
-            .sort()
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
-            .gt("age", new Person("", "", 40))
-            .sort()
-            .list()
-        );
-        System.out.printf("RESULT = %s%n", db.find()
-            .lt("age", new Person("", "", 18))
-            .sort()
-            .list()
-        );
-        System.out.printf("RESULT = %s%n", db.find()
-            .lt("age", new Person("", "", 17))
-            .sort()
-            .list()
-        );
-        System.out.printf("RESULT = %s%n", db.find()
-            .eq("name", new Person("Helene", "", 23))
-            .lt("age", new Person("Helene", "", 23))
-            .sort()
-            .list()
-        );
-        
-        
-        
-        
-        
-        assertEquals("[{Helene C 19}, {Helene C 23}]", db.find()
-            .eq("name", new Person("Helene", "", 0))
-            .sort()
-            .list()
-            .toString()
-        );
-        assertEquals("[{Tom B 23}, {Helene C 23}, {Andrew S 23}]", db.find()
-            .eq("age", new Person("", "", 23))
-            .sort()
-            .list()
-            .toString()
-        );
-        assertEquals("[{Helene C 23}]", db.find()
-            .eq("name", new Person("Helene", "", 0))
-            .eq("age", new Person("", "", 23))
-            .sort()
-            .list()
-            .toString()
-        );
-        assertEquals("[{Peter A 17}, {Tom A 17}, {George T 17}]", db.find()
-            .eq("age", new Person("", "", 17))
-            .sort()
-            .list()
-            .toString()
-        );
-        assertEquals("[{Peter A 17}, {Tom A 17}]", db.find()
+        assertQueryEquals("[{Peter A 17}, {Tom A 17}]", db.find()
             .eq("surname", new Person("", "A", 0))
             .eq("age", new Person("", "", 17))
-            .sort()    
-            .list()
-            .toString()
         );
-        assertEquals("[{Joe A 20}, {Tom B 23}, {Helene C 19}, {Helene C 23}, {Andrew S 23}]", db.find()
+        assertQueryEquals("[{Joe A 20}, {Tom B 23}, {Helene C 19}, {Helene C 23}, {Andrew S 23}]", db.find()
             .gt("age", new Person("", "", 17))
-            .sort()
-            .list()
-            .toString()
         );
-        assertEquals("[]", db.find()
+        assertQueryEquals("[]", db.find()
             .gt("age", new Person("", "", 40))
-            .sort()
-            .list()
-            .toString()
         );
-        assertEquals("[{Peter A 17}, {Tom A 17}, {George T 17}]", db.find()
+        assertQueryEquals("[{Peter A 17}, {Tom A 17}, {George T 17}]", db.find()
             .lt("age", new Person("", "", 18))
-            .sort()
-            .list()
-            .toString()
         );
-        assertEquals("[]", db.find()
+        assertQueryEquals("[]", db.find()
             .lt("age", new Person("", "", 17))
-            .sort()
-            .list()
-            .toString()
         );
-        assertEquals("[{Helene C 19}]", db.find()
+        assertQueryEquals("[{Helene C 19}]", db.find()
             .eq("name", new Person("Helene", "", 23))
             .lt("age", new Person("Helene", "", 23))
-            .sort()
-            .list()
-            .toString()
         );
     }
-    
+
     @Test
     public void testMapIndex() {
         
@@ -167,136 +74,58 @@ public class CIndexTreeMapTest {
         db.index("surname", a -> a.surname);
         db.index("age", a -> a.age);
         
-        db.put(new Person("Tom", "A", 17));
-        db.put(new Person("Tom", "B", 23));
-        db.put(new Person("Joe", "A", 20));
-        db.put(new Person("Helene", "C", 19));
-        db.put(new Person("Andrew", "S", 23));
-        db.put(new Person("Helene", "C", 23));
-        db.put(new Person("George", "T", 17));
-        db.put(new Person("Peter", "A", 17));
+        putData(db);
         
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[{Helene C 19}, {Helene C 23}]", db.find()
             .eq("name", "Helene")
-            .sort()
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[{Tom B 23}, {Helene C 23}, {Andrew S 23}]", db.find()
             .eq("age", 23)
-            .sort()
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[{Helene C 23}]", db.find()
             .eq("name", "Helene")
             .eq("age", 23)
-            .sort()
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[{Peter A 17}, {Tom A 17}, {George T 17}]", db.find()
             .eq("age", 17)
-            .sort()
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[{Peter A 17}, {Tom A 17}]", db.find()
             .eq("surname", "A")
             .eq("age", 17)
-            .sort()    
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[{Joe A 20}, {Tom B 23}, {Helene C 19}, {Helene C 23}, {Andrew S 23}]", db.find()
             .gt("age", 17)
-            .sort()
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[]", db.find()
             .gt("age", 40)
-            .sort()
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[{Peter A 17}, {Tom A 17}, {George T 17}]", db.find()
             .lt("age", 18)
-            .sort()
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[]", db.find()
             .lt("age", 17)
-            .sort()
-            .list()
         );
-        System.out.printf("RESULT = %s%n", db.find()
+        assertQueryEquals("[{Helene C 19}]", db.find()
             .eq("name", "Helene")
             .lt("age", 23)
-            .sort()
-            .list()
-        );
-        
-        
-        
-        assertEquals("[{Helene C 19}, {Helene C 23}]", db.find()
-            .eq("name", "Helene")
-            .sort()
-            .list()
-            .toString()
-        );
-        assertEquals("[{Tom B 23}, {Helene C 23}, {Andrew S 23}]", db.find()
-            .eq("age", 23)
-            .sort()
-            .list()
-            .toString()
-        );
-        assertEquals("[{Helene C 23}]", db.find()
-            .eq("name", "Helene")
-            .eq("age", 23)
-            .sort()
-            .list()
-            .toString()
-        );
-        assertEquals("[{Peter A 17}, {Tom A 17}, {George T 17}]", db.find()
-            .eq("age", 17)
-            .sort()
-            .list()
-            .toString()
-        );
-        assertEquals("[{Peter A 17}, {Tom A 17}]", db.find()
-            .eq("surname", "A")
-            .eq("age", 17)
-            .sort()    
-            .list()
-            .toString()
-        );
-        assertEquals("[{Joe A 20}, {Tom B 23}, {Helene C 19}, {Helene C 23}, {Andrew S 23}]", db.find()
-            .gt("age", 17)
-            .sort()
-            .list()
-            .toString()
-        );
-        assertEquals("[]", db.find()
-            .gt("age", 40)
-            .sort()
-            .list()
-            .toString()
-        );
-        assertEquals("[{Peter A 17}, {Tom A 17}, {George T 17}]", db.find()
-            .lt("age", 18)
-            .sort()
-            .list()
-            .toString()
-        );
-        assertEquals("[]", db.find()
-            .lt("age", 17)
-            .sort()
-            .list()
-            .toString()
-        );
-        assertEquals("[{Helene C 19}]", db.find()
-            .eq("name", "Helene")
-            .lt("age", 23)
-            .sort()
-            .list()
-            .toString()
         );
     }
     
+    private static void assertQueryEquals(String expected, IndexQuery<Person> query) {
+        assertEquals(expected, query.sort().list().toString());
+    }
+
+    private static void putData(IndexMap<Person> db) {
+        db.put(new Person("Tom", "A", 17));
+        db.put(new Person("Tom", "B", 23));
+        db.put(new Person("Joe", "A", 20));
+        db.put(new Person("Helene", "C", 19));
+        db.put(new Person("Andrew", "S", 23));
+        db.put(new Person("Helene", "C", 23));
+        db.put(new Person("George", "T", 17));
+        db.put(new Person("Peter", "A", 17));
+    }
+    
     private static final class Person implements Comparable<Person> {
         
         public final String name;
@@ -314,6 +143,20 @@ public class CIndexTreeMapTest {
             return "{" + name + " " + surname + " " + age + "}";
         }
 
+        @Override
+        public int hashCode() {
+            int hash = 7;
+            hash = 47 * hash + Objects.hashCode(this.name);
+            hash = 47 * hash + Objects.hashCode(this.surname);
+            hash = 47 * hash + this.age;
+            return hash;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            return this == obj || ((obj instanceof Person) && 0 == compareTo((Person) obj));
+        }
+        
         @Override
         public int compareTo(Person that) {
             int c;