import static org.junit.Assert.*; import java.util.Arrays; import java.util.Random; import java.util.Set; import java.util.TreeSet; import org.junit.Test; public class WordGamesUtilTest { private static final String LETTERS = "abcdefghijklmnopqrstuvwxyz"; private static Random rnd = new Random(); @Test public void testDictionary() { assertSame(Dictionary.INSTANCE, WordGamesUtil.DICTIONARY); } @Test public void testDistanceZero() { final String[] S = {"", "a", "bc", "def", "ghij", "klmno", WordGamesUtilTest.LETTERS}; for (int i = 0; i < S.length; i++) { String s = S[i]; assertEquals(0, WordGamesUtil.distance(s, s)); } } @Test public void testDistanceOne() { final String[] S = {"a", "bc", "def", "ghij", "klmno"}; for (int i = 0; i < S.length; i++) { final String s = S[i]; for (int idx = 0; idx < s.length(); idx++) { // t is s with the character at idx changed StringBuilder t = new StringBuilder(s); // change the character at idx to all possible letters for (int j = 0; j < WordGamesUtilTest.LETTERS.length(); j++) { char c = WordGamesUtilTest.LETTERS.charAt(j); if (c != s.charAt(idx)) { t.setCharAt(idx, c); assertEquals(1, WordGamesUtil.distance(s, t.toString())); assertEquals(1, WordGamesUtil.distance(t.toString(), s)); } } } } } @Test public void testDistance() { final String[] S = {"frog", "matter", "barter", "cramping", "impostor", "match"}; final String[] T = {"frog", "master", "career", "tripping", "imparted", "alarm"}; final int[] DISTANCE = {0, 1, 2, 3, 4, 5}; for (int i = 0; i < S.length; i++) { final String s = S[i]; final String t = T[i]; final int dist = DISTANCE[i]; assertEquals("distance between " + s + " and " + t, dist, WordGamesUtil.distance(s, t)); assertEquals("distance between " + s + " and " + t, dist, WordGamesUtil.distance(t, s)); } } @Test public void testAreAdjacentTrue() { final String[] S = {"be", "can", "dead", "ester", "fuller"}; final String[] T = {"me", "cat", "deed", "eater", "duller"}; for (int i = 0; i < S.length; i++) { final String s = S[i]; final String t = T[i]; assertTrue(s + " and " + t + " are adjacent", WordGamesUtil.areAdjacent(s, t)); assertTrue(s + " and " + t + " are adjacent", WordGamesUtil.areAdjacent(t, s)); } } @Test public void testAreAdjacentFalse() { final String[] S = {"be", "can", "dead", "ester", "fuller"}; final String[] T = {"id", "can", "deep", "eaten", "duster"}; for (int i = 0; i < S.length; i++) { final String s = S[i]; final String t = T[i]; assertFalse(s + " and " + t + " are not adjacent", WordGamesUtil.areAdjacent(s, t)); assertFalse(s + " and " + t + " are not adjacent", WordGamesUtil.areAdjacent(t, s)); } } @Test public void testAreAdjacentNotInDictionary() { final String[] S = {"be", "can", "dead", "ester", "fuller"}; final String[] T = {"mt", "cxt", "detd", "ejter", "euller"}; for (int i = 0; i < S.length; i++) { final String s = S[i]; final String t = T[i]; assertFalse(t + " is not in the dictionary", WordGamesUtil.areAdjacent(s, t)); assertFalse(t + " is not in the dictionary", WordGamesUtil.areAdjacent(t, s)); assertFalse(t + " is not in the dictionary", WordGamesUtil.areAdjacent(s, s)); } } @Test public void testAllAdjacent() { final String[] S = { "eat", "caste", "responsible", "zephyr" }; final String[][] ADJ = { {"bat", "cat", "ear", "eau", "eft", "fat", "gat", "hat", "kat", "lat", "mat", "oat", "pat", "qat", "rat", "sat", "tat", "vat", "wat"}, {"baste", "carte", "casts", "haste", "paste", "taste", "waste"}, {"responsibly"}, {/* nothing adjacent to zephyr */} }; for (int i = 0; i < S.length; i++) { final Set EXP = new TreeSet(Arrays.asList(ADJ[i])); assertEquals(EXP, WordGamesUtil.allAdjacentTo(S[i])); } } @Test public void testAllAdjacentIndex() { final String[] S = { "eat", "eat", "eat", "caste", "caste", "caste", "caste", "caste", "responsible", "responsible" }; final int[] INDEX = { 0, 1, 2, 0, 1, 2, 3, 4, 0, 10 }; final String[][] ADJ = { {"bat", "cat", "fat", "gat", "hat", "kat", "lat", "mat", "oat", "pat", "qat", "rat", "sat", "tat", "vat", "wat"}, {"eft"}, {"ear", "eau"}, {"baste", "haste", "paste", "taste", "waste"}, {}, {"carte"}, {}, {"casts"}, {}, {"responsibly"} }; for (int i = 0; i < S.length; i++) { final Set EXP = new TreeSet(Arrays.asList(ADJ[i])); assertEquals(EXP, WordGamesUtil.allAdjacentTo(S[i], INDEX[i])); } } }