diff --git a/exercises/pascals-triangle/src/example/java/PascalsTriangle.java b/exercises/pascals-triangle/src/example/java/PascalsTriangle.java deleted file mode 100644 index 130e92aa1..000000000 --- a/exercises/pascals-triangle/src/example/java/PascalsTriangle.java +++ /dev/null @@ -1,26 +0,0 @@ -import java.util.Arrays; - -public class PascalsTriangle { - - public static int[][] computeTriangle(int rows) { - if (rows < 0) { - throw new IllegalArgumentException("Rows can't be negative!"); - } - - int[][] triangle = new int[rows][]; - - for (int i = 0; i < rows; i++) { - triangle[i] = new int[i + 1]; - triangle[i][0] = 1; - for (int j = 1; j < i; j++) { - triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]; - } - triangle[i][i] = 1; - } - return triangle; - } - - public static boolean isTriangle(int[][] triangle) { - return Arrays.deepEquals(triangle, computeTriangle(triangle.length)); - } -} diff --git a/exercises/pascals-triangle/src/example/java/PascalsTriangleGenerator.java b/exercises/pascals-triangle/src/example/java/PascalsTriangleGenerator.java new file mode 100644 index 000000000..032930fe1 --- /dev/null +++ b/exercises/pascals-triangle/src/example/java/PascalsTriangleGenerator.java @@ -0,0 +1,21 @@ +class PascalsTriangleGenerator { + + int[][] generateTriangle(int rows) { + if (rows < 0) { + throw new IllegalArgumentException("Rows can't be negative!"); + } + + int[][] triangle = new int[rows][]; + + for (int i = 0; i < rows; i++) { + triangle[i] = new int[i + 1]; + triangle[i][0] = 1; + for (int j = 1; j < i; j++) { + triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]; + } + triangle[i][i] = 1; + } + return triangle; + } + +} diff --git a/exercises/pascals-triangle/src/test/java/PascalsTriangleGeneratorTest.java b/exercises/pascals-triangle/src/test/java/PascalsTriangleGeneratorTest.java new file mode 100644 index 000000000..dda21a72c --- /dev/null +++ b/exercises/pascals-triangle/src/test/java/PascalsTriangleGeneratorTest.java @@ -0,0 +1,86 @@ +import org.junit.Before; +import org.junit.Test; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.rules.ExpectedException; + + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +/* + * version: 1.0.0 + */ +public class PascalsTriangleGeneratorTest { + + private PascalsTriangleGenerator pascalsTriangleGenerator; + + @Before + public void setUp() { + pascalsTriangleGenerator = new PascalsTriangleGenerator(); + } + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testTriangleWithZeroRows() { + int[][] expectedOutput = new int[][]{}; + + assertArrayEquals(expectedOutput, pascalsTriangleGenerator.generateTriangle(0)); + } + + @Ignore + @Test + public void testTriangleWithOneRow() { + int[][] expectedOutput = new int[][]{ + {1} + }; + + assertArrayEquals(expectedOutput, pascalsTriangleGenerator.generateTriangle(1)); + } + + @Ignore + @Test + public void testTriangleWithTwoRows() { + int[][] expectedOutput = new int[][]{ + {1}, + {1, 1} + }; + + assertArrayEquals(expectedOutput, pascalsTriangleGenerator.generateTriangle(2)); + } + + @Ignore + @Test + public void testTriangleWithThreeRows() { + int[][] expectedOutput = new int[][]{ + {1}, + {1, 1}, + {1, 2, 1} + }; + + assertArrayEquals(expectedOutput, pascalsTriangleGenerator.generateTriangle(3)); + } + + @Ignore + @Test + public void testTriangleWithFourRows() { + int[][] expectedOutput = new int[][]{ + {1}, + {1, 1}, + {1, 2, 1}, + {1, 3, 3, 1} + }; + + assertArrayEquals(expectedOutput, pascalsTriangleGenerator.generateTriangle(4)); + } + + @Ignore + @Test + public void testValidatesNotNegativeRows() { + thrown.expect(IllegalArgumentException.class); + pascalsTriangleGenerator.generateTriangle(-1); + } + +} diff --git a/exercises/pascals-triangle/src/test/java/PascalsTriangleTest.java b/exercises/pascals-triangle/src/test/java/PascalsTriangleTest.java deleted file mode 100644 index 62a708d88..000000000 --- a/exercises/pascals-triangle/src/test/java/PascalsTriangleTest.java +++ /dev/null @@ -1,85 +0,0 @@ -import org.junit.Test; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.rules.ExpectedException; - - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; - -public class PascalsTriangleTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void testTriangleWithFourRows() { - int[][] expectedOutput = new int[][]{ - {1}, - {1, 1}, - {1, 2, 1}, - {1, 3, 3, 1}, - }; - - assertArrayEquals(expectedOutput, PascalsTriangle.computeTriangle(4)); - } - - @Ignore - @Test - public void testTriangleWithSixRows() { - int[][] expectedOutput = new int[][]{ - {1}, - {1, 1}, - {1, 2, 1}, - {1, 3, 3, 1}, - {1, 4, 6, 4, 1}, - {1, 5, 10, 10, 5, 1} - }; - - assertArrayEquals(expectedOutput, PascalsTriangle.computeTriangle(6)); - } - - @Ignore - @Test - public void testExpectEmptyTriangle() { - int[][] expectedOutput = new int[][]{ - - }; - - assertArrayEquals(expectedOutput, PascalsTriangle.computeTriangle(0)); - } - - @Ignore - @Test - public void testValidInput() { - int[][] input = new int[][]{ - {1}, - {1, 1}, - {1, 2, 1}, - {1, 3, 3, 1}, - {1, 4, 6, 4, 1}, - }; - - assertEquals(true, PascalsTriangle.isTriangle(input)); - } - - @Ignore - @Test - public void testInvalidInput() { - int[][] input = new int[][]{ - {1}, - {1, 1}, - {1, 2, 1}, - {1, 4, 4, 1}, - }; - - assertEquals(false, PascalsTriangle.isTriangle(input)); - } - - @Ignore - @Test - public void testValidatesNotNegativeRows() { - thrown.expect(IllegalArgumentException.class); - PascalsTriangle.computeTriangle(-1); - } -}