📄 testdatastructures.java
字号:
System.out.println( "Exception descrepancy with vector and arraylist"); } else if (!(linkedListException && vectorException)) { System.out.println( "Exception descrepancy with vector and linkedlist"); } else { System.out.println("Error in TestDataStructures"); } this.printListCommandsCalled(listCommandsCalled); fail("test failed"); //System.exit(0); } return; } if (!objectEquals(linkedListObject, arrayListObject, vectorObject)) { System.out.println("Objects returned inconsistent"); this.printListCommandsCalled(listCommandsCalled); fail("test failed"); //System.exit(0); } compareLists(arrayList, linkedList, vector); } } /** * Compare contents of lists to the vector. Print out stuff if they are * inconsistent and exit. */ public void compareLists(HsqlArrayList arrayList, HsqlLinkedList linkedList, Vector vector) { boolean arrayListError = false; boolean linkedListError = false; if (!equalsVector(arrayList, vector)) { System.out.println("Error in array list implementation"); arrayListError = true; } if (!equalsVector(linkedList, vector)) { System.out.println("Error in linked list implementation"); linkedListError = true; } if (arrayListError || linkedListError) { this.printListCommandsCalled(listCommandsCalled); System.out.flush(); fail("test failed"); System.exit(0); } } /** Prints the list of commands called so far */ public void printListCommandsCalled(Vector commands) { int commandCode = 0; for (int i = 0; i < commands.size(); i++) { System.out.println((String) commands.elementAt(i)); } System.out.flush(); } /** Returns whether three objects are equal */ private boolean objectEquals(Object lObject, Object aObject, Object vObject) { if (lObject == null && aObject == null && vObject == null) { return true; } try { if (!lObject.equals(vObject)) { System.out.println("LinkList object returned inconsistent"); return false; } else if (!aObject.equals(vObject)) { System.out.println("ArrayList object returned inconsistent"); return false; } else { return true; } } catch (NullPointerException ex) { return false; } } /** Returns a random integer in the range of the lowBound and highBound */ private int getRandomInt(int lowBound, int highBound) { double random = randomGenerator.nextDouble(); return ((int) (((highBound - lowBound) * random) + .5)) + lowBound; } /** * Returns an Integer object with a value between Integer.MIN_VALUE and * Integer.MAX_VALUE */ private Integer getRandomInteger() { return new Integer(getRandomInt(0, (int) (Integer.MAX_VALUE / 100.0))); } /** Tells whether the given list contains the same data as the vector */ private boolean equalsVector(HsqlList list, Vector vector) { if (list.size() != vector.size()) { return false; } org.hsqldb.lib.Iterator listElements = list.iterator(); Enumeration vectorElements = vector.elements(); Object listObj = null; Object vectorObj = null; while (listElements.hasNext()) { listObj = listElements.next(); vectorObj = vectorElements.nextElement(); if (!listObj.equals(vectorObj)) { return false; } } return true; } public void testGrowth() { HsqlArrayList d = new HsqlArrayList(); for (int i = 0; i < 12; i++) { d.add(new Integer(i)); } for (int i = 0; i < d.size(); i++) { System.out.println(d.get(i)); } d = new HsqlArrayList(); for (int i = 0; i < 12; i++) { d.add(new Integer(i)); } d.set(11, new Integer(11)); for (int i = 0; i < d.size(); i++) { System.out.println(d.get(i)); } org.hsqldb.lib.Iterator it = d.iterator(); for (int i = 0; it.hasNext(); i++) { Integer value = (Integer) it.next(); System.out.println(value); assertEquals(i, value.intValue()); } //- assertEquals(12, d.size()); } public void testSpeed() { randomGenerator = new Random(System.currentTimeMillis()); int TEST_RUNS = 100000; int LOOP_COUNT = 1000; HsqlArrayList arrayList = new HsqlArrayList(TEST_RUNS); ArrayList utilArrayList = new ArrayList(TEST_RUNS); Vector vector = new Vector(TEST_RUNS); Integer value = new Integer(randomGenerator.nextInt()); Integer INT_0 = new Integer(0); StopWatch sw = new StopWatch(); System.out.println(sw.currentElapsedTimeToMessage("time")); for (int i = 0; i < TEST_RUNS; i++) { arrayList.add(INT_0); } for (int i = 0; i < TEST_RUNS; i++) { for (int j = 0; j < LOOP_COUNT; j++) { arrayList.set(i, INT_0); } } System.out.println(sw.currentElapsedTimeToMessage("time")); sw.zero(); for (int i = 0; i < TEST_RUNS; i++) { utilArrayList.add(INT_0); } for (int i = 0; i < TEST_RUNS; i++) { for (int j = 0; j < LOOP_COUNT; j++) { utilArrayList.set(i, INT_0); } } System.out.println(sw.currentElapsedTimeToMessage("time")); sw.zero(); for (int i = 0; i < TEST_RUNS; i++) { vector.addElement(INT_0); } for (int i = 0; i < TEST_RUNS; i++) { for (int j = 0; j < LOOP_COUNT; j++) { vector.setElementAt(INT_0, i); } } System.out.println(sw.currentElapsedTimeToMessage("time")); } public static void main(String[] args) { TestDataStructures test = new TestDataStructures("testLists"); for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) { test.testLists(); if (i % 1000 == 0) { System.out.println("Finished " + i + " tests"); System.out.flush(); } } System.out.println( "After " + NUMBER_OF_TEST_RUNS + " tests of a maximum of " + NUMBER_OF_ITERATIONS_PER_RUN + " list commands each test, the list tests passed"); test.testGrowth(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -