// By Haw-ren Fang, TA of cmsc420-0401, Spring 2002 import java.io.*; import java.lang.*; public class BrentHashTableDriver1 { public static void main(String args[]) throws Exception { int total = 0; System.out.println("BrentHashTable.brentPut() Test:"); try{ BrentHashTable table = new BrentHashTable(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("brentPut() test #1: 0/5"); else if (!table.put("monday", "john")) System.out.println("brentPut() test #1: 0/5"); else if (!table.put("tuesday", "mary")) System.out.println("brentPut() test #1: 0/5"); else if (!table.put("wednesday", "joe")) System.out.println("brentPut() test #1: 0/5"); else if (!table.put( "thursday", "peggy")) System.out.println("brentPut() test #1: 0/5"); else if (!table.put( "friday", "david")) System.out.println("brentPut() test #1: 0/5"); else if (!table.brentPut( "saturday", "jerry")) // brentPut System.out.println("brentPut() test #1: 0/5"); else { table.probeCount = 0; if (!table.containsKey("friday")) System.out.println("brentPut() test #1: 0/5"); else if (table.probeCount != 6) System.out.println("brentPut() test #1: 0/5"); else { table.probeCount = 0; if (!table.containsKey("saturday")) System.out.println("brentPut() test #1: 0/5"); else if (table.probeCount != 1) System.out.println("brentPut() test #1: 0/5"); else { total += 5; System.out.println("brentPut() test #1: 5/5"); } } } table = new BrentHashTable(7); // Test put() // System.out.println(table.hashValue("sunday")); // 6 // System.out.println(table.probeHashValue("sunday")); // 3 // System.out.println(table.hashValue("monday")); // 5 // System.out.println(table.probeHashValue("monday")); // 5 // System.out.println(table.hashValue("tuesday")); // 2 // System.out.println(table.probeHashValue("tuesday")); // 5 // System.out.println(table.hashValue("wednesday")); // 1 // System.out.println(table.probeHashValue("wednesday")); // 2 // System.out.println(table.hashValue("thursday")); // 2 // System.out.println(table.probeHashValue("thursday")); // 4 // System.out.println(table.hashValue("friday")); // 6 // System.out.println(table.probeHashValue("friday")); // 4 // System.out.println(table.hashValue("saturday")); // 0 // System.out.println(table.probeHashValue("saturday")); // 3 boolean passTest2 = false; if (!table.brentPut("sunday", "none")) System.out.println("brentPut() test #2: 0/5"); else if (!table.brentPut("monday", "john")) System.out.println("brentPut() test #2: 0/5"); else if (!table.brentPut("tuesday", "mary")) System.out.println("brentPut() test #2: 0/5"); else if (!table.brentPut("wednesday", "joe")) System.out.println("brentPut() test #2: 0/5"); else if (!table.brentPut( "thursday", "peggy")) System.out.println("brentPut() test #2: 0/5"); else { table.probeCount = 0; if (!table.containsKey("tuesday")) System.out.println("brentPut() test #2: 0/5"); else if (table.probeCount != 2) System.out.println("brentPut() test #2: 0/5"); else { table.probeCount = 0; if (!table.containsKey("thursday")) System.out.println("brentPut() test #2: 0/5"); else if (table.probeCount != 1) System.out.println("brentPut() test #2: 0/5"); else { System.out.println("brentPut() test #2: 5/5"); total += 5; passTest2 = true; } } } if (!passTest2) System.out.println("brentPut() test #3: 0/5"); else if (!table.brentPut("friday", "david")) System.out.println("brentPut() test #3: 0/5"); else if (!table.brentPut("saturday", "jerry")) // brentPut System.out.println("brentPut() test #3: 0/5"); else { table.probeCount = 0; if (!table.containsKey("friday")) System.out.println("brentPut() test #3: 0/5"); else if (table.probeCount != 4) // 2 => 4 System.out.println("brentPut() test #3: 0/5"); else { table.probeCount = 0; if (!table.containsKey("saturday")) System.out.println("brentPut() test #3: 0/5"); else if (table.probeCount != 2) // replace "friday" System.out.println("brentPut() test #3: 0/5"); else { total += 5; System.out.println("brentPut() test #3: 5/5"); } } } if (table.longestProbe() != 4) System.out.println("longestProbe() test with brentPut(): 0/5"); else { total += 5; System.out.println("longestProbe() test with brentPut(): 5/5"); } } catch (Exception e) {} System.out.println("Total: " + total + "/20"); } }