ソースを参照

junit: parameters

Ranides Atterwim 10 年 前
コミット
7b05167e84

+ 1 - 1
assira.junit/src/main/java/net/ranides/assira/junit/QAssert.java

@@ -145,7 +145,7 @@ public final class QAssert {
         new ExceptionErasure<RuntimeException>().rethrow(cause);
         
         // not reachable, wyjątek bez wrappowania leci linię wyżej
-        throw new RuntimeException(cause); 
+        throw new RuntimeException(cause); // NOPMD
     }
     
     private static class ExceptionErasure<T extends Throwable> { // NOPMD

+ 54 - 3
assira.junit/src/main/java/net/ranides/assira/junit/QTesterSuite.java

@@ -8,15 +8,21 @@ package net.ranides.assira.junit;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.SortedSet;
 import java.util.TreeSet;
 import java.util.function.Supplier;
+import javax.annotation.Resource;
 import org.junit.Ignore;
 import org.junit.Test;
 
@@ -32,6 +38,8 @@ public class QTesterSuite {
 	
 	private final SortedSet<MethodRunner> methods = new TreeSet<>();
     
+    private final List<TesterWrapper> testers = new ArrayList<>();
+    
     private final OutputStream output;
 
     public QTesterSuite(OutputStream output) {
@@ -39,6 +47,7 @@ public class QTesterSuite {
     }
 	
 	public QTesterSuite append(Object tester) {
+        testers.add(new TesterWrapper(tester));
         for(Method m : tester.getClass().getMethods()) {
             if(Modifier.isStatic(m.getModifiers()) ) {
                 continue;
@@ -60,6 +69,13 @@ public class QTesterSuite {
 		return this;
 	}
     
+    public QTesterSuite param(String name, Object value) {
+        for(TesterWrapper t : testers) {
+            t.param(name, value);
+        }
+        return this;
+    }
+    
     public boolean run(Supplier<?> generator) {
         Class<?> type = generator.get().getClass();
         Counter counter = new Counter();
@@ -85,7 +101,7 @@ public class QTesterSuite {
 
         public MethodRunner(Object that, Method method) {
             this.supp = method.getParameterTypes()[0].equals(Supplier.class);
-            this.type = param(method);
+            this.type = typeof(method);
             this.that = that;
             this.method = method;
             this.uid = type.getName()+"#"+method.getDeclaringClass().getName() +"#" + method.getName();
@@ -129,13 +145,13 @@ public class QTesterSuite {
             try {
                 output.write(String.format(format, values).getBytes(StandardCharsets.UTF_8));
             } catch (IOException ex) {
-                throw new Error(ex);
+                throw new Error(ex); // NOPMD
             }
         }
         
     }
     
-    static Class<?> param(Method method) {
+    static Class<?> typeof(Method method) {
         Class<?> cparam = method.getParameterTypes()[0];
         if(!Supplier.class.equals(cparam)) {
             return cparam;
@@ -164,5 +180,40 @@ public class QTesterSuite {
             return ++value;
         }
     }
+    
+    private static final class TesterWrapper {
+        
+        private final Object object;
+        private final Map<String, Field> resources = new HashMap<>();
+
+        public TesterWrapper(Object object) {
+            this.object = object;
+            for(Class<?> c=object.getClass(); c!=null; c=c.getSuperclass()) {
+                for(Field f : c.getDeclaredFields()) {
+                    Resource info = f.getAnnotation(Resource.class);
+                    if(null != info) {
+                        String name = info.name();
+                        if("".equals(name)) {
+                            name = f.getName();
+                        }
+                        f.setAccessible(true);
+                        resources.put(name, f);
+                    }
+                }
+            }
+        }
+        
+        public void param(String name, Object value) {
+            if( !resources.containsKey(name) ) {
+                return;
+            }
+            try {
+                resources.get(name).set(object, value);
+            } catch (ReflectiveOperationException ex) {
+                throw QAssert.rethrow(ex);
+            } 
+        }
+        
+    }
 	
 }

+ 26 - 0
assira.junit/src/test/java/net/ranides/assira/junit/QTesterSuiteTest.java

@@ -59,6 +59,12 @@ public class QTesterSuiteTest {
         " - SUITE: net.ranides.assira.junit.mockup.TesterInterfaces$IntA [2] ForSupport.unsupported%n"+
         " - SUITE: net.ranides.assira.junit.mockup.TesterInterfaces$IntA [2] ForSupport.unsupported - UNSUPPORTED%n"
     );
+    
+    private static final List<String> EXPECTED_RES = Arrays.asList(
+        "n=ranides c=33", 
+        "n=ranides c=34", 
+        "n=ot c=34"
+    );
 
 
     @Test
@@ -108,6 +114,8 @@ public class QTesterSuiteTest {
     
     @Test
     public void testMapSupplier() {
+        TesterObjects.OUT.clear();
+        
         new QTesterSuite(System.err)
             .append(new Testers.ForMap())
             .run(() -> new TreeMap<>());
@@ -123,6 +131,24 @@ public class QTesterSuiteTest {
         });
     }
     
+    @Test
+    public void testResources() {
+        TesterObjects.OUT.clear();
+        QTesterSuite suite = new QTesterSuite(System.err)
+            .append(new Testers.ForResource());
+        suite
+            .param("name", "ranides")
+            .param("count!", 33)
+            .run(() -> new Object());
+        suite
+            .param("count!", 34)
+            .run(() -> new Object());
+        suite
+            .param("name", "ot")
+            .run(() -> new Object());
+        Assert.assertEquals(EXPECTED_RES, TesterObjects.OUT);
+    }
+    
     private static class TeeStream extends FilterOutputStream {
 
         ByteArrayOutputStream bytes = new ByteArrayOutputStream();

+ 16 - 0
assira.junit/src/test/java/net/ranides/assira/junit/mockup/Testers.java

@@ -8,6 +8,7 @@ package net.ranides.assira.junit.mockup;
 
 import java.util.Map;
 import java.util.function.Supplier;
+import javax.annotation.Resource;
 import net.ranides.assira.junit.mockup.TesterObjects.*;
 import net.ranides.assira.junit.mockup.TesterInterfaces.*;
 import org.junit.Ignore;
@@ -147,4 +148,19 @@ public class Testers {
         
     }
     
+    public static class ForResource {
+        
+        @Resource
+        private String name;
+        
+        @Resource(name="count!")
+        private int count;
+        
+        @Test
+        public void run(Object input) {
+            TesterObjects.OUT.add("n=" +name + " c="+count);
+        }
+        
+    }
+    
 }

+ 1 - 1
assira.rules/src/main/java/net/ranides/assira/rules/TestECB.java

@@ -30,7 +30,7 @@ public class TestECB extends AbstractStatisticalJavaRule {
             final DataPoint point = new DataPoint();
             point.setNode(node);
             point.setScore(1.0 * (node.getEndLine() - node.getBeginLine()));
-            point.setMessage(getMessage());
+            point.setMessage(getMessage() + " ert :):)");
             addDataPoint(point);
         }
 

+ 0 - 2
assira/src/main/java/net/ranides/assira/collection/maps/IntMap.java

@@ -6,12 +6,10 @@
  */
 package net.ranides.assira.collection.maps;
 
-import java.util.AbstractMap;
 import net.ranides.assira.collection.sets.IntSet;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.Objects;
 import java.util.Set;
 import net.ranides.assira.collection.ACollection;
 import net.ranides.assira.collection.iterators.IntIterator;

+ 0 - 147
assira/src/test/java/net/ranides/assira/collection/mockup/MapValues.java

@@ -1,147 +0,0 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/???
- */
-package net.ranides.assira.collection.mockup;
-
-import java.util.Map;
-import java.util.Random;
-import java.util.TreeMap;
-import net.ranides.assira.collection.HashComparator;
-import net.ranides.assira.collection.arrays.ArrayAllocator;
-import static net.ranides.assira.collection.mockup.IntValueUtils.$equals;
-import static net.ranides.assira.collection.mockup.IntValueUtils.$key;
-import static net.ranides.assira.collection.mockup.IntValueUtils.$value;
-import static net.ranides.assira.collection.mockup.IntValueUtils.$values;
-import static net.ranides.assira.collection.mockup.ValueEnum.B1;
-import static org.junit.Assert.fail;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public abstract class MapValues<K,V> {
-    
-    private Class<K> ckey;
-    
-    private Class<V> cval;
-    
-    private HashComparator<K> cmp[];
-    
-    public HashComparator<K> comparator(int index) {
-        return cmp[index];
-    }
-    
-    public abstract K key(ValueEnum vid);
-    
-    public abstract K key(int vno);
-    
-    public abstract V value(ValueEnum vid);
-    
-    public abstract V value(int vno);
-    
-    public K[] keys(ValueEnum... values) {
-        K[] array = ArrayAllocator.allocate(ckey, values.length);
-        for(int i=0; i<values.length; i++) {
-            array[i] = key(values[i]);
-        }
-        return array;
-    }
-
-    public K[] keys(int count) {
-        K[] array = ArrayAllocator.allocate(ckey, count);
-        for(int i=0; i<count; i++) {
-            array[i] = key(i);
-        }
-        return array;
-    }
-    
-    public V[] values(ValueEnum... values) {
-        V[] array = ArrayAllocator.allocate(cval, values.length);
-        for(int i=0; i<values.length; i++) {
-            array[i] = value(values[i]);
-        }
-        return array;
-    }
-
-    public V[] values(int count) {
-        V[] array = ArrayAllocator.allocate(cval, count);
-        for(int i=0; i<count; i++) {
-            array[i] = value(i);
-        }
-        return array;
-    }
-    
-    public Map<K,V> map(K[] keys) {
-        return map(keys, values(keys.length));
-    }
-
-    public Map<K,V> map(K[] points, V[] values) {
-        Map<K, V> target = new TreeMap<>(comparator(0));
-        for (int i = 0; i < points.length; i++) {
-            target.put(points[i], values[i]);
-        }
-        return target;
-    }
-    
-    public void put(Map<K, V> target, ValueEnum... items) {
-        for(ValueEnum item : items) {
-            target.put(key(item), value(item));
-        }
-    }
-    
-    public void put(Map<K, V> target, int... items) {
-        for (int vno : items) {
-            target.put(key(vno), value(vno));
-        }
-    }
-    
-    public Map.Entry<K, V> find(Map<K, V> target, ValueEnum key) {
-        for(Map.Entry<K, V> entry : target.entrySet()) {
-            if(equals(entry.getKey(), key)) {
-                return entry;
-            }
-        }
-        fail("Entry not found: " + key);
-        return null;
-    }
-    
-    public K[] shuffle(K[] keys) {
-		shuffle(keys, null);
-        return keys;
-	}
-    
-    public void shuffle(K[] keys, V[] values) {
-        shuffle(777, keys, values);
-    }
-
-    private void shuffle(long seed, K[] keys, V[] values) {
-		int n = keys.length;
-		Random random = new Random(seed);
-		for(int i=0; i<n; i++) {
-			int a = random.nextInt(n);
-			int b = random.nextInt(n);
-            swap(keys, a, b);
-            if(null !=values) {
-                swap(values, a, b);
-            }
-		}
-	}
-    
-    private static void swap(Object[] array, int ai, int bi) {
-        Object a = array[ai];
-        array[ai] = array[bi];
-        array[bi] = a;
-    }
-    
-    public boolean equals(K key1, K key2) {
-        return comparator(0).equals(key1, key2);
-    }
-    
-    public boolean equals(K key1, ValueEnum key2) {
-        return comparator(0).equals(key1, key(key2));
-    }
-    
-}

+ 172 - 0
assira/src/test/java/net/ranides/assira/collection/mockup/TMap.java

@@ -0,0 +1,172 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/???
+ */
+package net.ranides.assira.collection.mockup;
+
+import java.util.AbstractMap;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Random;
+import java.util.TreeMap;
+import net.ranides.assira.collection.arrays.ArrayAllocator;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public abstract class TMap<K,V> {
+    
+    public final class TItem {
+        
+        private final K key;
+        
+        private final V value;
+
+        protected TItem(K key, V value) {
+            this.key = key;
+            this.value = value;
+        }
+        
+        public K key() {
+            return key;
+        }
+        
+        public V value() {
+            return value;
+        }
+        
+        public Entry<K,V> entry() {
+            return new AbstractMap.SimpleImmutableEntry<>(key, value);
+        }
+        
+        public TItem next() {
+            return new TItem(nextKey(key), nextValue(value));
+        }
+        
+    }
+    
+    public final class TItems implements Iterable<TItem> {
+        
+        private final List<TItem> data = new ArrayList<>();
+        
+        public K[] keys() {
+            K[] array = ArrayAllocator.allocate(key(0).getClass(), data.size());
+            for(int i=0,n=data.size(); i<n; i++) {
+                array[i] = data.get(i).key();
+            }
+            return array;
+        }
+        
+        public V[] values() {
+            V[] array = ArrayAllocator.allocate(value(0).getClass(), data.size());
+            for(int i=0,n=data.size(); i<n; i++) {
+                array[i] = data.get(i).value();
+            }
+            return array;
+        }
+        
+        public Entry<K,V>[] entries() {
+            Entry<K,V> array[] = new Entry[data.size()];
+            for(int i=0, n=data.size(); i<n; i++) {
+                array[i] = data.get(i).entry();
+            }
+            return array;
+        }
+        
+        public Map<K,V> map(Comparator<K> cmp) {
+            Map<K, V> target = new TreeMap<>(cmp);
+            for (int i = 0, n=data.size(); i<n; i++) {
+                target.put(data.get(i).key(), data.get(i).value());
+            }
+            return target;
+        }
+        
+        public TItems next() {
+            TItems items = new TItems();
+            for(int i=0, n=data.size(); i<n; i++) {
+                items.data.add(data.get(i).next());
+            }
+            return items;
+        }
+        
+        public TItems shuffle(long seed) {
+            int n = data.size();
+            Random random = new Random(seed);
+            for(int i=0; i<n; i++) {
+                int a = random.nextInt(n);
+                int b = random.nextInt(n);
+                swap(a, b);
+            }
+            return this;
+        }
+
+        private void swap(int ai, int bi) {
+            TItem a = data.get(ai);
+            data.set(ai, data.get(bi));
+            data.set(bi, a);
+        }
+        
+        public TItems add(TItem item) {
+            data.add(item);
+            return this;
+        }
+        
+        public TItems add(TItems items) {
+            data.addAll(items.data);
+            return this;
+        }
+
+        @Override
+        public Iterator<TItem> iterator() {
+            return data.iterator();
+        }
+        
+    }
+    
+    protected abstract K key(int index);
+    
+    protected abstract V value(int index);
+    
+    protected abstract K nextKey(K key);
+    
+    protected abstract V nextValue(V value);
+    
+    public TItem item(int index) {
+        return new TItem(key(index), value(index));
+    }
+    
+    public TItems list(int... indexes) {
+        TItems ret = new TItems();
+        for(int i=0; i<indexes.length; i++) {
+            ret.data.add(item(indexes[i]));
+        }
+        return ret;
+    }
+    
+    public TItems range(int begin, int end) {
+        TItems ret = new TItems();
+        for(int i=begin; i<end; i++) {
+            ret.data.add(item(i));
+        }
+        return ret;
+    }
+    
+    public TItems urange(int begin, int end) {
+        TItems items = range(begin, end);
+        return new TItems().add(items).add(items).add(items.next()).shuffle(777);
+    }
+    
+    public void put(Map<K,V> target, TItems items) {
+        for(TItem item : items) {
+            target.put(item.key(), item.value());
+        }
+    }
+
+}

+ 14 - 11
assira/src/test/java/net/ranides/assira/collection/suite/AHashLookupTester.java

@@ -6,9 +6,9 @@
  */
 package net.ranides.assira.collection.suite;
 
+import javax.annotation.Resource;
 import net.ranides.assira.collection.lookups.AHashLookup;
-import net.ranides.assira.collection.mockup.TPoint;
-import static net.ranides.assira.collection.mockup.TPointUtils.*;
+import net.ranides.assira.collection.mockup.TMap;
 import net.ranides.assira.junit.QAssert;
 import org.junit.Test;
 
@@ -16,32 +16,35 @@ import org.junit.Test;
  *
  * @author Ranides Atterwim <ranides@gmail.com>
  */
-public final class AHashLookupTester {
+public final class AHashLookupTester<K> {
+    
+    @Resource
+    private TMap<K,Integer> $map;
 
     @Test
-    public void basicTrim(AHashLookup<TPoint> target) {
-        $put(target, $ukeys(1024));
+    public void basicTrim(AHashLookup<K> target) {
+        $map.put(target, $map.urange(0, 1024));
         target.clear();
-        $put(target, $ukeys(128));
+        $map.put(target, $map.urange(0, 128));
         target.trim();
     }
     
     @Test
-    public void basicTrimN1(AHashLookup<TPoint> target) {
+    public void basicTrimN1(AHashLookup<K> target) {
         basicTrim(target, 5*32, 32, 1024);
     }
     
     @Test
-    public void basicTrimN2(AHashLookup<TPoint> target) {
+    public void basicTrimN2(AHashLookup<K> target) {
         QAssert.assertThrows(IllegalArgumentException.class, () -> 
             basicTrim(target, 5*32, 32, 1)
         );
     }
     
-    private static void basicTrim(AHashLookup<TPoint> target, int put1st, int put2nd, int n) {
-        $put(target, $ukeys(put1st));
+    private void basicTrim(AHashLookup<K> target, int put1st, int put2nd, int n) {
+        $map.put(target, $map.urange(0, put1st));
         target.clear();
-        $put(target, $ukeys(put2nd));
+        $map.put(target, $map.urange(0, put2nd));
         target.trim(n);
     }