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

perfect-numbers: update to canonical tests v1.0.0 (#336)

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

+ 1
- 1
exercises/perfect-numbers/src/example/java/NaturalNumber.java Просмотреть файл

5
     private final int naturalNumber;
5
     private final int naturalNumber;
6
 
6
 
7
     NaturalNumber(int naturalNumber) {
7
     NaturalNumber(int naturalNumber) {
8
-        if (naturalNumber <= 0) throw new IllegalStateException("Natural numbers are all positive.");
8
+        if (naturalNumber <= 0) throw new IllegalArgumentException("You must supply a natural number (positive integer)");
9
         this.naturalNumber = naturalNumber;
9
         this.naturalNumber = naturalNumber;
10
     }
10
     }
11
 
11
 

+ 52
- 5
exercises/perfect-numbers/src/test/java/NaturalNumberTest.java Просмотреть файл

1
 import org.junit.Ignore;
1
 import org.junit.Ignore;
2
+import org.junit.Rule;
2
 import org.junit.Test;
3
 import org.junit.Test;
4
+import org.junit.rules.ExpectedException;
3
 
5
 
4
 import static org.junit.Assert.assertEquals;
6
 import static org.junit.Assert.assertEquals;
5
 
7
 
8
+/*
9
+ * version: 1.0.0
10
+ * https://github.com/exercism/x-common/blob/19d0c7714ce664a1ad762af624c17f8e269fa8b2/exercises/perfect-numbers/canonical-data.json
11
+ */
6
 public final class NaturalNumberTest {
12
 public final class NaturalNumberTest {
7
 
13
 
14
+    /*
15
+     * See https://github.com/junit-team/junit4/wiki/Rules for information on JUnit Rules in general and
16
+     * ExpectedExceptions in particular.
17
+     */
18
+    @Rule
19
+    public ExpectedException expectedException = ExpectedException.none();
20
+
8
     @Test
21
     @Test
9
     public void testSmallPerfectNumberIsClassifiedCorrectly() {
22
     public void testSmallPerfectNumberIsClassifiedCorrectly() {
10
         assertEquals(Classification.PERFECT, new NaturalNumber(6).getClassification());
23
         assertEquals(Classification.PERFECT, new NaturalNumber(6).getClassification());
31
     @Ignore
44
     @Ignore
32
     @Test
45
     @Test
33
     public void testMediumAbundantNumberIsClassifiedCorrectly() {
46
     public void testMediumAbundantNumberIsClassifiedCorrectly() {
34
-        assertEquals(Classification.ABUNDANT, new NaturalNumber(24).getClassification());
47
+        assertEquals(Classification.ABUNDANT, new NaturalNumber(30).getClassification());
35
     }
48
     }
36
 
49
 
37
     @Ignore
50
     @Ignore
42
 
55
 
43
     @Ignore
56
     @Ignore
44
     @Test
57
     @Test
45
-    public void testSmallDeficientNumberIsClassifiedCorrectly() {
46
-        assertEquals(Classification.DEFICIENT, new NaturalNumber(8).getClassification());
58
+    public void testSmallestPrimeDeficientNumberIsClassifiedCorrectly() {
59
+        assertEquals(Classification.DEFICIENT, new NaturalNumber(2).getClassification());
60
+    }
61
+
62
+    @Ignore
63
+    @Test
64
+    public void testSmallestNonPrimeDeficientNumberIsClassifiedCorrectly() {
65
+        assertEquals(Classification.DEFICIENT, new NaturalNumber(4).getClassification());
47
     }
66
     }
48
 
67
 
49
     @Ignore
68
     @Ignore
50
     @Test
69
     @Test
51
-    public void testMediumNumberIsClassifiedCorrectly() {
52
-        assertEquals(Classification.DEFICIENT, new NaturalNumber(31).getClassification());
70
+    public void testMediumDeficientNumberIsClassifiedCorrectly() {
71
+        assertEquals(Classification.DEFICIENT, new NaturalNumber(32).getClassification());
53
     }
72
     }
54
 
73
 
55
     @Ignore
74
     @Ignore
58
         assertEquals(Classification.DEFICIENT, new NaturalNumber(33550337).getClassification());
77
         assertEquals(Classification.DEFICIENT, new NaturalNumber(33550337).getClassification());
59
     }
78
     }
60
 
79
 
80
+    @Ignore
81
+    @Test
82
+    /*
83
+     * The number 1 has no proper divisors (https://en.wikipedia.org/wiki/Divisor#Further_notions_and_facts), and the
84
+     * additive identity is 0, so the aliquot sum of 1 should be 0. Hence 1 should be classified as deficient.
85
+     */
86
+    public void testThatOneIsCorrectlyClassifiedAsDeficient() {
87
+        assertEquals(Classification.DEFICIENT, new NaturalNumber(1).getClassification());
88
+    }
89
+
90
+    @Ignore
91
+    @Test
92
+    public void testThatNonNegativeIntegerIsRejected() {
93
+        expectedException.expect(IllegalArgumentException.class);
94
+        expectedException.expectMessage("You must supply a natural number (positive integer)");
95
+
96
+        new NaturalNumber(0);
97
+    }
98
+
99
+    @Ignore
100
+    @Test
101
+    public void testThatNegativeIntegerIsRejected() {
102
+        expectedException.expect(IllegalArgumentException.class);
103
+        expectedException.expectMessage("You must supply a natural number (positive integer)");
104
+
105
+        new NaturalNumber(-1);
106
+    }
107
+
61
 }
108
 }