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

RectangleCounterTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import org.junit.Before;
  2. import org.junit.Ignore;
  3. import org.junit.Test;
  4. import static org.junit.Assert.assertEquals;
  5. public class RectangleCounterTest {
  6. private RectangleCounter rectangleCounter;
  7. @Before
  8. public void setUp() {
  9. rectangleCounter = new RectangleCounter();
  10. }
  11. @Test
  12. public void testInputWithNoRowsContainsNoRectangles() {
  13. String[] inputGrid = new String[]{};
  14. assertEquals(0, rectangleCounter.countRectangles(inputGrid));
  15. }
  16. @Ignore("Remove to run test")
  17. @Test
  18. public void testInputWithNoColumnsContainsNoRectangles() {
  19. String[] inputGrid = new String[]{""};
  20. assertEquals(0, rectangleCounter.countRectangles(inputGrid));
  21. }
  22. @Ignore("Remove to run test")
  23. @Test
  24. public void testNonTrivialInputWithNoRectangles() {
  25. String[] inputGrid = new String[]{" "};
  26. assertEquals(0, rectangleCounter.countRectangles(inputGrid));
  27. }
  28. @Ignore("Remove to run test")
  29. @Test
  30. public void testInputWithOneRectangle() {
  31. String[] inputGrid = new String[]{
  32. "+-+",
  33. "| |",
  34. "+-+"
  35. };
  36. assertEquals(1, rectangleCounter.countRectangles(inputGrid));
  37. }
  38. @Ignore("Remove to run test")
  39. @Test
  40. public void testInputWithTwoRectanglesWithoutSharedEdges() {
  41. String[] inputGrid = new String[]{
  42. " +-+",
  43. " | |",
  44. "+-+-+",
  45. "| | ",
  46. "+-+ "
  47. };
  48. assertEquals(2, rectangleCounter.countRectangles(inputGrid));
  49. }
  50. @Ignore("Remove to run test")
  51. @Test
  52. public void testInputWithFiveRectanglesWithSharedEdges() {
  53. String[] inputGrid = new String[]{
  54. " +-+",
  55. " | |",
  56. "+-+-+",
  57. "| | |",
  58. "+-+-+"
  59. };
  60. assertEquals(5, rectangleCounter.countRectangles(inputGrid));
  61. }
  62. @Ignore("Remove to run test")
  63. @Test
  64. public void testThatRectangleOfHeightOneIsCounted() {
  65. String[] inputGrid = new String[]{
  66. "+--+",
  67. "+--+"
  68. };
  69. assertEquals(1, rectangleCounter.countRectangles(inputGrid));
  70. }
  71. @Ignore("Remove to run test")
  72. @Test
  73. public void testThatRectangleOfWidthOneIsCounted() {
  74. String[] inputGrid = new String[]{
  75. "++",
  76. "||",
  77. "++"
  78. };
  79. assertEquals(1, rectangleCounter.countRectangles(inputGrid));
  80. }
  81. @Ignore("Remove to run test")
  82. @Test
  83. public void testThatOneByOneSquareIsCounted() {
  84. String[] inputGrid = new String[]{
  85. "++",
  86. "++"
  87. };
  88. assertEquals(1, rectangleCounter.countRectangles(inputGrid));
  89. }
  90. @Ignore("Remove to run test")
  91. @Test
  92. public void testThatIncompleteRectanglesAreNotCounted() {
  93. String[] inputGrid = new String[]{
  94. " +-+",
  95. " |",
  96. "+-+-+",
  97. "| | -",
  98. "+-+-+"
  99. };
  100. assertEquals(1, rectangleCounter.countRectangles(inputGrid));
  101. }
  102. @Ignore("Remove to run test")
  103. @Test
  104. public void testThatRectanglesOfDifferentSizesAreAllCounted() {
  105. String[] inputGrid = new String[]{
  106. "+------+----+",
  107. "| | |",
  108. "+---+--+ |",
  109. "| | |",
  110. "+---+-------+"
  111. };
  112. assertEquals(3, rectangleCounter.countRectangles(inputGrid));
  113. }
  114. @Ignore("Remove to run test")
  115. @Test
  116. public void testThatIntersectionsWithoutCornerCharacterDoNotCountAsRectangleCorners() {
  117. String[] inputGrid = new String[]{
  118. "+------+----+",
  119. "| | |",
  120. "+------+ |",
  121. "| | |",
  122. "+---+-------+"
  123. };
  124. assertEquals(2, rectangleCounter.countRectangles(inputGrid));
  125. }
  126. @Ignore("Remove to run test")
  127. @Test
  128. public void testLargeInputWithManyRectangles() {
  129. String[] inputGrid = new String[]{
  130. "+---+--+----+",
  131. "| +--+----+",
  132. "+---+--+ |",
  133. "| +--+----+",
  134. "+---+--+--+-+",
  135. "+---+--+--+-+",
  136. "+------+ | |",
  137. " +-+"
  138. };
  139. assertEquals(60, rectangleCounter.countRectangles(inputGrid));
  140. }
  141. }