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

StrainTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import org.junit.Assert;
  2. import org.junit.Test;
  3. import org.junit.Ignore;
  4. import java.util.Arrays;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. public class StrainTest {
  8. @Test
  9. public void emptyKeep() {
  10. List<Integer> input = new LinkedList<>();
  11. List<Integer> expectedOutput = new LinkedList<>();
  12. Assert.assertEquals(expectedOutput, Strain.keep(input, x -> x < 10));
  13. }
  14. @Ignore("Remove to run test")
  15. @Test
  16. public void keepEverything() {
  17. List<Integer> input = Arrays.asList(1, 2, 3);
  18. List<Integer> expectedOutput = Arrays.asList(1, 2, 3);
  19. Assert.assertEquals(expectedOutput, Strain.keep(input, x -> x < 10));
  20. }
  21. @Ignore("Remove to run test")
  22. @Test
  23. public void keepFirstAndLast() {
  24. List<Integer> input = Arrays.asList(1, 2, 3);
  25. List<Integer> expectedOutput = Arrays.asList(1, 3);
  26. Assert.assertEquals(expectedOutput, Strain.keep(input, x -> x % 2 != 0));
  27. }
  28. @Ignore("Remove to run test")
  29. @Test
  30. public void keepNeitherFirstNorLast() {
  31. List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
  32. List<Integer> expectedOutput = Arrays.asList(2, 4);
  33. Assert.assertEquals(expectedOutput, Strain.keep(input, x -> x % 2 == 0));
  34. }
  35. @Ignore("Remove to run test")
  36. @Test
  37. public void KeepStrings() {
  38. List<String> words = Arrays
  39. .asList("apple zebra banana zombies cherimoya zelot".split(" "));
  40. List<String> expectedOutput = Arrays.asList("zebra", "zombies", "zelot");
  41. Assert.assertEquals(expectedOutput,
  42. Strain.keep(words, x -> x.startsWith("z")));
  43. }
  44. @Ignore("Remove to run test")
  45. @Test
  46. public void KeepArrays() {
  47. List<List<Integer>> actual = Arrays.asList(
  48. Arrays.asList(1, 2, 3),
  49. Arrays.asList(5, 5, 5),
  50. Arrays.asList(5, 1, 2),
  51. Arrays.asList(2, 1, 2),
  52. Arrays.asList(1, 5, 2),
  53. Arrays.asList(2, 2, 1),
  54. Arrays.asList(1, 2, 5));
  55. List<List<Integer>> expectedOutput = Arrays.asList(
  56. Arrays.asList(5, 5, 5),
  57. Arrays.asList(5, 1, 2),
  58. Arrays.asList(1, 5, 2),
  59. Arrays.asList(1, 2, 5));
  60. Assert.assertEquals(expectedOutput,
  61. Strain.keep(actual, col -> col.contains(5)));
  62. }
  63. @Ignore("Remove to run test")
  64. @Test
  65. public void emptyDiscard() {
  66. List<Integer> input = new LinkedList<>();
  67. List<Integer> expectedOutput = new LinkedList<>();
  68. Assert.assertEquals(expectedOutput, Strain.discard(input, x -> x < 10));
  69. }
  70. @Ignore("Remove to run test")
  71. @Test
  72. public void discardNothing() {
  73. List<Integer> input = Arrays.asList(1, 2, 3);
  74. List<Integer> expectedOutput = Arrays.asList(1, 2, 3);
  75. Assert.assertEquals(expectedOutput, Strain.discard(input, x -> x > 10));
  76. }
  77. @Ignore("Remove to run test")
  78. @Test
  79. public void discardFirstAndLast() {
  80. List<Integer> input = Arrays.asList(1, 2, 3);
  81. List<Integer> expectedOutput = Arrays.asList(2);
  82. Assert.assertEquals(expectedOutput, Strain.discard(input, x -> x % 2 != 0));
  83. }
  84. @Ignore("Remove to run test")
  85. @Test
  86. public void discardNeitherFirstNorLast() {
  87. List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
  88. List<Integer> expectedOutput = Arrays.asList(1, 3, 5);
  89. Assert.assertEquals(expectedOutput, Strain.discard(input, x -> x % 2 == 0));
  90. }
  91. @Ignore("Remove to run test")
  92. @Test
  93. public void discardStrings() {
  94. List<String> words = Arrays
  95. .asList("apple zebra banana zombies cherimoya zelot".split(" "));
  96. List<String> expectedOutput = Arrays.asList("apple", "banana", "cherimoya");
  97. Assert.assertEquals(expectedOutput,
  98. Strain.discard(words, x -> x.startsWith("z")));
  99. }
  100. @Ignore("Remove to run test")
  101. @Test
  102. public void discardArrays() {
  103. List<List<Integer>> actual = Arrays.asList(
  104. Arrays.asList(1, 2, 3),
  105. Arrays.asList(5, 5, 5),
  106. Arrays.asList(5, 1, 2),
  107. Arrays.asList(2, 1, 2),
  108. Arrays.asList(1, 5, 2),
  109. Arrays.asList(2, 2, 1),
  110. Arrays.asList(1, 2, 5));
  111. List<List<Integer>> expectedOutput = Arrays.asList(
  112. Arrays.asList(1, 2, 3),
  113. Arrays.asList(2, 1, 2),
  114. Arrays.asList(2, 2, 1));
  115. Assert.assertEquals(expectedOutput,
  116. Strain.discard(actual, col -> col.contains(5)));
  117. }
  118. }