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

ListOpsTest.java 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import java.util.function.BiFunction;
  6. import java.util.function.BinaryOperator;
  7. import java.util.function.Predicate;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.IntStream;
  10. import static junit.framework.TestCase.assertEquals;
  11. import static junit.framework.TestCase.assertFalse;
  12. import static junit.framework.TestCase.assertNotNull;
  13. import static junit.framework.TestCase.assertTrue;
  14. import org.junit.Ignore;
  15. import org.junit.Test;
  16. public class ListOpsTest {
  17. private static final List<Integer> EMPTY_LIST
  18. = Collections.emptyList();
  19. @Test
  20. public void lengthOfAnEmptyListShouldBeZero() {
  21. final int expected = 0;
  22. final int actual = ListOps.length(EMPTY_LIST);
  23. assertEquals(expected, actual);
  24. }
  25. @Test
  26. @Ignore("Remove to run test")
  27. public void shouldReturnTheCorrectLengthOfAnNonEmptyList() {
  28. final List<Integer> list = Collections.unmodifiableList(
  29. Arrays.asList(0, 1, 2, 3, 4)
  30. );
  31. final int actual = ListOps.length(list);
  32. final int expected = list.size();
  33. assertEquals(expected, actual);
  34. }
  35. @Test
  36. @Ignore("Remove to run test")
  37. public void shouldReverseAnEmptyList() {
  38. final List<Integer> actual = ListOps.reverse(EMPTY_LIST);
  39. assertNotNull(actual);
  40. assertTrue(actual.isEmpty());
  41. }
  42. @Test
  43. @Ignore("Remove to run test")
  44. public void shouldReverseANonEmptyList() {
  45. final List<Integer> list = Collections.unmodifiableList(
  46. Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8)
  47. );
  48. final List<Integer> actual
  49. = ListOps.reverse(list);
  50. final List<Integer> expected
  51. = Arrays.asList(8, 7, 6, 5, 4, 3, 2, 1, 0);
  52. assertNotNull(actual);
  53. assertFalse(actual.isEmpty());
  54. assertEquals(expected, actual);
  55. }
  56. @Test
  57. @Ignore("Remove to run test")
  58. public void shouldMapAnEmptyListAndReturnAnEmptyList() {
  59. final List<Integer> actual = ListOps.map(EMPTY_LIST, x -> x + 1);
  60. assertNotNull(actual);
  61. assertTrue(actual.isEmpty());
  62. }
  63. @Test
  64. @Ignore("Remove to run test")
  65. public void shouldMapNonEmptyList() {
  66. final List<Integer> list
  67. = Collections.unmodifiableList(Arrays.asList(1, 3, 5, 7));
  68. final List<Integer> actual = ListOps.map(list, x -> x + 1);
  69. assertNotNull(actual);
  70. assertFalse(actual.isEmpty());
  71. assertEquals(Arrays.asList(2, 4, 6, 8), actual);
  72. }
  73. @Test
  74. @Ignore("Remove to run test")
  75. public void shouldFilterAnEmptyListanddReturnAnEmptyList() {
  76. final List<Integer> actual = ListOps.filter(EMPTY_LIST, x -> x > 0);
  77. assertNotNull(actual);
  78. assertTrue(actual.isEmpty());
  79. }
  80. @Test
  81. @Ignore("Remove to run test")
  82. public void shouldFilterNonEmptyList() {
  83. Predicate<Integer> predicate = x -> x % 2 > 0;
  84. final List<Integer> list = Collections.unmodifiableList(
  85. IntStream.range(0, 100).boxed().collect(Collectors.toList())
  86. );
  87. final List<Integer> actual = ListOps.filter(list, predicate);
  88. final List<Integer> expected = list.stream()
  89. .filter(predicate)
  90. .collect(Collectors.toList());
  91. assertNotNull(actual);
  92. assertFalse(actual.isEmpty());
  93. assertEquals(expected, actual);
  94. }
  95. @Test
  96. @Ignore("Remove to run test")
  97. public void shouldConcatenateZeroLists() {
  98. List<Integer> actual = ListOps.concat();
  99. assertNotNull(actual);
  100. assertTrue(actual.isEmpty());
  101. }
  102. @Test
  103. @Ignore("Remove to run test")
  104. public void shouldConcatenateOneNonEmptyList() {
  105. final List<Integer> list
  106. = Collections.unmodifiableList(
  107. Arrays.asList(0, 1, 2, 3, 4)
  108. );
  109. final List<Integer> actual = ListOps.concat(list);
  110. final List<Integer> expected = Arrays.asList(0, 1, 2, 3, 4);
  111. assertNotNull(actual);
  112. assertFalse(actual.isEmpty());
  113. assertEquals(expected, actual);
  114. }
  115. @Test
  116. @Ignore("Remove to run test")
  117. public void shouldConcatenateOneEmptyList() {
  118. final List<Integer> actual = ListOps.concat(EMPTY_LIST);
  119. assertNotNull(actual);
  120. assertTrue(actual.isEmpty());
  121. }
  122. @Test
  123. @Ignore("Remove to run test")
  124. public void shouldConcatenateTwoEmptyLists() {
  125. final List<Integer> actual = ListOps.concat(EMPTY_LIST, EMPTY_LIST);
  126. assertNotNull(actual);
  127. assertTrue(actual.isEmpty());
  128. }
  129. @Test
  130. @Ignore("Remove to run test")
  131. public void shouldConcatenateOneEmptyAndOneNonEmptyLists() {
  132. final List<Integer> list
  133. = Collections.unmodifiableList(
  134. Arrays.asList(0, 1, 2, 3, 4)
  135. );
  136. final List<Integer> actual = ListOps.concat(list, EMPTY_LIST);
  137. final List<Integer> expected
  138. = Arrays.asList(0, 1, 2, 3, 4);
  139. assertNotNull(actual);
  140. assertFalse(actual.isEmpty());
  141. assertEquals(expected, actual);
  142. }
  143. @Test
  144. @Ignore("Remove to run test")
  145. public void shouldConcatenateOneNonEmptyAndOneEmptyLists() {
  146. final List<Integer> list
  147. = Collections.unmodifiableList(
  148. Arrays.asList(0, 1, 2, 3, 4)
  149. );
  150. final List<Integer> actual = ListOps.concat(EMPTY_LIST, list);
  151. final List<Integer> expected
  152. = Arrays.asList(0, 1, 2, 3, 4);
  153. assertNotNull(actual);
  154. assertFalse(actual.isEmpty());
  155. assertEquals(expected, actual);
  156. }
  157. @Test
  158. @Ignore("Remove to run test")
  159. public void shouldConcatenateTwoListsWithSameElements() {
  160. final List<Integer> list1 = Collections.unmodifiableList(
  161. Arrays.asList(0, 1, 2, 3, 4)
  162. );
  163. final List<Integer> list2 = Collections.unmodifiableList(
  164. Arrays.asList(1, 2, 3, 4, 5, 6)
  165. );
  166. final List<Integer> expected
  167. = Arrays.asList(0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6);
  168. final List<Integer> actual = ListOps.concat(list1, list2);
  169. assertNotNull(actual);
  170. assertFalse(actual.isEmpty());
  171. assertEquals(expected, actual);
  172. }
  173. @Test
  174. @Ignore("Remove to run test")
  175. public void shouldConcatenateSeveralLists() {
  176. final List<Integer> list1 = Collections.unmodifiableList(
  177. Arrays.asList(0, 1, 2, 3)
  178. );
  179. final List<Integer> list2 = Collections.unmodifiableList(
  180. Arrays.asList(4, 5, 6, 7)
  181. );
  182. final List<Integer> list3 = Collections.unmodifiableList(
  183. Arrays.asList(8, 9, 10, 11)
  184. );
  185. final List<Integer> list4 = Collections.unmodifiableList(
  186. Arrays.asList(12, 13, 14, 15)
  187. );
  188. final List<Integer> expected
  189. = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  190. 14, 15);
  191. final List<Integer> actual
  192. = ListOps.concat(list1, list2, EMPTY_LIST, list3, list4);
  193. assertNotNull(actual);
  194. assertFalse(actual.isEmpty());
  195. assertEquals(expected, actual);
  196. }
  197. @Test
  198. @Ignore("Remove to run test")
  199. public void shouldReturnIdentityWhenAnEmptyListIsReduced() {
  200. final int expected = 0;
  201. final int actual
  202. = ListOps.reduce(EMPTY_LIST, 0, (x, y) -> x + y, Integer::sum);
  203. assertEquals(expected, actual);
  204. }
  205. @Test
  206. @Ignore("Remove to run test")
  207. public void shouldCalculateTheSumOfANonEmptyIntegerList() {
  208. final List<Integer> list = Collections.unmodifiableList(
  209. Arrays.asList(0, 1, 2, 3, 4)
  210. );
  211. final int actual = ListOps.reduce(list, 0,
  212. (x, y) -> x + y,
  213. Integer::sum);
  214. assertEquals(10, actual);
  215. }
  216. /*
  217. https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
  218. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#reduce-U-java.util.function.BiFunction-java.util.function.BinaryOperator-
  219. */
  220. private BiFunction<List<Integer>, Integer, List<Integer>> accumulator
  221. = (List<Integer> partial, Integer elem) -> {
  222. List<Integer> result = new ArrayList<>(partial);
  223. result.add(elem);
  224. return result;
  225. };
  226. private BinaryOperator<List<Integer>> combiner
  227. = (list1, list2) -> {
  228. List<Integer> result = new ArrayList<>(list1);
  229. result.addAll(list2);
  230. return result;
  231. };
  232. @Test
  233. @Ignore("Remove to run test")
  234. public void shouldReduceAnEmptyListAndANonEmptyListAndReturnConcatenation() {
  235. final List<Integer> list = Collections.unmodifiableList(
  236. Arrays.asList(0, 1, 2, 3, 4, 5)
  237. );
  238. final List<Integer> actual
  239. = ListOps.reduce(list,
  240. new ArrayList<Integer>(),
  241. accumulator,
  242. combiner);
  243. final List<Integer> expected
  244. = Arrays.asList(0, 1, 2, 3, 4, 5);
  245. assertNotNull(actual);
  246. assertFalse(actual.isEmpty());
  247. assertEquals(expected, actual);
  248. }
  249. @Test
  250. @Ignore("Remove to run test")
  251. public void shouldReduceTwoNonEmptyListsAndReturnConcatenation() {
  252. final List<Integer> listOne = Collections.unmodifiableList(
  253. Arrays.asList(0, 1, 2, 3, 4)
  254. );
  255. final List<Integer> listTwo = Collections.unmodifiableList(
  256. Arrays.asList(5, 6, 7, 8, 9)
  257. );
  258. final List<Integer> actual
  259. = ListOps.reduce(listTwo,
  260. listOne,
  261. accumulator,
  262. combiner);
  263. final List<Integer> expected
  264. = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  265. assertNotNull(actual);
  266. assertFalse(actual.isEmpty());
  267. assertEquals(expected, actual);
  268. }
  269. }