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

FlattenerTest.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import org.junit.Before;
  2. import org.junit.Test;
  3. import org.junit.Ignore;
  4. import static java.util.Arrays.asList;
  5. import static java.util.Collections.emptyList;
  6. import static java.util.Collections.singletonList;
  7. import static org.junit.Assert.assertEquals;
  8. public class FlattenerTest {
  9. private Flattener flattener;
  10. @Before
  11. public void setUp() {
  12. flattener = new Flattener();
  13. }
  14. @Test
  15. public void testFlatListIsPreserved() {
  16. assertEquals(
  17. asList(0, '1', "two"),
  18. flattener.flatten(
  19. asList(
  20. 0,
  21. '1',
  22. "two")));
  23. }
  24. @Ignore("Remove to run test")
  25. @Test
  26. public void testASingleLevelOfNestingWithNoNulls() {
  27. assertEquals(
  28. asList(1, '2', 3, 4, 5, "six", "7", 8),
  29. flattener.flatten(
  30. asList(
  31. 1,
  32. asList(
  33. '2',
  34. 3,
  35. 4,
  36. 5,
  37. "six",
  38. "7"),
  39. 8)));
  40. }
  41. @Ignore("Remove to run test")
  42. @Test
  43. public void testFiveLevelsOfNestingWithNoNulls() {
  44. assertEquals(
  45. asList(0, '2', 2, "three", '8', 100, "four", 50, "-2"),
  46. flattener.flatten(
  47. asList(
  48. 0,
  49. '2',
  50. asList(
  51. asList(
  52. 2,
  53. "three"),
  54. '8',
  55. 100,
  56. "four",
  57. singletonList(
  58. singletonList(
  59. singletonList(
  60. 50)))),
  61. "-2")));
  62. }
  63. @Ignore("Remove to run test")
  64. @Test
  65. public void testSixLevelsOfNestingWithNoNulls() {
  66. assertEquals(
  67. asList("one", '2', 3, '4', 5, "six", 7, "8"),
  68. flattener.flatten(
  69. asList(
  70. "one",
  71. asList(
  72. '2',
  73. singletonList(
  74. singletonList(
  75. 3)),
  76. asList(
  77. '4',
  78. singletonList(
  79. singletonList(
  80. 5))),
  81. "six",
  82. 7),
  83. "8")));
  84. }
  85. @Ignore("Remove to run test")
  86. @Test
  87. public void testSixLevelsOfNestingWithNulls() {
  88. assertEquals(
  89. asList("0", 2, "two", '3', "8", "one hundred", "negative two"),
  90. flattener.flatten(
  91. asList(
  92. "0",
  93. 2,
  94. asList(
  95. asList(
  96. "two",
  97. '3'),
  98. "8",
  99. singletonList(
  100. singletonList(
  101. "one hundred")),
  102. null,
  103. singletonList(
  104. singletonList(
  105. null))),
  106. "negative two")));
  107. }
  108. @Ignore("Remove to run test")
  109. @Test
  110. public void testNestedListsFullOfNullsOnly() {
  111. assertEquals(
  112. emptyList(),
  113. flattener.flatten(
  114. asList(
  115. null,
  116. singletonList(
  117. singletonList(
  118. singletonList(
  119. null))),
  120. null,
  121. null,
  122. asList(
  123. asList(
  124. null,
  125. null),
  126. null),
  127. null)));
  128. }
  129. }