01: /* Anagram Game Application */
02:
03: package com.toy.anagrams.lib;
04:
05: import java.util.Arrays;
06: import junit.framework.TestCase;
07:
08: /**
09: * Test of the functionality of {@link WordLibrary}.
10: */
11: public class WordLibraryTest extends TestCase {
12:
13: public WordLibraryTest(String testName) {
14: super (testName);
15: }
16:
17: /**
18: * Test of {@link WordLibrary#isCorrect}.
19: */
20: public void testIsCorrect() {
21: for (int i = 0; i < WordLibrary.getSize(); i++) {
22: String clearWord = WordLibrary.getWord(i);
23: String scrambledWord = WordLibrary.getScrambledWord(i);
24: assertTrue("Scrambled word \"" + scrambledWord
25: + "\" at index: " + i
26: + " does not represent the word \"" + clearWord
27: + "\"", isAnagram(clearWord, scrambledWord));
28: }
29: }
30:
31: /**
32: * Tests whether given anagram represents the word.
33: * @param clearWord The word in clear text
34: * @param scrambledWord Scrambled version of the word
35: * @return true if the scrambledWord is correct anagram of clearWord
36: */
37: private boolean isAnagram(String clearWord, String scrambledWord) {
38: char[] clearArray = clearWord.toCharArray();
39: char[] scrambledArray = scrambledWord.toCharArray();
40: Arrays.sort(clearArray);
41: Arrays.sort(scrambledArray);
42: return Arrays.equals(clearArray, scrambledArray);
43: }
44:
45: }
|