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

ListOpsTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import org.junit.Ignore;
  2. import org.junit.Test;
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import static org.junit.Assert.assertEquals;
  7. public class ListOpsTest {
  8. @Test
  9. public void testAppendingEmptyLists() {
  10. assertEquals(
  11. Collections.emptyList(),
  12. ListOps.append(Collections.emptyList(), Collections.emptyList()));
  13. }
  14. @Ignore("Remove to run test")
  15. @Test
  16. public void testAppendingNonEmptyListOnEmptyList() {
  17. assertEquals(
  18. Arrays.asList('1', '2', '3', '4'),
  19. ListOps.append(Collections.emptyList(), Arrays.asList('1', '2', '3', '4')));
  20. }
  21. @Ignore("Remove to run test")
  22. @Test
  23. public void testAppendingNonEmptyListOnNonEmptyList() {
  24. assertEquals(
  25. Arrays.asList("1", "2", "2", "3", "4", "5"),
  26. ListOps.append(Arrays.asList("1", "2"), Arrays.asList("2", "3", "4", "5")));
  27. }
  28. @Ignore("Remove to run test")
  29. @Test
  30. public void testConcatOnEmptyListOfLists() {
  31. assertEquals(
  32. Collections.emptyList(),
  33. ListOps.concat(Collections.emptyList()));
  34. }
  35. @Ignore("Remove to run test")
  36. @Test
  37. public void testConcatOnNonEmptyListOfLists() {
  38. List<List<Character>> listOfLists = Arrays.asList(
  39. Arrays.asList('1', '2'),
  40. Collections.singletonList('3'),
  41. Collections.emptyList(),
  42. Arrays.asList('4', '5', '6'));
  43. assertEquals(
  44. Arrays.asList('1', '2', '3', '4', '5', '6'),
  45. ListOps.concat(listOfLists));
  46. }
  47. @Ignore("Remove to run test")
  48. @Test
  49. public void testFilteringEmptyList() {
  50. assertEquals(
  51. Collections.emptyList(),
  52. ListOps.filter(Collections.<Integer>emptyList(), integer -> integer % 2 == 1));
  53. }
  54. @Ignore("Remove to run test")
  55. @Test
  56. public void testFilteringNonEmptyList() {
  57. assertEquals(
  58. Arrays.asList(1, 3, 5),
  59. ListOps.filter(Arrays.asList(1, 2, 3, 5), integer -> integer % 2 == 1));
  60. }
  61. @Ignore("Remove to run test")
  62. @Test
  63. public void testSizeOfEmptyList() {
  64. assertEquals(0, ListOps.size(Collections.emptyList()));
  65. }
  66. @Ignore("Remove to run test")
  67. @Test
  68. public void testSizeOfNonEmptyList() {
  69. assertEquals(4, ListOps.size(Arrays.asList("one", "two", "three", "four")));
  70. }
  71. @Ignore("Remove to run test")
  72. @Test
  73. public void testTransformingEmptyList() {
  74. assertEquals(
  75. Collections.emptyList(),
  76. ListOps.map(Collections.<Integer>emptyList(), integer -> integer + 1 ));
  77. }
  78. @Ignore("Remove to run test")
  79. @Test
  80. public void testTransformingNonEmptyList() {
  81. assertEquals(
  82. Arrays.asList(2, 4, 6, 8),
  83. ListOps.map(Arrays.asList(1, 3, 5, 7), integer -> integer + 1 ));
  84. }
  85. @Ignore("Remove to run test")
  86. @Test
  87. public void testFoldLeftOnEmptyList() {
  88. assertEquals(
  89. new Double(2.0), // Boxing required for method overload disambiguation.
  90. ListOps.foldLeft(
  91. Collections.<Double>emptyList(),
  92. 2.0,
  93. (x, y) -> x * y));
  94. }
  95. @Ignore("Remove to run test")
  96. @Test
  97. public void testFoldLeftWithDirectionIndependentOperationOnNonEmptyList() {
  98. assertEquals(
  99. new Integer(15), // Boxing required for method overload disambiguation.
  100. ListOps.foldLeft(
  101. Arrays.asList(1, 2, 3, 4),
  102. 5,
  103. (x, y) -> x + y));
  104. }
  105. @Ignore("Remove to run test")
  106. @Test
  107. public void testFoldLeftWithDirectionDependentOperationOnNonEmptyList() {
  108. assertEquals(
  109. new Integer(0), // Boxing required for method overload disambiguation.
  110. ListOps.foldLeft(
  111. Arrays.asList(2, 5),
  112. 5,
  113. (x, y) -> x / y));
  114. }
  115. @Ignore("Remove to run test")
  116. @Test
  117. public void testFoldRightOnEmptyList() {
  118. assertEquals(
  119. new Double(2.0), // Boxing required for method overload disambiguation.
  120. ListOps.foldRight(
  121. Collections.<Double>emptyList(),
  122. 2.0,
  123. (x, y) -> x * y));
  124. }
  125. @Ignore("Remove to run test")
  126. @Test
  127. public void testFoldRightWithDirectionIndependentOperationOnNonEmptyList() {
  128. assertEquals(
  129. new Integer(15), // Boxing required for method overload disambiguation.
  130. ListOps.foldRight(
  131. Arrays.asList(1, 2, 3, 4),
  132. 5,
  133. (x, y) -> x + y));
  134. }
  135. @Ignore("Remove to run test")
  136. @Test
  137. public void testFoldRightWithDirectionDependentOperationOnNonEmptyList() {
  138. assertEquals(
  139. new Integer(2), // Boxing required for method overload disambiguation.
  140. ListOps.foldRight(
  141. Arrays.asList(2, 5),
  142. 5,
  143. (x, y) -> x / y));
  144. }
  145. @Ignore("Remove to run test")
  146. @Test
  147. public void testReversingEmptyList() {
  148. assertEquals(
  149. Collections.emptyList(),
  150. ListOps.reverse(Collections.emptyList()));
  151. }
  152. @Ignore("Remove to run test")
  153. @Test
  154. public void testReversingNonEmptyList() {
  155. assertEquals(
  156. Arrays.asList('7', '5', '3', '1'),
  157. ListOps.reverse(Arrays.asList('1', '3', '5', '7')));
  158. }
  159. }