1 2 3 package com.toy.anagrams.lib; 4 5 import java.util.Arrays ; 6 import junit.framework.TestCase; 7 8 11 public class WordLibraryTest extends TestCase { 12 13 public WordLibraryTest(String testName) { 14 super(testName); 15 } 16 17 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 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 } 46 | Popular Tags |