diff --git a/src/com/jwetherell/algorithms/data_structures/timing/DataStructuresTiming.java b/src/com/jwetherell/algorithms/data_structures/timing/DataStructuresTiming.java index c3132e7d..d4a4f0b9 100644 --- a/src/com/jwetherell/algorithms/data_structures/timing/DataStructuresTiming.java +++ b/src/com/jwetherell/algorithms/data_structures/timing/DataStructuresTiming.java @@ -1,5 +1,6 @@ package com.jwetherell.algorithms.data_structures.timing; +import java.lang.reflect.Array; import java.text.DecimalFormat; import java.util.Arrays; import java.util.Collection; @@ -36,24 +37,21 @@ public class DataStructuresTiming { private static final Random RANDOM = new Random(); private static final DecimalFormat FORMAT = new DecimalFormat("0.##"); - private static final int NUMBER_OF_TESTS = 3; // There will always be NUMBER_OF_TESTS+1 tests run, since the first round is thrown away (JITing) + private static final int NUMBER_OF_TESTS = 3; // There will always be NUMBER_OF_TESTS+1 runs since the first round is thrown away (JITing) private static final int ARRAY_SIZE = 1024*20; // Number of items to add/remove/look-up from each data structure private static final int RANDOM_SIZE = 1000 * ARRAY_SIZE; private static final Integer INVALID = RANDOM_SIZE + 10; - private static Integer[] unsorted = null; - private static Integer[] sorted = null; - private static String string = null; + private static final int TESTS = 39; // Max number of dynamic data structures to test + private static final String[] testNames = new String[TESTS]; // Array to hold the test names + private static final long[][] testResults = new long[TESTS][]; // Array to hold the test results - private static boolean firstTimeThru = true; // We throw away the first set of data to avoid JITing private static int debug = 1; // Debug level. 0=None, 1=Time and Memory (if enabled), 2=Time, Memory, data structure debug private static boolean debugTime = true; // How much time to: add all, remove all, add all items in reverse order, remove all private static boolean debugMemory = true; // How much memory is used by the data structure - private static final int TESTS = 39; // Max number of dynamic data structures to test - private static final String[] testNames = new String[TESTS]; // Array to hold the test names - private static final long[][] testResults = new long[TESTS][]; // Array to hold the test results private static int testIndex = 0; // Index into the tests + private static boolean firstTimeThru = true; // We throw away the first set of data to avoid JITing public static void main(String[] args) { System.out.println("Starting tests."); @@ -61,7 +59,6 @@ public static void main(String[] args) { try { passed = runTests(); } catch (NullPointerException e) { - System.err.println(string); throw e; } if (passed) System.out.println("Tests finished. All passed."); @@ -95,7 +92,7 @@ private static void generateTestData(int idx, int size, Integer[][] unsorteds, I set = null; builder.append('\n'); strings[idx] = builder.toString(); - if (debug > 1) System.out.println(string); + if (debug > 1) System.out.println(strings[idx]); sorteds[idx] = Arrays.copyOf(unsorteds[idx], unsorteds[idx].length); Arrays.sort(sorteds[idx]); @@ -106,8 +103,6 @@ private static void generateTestData(int idx, int size, Integer[][] unsorteds, I private static boolean runTests() { testIndex = 0; - boolean passed = true; - // requested number of tests plus the warm-up round int tests = NUMBER_OF_TESTS+1; Integer[][] unsorteds = new Integer[tests][]; @@ -115,839 +110,158 @@ private static boolean runTests() { String[] strings = new String[tests]; for (int i=0; i avlTree = new AVLTree(); - private static boolean testAVLTree() { - String avlName = "AVL Tree "; + public static abstract class Testable { + String input = null; + public String getInput() { + return input; + } + public abstract String getName(); + public abstract boolean run(Integer[] unsorted, Integer[] sorted, String input); + } + + private static class testAVLTree extends Testable { + BinarySearchTree avlTree = new AVLTree(); + String name = "AVL Tree "; Collection bstCollection = avlTree.toCollection(); - if (!testJavaCollection(bstCollection,Integer.class,avlName)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(bstCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final BTree bTree = new BTree(2); - private static boolean testBTree() { - String bTreeName = "B-Tree "; + private static class testBTree extends Testable { + BTree bTree = new BTree(2); + String name = "B-Tree "; Collection bstCollection = bTree.toCollection(); - if (!testJavaCollection(bstCollection,Integer.class,bTreeName)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(bstCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final BinarySearchTree bst = new BinarySearchTree(); - private static boolean testBST() { + private static class testBST extends Testable { + BinarySearchTree bst = new BinarySearchTree(); String name = "BST "; Collection bstCollection = bst.toCollection(); - if (!testJavaCollection(bstCollection,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(bstCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final BinaryHeap.BinaryHeapArray aHeapMin = new BinaryHeap.BinaryHeapArray(BinaryHeap.Type.MIN); - private static final BinaryHeap.BinaryHeapTree tHeapMin = new BinaryHeap.BinaryHeapTree(BinaryHeap.Type.MIN); - private static boolean testMinHeap() { - String aNameMin = "Min-Heap [array]"; + private static class testMinHeapArray extends Testable { + BinaryHeap.BinaryHeapArray aHeapMin = new BinaryHeap.BinaryHeapArray(BinaryHeap.Type.MIN); + String name = "Min-Heap [array]"; Collection aCollectionMin = aHeapMin.toCollection(); - if (!testJavaCollection(aCollectionMin,Integer.class,aNameMin)) return false; - String tNameMin = "Min-Heap [tree]"; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(aCollectionMin,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + + } + + private static class testMinHeapTree extends Testable { + BinaryHeap.BinaryHeapTree tHeapMin = new BinaryHeap.BinaryHeapTree(BinaryHeap.Type.MIN); + String name = "Min-Heap [tree]"; Collection tCollectionMin = tHeapMin.toCollection(); - if (!testJavaCollection(tCollectionMin,Integer.class,tNameMin)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(tCollectionMin,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final BinaryHeap.BinaryHeapArray aHeapMax = new BinaryHeap.BinaryHeapArray(BinaryHeap.Type.MAX); - private static final BinaryHeap.BinaryHeapTree tHeapMax = new BinaryHeap.BinaryHeapTree(BinaryHeap.Type.MAX); - private static boolean testMaxHeap() { - String aNameMax = "Max-Heap [array]"; + private static class testMaxHeapArray extends Testable { + BinaryHeap.BinaryHeapArray aHeapMax = new BinaryHeap.BinaryHeapArray(BinaryHeap.Type.MAX); + String name = "Max-Heap [array]"; Collection aCollectionMax = aHeapMax.toCollection(); - if (!testJavaCollection(aCollectionMax,Integer.class,aNameMax)) return false; - String lNameMax = "Max-Heap [tree]"; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(aCollectionMax,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + + } + + private static class testMaxHeapTree extends Testable { + BinaryHeap.BinaryHeapTree tHeapMax = new BinaryHeap.BinaryHeapTree(BinaryHeap.Type.MAX); + String name = "Max-Heap [tree]"; Collection tCollectionMax = tHeapMax.toCollection(); - if (!testJavaCollection(tCollectionMax,Integer.class,lNameMax)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(tCollectionMax,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final HashMap pHashMap = new HashMap(HashMap.Type.PROBING, ARRAY_SIZE/2); - private static final HashMap cHashMap = new HashMap(HashMap.Type.CHAINING, ARRAY_SIZE/2); - private static boolean testHashMap() { + private static class testHashMapProbing extends Testable { + HashMap pHashMap = new HashMap(HashMap.Type.PROBING, ARRAY_SIZE/2); String name = "Probing HashMap "; java.util.Map jMap = pHashMap.toMap(); - if (!testJavaMap(jMap,Integer.class,name)) return false; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaMap(jMap,Integer.class,String.class,name, unsorted, sorted, input)) return false; + return true; + } - name = "Chaining HashMap "; - jMap = cHashMap.toMap(); + } - if (!testJavaMap(jMap,Integer.class,name)) return false; + private static class testHashMapChaining extends Testable { + HashMap cHashMap = new HashMap(HashMap.Type.CHAINING, ARRAY_SIZE/2); + String name = "Chaining HashMap "; + java.util.Map jMap = cHashMap.toMap(); + + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaMap(jMap,Integer.class,String.class,name, unsorted, sorted, input)) return false; + return true; + } - return true; } - private static final HashArrayMappedTrie hamt = new HashArrayMappedTrie(); - private static boolean testHAMT() { - String hamtName = "HAMT "; + private static class testHAMT extends Testable { + HashArrayMappedTrie hamt = new HashArrayMappedTrie(); + String name = "HAMT "; java.util.Map jMap = hamt.toMap(); - if (!testJavaMap(jMap,Integer.class,hamtName)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaMap(jMap,Integer.class,String.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final java.util.Map javaHashMap = new java.util.HashMap(ARRAY_SIZE/2); - private static boolean testJavaHashMap() { + private static class testJavaHashMap extends Testable { + java.util.Map javaHashMap = new java.util.HashMap(ARRAY_SIZE/2); String name = "Java's HashMap "; - if (!testJavaMap(javaHashMap,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaMap(javaHashMap,Integer.class,String.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final java.util.PriorityQueue javaMinArrayHeap = new java.util.PriorityQueue(10, - new Comparator() { - @Override - public int compare(Integer arg0, Integer arg1) { - if (arg0.compareTo(arg1) < 0) - return -1; - else if (arg1.compareTo(arg0) < 0) - return 1; - return 0; + private static class testJavaMinHeap extends Testable { + java.util.PriorityQueue javaMinArrayHeap = new java.util.PriorityQueue(10, + new Comparator() { + @Override + public int compare(Integer arg0, Integer arg1) { + if (arg0.compareTo(arg1) > 0) + return 1; + else if (arg1.compareTo(arg0) > 0) + return -1; + return 0; + } } - } - ); - private static boolean testJavaMinHeap() { + ); String name = "Java's Min-Heap [array]"; - if (!testJavaCollection(javaMinArrayHeap,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(javaMinArrayHeap,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final java.util.PriorityQueue javaMaxArrayHeap = new java.util.PriorityQueue(10, - new Comparator() { - @Override - public int compare(Integer arg0, Integer arg1) { - if (arg0.compareTo(arg1) > 0) - return -1; - else if (arg1.compareTo(arg0) > 0) - return 1; - return 0; + private static class testJavaMaxHeap extends Testable { + java.util.PriorityQueue javaMaxArrayHeap = new java.util.PriorityQueue(10, + new Comparator() { + @Override + public int compare(Integer arg0, Integer arg1) { + if (arg0.compareTo(arg1) > 0) + return -1; + else if (arg1.compareTo(arg0) > 0) + return 1; + return 0; + } } - } - ); - private static boolean testJavaMaxHeap() { + ); String name = "Java's Max-Heap [array]"; - if (!testJavaCollection(javaMaxArrayHeap,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(javaMaxArrayHeap,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final java.util.List javaArrayList = new java.util.ArrayList(); - private static boolean testJavaArrayList() { + private static class testJavaArrayList extends Testable { + java.util.List javaArrayList = new java.util.ArrayList(); String name = "Java's List [array]"; - if (!testJavaCollection(javaArrayList,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(javaArrayList,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final java.util.List javaLinkedList = new java.util.LinkedList(); - private static boolean testJavaLinkedList() { + private static class testJavaLinkedList extends Testable { + java.util.List javaLinkedList = new java.util.LinkedList(); String name = "Java's List [linked]"; - if (!testJavaCollection(javaLinkedList,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(javaLinkedList,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final java.util.Deque javaArrayQueue = new java.util.ArrayDeque(); - private static boolean testJavaArrayQueue() { + private static class testJavaArrayQueue extends Testable { + java.util.Deque javaArrayQueue = new java.util.ArrayDeque(); String name = "Java's Queue [array]"; - if (!testJavaCollection(javaArrayQueue,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(javaArrayQueue,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final java.util.Deque javaLinkedQueue = new java.util.LinkedList(); - private static boolean testJavaLinkedQueue() { + private static class testJavaLinkedQueue extends Testable { + java.util.Deque javaLinkedQueue = new java.util.LinkedList(); String name = "Java's Queue [linked]"; - if (!testJavaCollection(javaLinkedQueue,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(javaLinkedQueue,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final java.util.TreeSet javaRedBlackTreeInteger = new java.util.TreeSet(); - private static boolean testJavaRedBlackIntegerTree() { + private static class testJavaRedBlackIntegerTree extends Testable { + java.util.TreeSet javaRedBlackTreeInteger = new java.util.TreeSet(); String name = "Java's Red-Black Tree "; - if (!testJavaCollection(javaRedBlackTreeInteger,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(javaRedBlackTreeInteger,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final java.util.TreeSet javaRedBlackTreeString = new java.util.TreeSet(); - private static boolean testJavaRedBlackStringTree() { + private static class testJavaRedBlackStringTree extends Testable { + java.util.TreeSet javaRedBlackTreeString = new java.util.TreeSet(); String name = "Java's Red-Black Tree "; - if (!testJavaCollection(javaRedBlackTreeString,String.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(javaRedBlackTreeString,String.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final java.util.Stack javaStack = new java.util.Stack(); - private static boolean testJavaStack() { + private static class testJavaStack extends Testable { + java.util.Stack javaStack = new java.util.Stack(); String name = "Java's Stack [array]"; - if (!testJavaCollection(javaStack,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(javaStack,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final java.util.Map javaTreeMap = new java.util.TreeMap(); - private static boolean testJavaTreeMap() { + private static class testJavaTreeMap extends Testable { + java.util.Map javaTreeMap = new java.util.TreeMap(); String name = "Java's TreeMap "; - if (!testJavaMap(javaTreeMap,String.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaMap(javaTreeMap,String.class,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final List.ArrayList arrayList = new List.ArrayList(); - private static boolean testArrayList() { + private static class testArrayList extends Testable { + List.ArrayList arrayList = new List.ArrayList(); String name = "List [array]"; Collection aCollection = arrayList.toCollection(); - if (!testJavaCollection(aCollection,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(aCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final List.LinkedList linkedList = new List.LinkedList(); - private static boolean testLinkedList() { + private static class testLinkedList extends Testable { + List.LinkedList linkedList = new List.LinkedList(); String name = "List [linked]"; Collection lCollection = linkedList.toCollection(); - if (!testJavaCollection(lCollection,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(lCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final PatriciaTrie patriciaTrie = new PatriciaTrie(); - private static boolean testPatriciaTrie() { + private static class testPatriciaTrie extends Testable { + PatriciaTrie patriciaTrie = new PatriciaTrie(); String name = "PatriciaTrie "; Collection bstCollection = patriciaTrie.toCollection(); - - if (!testJavaCollection(bstCollection,String.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(bstCollection,String.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final Queue.ArrayQueue arrayQueue = new Queue.ArrayQueue(); - private static boolean testArrayQueue() { + private static class testArrayQueue extends Testable { + Queue.ArrayQueue arrayQueue = new Queue.ArrayQueue(); String name = "Queue [array]"; Collection aCollection = arrayQueue.toCollection(); - if (!testJavaCollection(aCollection,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(aCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final Queue.LinkedQueue linkedQueue = new Queue.LinkedQueue(); - private static boolean testLinkedQueue() { + private static class testLinkedQueue extends Testable { + Queue.LinkedQueue linkedQueue = new Queue.LinkedQueue(); String name = "Queue [linked]"; Collection lCollection = linkedQueue.toCollection(); - if (!testJavaCollection(lCollection,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(lCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final RadixTrie radixTrie = new RadixTrie(); - private static boolean testRadixTrie() { + private static class testRadixTrie extends Testable { + RadixTrie radixTrie = new RadixTrie(); String name = "RadixTrie "; java.util.Map jMap = radixTrie.toMap(); - if (!testJavaMap(jMap,String.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaMap(jMap,String.class,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final BinarySearchTree redBlackTree = new RedBlackTree(); - private static boolean testRedBlackTree() { + private static class testRedBlackTree extends Testable { + BinarySearchTree redBlackTree = new RedBlackTree(); String name = "Red-Black Tree "; Collection bstCollection = redBlackTree.toCollection(); - if (!testJavaCollection(bstCollection,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(bstCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final NavigableSet javaSkipList = new ConcurrentSkipListSet(); - private static boolean testJavaSkipList() { + private static class testJavaSkipList extends Testable { + NavigableSet javaSkipList = new ConcurrentSkipListSet(); String name = "Java's SkipListSet "; Collection lCollection = javaSkipList; - if (!testJavaCollection(lCollection,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(lCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final SkipList skipList = new SkipList(); - private static boolean testSkipList() { + private static class testSkipList extends Testable { + SkipList skipList = new SkipList(); String name = "SkipList "; Collection lCollection = skipList.toCollection(); - if (!testJavaCollection(lCollection,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(lCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final BinarySearchTree splayTree = new SplayTree(); - private static boolean testSplayTree() { + private static class testSplayTree extends Testable { + BinarySearchTree splayTree = new SplayTree(); String name = "Splay Tree "; Collection bstCollection = splayTree.toCollection(); - if (!testJavaCollection(bstCollection,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(bstCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final Stack.ArrayStack arrayStack = new Stack.ArrayStack(); - private static boolean testArrayStack() { + private static class testArrayStack extends Testable { + Stack.ArrayStack arrayStack = new Stack.ArrayStack(); String name = "Stack [array]"; Collection aCollection = arrayStack.toCollection(); - if (!testJavaCollection(aCollection,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(aCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final Stack.LinkedStack linkedStack = new Stack.LinkedStack(); - private static boolean testLinkedStack() { + private static class testLinkedStack extends Testable { + Stack.LinkedStack linkedStack = new Stack.LinkedStack(); String name = "Stack [linked]"; Collection lCollection = linkedStack.toCollection(); - if (!testJavaCollection(lCollection,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(lCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final BinarySearchTree treap = new Treap(); - private static boolean testTreap() { + private static class testTreap extends Testable { + BinarySearchTree treap = new Treap(); String name = "Treap "; Collection treapCollection = treap.toCollection(); - if (!testJavaCollection(treapCollection,Integer.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(treapCollection,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final TreeMap treeMap = new TreeMap(); - private static boolean testTreeMap() { + private static class testTreeMap extends Testable { + TreeMap treeMap = new TreeMap(); String name = "TreeMap "; java.util.Map jMap = treeMap.toMap(); - if (!testJavaMap(jMap,String.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaMap(jMap,String.class,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final Trie trie = new Trie(); - private static boolean testTrie() { + private static class testTrie extends Testable { + Trie trie = new Trie(); String name = "Trie "; Collection trieCollection = trie.toCollection(); - if (!testJavaCollection(trieCollection,String.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaCollection(trieCollection,String.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final TrieMap trieMap = new TrieMap(); - private static boolean testTrieMap() { + private static class testTrieMap extends Testable { + TrieMap trieMap = new TrieMap(); String name = "TrieMap "; java.util.Map jMap = trieMap.toMap(); - if (!testJavaMap(jMap,String.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaMap(jMap,String.class,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final ConcurrentSkipListMap javaSkipListMap = new ConcurrentSkipListMap(); - private static boolean testJavaSkipListMap() { + private static class testJavaSkipListMap extends Testable { + ConcurrentSkipListMap javaSkipListMap = new ConcurrentSkipListMap(); String name = "Java's SkipListMap "; - if (!testJavaMap(javaSkipListMap,String.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaMap(javaSkipListMap,String.class,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static final SkipListMap skipListMap = new SkipListMap(); - private static boolean testSkipListMap() { + private static class testSkipListMap extends Testable { + SkipListMap skipListMap = new SkipListMap(); String name = "SkipListMap "; java.util.Map jMap = skipListMap.toMap(); - if (!testJavaMap(jMap,String.class,name)) return false; - return true; + public String getName() { + return name; + } + + public boolean run(Integer[] unsorted, Integer[] sorted, String input) { + this.input = input; + if (!testJavaMap(jMap,String.class,Integer.class,name, unsorted, sorted, input)) return false; + return true; + } + } - private static > boolean testJavaCollection(Collection collection, Class type, String name) { + @SuppressWarnings("unchecked") + private static > boolean testJavaCollection(Collection collection, Class type, String name, Integer[] _unsorted, Integer[] _sorted, String input) { // Make sure the collection is empty if (!collection.isEmpty()) { System.err.println(name+" initial isEmpty() failed."); - handleError(collection); + handleError(input,collection); return false; } if (collection.size()!=0) { System.err.println(name+" initial size() failed."); - handleError(collection); + handleError(input,collection); return false; } + T[] unsorted = (T[]) Array.newInstance(type, _unsorted.length); + T[] sorted = (T[]) Array.newInstance(type, _sorted.length); + for (int i=0; i> boolean testJavaCollection(Collection> boolean testJavaCollection(Collection> boolean testJavaCollection(Collection> boolean testJavaCollection(Collection> boolean testJavaCollection(Collection= 0; i--) { - Integer value = unsorted[i]; - T item = Utils.parseT(value, type); + T item = unsorted[i]; boolean added = collection.add(item); if (!added) { System.err.println(name+" unsorted add failed."); - handleError(collection); + handleError(input,collection); return false; } } @@ -1475,12 +1120,11 @@ private static > boolean testJavaCollection(Collection> boolean testJavaCollection(Collection> boolean testJavaCollection(Collection> boolean testJavaCollection(Collection> boolean testJavaCollection(Collection> boolean testJavaCollection(Collection> boolean testJavaCollection(Collection> boolean testJavaCollection(Collection> boolean testJavaCollection(Collection> boolean testJavaCollection(Collection= 0; i--) { - Integer value = sorted[i]; - T item = Utils.parseT(value, type); + T item = sorted[i]; boolean removed = collection.remove(item); if (!removed) { System.err.println(name+" sorted remove failed."); - handleError(collection); + handleError(input,collection); return false; } } @@ -1682,12 +1319,12 @@ private static > boolean testJavaCollection(Collection> boolean testJavaCollection(Collection,V> boolean testJavaMap(java.util.Map map, Class keyType, String name) { + private static ,V> boolean testJavaMap(java.util.Map map, Class keyType, Class valueType, String name, Integer[] _unsorted, Integer[] _sorted, String input) { // Make sure the map is empty if (!map.isEmpty()) { System.err.println(name+" initial isEmpty() failed."); - handleError(map); + handleError(input,map); return false; } if (map.size()!=0) { System.err.println(name+" initial size() failed."); - handleError(map); + handleError(input,map); return false; } + K[] kUnsorted = (K[]) Array.newInstance(keyType, _unsorted.length); + K[] kSorted = (K[]) Array.newInstance(keyType, _sorted.length); + V[] vUnsorted = (V[]) Array.newInstance(valueType, _unsorted.length); + V[] vSorted = (V[]) Array.newInstance(valueType, _sorted.length); + for (int i=0; i,V> boolean testJavaMap(java.util.Map,V> boolean testJavaMap(java.util.Map,V> boolean testJavaMap(java.util.Map,V> boolean testJavaMap(java.util.Map= 0; i--) { - Integer item = unsorted[i]; - K k = null; - V v = null; - if (keyType.isAssignableFrom(Integer.class)) { - k = (K)Utils.parseT(item, keyType); - v = (V)Utils.parseT(item, String.class); - } else if (keyType.isAssignableFrom(String.class)) { - k = (K)Utils.parseT(item, keyType); - v = (V)Utils.parseT(item, Integer.class); - } + for (int i = kUnsorted.length - 1; i >= 0; i--) { + K k = kUnsorted[i]; + V v = vUnsorted[i]; map.put(k, v); } if (debugTime) { @@ -1879,8 +1511,7 @@ private static ,V> boolean testJavaMap(java.util.Map,V> boolean testJavaMap(java.util.Map= 0; i--) { - Integer item = unsorted[i]; - K k = (K) Utils.parseT(item, keyType); + for (int i = kUnsorted.length - 1; i >= 0; i--) { + K k = kUnsorted[i]; removed = map.remove(k); if (removed==null) { System.err.println(name+" unsorted invalidity check. removed=" + removed); @@ -1909,12 +1539,12 @@ private static ,V> boolean testJavaMap(java.util.Map,V> boolean testJavaMap(java.util.Map,V> boolean testJavaMap(java.util.Map,V> boolean testJavaMap(java.util.Map,V> boolean testJavaMap(java.util.Map,V> boolean testJavaMap(java.util.Map,V> boolean testJavaMap(java.util.Map= 0; i--) { - Integer item = sorted[i]; - K k = (K) Utils.parseT(item, keyType); + for (int i = kSorted.length - 1; i >= 0; i--) { + K k = kSorted[i]; removed = map.remove(k); if (removed==null) { System.err.println(name+" unsorted invalidity check. removed=" + removed); @@ -2088,12 +1698,12 @@ private static ,V> boolean testJavaMap(java.util.Map