/** * Dictionary test class. * * usage: "Tester unsorted" applies some tests using the unsorted-array * implementation of Dictionary. * "Tester sorted" applies some tests using the sorted-array version. * * David Liben-Nowell (CS127, Carleton College) * Winter 2006 * */ public class Tester { /** * Looks up a word into the dictionary, catching "word not in * dictionary" errors and reporting either the definition or the * fact that the word was not found. */ private static void testDefineWord(String word, Dictionary dict) { try { System.out.println(word + " is defined as \"" + dict.define(word) + "\""); } catch (NotInDictionaryException exception) { System.out.println(word + " doesn't seem to be in the dictionary"); } } /** * Inserts a word into the dictionary, catching "array full" errors * and simply reporting success or failure. */ private static void testInsertWord(String word, String definition, Dictionary dict) { try { System.out.print("attempting to insert " + word + " ... "); dict.insert(word, definition); System.out.println("succeeded!"); } catch (RuntimeException exception) { System.out.println("failed!"); } } /** * Creates a dictionary (either unsorted or sorted array * implementation), inserts a bunch of words, then looks up a * bunch of words. */ public static void main(String[] args) { Dictionary dict; if (args.length == 1 && args[0].equals("unsorted")) { dict = new DictionaryUnsortedArray(9); } else if (args.length == 1 && args[0].equals("sorted")) { dict = new DictionarySortedArray(9); } else { System.out.println("Usage: java Tester [un]sorted"); return; } testInsertWord("petrichor", "the smell after a rainstorm", dict); testInsertWord("thalweg", "the line of a stream", dict); testInsertWord("abattoir", "slaughterhouse", dict); testInsertWord("eclat", "enthusiastic approval", dict); testInsertWord("postprandial", "after a meal", dict); testInsertWord("schadenfreude", "pleasure in others' pain", dict); testInsertWord("lambent", "flickering, as light on a surface", dict); testInsertWord("emolument", "profit, gain", dict); testInsertWord("chthonic", "dwelling in or under the earth", dict); testInsertWord("unctuous", "overearnest", dict); testInsertWord("crepuscular", "related to/resembling twilight", dict); testInsertWord("oneirism", "absent-minded dreaming while awake", dict); testInsertWord("tmesis","infix interjection (absobloodylutely)", dict); System.out.println(""); testDefineWord("petrichor",dict); testDefineWord("foobar",dict); testDefineWord("thalweg",dict); testDefineWord("postprandial",dict); testDefineWord("chthonic",dict); testDefineWord("abattoir",dict); testDefineWord("tmesis",dict); } }