package edu.vt.marian.search; import java.util.*; import java.io.*; import java.util.*; import java.lang.Math.*; import edu.vt.marian.common.*; import edu.vt.marian.search.*; /** * Test wrapper for InsertionOrderWtdObjTable class * * @author Paul Mather * @version 0.9 * @see edu.vt.marian.common.InsertionOrderWtdObjTable * @see edu.vt.marian.common.WtdObjTable * @see java.util.Hashtable * @see edu.vt.marian.common.Debug */ public class InsertionOrderWtdObjTableTester { /** The ever-present debug object */ Debug debug; /** * InsertionOrderWtdObjTable used for testing */ InsertionOrderWtdObjTable testTable; /** FullID class of weighted objects we test with */ private static final int testClassID = ClassIDs.CLASS_FULLID; public InsertionOrderWtdObjTableTester(Debug debug) { this.debug = debug; testTable = new InsertionOrderWtdObjTable( debug ); } /** * Test insertion of N random WtdObjs into test table * @param numObjs number of random weighted objects to be * inserted into table * @return number of objects successfully inserted into table */ public int testInsert( int numObjs ) { WtdObj w; int successfulInserts = 0; if (numObjs < 0) { debug.dumpTrace("InsertionOrderWtdObjTableTester.testInsert: " + "can't insert negative number of objects!"); return 0; } for (int i = 0; i < numObjs; i++) { // Make a random weighted object w = new WtdObj( testClassID, i, new Weight(Math.random(),debug), debug ); // Insert it into the table if (testTable.insert(w) == null) // WtdObj was inserted successfully successfulInserts++; // Log it to the debug stream debug.dumpTrace("InsertionOrderWtdObjTableTester.testInsert: " + "WtdObj " + successfulInserts + ": " + w.toString()); } return successfulInserts; } /** * Test iteration through the table * @return number of elements successfully iterated through */ public int testIterate() { int successfulIterations = 0; WtdObj w; Enumeration wtdObjs; // Get all elements of table wtdObjs = testTable.elements(); // Issue a warning if we're iterating over an empty table if (wtdObjs == null) debug.dumpTrace("InsertionOrderWtdObjTableTester.testIterate: " + "returned enumeration is null!"); // Iterate through the elements while (wtdObjs.hasMoreElements()) { // Get next element from the table enumeration w = (WtdObj) wtdObjs.nextElement(); successfulIterations++; // Log it to the debug stream debug.dumpTrace("InsertionOrderWtdObjTableTester.testIterate: " + "WtdObj " + successfulIterations + ": " + w.toString()); } return successfulIterations; } /** * Perform all tests. * @param tableSize number of random test weighted objects in table * @return true if all tests pass; false otherwise */ public boolean test(int tableSize) { boolean passed; int testResult; // Make sure tableSize is valid if (tableSize < 0) { debug.dumpTrace("InsertionOrderWtdObjTableTester.test: " + "negative size give; making positive (" + -tableSize + ")"); tableSize = -tableSize; } System.out.println("InsertionOrderWtdObjTableTester.test:"); System.out.print("\tTesting insertion of " + tableSize + " entries....."); // Successfull test should signify insertion of all "tableSize" objects testResult = this.testInsert( tableSize ); if (testResult == tableSize) { System.out.println("PASS"); passed = true; } else { System.out.println("FAIL"); passed = false; } System.out.print("\tTesting iteration over " + tableSize + " entries..."); // Successfull test should signify iteration over all "tableSize" objects testResult = this.testIterate(); if (testResult == tableSize) { System.out.println("PASS"); passed = passed && true; } else { System.out.println("FAIL (returned " + testResult + ")" ); passed = false; } return passed; } }