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

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,7 +5,7 @@ final class NaturalNumber {
5 5
     private final int naturalNumber;
6 6
 
7 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 9
         this.naturalNumber = naturalNumber;
10 10
     }
11 11
 

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

@@ -1,10 +1,23 @@
1 1
 import org.junit.Ignore;
2
+import org.junit.Rule;
2 3
 import org.junit.Test;
4
+import org.junit.rules.ExpectedException;
3 5
 
4 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 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 21
     @Test
9 22
     public void testSmallPerfectNumberIsClassifiedCorrectly() {
10 23
         assertEquals(Classification.PERFECT, new NaturalNumber(6).getClassification());
@@ -31,7 +44,7 @@ public final class NaturalNumberTest {
31 44
     @Ignore
32 45
     @Test
33 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 50
     @Ignore
@@ -42,14 +55,20 @@ public final class NaturalNumberTest {
42 55
 
43 56
     @Ignore
44 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 68
     @Ignore
50 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 74
     @Ignore
@@ -58,4 +77,32 @@ public final class NaturalNumberTest {
58 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
 }