/* * Created on 2005. 1. 11. * */ //package hw01; /** * @author yongjoon * */ public class AnagramTester { public static void main(String[] args) { AnagramTester tester = new AnagramTester(); IAnagramChecker checker = new AnagramChecker(); boolean result = tester.testIsAnagram(checker); if (result) { System.out.println("Anagram checker was determined to be correct"); } else { System.out.println("Anagram checker is incorrect"); } } /** Test the isAnagram method of an IAnagramChecker object. * * @return true if the isAnagram method is correct (always * gives the correct answer), false otherwise. In the case * of a false return, the method should print details of * at least one case where isAnagram failed. * * Some of the test cases are quoted from * http://www.wordsmith.org/anagram/hof.html */ public boolean testIsAnagram(IAnagramChecker checker) { boolean result = true; String[] word1 = { "ab", "aB", "ab", " ab ", "0123", " 1a...", "ab", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "zbacdefghijklmnopqrstuvwxy", "a", "a", "aa", "", "a", "Eleven Plus Two", "Swear Oft", "ANightSnow", "Monkeys write", "Mitchell", //"A", //ambiguous spec -- don't test "CEFBAD", "I'm a dot in place", "dirTY roOM", "Buck", //"", //ambiguous spec -- dont' test "T-1000", "!Save&g", "DDDD%", "!&:MainMic", "To be or not to be: that is the question, whether tis nobler in the mind to suffer the slings and arrows of outrageous fortune." //From Hamlet by Shakespear }; String[] word2 = { "ba", "bA", "b a", "b a", "03 2 1", "a1,,,,,,", "!@#$%^&*()_+-=[]{}:;\"'<>?,./|\\ ba", "bacdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "b", "aa", "a", "a", "", "Twelve Plus One", "Software", "Washington", "New York Times", "Michelle", //"a", "ABCDEE", "A decimal point", "dormitory", "Duck", //"", "-10T01", "Vegas!&", "DDDDD", "Mac-Mini", "In one of the Bard's best-thought-of tragedies, our insistent hero, Hamlet, queries on two fronts about how life turns rotten." }; boolean[] answers = { true, true, true, true, true, true, true, true, true, false, false, false, false, false, true, true, true, true, false, //true, false, true, true, false, //true, false, true, false, true, true}; //internal check that the cases are lined up assert word2.length == word1.length: word2.length; assert word1.length == answers.length: answers.length; for ( int i = 0 ; i < word2.length ; i++ ){ if ( answers[i] == checker.isAnagram( word1[i], word2[i]) && answers[i] == checker.isAnagram( word2[i], word1[i]) ){ continue; } else { System.out.print("ERROR!!!"); System.out.println( "\t(\"" + word1[i] + "\" ,\t \"" + word2[i] + "\")"); if ( answers[i] ) System.out.println("\t\t -> Anagrams."); else { System.out.println("\t\t -> NOT anagrams."); } result = false ; } } return result; } }