/* * Created on Mar 31, 2005 */ package SimpleSpeller; /** Starter code for testing the Spell Checker */ public class TestSpellChecker { /** You can structure the method as you like. However, at the end of * its execution, it should display one of the following two messages * on the console: * TESTER FOUND NO PROBLEMS FOUND WITH MYSPELLCHECKER CLASS. * or * TEST FOUND PROBLEMS WITH MYSPELLCHECKER CLASS. * In the latter case, there should be something earlier on the console * calling attention to what the error is. * @param args not used */ public static void main(String[] args) { final String OKMessage = "TESTER FOUND NO PROBLEMS FOUND WITH MYSPELLCHECKER CLASS."; final String NotOKMessage = "TEST FOUND PROBLEMS WITH MYSPELLCHECKER CLASS."; /* DON'T CHANGE ANY OF THE FOLLOWING LINES * SERVES AS A CHECK THAT THE NAMES AND PACKAGING ARE RIGHT. * IF THIS DOESN'T COMPILE AS-IS, SOMETHING IS WRONG. */ String[] correctWords = {"cat", "dog", "gerbil", "guppy", "dont" + "stop" + "at" + "this"}; SimpleSpeller.ISpellChecker speller = new SimpleSpeller.MySpellChecker(correctWords); String testword = "CAT"; String ncat = speller.normalize(testword); boolean result = speller.isCorrectlySpelled(testword); /* You take it from here... You can and should add code below here. */ if (testNormalize(speller) && testIsCorrectlySpelled()) { System.out.println(OKMessage); } else { System.out.println(NotOKMessage); } } /** Test the normalize method in isolation. * * @return true iff no error was found. */ static boolean testNormalize(ISpellChecker speller) { String[] cases = new String[]{ "cat", "cat", "", "", null, null, "a ", "a", " b", "b", " a ", "a", "DOG", "dog", "dOg", "dog", "1 2", "1 2", "a B C d e F", "a b c d e f", "1 2", "1 2", "1234567890", "1234567890", " inner spaces ", "inner spaces", //"\t\t\n", "\t\t\n", //problematic "!@#$%%%%<>??:\"", "!@#$%%%%<>??:\"", " 2 aND Two too ", "2 and two too"}; boolean testresult = true; //provisional for (int c = 0; c < cases.length; c = c+2) { String normalized = speller.normalize(cases[c]); if (normalized == cases[c+1]) { //this includes the null case continue; //good } if (!normalized.equals(cases[c+1])) { System.out.println("normalization failure: " + cases[c] + ": " + normalized + " vs. " + cases[c+1]); testresult = false; } } return testresult; } /** Test the isCorrectlySpelled method. * * @return true iff all tests are successful. */ static boolean testIsCorrectlySpelled() { String[] smallDictA = new String[] {"a", "aaa"}; String[] nonWordsA = new String[] {"aa", "aaaa", "aaaaaa"}; String[] realWordsA = new String[] { "egg", "apple", "pear", "banana", "qumquat", "grapes", "juice", "Rice Crispies", "date", "dates", "green pepper", "bell pepper" }; String[] realWordsB = new String[] {"steak", "masala dosa", "rice", "cracker", "biscuit", "scone", "loaf", "loaves", "leaf", "leaves", "celery", "catalpa", "congee"}; String[] unnormalized = new String[] {" rat" , " mouse", " tapir ", " dog! ", "DAWG", " Dogg" }; boolean result = testOneChecker(smallDictA, nonWordsA); result = result && testOneChecker(nonWordsA, realWordsA); result = result && testOneChecker(realWordsA, realWordsB); result = result && testOneChecker(realWordsB, unnormalized); result = result && testOneChecker(unnormalized, smallDictA); //Some base cases try { result = result && testOneChecker(null, null); System.out.println("FYI: no error on null dictionary."); } catch (Exception e) { System.out.println("Error, as expected, on null dictionary."); } result = result && testOneChecker(new String[] {}, new String[] {}); result = result && testOneChecker(new String[] {}, null); result = result && testOneChecker(realWordsA, new String[] {}); return result ; } /** Create and test a speller. * * @param goodWords words to be considered "good" (in the dictionary) * @param badWords some words which are not in the dictionary * @return true iff tests pass. */ private static boolean testOneChecker(String goodWords[], String badWords[]) { ISpellChecker aChecker = new MySpellChecker(goodWords); boolean testResult = true; //provisional //make sure all of the good words are found for (int w = 0; w < goodWords.length; w++) { String thisWord = goodWords[w]; boolean oneResult = checkWord(aChecker, thisWord, true); testResult = testResult && oneResult; //make sure normalization doesn't mess things up oneResult = checkWord(aChecker, aChecker.normalize(thisWord), true); testResult = testResult && oneResult; //make sure the checker is normalizing if (thisWord == null) { continue; } String wordToCheck = " " + thisWord.toUpperCase() + " "; oneResult = checkWord(aChecker, wordToCheck, true); testResult = testResult && oneResult; //turn a good word into a bad one if (thisWord.length() < 1) { continue; } String fakeWord = "ww" + thisWord + "xx" + thisWord + "yy"; oneResult = checkWord(aChecker, fakeWord, false); testResult = testResult && oneResult; } //now make sure no non-words are found if (badWords == null) { return testResult; } for (int w = 0; w < badWords.length; w++) { boolean oneResult; String wordToCheck = badWords[w]; oneResult = checkWord(aChecker, wordToCheck, false); testResult = testResult && oneResult; } if (!testResult) { System.out.print("Failure was for dictionary beginning {"); for (int i = 0; i < goodWords.length && i < 4; i++) { System.out.print(goodWords[i] + " "); } System.out.println("}"); } return testResult; } /** Check a word to see if the result is as expected. If not, * print an error message. * @param checker a non-null checker * @param word any non-null string * @param expectation * @return true if the expectation is met, false otherwise */ private static boolean checkWord(ISpellChecker checker, String word, boolean expectation) { //expectation = !expectation; //fault injection if (checker.isCorrectlySpelled(word) == expectation) { return true; } System.out.println("Error in isCorrectlySpelled test: " + word + " returned " + !expectation + ", expected " + expectation); return false; } }