Przeglądaj źródła

luhn: update to latest canonical tests

Stuart Kent 9 lat temu
rodzic
commit
447c5dc2f1

+ 10
- 9
exercises/luhn/src/example/java/LuhnValidator.java Wyświetl plik

@@ -9,6 +9,10 @@ final class LuhnValidator {
9 9
     boolean isValid(final String candidate) {
10 10
         final String sanitizedCandidate = SPACE_PATTERN.matcher(candidate).replaceAll("");
11 11
 
12
+        if (sanitizedCandidate.length() <= 1) {
13
+            return false;
14
+        }
15
+
12 16
         // We need to alter every second digit counting from the right. Reversing makes this easy!
13 17
         final String reversedSanitizedCandidate = reverse(sanitizedCandidate);
14 18
 
@@ -26,21 +30,18 @@ final class LuhnValidator {
26 30
             }
27 31
 
28 32
             if (charIndex % 2 == 1) {
29
-                /*
30
-                 * Since our doubled input digit must lie in [2, 18], the operation
31
-                 *
32
-                 *   "subtract 9 from the doubled input digit if it exceeds 9 in value"
33
-                 *
34
-                 * is equivalent to applying the modulo operation below universally.
35
-                 */
36
-                inputDigit = (2 * inputDigit) % 9;
33
+                inputDigit = 2 * inputDigit;
34
+
35
+                if (inputDigit > 9) {
36
+                    inputDigit -= 9;
37
+                }
37 38
             }
38 39
 
39 40
             computedDigits.add(inputDigit);
40 41
         }
41 42
 
42 43
         final int digitSum = computedDigits.stream().mapToInt(Integer::intValue).sum();
43
-        return digitSum > 0 && digitSum % 10 == 0;
44
+        return digitSum % 10 == 0;
44 45
     }
45 46
 
46 47
     private String reverse(final String string) {

+ 43
- 1
exercises/luhn/src/test/java/LuhnValidatorTest.java Wyświetl plik

@@ -19,12 +19,24 @@ public class LuhnValidatorTest {
19 19
 
20 20
     @Ignore
21 21
     @Test
22
-    public void testThatAValidCanadianSocialInsuranceNumberIsIdentifiedAsValid() {
22
+    public void testThatASimpleValidCanadianSocialInsuranceNumberIsIdentifiedAsValid() {
23
+        assertTrue(new LuhnValidator().isValid(" 5 9 "));
24
+    }
25
+
26
+    @Ignore
27
+    @Test
28
+    public void testThatAComplexValidCanadianSocialInsuranceNumberIsIdentifiedAsValidV1() {
23 29
         assertTrue(new LuhnValidator().isValid("046 454 286"));
24 30
     }
25 31
 
26 32
     @Ignore
27 33
     @Test
34
+    public void testThatAComplexValidCanadianSocialInsuranceNumberIsIdentifiedAsValidV2() {
35
+        assertTrue(new LuhnValidator().isValid("055 444 285"));
36
+    }
37
+
38
+    @Ignore
39
+    @Test
28 40
     public void testThatAnInvalidCanadianSocialInsuranceNumberIsIdentifiedAsInvalid() {
29 41
         assertFalse(new LuhnValidator().isValid("046 454 287"));
30 42
     }
@@ -41,4 +53,34 @@ public class LuhnValidatorTest {
41 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
 }