Просмотр исходного кода

pascals-triangle: update to v1.0.0 tests

Stuart Kent 9 лет назад
Родитель
Сommit
4024f15fba

+ 0
- 26
exercises/pascals-triangle/src/example/java/PascalsTriangle.java Просмотреть файл

1
-import java.util.Arrays;
2
-
3
-public class PascalsTriangle {
4
-
5
-    public static int[][] computeTriangle(int rows) {
6
-        if (rows < 0) {
7
-            throw new IllegalArgumentException("Rows can't be negative!");
8
-        }
9
-
10
-        int[][] triangle = new int[rows][];
11
-
12
-        for (int i = 0; i < rows; i++) {
13
-            triangle[i] = new int[i + 1];
14
-            triangle[i][0] = 1;
15
-            for (int j = 1; j < i; j++) {
16
-                triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
17
-            }
18
-            triangle[i][i] = 1;
19
-        }
20
-        return triangle;
21
-    }
22
-
23
-    public static boolean isTriangle(int[][] triangle) {
24
-        return Arrays.deepEquals(triangle, computeTriangle(triangle.length));
25
-    }
26
-}

+ 21
- 0
exercises/pascals-triangle/src/example/java/PascalsTriangleGenerator.java Просмотреть файл

1
+class PascalsTriangleGenerator {
2
+
3
+  int[][] generateTriangle(int rows) {
4
+    if (rows < 0) {
5
+      throw new IllegalArgumentException("Rows can't be negative!");
6
+    }
7
+
8
+    int[][] triangle = new int[rows][];
9
+
10
+    for (int i = 0; i < rows; i++) {
11
+      triangle[i] = new int[i + 1];
12
+      triangle[i][0] = 1;
13
+      for (int j = 1; j < i; j++) {
14
+        triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
15
+      }
16
+      triangle[i][i] = 1;
17
+    }
18
+    return triangle;
19
+  }
20
+
21
+}

+ 86
- 0
exercises/pascals-triangle/src/test/java/PascalsTriangleGeneratorTest.java Просмотреть файл

1
+import org.junit.Before;
2
+import org.junit.Test;
3
+import org.junit.Ignore;
4
+import org.junit.Rule;
5
+import org.junit.rules.ExpectedException;
6
+
7
+
8
+import static org.junit.Assert.assertArrayEquals;
9
+import static org.junit.Assert.assertEquals;
10
+
11
+/*
12
+ * version: 1.0.0
13
+ */
14
+public class PascalsTriangleGeneratorTest {
15
+
16
+    private PascalsTriangleGenerator pascalsTriangleGenerator;
17
+
18
+    @Before
19
+    public void setUp() {
20
+        pascalsTriangleGenerator = new PascalsTriangleGenerator();
21
+    }
22
+
23
+    @Rule
24
+    public ExpectedException thrown = ExpectedException.none();
25
+
26
+    @Test
27
+    public void testTriangleWithZeroRows() {
28
+        int[][] expectedOutput = new int[][]{};
29
+
30
+        assertArrayEquals(expectedOutput, pascalsTriangleGenerator.generateTriangle(0));
31
+    }
32
+
33
+    @Ignore
34
+    @Test
35
+    public void testTriangleWithOneRow() {
36
+        int[][] expectedOutput = new int[][]{
37
+            {1}
38
+        };
39
+
40
+        assertArrayEquals(expectedOutput, pascalsTriangleGenerator.generateTriangle(1));
41
+    }
42
+
43
+    @Ignore
44
+    @Test
45
+    public void testTriangleWithTwoRows() {
46
+        int[][] expectedOutput = new int[][]{
47
+            {1},
48
+            {1, 1}
49
+        };
50
+
51
+        assertArrayEquals(expectedOutput, pascalsTriangleGenerator.generateTriangle(2));
52
+    }
53
+
54
+    @Ignore
55
+    @Test
56
+    public void testTriangleWithThreeRows() {
57
+        int[][] expectedOutput = new int[][]{
58
+            {1},
59
+            {1, 1},
60
+            {1, 2, 1}
61
+        };
62
+
63
+        assertArrayEquals(expectedOutput, pascalsTriangleGenerator.generateTriangle(3));
64
+    }
65
+
66
+    @Ignore
67
+    @Test
68
+    public void testTriangleWithFourRows() {
69
+        int[][] expectedOutput = new int[][]{
70
+            {1},
71
+            {1, 1},
72
+            {1, 2, 1},
73
+            {1, 3, 3, 1}
74
+        };
75
+
76
+        assertArrayEquals(expectedOutput, pascalsTriangleGenerator.generateTriangle(4));
77
+    }
78
+
79
+    @Ignore
80
+    @Test
81
+    public void testValidatesNotNegativeRows() {
82
+        thrown.expect(IllegalArgumentException.class);
83
+        pascalsTriangleGenerator.generateTriangle(-1);
84
+    }
85
+
86
+}

+ 0
- 85
exercises/pascals-triangle/src/test/java/PascalsTriangleTest.java Просмотреть файл

1
-import org.junit.Test;
2
-import org.junit.Ignore;
3
-import org.junit.Rule;
4
-import org.junit.rules.ExpectedException;
5
-
6
-
7
-import static org.junit.Assert.assertArrayEquals;
8
-import static org.junit.Assert.assertEquals;
9
-
10
-public class PascalsTriangleTest {
11
-
12
-    @Rule
13
-    public ExpectedException thrown = ExpectedException.none();
14
-    
15
-    @Test
16
-    public void testTriangleWithFourRows() {
17
-        int[][] expectedOutput = new int[][]{
18
-                {1},
19
-                {1, 1},
20
-                {1, 2, 1},
21
-                {1, 3, 3, 1},
22
-        };
23
-
24
-        assertArrayEquals(expectedOutput, PascalsTriangle.computeTriangle(4));
25
-    }
26
-
27
-    @Ignore
28
-    @Test
29
-    public void testTriangleWithSixRows() {
30
-        int[][] expectedOutput = new int[][]{
31
-                {1},
32
-                {1, 1},
33
-                {1, 2, 1},
34
-                {1, 3, 3, 1},
35
-                {1, 4, 6, 4, 1},
36
-                {1, 5, 10, 10, 5, 1}
37
-        };
38
-
39
-        assertArrayEquals(expectedOutput, PascalsTriangle.computeTriangle(6));
40
-    }
41
-
42
-    @Ignore
43
-    @Test
44
-    public void testExpectEmptyTriangle() {
45
-        int[][] expectedOutput = new int[][]{
46
-
47
-        };
48
-
49
-        assertArrayEquals(expectedOutput, PascalsTriangle.computeTriangle(0));
50
-    }
51
-
52
-    @Ignore
53
-    @Test
54
-    public void testValidInput() {
55
-        int[][] input = new int[][]{
56
-                {1},
57
-                {1, 1},
58
-                {1, 2, 1},
59
-                {1, 3, 3, 1},
60
-                {1, 4, 6, 4, 1},
61
-        };
62
-
63
-        assertEquals(true, PascalsTriangle.isTriangle(input));
64
-    }
65
-
66
-    @Ignore
67
-    @Test
68
-    public void testInvalidInput() {
69
-        int[][] input = new int[][]{
70
-                {1},
71
-                {1, 1},
72
-                {1, 2, 1},
73
-                {1, 4, 4, 1},
74
-        };
75
-
76
-        assertEquals(false, PascalsTriangle.isTriangle(input));
77
-    }
78
-
79
-    @Ignore
80
-    @Test
81
-    public void testValidatesNotNegativeRows() {
82
-        thrown.expect(IllegalArgumentException.class);
83
-        PascalsTriangle.computeTriangle(-1);
84
-    }
85
-}