KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > toy > anagrams > lib > WordLibraryTest


1 /* Anagram Game Application */
2
3 package com.toy.anagrams.lib;
4
5 import java.util.Arrays JavaDoc;
6 import junit.framework.TestCase;
7
8 /**
9  * Test of the functionality of {@link WordLibrary}.
10  */

11 public class WordLibraryTest extends TestCase {
12
13     public WordLibraryTest(String JavaDoc 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 JavaDoc clearWord = WordLibrary.getWord(i);
23             String JavaDoc 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 JavaDoc clearWord, String JavaDoc 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 }
46
Popular Tags