Skip to content

Commit

Permalink
Update Luhn tests and sample implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
stkent committed Jan 20, 2017
1 parent 63020d4 commit c6cb859
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 185 deletions.
90 changes: 0 additions & 90 deletions exercises/luhn/src/example/java/Luhn.java

This file was deleted.

43 changes: 43 additions & 0 deletions exercises/luhn/src/example/java/LuhnValidator.java
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;
}

}
95 changes: 0 additions & 95 deletions exercises/luhn/src/test/java/LuhnTest.java

This file was deleted.

44 changes: 44 additions & 0 deletions exercises/luhn/src/test/java/LuhnValidatorTest.java
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"));
}

}

0 comments on commit c6cb859

Please sign in to comment.