-
-
Notifications
You must be signed in to change notification settings - Fork 691
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Luhn tests and sample implementation
Fixes #245. Tracking issue: exercism/problem-specifications#491
- Loading branch information
Showing
4 changed files
with
87 additions
and
185 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
final class LuhnValidator { | ||
|
||
private static final Pattern SPACE_PATTERN = Pattern.compile("\\s+"); | ||
|
||
boolean isValid(final String candidate) { | ||
final String sanitizedCandidate = SPACE_PATTERN.matcher(candidate).replaceAll(""); | ||
|
||
final List<Integer> computedDigits = new ArrayList<>(); | ||
|
||
for (int charIndex = 0; charIndex < sanitizedCandidate.length(); charIndex++) { | ||
int inputDigit = Character.digit(sanitizedCandidate.charAt(charIndex), 10); | ||
|
||
/* | ||
* Character.digit returns a negative int if the supplied character does not represent a digit with respect | ||
* to the given radix. | ||
*/ | ||
if (inputDigit < 0) { | ||
return false; | ||
} | ||
|
||
if (charIndex % 2 == 1) { | ||
/* | ||
* Since our doubled input digit must lie in [2, 18], the operation | ||
* | ||
* "subtract 9 from the doubled input digit if it exceeds 9 in value" | ||
* | ||
* is equivalent to applying the modulo operation below universally. | ||
*/ | ||
inputDigit = (2 * inputDigit) % 9; | ||
} | ||
|
||
computedDigits.add(inputDigit); | ||
} | ||
|
||
final int digitSum = computedDigits.stream().mapToInt(Integer::intValue).sum(); | ||
return digitSum > 0 && digitSum % 10 == 0; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class LuhnValidatorTest { | ||
|
||
@Test | ||
public void testThatSingleDigitStringIsNotValid() { | ||
assertFalse(new LuhnValidator().isValid("1")); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void testThatTheStringConsistingOfASingleZeroIsInvalid() { | ||
assertFalse(new LuhnValidator().isValid("0")); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void testThatAValidCanadianSocialInsuranceNumberIsIdentifiedAsValid() { | ||
assertTrue(new LuhnValidator().isValid("046 454 286")); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void testThatAnInvalidCanadianSocialInsuranceNumberIsIdentifiedAsInvalid() { | ||
assertFalse(new LuhnValidator().isValid("046 454 287")); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void testThatAnInvalidCreditCardIsIdentifiedAsInvalid() { | ||
assertFalse(new LuhnValidator().isValid("8273 1232 7352 0569")); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void testThatAddingANonDigitCharacterToAValidStringInvalidatesTheString() { | ||
assertFalse(new LuhnValidator().isValid("046a 454 286")); | ||
} | ||
|
||
} |