lots of exercises in java... from https://github.com/exercism/java

WordSearcherTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import org.junit.Before;
  2. import org.junit.Test;
  3. import java.util.*;
  4. import static org.junit.Assert.assertEquals;
  5. public class WordSearcherTest {
  6. private WordSearcher wordSearcher;
  7. @Before
  8. public void setUp() {
  9. wordSearcher = new WordSearcher();
  10. }
  11. @Test
  12. public void testLocatesWordsWrittenLeftToRight() {
  13. Map<String, Optional<WordLocation>> expectedLocations = new HashMap<>();
  14. expectedLocations.put("clojure", Optional.of(new WordLocation(new Pair(1, 10), new Pair(7, 10))));
  15. Set<String> searchWords = expectedLocations.keySet();
  16. Map<String, Optional<WordLocation>> actualLocations = wordSearcher.search(
  17. searchWords,
  18. new char[][]{
  19. {'j', 'e', 'f', 'b', 'l', 'p', 'e', 'p', 'r', 'e'},
  20. {'c', 'a', 'm', 'd', 'c', 'i', 'm', 'g', 't', 'c'},
  21. {'o', 'i', 'v', 'o', 'k', 'p', 'r', 'j', 's', 'm'},
  22. {'p', 'b', 'w', 'a', 's', 'q', 'r', 'o', 'u', 'a'},
  23. {'r', 'i', 'x', 'i', 'l', 'e', 'l', 'h', 'r', 's'},
  24. {'w', 'o', 'l', 'c', 'q', 'l', 'i', 'r', 'p', 'c'},
  25. {'s', 'c', 'r', 'e', 'e', 'a', 'u', 'm', 'g', 'r'},
  26. {'a', 'l', 'x', 'h', 'p', 'b', 'u', 'r', 'y', 'i'},
  27. {'j', 'a', 'l', 'a', 'y', 'c', 'a', 'l', 'm', 'p'},
  28. {'c', 'l', 'o', 'j', 'u', 'r', 'e', 'r', 'm', 't'}
  29. }
  30. );
  31. assertEquals(expectedLocations, actualLocations);
  32. }
  33. @Test
  34. public void testLocatesWordsWrittenRightToLeft() {
  35. Map<String, Optional<WordLocation>> expectedLocations = new HashMap<>();
  36. expectedLocations.put("clojure", Optional.of(new WordLocation(new Pair(1, 10), new Pair(7, 10))));
  37. expectedLocations.put("elixir", Optional.of(new WordLocation(new Pair(6, 5), new Pair(1, 5))));
  38. Set<String> searchWords = expectedLocations.keySet();
  39. Map<String, Optional<WordLocation>> actualLocations = wordSearcher.search(
  40. searchWords,
  41. new char[][]{
  42. {'j', 'e', 'f', 'b', 'l', 'p', 'e', 'p', 'r', 'e'},
  43. {'c', 'a', 'm', 'd', 'c', 'i', 'm', 'g', 't', 'c'},
  44. {'o', 'i', 'v', 'o', 'k', 'p', 'r', 'j', 's', 'm'},
  45. {'p', 'b', 'w', 'a', 's', 'q', 'r', 'o', 'u', 'a'},
  46. {'r', 'i', 'x', 'i', 'l', 'e', 'l', 'h', 'r', 's'},
  47. {'w', 'o', 'l', 'c', 'q', 'l', 'i', 'r', 'p', 'c'},
  48. {'s', 'c', 'r', 'e', 'e', 'a', 'u', 'm', 'g', 'r'},
  49. {'a', 'l', 'x', 'h', 'p', 'b', 'u', 'r', 'y', 'i'},
  50. {'j', 'a', 'l', 'a', 'y', 'c', 'a', 'l', 'm', 'p'},
  51. {'c', 'l', 'o', 'j', 'u', 'r', 'e', 'r', 'm', 't'}
  52. }
  53. );
  54. assertEquals(expectedLocations, actualLocations);
  55. }
  56. @Test
  57. public void testLocatesWordsWrittenTopToBottom() {
  58. Map<String, Optional<WordLocation>> expectedLocations = new HashMap<>();
  59. expectedLocations.put("clojure", Optional.of(new WordLocation(new Pair( 1, 10), new Pair( 7, 10))));
  60. expectedLocations.put("elixir", Optional.of(new WordLocation(new Pair( 6, 5), new Pair( 1, 5))));
  61. expectedLocations.put("ecmascript", Optional.of(new WordLocation(new Pair(10, 1), new Pair(10, 10))));
  62. Set<String> searchWords = expectedLocations.keySet();
  63. Map<String, Optional<WordLocation>> actualLocations = wordSearcher.search(
  64. searchWords,
  65. new char[][]{
  66. {'j', 'e', 'f', 'b', 'l', 'p', 'e', 'p', 'r', 'e'},
  67. {'c', 'a', 'm', 'd', 'c', 'i', 'm', 'g', 't', 'c'},
  68. {'o', 'i', 'v', 'o', 'k', 'p', 'r', 'j', 's', 'm'},
  69. {'p', 'b', 'w', 'a', 's', 'q', 'r', 'o', 'u', 'a'},
  70. {'r', 'i', 'x', 'i', 'l', 'e', 'l', 'h', 'r', 's'},
  71. {'w', 'o', 'l', 'c', 'q', 'l', 'i', 'r', 'p', 'c'},
  72. {'s', 'c', 'r', 'e', 'e', 'a', 'u', 'm', 'g', 'r'},
  73. {'a', 'l', 'x', 'h', 'p', 'b', 'u', 'r', 'y', 'i'},
  74. {'j', 'a', 'l', 'a', 'y', 'c', 'a', 'l', 'm', 'p'},
  75. {'c', 'l', 'o', 'j', 'u', 'r', 'e', 'r', 'm', 't'}
  76. }
  77. );
  78. assertEquals(expectedLocations, actualLocations);
  79. }
  80. @Test
  81. public void testLocatesWordsWrittenBottomToTop() {
  82. Map<String, Optional<WordLocation>> expectedLocations = new HashMap<>();
  83. expectedLocations.put("clojure", Optional.of(new WordLocation(new Pair( 1, 10), new Pair( 7, 10))));
  84. expectedLocations.put("elixir", Optional.of(new WordLocation(new Pair( 6, 5), new Pair( 1, 5))));
  85. expectedLocations.put("ecmascript", Optional.of(new WordLocation(new Pair(10, 1), new Pair(10, 10))));
  86. expectedLocations.put("rust", Optional.of(new WordLocation(new Pair( 9, 5), new Pair( 9, 2))));
  87. Set<String> searchWords = expectedLocations.keySet();
  88. Map<String, Optional<WordLocation>> actualLocations = wordSearcher.search(
  89. searchWords,
  90. new char[][]{
  91. {'j', 'e', 'f', 'b', 'l', 'p', 'e', 'p', 'r', 'e'},
  92. {'c', 'a', 'm', 'd', 'c', 'i', 'm', 'g', 't', 'c'},
  93. {'o', 'i', 'v', 'o', 'k', 'p', 'r', 'j', 's', 'm'},
  94. {'p', 'b', 'w', 'a', 's', 'q', 'r', 'o', 'u', 'a'},
  95. {'r', 'i', 'x', 'i', 'l', 'e', 'l', 'h', 'r', 's'},
  96. {'w', 'o', 'l', 'c', 'q', 'l', 'i', 'r', 'p', 'c'},
  97. {'s', 'c', 'r', 'e', 'e', 'a', 'u', 'm', 'g', 'r'},
  98. {'a', 'l', 'x', 'h', 'p', 'b', 'u', 'r', 'y', 'i'},
  99. {'j', 'a', 'l', 'a', 'y', 'c', 'a', 'l', 'm', 'p'},
  100. {'c', 'l', 'o', 'j', 'u', 'r', 'e', 'r', 'm', 't'}});
  101. assertEquals(expectedLocations, actualLocations);
  102. }
  103. @Test
  104. public void testLocatesWordsWrittenTopLeftToBottomRight() {
  105. Map<String, Optional<WordLocation>> expectedLocations = new HashMap<>();
  106. expectedLocations.put("clojure", Optional.of(new WordLocation(new Pair( 1, 10), new Pair( 7, 10))));
  107. expectedLocations.put("elixir", Optional.of(new WordLocation(new Pair( 6, 5), new Pair( 1, 5))));
  108. expectedLocations.put("ecmascript", Optional.of(new WordLocation(new Pair(10, 1), new Pair(10, 10))));
  109. expectedLocations.put("rust", Optional.of(new WordLocation(new Pair( 9, 5), new Pair( 9, 2))));
  110. expectedLocations.put("java", Optional.of(new WordLocation(new Pair( 1, 1), new Pair( 4, 4))));
  111. Set<String> searchWords = expectedLocations.keySet();
  112. Map<String, Optional<WordLocation>> actualLocations = wordSearcher.search(
  113. searchWords,
  114. new char[][]{
  115. {'j', 'e', 'f', 'b', 'l', 'p', 'e', 'p', 'r', 'e'},
  116. {'c', 'a', 'm', 'd', 'c', 'i', 'm', 'g', 't', 'c'},
  117. {'o', 'i', 'v', 'o', 'k', 'p', 'r', 'j', 's', 'm'},
  118. {'p', 'b', 'w', 'a', 's', 'q', 'r', 'o', 'u', 'a'},
  119. {'r', 'i', 'x', 'i', 'l', 'e', 'l', 'h', 'r', 's'},
  120. {'w', 'o', 'l', 'c', 'q', 'l', 'i', 'r', 'p', 'c'},
  121. {'s', 'c', 'r', 'e', 'e', 'a', 'u', 'm', 'g', 'r'},
  122. {'a', 'l', 'x', 'h', 'p', 'b', 'u', 'r', 'y', 'i'},
  123. {'j', 'a', 'l', 'a', 'y', 'c', 'a', 'l', 'm', 'p'},
  124. {'c', 'l', 'o', 'j', 'u', 'r', 'e', 'r', 'm', 't'}});
  125. assertEquals(expectedLocations, actualLocations);
  126. }
  127. @Test
  128. public void testLocatesWordsWrittenBottomRightToTopLeft() {
  129. Map<String, Optional<WordLocation>> expectedLocations = new HashMap<>();
  130. expectedLocations.put("clojure", Optional.of(new WordLocation(new Pair( 1, 10), new Pair( 7, 10))));
  131. expectedLocations.put("elixir", Optional.of(new WordLocation(new Pair( 6, 5), new Pair( 1, 5))));
  132. expectedLocations.put("ecmascript", Optional.of(new WordLocation(new Pair(10, 1), new Pair(10, 10))));
  133. expectedLocations.put("rust", Optional.of(new WordLocation(new Pair( 9, 5), new Pair( 9, 2))));
  134. expectedLocations.put("java", Optional.of(new WordLocation(new Pair( 1, 1), new Pair( 4, 4))));
  135. expectedLocations.put("lua", Optional.of(new WordLocation(new Pair( 8, 9), new Pair( 6, 7))));
  136. Set<String> searchWords = expectedLocations.keySet();
  137. Map<String, Optional<WordLocation>> actualLocations = wordSearcher.search(
  138. searchWords,
  139. new char[][]{
  140. {'j', 'e', 'f', 'b', 'l', 'p', 'e', 'p', 'r', 'e'},
  141. {'c', 'a', 'm', 'd', 'c', 'i', 'm', 'g', 't', 'c'},
  142. {'o', 'i', 'v', 'o', 'k', 'p', 'r', 'j', 's', 'm'},
  143. {'p', 'b', 'w', 'a', 's', 'q', 'r', 'o', 'u', 'a'},
  144. {'r', 'i', 'x', 'i', 'l', 'e', 'l', 'h', 'r', 's'},
  145. {'w', 'o', 'l', 'c', 'q', 'l', 'i', 'r', 'p', 'c'},
  146. {'s', 'c', 'r', 'e', 'e', 'a', 'u', 'm', 'g', 'r'},
  147. {'a', 'l', 'x', 'h', 'p', 'b', 'u', 'r', 'y', 'i'},
  148. {'j', 'a', 'l', 'a', 'y', 'c', 'a', 'l', 'm', 'p'},
  149. {'c', 'l', 'o', 'j', 'u', 'r', 'e', 'r', 'm', 't'}});
  150. assertEquals(expectedLocations, actualLocations);
  151. }
  152. @Test
  153. public void testLocatesWordsWrittenBottomLeftToTopRight() {
  154. Map<String, Optional<WordLocation>> expectedLocations = new HashMap<>();
  155. expectedLocations.put("clojure", Optional.of(new WordLocation(new Pair( 1, 10), new Pair( 7, 10))));
  156. expectedLocations.put("elixir", Optional.of(new WordLocation(new Pair( 6, 5), new Pair( 1, 5))));
  157. expectedLocations.put("ecmascript", Optional.of(new WordLocation(new Pair(10, 1), new Pair(10, 10))));
  158. expectedLocations.put("rust", Optional.of(new WordLocation(new Pair( 9, 5), new Pair( 9, 2))));
  159. expectedLocations.put("java", Optional.of(new WordLocation(new Pair( 1, 1), new Pair( 4, 4))));
  160. expectedLocations.put("lua", Optional.of(new WordLocation(new Pair( 8, 9), new Pair( 6, 7))));
  161. expectedLocations.put("lisp", Optional.of(new WordLocation(new Pair( 3, 6), new Pair( 6, 3))));
  162. Set<String> searchWords = expectedLocations.keySet();
  163. Map<String, Optional<WordLocation>> actualLocations = wordSearcher.search(
  164. searchWords,
  165. new char[][]{
  166. {'j', 'e', 'f', 'b', 'l', 'p', 'e', 'p', 'r', 'e'},
  167. {'c', 'a', 'm', 'd', 'c', 'i', 'm', 'g', 't', 'c'},
  168. {'o', 'i', 'v', 'o', 'k', 'p', 'r', 'j', 's', 'm'},
  169. {'p', 'b', 'w', 'a', 's', 'q', 'r', 'o', 'u', 'a'},
  170. {'r', 'i', 'x', 'i', 'l', 'e', 'l', 'h', 'r', 's'},
  171. {'w', 'o', 'l', 'c', 'q', 'l', 'i', 'r', 'p', 'c'},
  172. {'s', 'c', 'r', 'e', 'e', 'a', 'u', 'm', 'g', 'r'},
  173. {'a', 'l', 'x', 'h', 'p', 'b', 'u', 'r', 'y', 'i'},
  174. {'j', 'a', 'l', 'a', 'y', 'c', 'a', 'l', 'm', 'p'},
  175. {'c', 'l', 'o', 'j', 'u', 'r', 'e', 'r', 'm', 't'}});
  176. assertEquals(expectedLocations, actualLocations);
  177. }
  178. @Test
  179. public void testLocatesWordsWrittenTopRightToBottomLeft() {
  180. Map<String, Optional<WordLocation>> expectedLocations = new HashMap<>();
  181. expectedLocations.put("clojure", Optional.of(new WordLocation(new Pair( 1, 10), new Pair( 7, 10))));
  182. expectedLocations.put("elixir", Optional.of(new WordLocation(new Pair( 6, 5), new Pair( 1, 5))));
  183. expectedLocations.put("ecmascript", Optional.of(new WordLocation(new Pair(10, 1), new Pair(10, 10))));
  184. expectedLocations.put("rust", Optional.of(new WordLocation(new Pair( 9, 5), new Pair( 9, 2))));
  185. expectedLocations.put("java", Optional.of(new WordLocation(new Pair( 1, 1), new Pair( 4, 4))));
  186. expectedLocations.put("lua", Optional.of(new WordLocation(new Pair( 8, 9), new Pair( 6, 7))));
  187. expectedLocations.put("lisp", Optional.of(new WordLocation(new Pair( 3, 6), new Pair( 6, 3))));
  188. expectedLocations.put("ruby", Optional.of(new WordLocation(new Pair( 8, 6), new Pair( 5, 9))));
  189. Set<String> searchWords = expectedLocations.keySet();
  190. Map<String, Optional<WordLocation>> actualLocations = wordSearcher.search(
  191. searchWords,
  192. new char[][]{
  193. {'j', 'e', 'f', 'b', 'l', 'p', 'e', 'p', 'r', 'e'},
  194. {'c', 'a', 'm', 'd', 'c', 'i', 'm', 'g', 't', 'c'},
  195. {'o', 'i', 'v', 'o', 'k', 'p', 'r', 'j', 's', 'm'},
  196. {'p', 'b', 'w', 'a', 's', 'q', 'r', 'o', 'u', 'a'},
  197. {'r', 'i', 'x', 'i', 'l', 'e', 'l', 'h', 'r', 's'},
  198. {'w', 'o', 'l', 'c', 'q', 'l', 'i', 'r', 'p', 'c'},
  199. {'s', 'c', 'r', 'e', 'e', 'a', 'u', 'm', 'g', 'r'},
  200. {'a', 'l', 'x', 'h', 'p', 'b', 'u', 'r', 'y', 'i'},
  201. {'j', 'a', 'l', 'a', 'y', 'c', 'a', 'l', 'm', 'p'},
  202. {'c', 'l', 'o', 'j', 'u', 'r', 'e', 'r', 'm', 't'}});
  203. assertEquals(expectedLocations, actualLocations);
  204. }
  205. @Test
  206. public void testFailsToLocateAWordsThatIsNotInThePuzzle() {
  207. Map<String, Optional<WordLocation>> expectedLocations = new HashMap<>();
  208. expectedLocations.put("clojure", Optional.of(new WordLocation(new Pair( 1, 10), new Pair( 7, 10))));
  209. expectedLocations.put("elixir", Optional.of(new WordLocation(new Pair( 6, 5), new Pair( 1, 5))));
  210. expectedLocations.put("ecmascript", Optional.of(new WordLocation(new Pair(10, 1), new Pair(10, 10))));
  211. expectedLocations.put("rust", Optional.of(new WordLocation(new Pair( 9, 5), new Pair( 9, 2))));
  212. expectedLocations.put("java", Optional.of(new WordLocation(new Pair( 1, 1), new Pair( 4, 4))));
  213. expectedLocations.put("lua", Optional.of(new WordLocation(new Pair( 8, 9), new Pair( 6, 7))));
  214. expectedLocations.put("lisp", Optional.of(new WordLocation(new Pair( 3, 6), new Pair( 6, 3))));
  215. expectedLocations.put("ruby", Optional.of(new WordLocation(new Pair( 8, 6), new Pair( 5, 9))));
  216. expectedLocations.put("haskell", Optional.empty());
  217. Set<String> searchWords = expectedLocations.keySet();
  218. Map<String, Optional<WordLocation>> actualLocations = wordSearcher.search(
  219. searchWords,
  220. new char[][]{
  221. {'j', 'e', 'f', 'b', 'l', 'p', 'e', 'p', 'r', 'e'},
  222. {'c', 'a', 'm', 'd', 'c', 'i', 'm', 'g', 't', 'c'},
  223. {'o', 'i', 'v', 'o', 'k', 'p', 'r', 'j', 's', 'm'},
  224. {'p', 'b', 'w', 'a', 's', 'q', 'r', 'o', 'u', 'a'},
  225. {'r', 'i', 'x', 'i', 'l', 'e', 'l', 'h', 'r', 's'},
  226. {'w', 'o', 'l', 'c', 'q', 'l', 'i', 'r', 'p', 'c'},
  227. {'s', 'c', 'r', 'e', 'e', 'a', 'u', 'm', 'g', 'r'},
  228. {'a', 'l', 'x', 'h', 'p', 'b', 'u', 'r', 'y', 'i'},
  229. {'j', 'a', 'l', 'a', 'y', 'c', 'a', 'l', 'm', 'p'},
  230. {'c', 'l', 'o', 'j', 'u', 'r', 'e', 'r', 'm', 't'}});
  231. assertEquals(expectedLocations, actualLocations);
  232. }
  233. }