Ranides Atterwim 11 lat temu
rodzic
commit
72a905d5a5

+ 20 - 17
assira/src/main/java/net/ranides/assira/collection/lookups/SortedLookup.java

@@ -95,7 +95,7 @@ public abstract class SortedLookup<K> extends Lookup<K> implements SortedMap<K,
      */
     @Override
     public final ASortedSet<LookupEntry<K>> fastEntrySet() {
-		return new EntrySet();
+		return new EntrySet(this);
 	} 
 	
 	@Override
@@ -369,20 +369,23 @@ public abstract class SortedLookup<K> extends Lookup<K> implements SortedMap<K,
 		
     }
 
-	protected class EntrySet extends ASortedSet<LookupEntry<K>> {
+	protected static class EntrySet<K> extends ASortedSet<LookupEntry<K>> {
+        
+        private final SortedLookup<K> that;
 
-		public EntrySet() {
-			super(SortedLookup.this.entrySet().comparator());
+		public EntrySet(SortedLookup<K> that) {
+            super((entry1, entry2) -> that.acmp.compare(entry1.getKey(), entry2.getKey()));
+            this.that = that;
 		}
 		
 		@Override
 		public Iterator<LookupEntry<K>> iterator() {
-			return entryIterator();
+			return that.entryIterator();
 		}
 
 		@Override
 		public Iterator<LookupEntry<K>> iterator(LookupEntry<K> from) {
-			return entryIterator(from.getKey());
+			return that.entryIterator(from.getKey());
 		}
 
 		@SuppressWarnings("unchecked")
@@ -392,7 +395,7 @@ public abstract class SortedLookup<K> extends Lookup<K> implements SortedMap<K,
 				return false;
 			}
 			Map.Entry<K, Integer> entry = (Map.Entry<K, Integer>) object;
-			LookupEntry<K> found = findEntry(entry.getKey());
+			LookupEntry<K> found = that.findEntry(entry.getKey());
 			return entry.equals(found);
 		}
 
@@ -403,9 +406,9 @@ public abstract class SortedLookup<K> extends Lookup<K> implements SortedMap<K,
 				return false;
 			}
 			Map.Entry<K, Integer> entry = (Map.Entry<K, Integer>) object;
-			LookupEntry<K> found = findEntry(entry.getKey());
+			LookupEntry<K> found = that.findEntry(entry.getKey());
 			if (found != null) {
-				SortedLookup.this.removeInt(found.getIntValue());
+				that.removeInt(found.getIntValue());
 			}
 			return found != null;
 		}
@@ -421,44 +424,44 @@ public abstract class SortedLookup<K> extends Lookup<K> implements SortedMap<K,
 
 		@Override
 		public boolean isEmpty() {
-			return !entryIterator().hasNext();
+			return !that.entryIterator().hasNext();
 		}
 
 		@Override
 		public void clear() {
-			SortedLookup.this.clear();
+			that.clear();
 		}
 
 		@Override
 		public LookupEntry<K> first() {
-			return SortedLookup.this.firstEntry();
+			return that.firstEntry();
 		}
 
 		@Override
 		public LookupEntry<K> last() {
-			return SortedLookup.this.lastEntry();
+			return that.lastEntry();
 		}
 
 		@Override
 		public ASortedSet<LookupEntry<K>> subSet(LookupEntry<K> begin, LookupEntry<K> end) {
-			return subMap(begin.getKey(), end.getKey()).fastEntrySet();
+			return that.subMap(begin.getKey(), end.getKey()).fastEntrySet();
 		}
 
 		@Override
 		public ASortedSet<LookupEntry<K>> headSet(LookupEntry<K> end) {
-			return headMap(end.getKey()).fastEntrySet();
+			return that.headMap(end.getKey()).fastEntrySet();
 		}
 
 		@Override
 		public ASortedSet<LookupEntry<K>> tailSet(LookupEntry<K> begin) {
-			return tailMap(begin.getKey()).fastEntrySet();
+			return that.tailMap(begin.getKey()).fastEntrySet();
 		}
 		
 		@Override
 		protected ASortedSet<LookupEntry<K>> subset(LookupEntry<K> begin, boolean bottom, LookupEntry<K> end, boolean top) {
 			K begkey = (null == begin) ? null : begin.getKey();
 			K endkey = (null == end) ? null : end.getKey();
-			return submap(begkey, bottom, endkey, top).fastEntrySet();
+			return that.submap(begkey, bottom, endkey, top).fastEntrySet();
 		}
 	}
 	

+ 92 - 0
assira/src/test/java/net/ranides/assira/collection/lookups/AVLTreeLookupTest.java

@@ -0,0 +1,92 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/???
+ */
+package net.ranides.assira.collection.lookups;
+
+import java.util.Map;
+import net.ranides.assira.collection.mockup.ALookupTester;
+import net.ranides.assira.collection.mockup.TPoint;
+import net.ranides.assira.collection.mockup.TPointGenerator;
+import static net.ranides.assira.junit.QAssert.*;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ * @todo (assira # 1) test: review & backport
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class AVLTreeLookupTest {
+    
+    private final ALookupTester tester = new ALookupTester(new TPointGenerator());
+
+    @Test
+    public void testConstruct() {
+        assertNotNull( new AVLTreeLookup<>() );
+        assertNotNull( new AVLTreeLookup<>(TPoint.SUM) );
+    }
+    
+    @Test
+    public void testConstructCopy() {
+        TPointGenerator generator = new TPointGenerator();
+        Map<TPoint, Integer> imap = generator.map(generator.ukeys(60));
+        
+//        assertEquals(120, new AVLTreeLookup<>(imap, TPoint.SUM).size());
+        
+//        assertEquals(120, new AVLTreeLookup<>(new CustomLookup<>(imap, TPoint.SUM), TPoint.SUM).size());
+//        assertEquals(120, new AVLTreeLookup<>(new CustomLookup<>(imap, TPoint.SUM), TPoint.STD).size());
+//        assertEquals(120, new AVLTreeLookup<>(new CustomLookup<>(imap, TPoint.STD), TPoint.SUM).size());
+//        assertEquals(180, new AVLTreeLookup<>(new CustomLookup<>(imap, TPoint.STD), TPoint.STD).size());
+        
+        assertEquals(120, new AVLTreeLookup<>(generator.ukeys(60), generator.values(180), TPoint.SUM).size());
+        
+        assertThrows(IllegalArgumentException.class, () -> 
+            new AVLTreeLookup<>(generator.ukeys(60), generator.values(10), TPoint.SUM)
+        );
+    }
+    
+    @Test
+    public void testPut() {
+        tester.basicPutInt(new AVLTreeLookup<>(TPoint.SUM));
+        tester.basicPutInt(new AVLTreeLookup<>(TPoint.SUM), 128);
+        tester.basicReplaceInt(new AVLTreeLookup<>(TPoint.SUM), 40);
+        
+        tester.basicPutInteger(new AVLTreeLookup<>(TPoint.SUM));
+        tester.basicPutInteger(new AVLTreeLookup<>(TPoint.SUM), 128);
+        tester.basicReplaceInteger(new AVLTreeLookup<>(TPoint.SUM), 40);
+        
+        assertTrue(true);
+    }
+
+    @Test
+    public void testRemove() {
+        tester.basicRemoveInt(new AVLTreeLookup<>(TPoint.SUM));
+        tester.basicRemoveInt(new AVLTreeLookup<>(TPoint.SUM), 97);
+        tester.logicRemoveInt(new AVLTreeLookup<>(TPoint.SUM));
+        
+        tester.basicRemoveInteger(new AVLTreeLookup<>(TPoint.SUM));
+        tester.basicRemoveInteger(new AVLTreeLookup<>(TPoint.SUM), 97);
+        tester.logicRemoveInteger(new AVLTreeLookup<>(TPoint.SUM));
+        
+        assertTrue(true);
+    }
+    
+    @Test
+    public void testGet() {
+        tester.basicGetInt( new AVLTreeLookup<>(TPoint.SUM) );
+        tester.basicGetInteger( new AVLTreeLookup<>(TPoint.SUM) );
+        
+        assertTrue(true);
+    }
+    
+    @Test
+    public void testContainsValue() {
+        tester.basicContainsInt( new AVLTreeLookup<>(TPoint.SUM) );
+        tester.basicContainsInteger( new AVLTreeLookup<>(TPoint.SUM) );
+        
+        assertTrue(true);
+    }
+    
+}

+ 1 - 1
assira/src/test/java/net/ranides/assira/collection/mockup/ALookupTester.java

@@ -179,7 +179,7 @@ public class ALookupTester extends TPointTester {
         assertEquals(0, target.getInt($key(A1)));
         
         put(target, A1, B1, C1);
-        
+        assertEquals(3, target.size());
         assertEquals(0, target.getInt($key(A1_NE)));
         
         assertEquals($value(A1), target.getInt($key(A1)));

+ 11 - 11
assira/src/test/java/net/ranides/assira/collection/mockup/TPointGenerator.java

@@ -19,21 +19,21 @@ public final class TPointGenerator {
     //      punkty hashcode < 100
     //      punkty suma < 100
     //      wartości mniejsze < 100
-    
+
     private final TPoint[] mkeys = new TPoint[]{
-        new TPoint(7, 4, 6), 
-        new TPoint(7, 1, 9), 
-        new TPoint(7, 1, 2), 
-        new TPoint(8, 4, 5),
-        new TPoint(9, 4, 13)
+        new TPoint(7, 4, 6), // A1
+        new TPoint(7, 1, 9), // A1_EQ
+        new TPoint(7, 1, 2), // A1_NE
+        new TPoint(8, 4, 12), // B1
+        new TPoint(9, 4, 13) // C1
     };
     
     private final int[] mvalues = new int[]{
-        17, 
-        19, 
-        20, 
-        21,
-        22
+        17, // A1
+        19, // A1_EQ
+        20, // A1_NE
+        21, // B1
+        22  // C1
     };
 
     public TPoint key(TPointEnum type) {