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

AccumulateTest.java 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import org.junit.Test;
  2. import org.junit.Ignore;
  3. import java.util.Arrays;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import static org.junit.Assert.assertEquals;
  7. public class AccumulateTest {
  8. @Test
  9. public void emptyAccumulateProducesEmptyAccumulation() {
  10. List<Integer> input = new LinkedList<>();
  11. List<Integer> expectedOutput = new LinkedList<>();
  12. assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x * x));
  13. }
  14. @Ignore("Remove to run test")
  15. @Test
  16. public void accumulateSquares() {
  17. List<Integer> input = Arrays.asList(1, 2, 3);
  18. List<Integer> expectedOutput = Arrays.asList(1, 4, 9);
  19. assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x * x));
  20. }
  21. @Ignore("Remove to run test")
  22. @Test
  23. public void accumulateUpperCases() {
  24. List<String> input = Arrays.asList("hello", "world");
  25. List<String> expectedOutput = Arrays.asList("HELLO", "WORLD");
  26. assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x.toUpperCase()));
  27. }
  28. @Ignore("Remove to run test")
  29. @Test
  30. public void accumulateReversedStrings() {
  31. List<String> input = Arrays.asList("the quick brown fox etc".split(" "));
  32. List<String> expectedOutput = Arrays.asList("eht kciuq nworb xof cte".split(" "));
  33. assertEquals(expectedOutput, Accumulate.accumulate(input, this::reverse));
  34. }
  35. private String reverse(String input) {
  36. return new StringBuilder(input).reverse().toString();
  37. }
  38. @Ignore("Remove to run test")
  39. @Test
  40. public void accumulateWithinAccumulate() {
  41. List<String> input1 = Arrays.asList("a", "b", "c");
  42. List<String> input2 = Arrays.asList("1", "2", "3");
  43. List<String> expectedOutput = Arrays.asList("a1 a2 a3", "b1 b2 b3", "c1 c2 c3");
  44. assertEquals(expectedOutput, Accumulate.accumulate(
  45. input1, c ->
  46. String.join(" ", Accumulate.accumulate(input2, d -> c + d))
  47. ));
  48. }
  49. }