// By Haw-ren Fang, TA of cmsc420-0401, Spring 2002 import java.io.*; import java.lang.*; public class HashTableDriver1 { public static void main(String args[]) throws Exception { int total = 0; System.out.println("HashTable Basic Functionality Test:"); try{ // Test constructor and getSize() HashTable table = new HashTable(20); if (table.getSize() == 23) { total += 5; System.out.println("Constructor and getSize() test: 5/5"); } else System.out.println("Constructor and getSize() test: 0/5"); // Test hashValue() #1 // System.out.println (table.hashValue("test")); if (table.hashValue("test") == 1) { total += 5; System.out.println("hashValue() test #1: 5/5"); } else System.out.println("hashValue() test #1: 0/5"); // Test hashValue() #2 table.setHashArgument(33); table.setMADArguments(2, 3); // System.out.println (table.hashValue("test")); if (table.hashValue("test") == 10) { total += 5; System.out.println ("hashValue() test #2: 5/5"); } else System.out.println ("hashValue() test #2: 0/5"); table = new HashTable(7) { public int probeHashValue (String key) { return 1; // linear probing ... } }; // Test put() // System.out.println( table.hashValue("sunday") ); // 6 // System.out.println( table.hashValue("monday") ); // 5 // System.out.println( table.hashValue("tuesday") ); // 2 // System.out.println( table.hashValue("wednesday") ); // 1 // System.out.println( table.hashValue("thursday") ); // 2 // System.out.println( table.hashValue("friday") ); // 6 // System.out.println( table.hashValue("saturday") ); // 0 if (!table.put("sunday", "none")) System.out.println("put() test: 0/5"); else if (!table.put("monday", "john")) System.out.println("put() test: 0/5"); else if (!table.put("tuesday", "mary")) System.out.println("put() test: 0/5"); else if (!table.put("wednesday", "joe")) System.out.println("put() test: 0/5"); else if (!table.put( "thursday", "peggy")) System.out.println("put() test: 0/5"); else if (!table.put( "friday", "david")) System.out.println("put() test: 0/5"); else if (!table.put( "saturday", "jerry")) System.out.println("put() test: 0/5"); else if (table.probeCount != 13 ) System.out.println("put() test: 0/5"); else { total += 5; System.out.println("put() test: 5/5"); } // System.out.println("probeCount = " + table.probeCount); // Test containsKey() #1 if (table.containsKey("holiday")) System.out.println("containsKey() test #1: 0/5"); else { table.probeCount = 0; if (!table.containsKey("saturday")) System.out.println("containsKey() test #1: 0/5"); else if (table.probeCount != 5) System.out.println("containsKey() test #1: 0/5"); else { total += 5; System.out.println("containsKey() test #1: 5/5"); } // System.out.println("probeCount = " + table.probeCount); } // Test remove() #1 boolean passRemove = false; if (table.remove("weekdays")) System.out.println("remove() test #1: 0/5"); else { table.probeCount = 0; if (!table.remove("tuesday")) System.out.println("remove() test #1: 0/5"); else if (table.probeCount != 1) System.out.println("remove() test #1: 0/5"); else if (table.containsKey("tuesday")) System.out.println("remove() test #1: 0/5"); else { total += 5; passRemove = true; System.out.println ("remove() test #1: 5/5"); } } // Test containsKey #2 boolean passContainsKey = false; if (!passRemove) System.out.println ("containsKey() test #2: 0/5"); else { table.probeCount = 0; if (!table.containsKey("saturday")) System.out.println("containsKey() test #2: 0/5"); else if (table.probeCount != 5) System.out.println("containsKey() test #2: 0/5"); else { total += 5; passContainsKey = true; System.out.println("containsKey() test #2: 5/5"); } // System.out.println("probeCount = " + table.probeCount); } // Test remove() #2 if (!passContainsKey) System.out.println ("remove() test #2: 0/5"); else { table.probeCount = 0; if (!table.remove("saturday")) System.out.println("!remove() test #2: 0/5"); else if (table.probeCount != 5) System.out.println("remove() test #2: 0/5"); else if (table.containsKey("saturday")) System.out.println("remove() test #2: 0/5"); else { total += 5; System.out.println("remove() test #2: 5/5"); } } } catch (Exception e) {} System.out.println("Total: " + total + "/40"); } }