Sfoglia il codice sorgente

Merge pull request #289 from exercism/luhn-update-test-suite

luhn: update to latest canonical tests
FridaTveit 9 anni fa
parent
commit
c5fb479925

+ 19
- 11
exercises/luhn/src/example/java/LuhnValidator.java Vedi File

9
     boolean isValid(final String candidate) {
9
     boolean isValid(final String candidate) {
10
         final String sanitizedCandidate = SPACE_PATTERN.matcher(candidate).replaceAll("");
10
         final String sanitizedCandidate = SPACE_PATTERN.matcher(candidate).replaceAll("");
11
 
11
 
12
+        if (sanitizedCandidate.length() <= 1) {
13
+            return false;
14
+        }
15
+
16
+        // We need to alter every second digit counting from the right. Reversing makes this easy!
17
+        final String reversedSanitizedCandidate = reverse(sanitizedCandidate);
18
+
12
         final List<Integer> computedDigits = new ArrayList<>();
19
         final List<Integer> computedDigits = new ArrayList<>();
13
 
20
 
14
-        for (int charIndex = 0; charIndex < sanitizedCandidate.length(); charIndex++) {
15
-            int inputDigit = Character.digit(sanitizedCandidate.charAt(charIndex), 10);
21
+        for (int charIndex = 0; charIndex < reversedSanitizedCandidate.length(); charIndex++) {
22
+            int inputDigit = Character.digit(reversedSanitizedCandidate.charAt(charIndex), 10);
16
 
23
 
17
             /*
24
             /*
18
              * Character.digit returns a negative int if the supplied character does not represent a digit with respect
25
              * Character.digit returns a negative int if the supplied character does not represent a digit with respect
23
             }
30
             }
24
 
31
 
25
             if (charIndex % 2 == 1) {
32
             if (charIndex % 2 == 1) {
26
-                /*
27
-                 * Since our doubled input digit must lie in [2, 18], the operation
28
-                 *
29
-                 *   "subtract 9 from the doubled input digit if it exceeds 9 in value"
30
-                 *
31
-                 * is equivalent to applying the modulo operation below universally.
32
-                 */
33
-                inputDigit = (2 * inputDigit) % 9;
33
+                inputDigit = 2 * inputDigit;
34
+
35
+                if (inputDigit > 9) {
36
+                    inputDigit -= 9;
37
+                }
34
             }
38
             }
35
 
39
 
36
             computedDigits.add(inputDigit);
40
             computedDigits.add(inputDigit);
37
         }
41
         }
38
 
42
 
39
         final int digitSum = computedDigits.stream().mapToInt(Integer::intValue).sum();
43
         final int digitSum = computedDigits.stream().mapToInt(Integer::intValue).sum();
40
-        return digitSum > 0 && digitSum % 10 == 0;
44
+        return digitSum % 10 == 0;
45
+    }
46
+
47
+    private String reverse(final String string) {
48
+        return new StringBuilder(string).reverse().toString();
41
     }
49
     }
42
 
50
 
43
 }
51
 }

+ 43
- 1
exercises/luhn/src/test/java/LuhnValidatorTest.java Vedi File

19
 
19
 
20
     @Ignore
20
     @Ignore
21
     @Test
21
     @Test
22
-    public void testThatAValidCanadianSocialInsuranceNumberIsIdentifiedAsValid() {
22
+    public void testThatASimpleValidNumberIsIdentifiedAsValid() {
23
+        assertTrue(new LuhnValidator().isValid(" 5 9 "));
24
+    }
25
+
26
+    @Ignore
27
+    @Test
28
+    public void testThatAValidCanadianSocialInsuranceNumberIsIdentifiedAsValidV1() {
23
         assertTrue(new LuhnValidator().isValid("046 454 286"));
29
         assertTrue(new LuhnValidator().isValid("046 454 286"));
24
     }
30
     }
25
 
31
 
26
     @Ignore
32
     @Ignore
27
     @Test
33
     @Test
34
+    public void testThatAValidCanadianSocialInsuranceNumberIsIdentifiedAsValidV2() {
35
+        assertTrue(new LuhnValidator().isValid("055 444 285"));
36
+    }
37
+
38
+    @Ignore
39
+    @Test
28
     public void testThatAnInvalidCanadianSocialInsuranceNumberIsIdentifiedAsInvalid() {
40
     public void testThatAnInvalidCanadianSocialInsuranceNumberIsIdentifiedAsInvalid() {
29
         assertFalse(new LuhnValidator().isValid("046 454 287"));
41
         assertFalse(new LuhnValidator().isValid("046 454 287"));
30
     }
42
     }
41
         assertFalse(new LuhnValidator().isValid("046a 454 286"));
53
         assertFalse(new LuhnValidator().isValid("046a 454 286"));
42
     }
54
     }
43
 
55
 
56
+    @Ignore
57
+    @Test
58
+    public void testThatStringContainingPunctuationIsInvalid() {
59
+        assertFalse(new LuhnValidator().isValid("055-444-285"));
60
+    }
61
+
62
+    @Ignore
63
+    @Test
64
+    public void testThatStringContainingSymbolsIsInvalid() {
65
+        assertFalse(new LuhnValidator().isValid("055£ 444$ 285"));
66
+    }
67
+
68
+    @Ignore
69
+    @Test
70
+    public void testThatTheStringConsistingOfASpaceAndASingleZeroIsInvalid() {
71
+        assertFalse(new LuhnValidator().isValid(" 0"));
72
+    }
73
+
74
+    @Ignore
75
+    @Test
76
+    public void testThatStringContainingMultipleZerosIsValid() {
77
+        assertTrue(new LuhnValidator().isValid(" 00000"));
78
+    }
79
+
80
+    @Ignore
81
+    @Test
82
+    public void testThatDoublingNineIsHandledCorrectly() {
83
+        assertTrue(new LuhnValidator().isValid("091"));
84
+    }
85
+
44
 }
86
 }