Skip to content

Commit

Permalink
Update Luhn tests for exercism#161
Browse files Browse the repository at this point in the history
  • Loading branch information
robkeim committed Jan 11, 2017
1 parent 14e0ca5 commit 6578a96
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 86 deletions.
46 changes: 15 additions & 31 deletions exercises/luhn/Example.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using System;
using System.Linq;

public class Luhn
public static class Luhn
{
private readonly long number;
public static bool IsValid(string number)
{
number = number.Replace(" ", "");

public long CheckDigit { get { return number % 10; } }
public int[] Addends { get; private set; }
public int Checksum { get { return Addends.Sum(); } }
public bool Valid { get { return Checksum % 10 == 0; } }
if (number.Length < 2 || number.Any(c => c < '0' || c > '9'))
{
return false;
}

public Luhn(long number)
{
this.number = number;
Addends = GenerateAddends();
var checksum = GenerateChecksum(number);

return checksum % 10 == 0;
}

private int[] GenerateAddends()
private static int GenerateChecksum(string number)
{
var reversedIntArray = SplitToReversedIntArray(number);
for (int i = 1; i < reversedIntArray.Length; i++)
Expand All @@ -25,34 +26,17 @@ private int[] GenerateAddends()
reversedIntArray[i] = ConvertDigitForAddend(reversedIntArray[i]);
}
Array.Reverse(reversedIntArray);
return reversedIntArray;
return reversedIntArray.Sum();
}

private static int[] SplitToReversedIntArray(long value)
private static int[] SplitToReversedIntArray(string value)
{
return value.ToString().Select(c => int.Parse(c.ToString())).Reverse().ToArray();
return value.Select(c => int.Parse(c.ToString())).Reverse().ToArray();
}

private static int ConvertDigitForAddend(int value)
{
var doubled = value * 2;
return doubled < 10 ? doubled : doubled - 9;
}

public static long Create(long number)
{
var zeroCheckDigitNumber = number * 10;
var luhn = new Luhn(zeroCheckDigitNumber);

if (luhn.Valid)
return zeroCheckDigitNumber;

return zeroCheckDigitNumber + CreateCheckDigit(luhn.Checksum);
}

private static int CreateCheckDigit(int value)
{
var nearestTen = (int)(Math.Ceiling(value / 10.0m) * 10);
return nearestTen - value;
}
}
64 changes: 9 additions & 55 deletions exercises/luhn/LuhnTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,14 @@
[TestFixture]
public class LuhnTest
{
[Test]
public void Check_digit_is_the_rightmost_digit()
{
Assert.That(new Luhn(34567).CheckDigit, Is.EqualTo(7));
}

[Ignore("Remove to run test")]
[Test]
public void Addends_doubles_every_other_number_from_the_right()
{
Assert.That(new Luhn(12121).Addends, Is.EqualTo(new[] { 1, 4, 1, 4, 1 }));
}

[Ignore("Remove to run test")]
[Test]
public void Addends_subtracts_9_when_doubled_number_is_more_than_9()
{
Assert.That(new Luhn(8631).Addends, Is.EqualTo(new[] { 7, 6, 6, 1 }));
}

[Ignore("Remove to run test")]
[TestCase(4913, ExpectedResult = 22)]
[TestCase(201773, ExpectedResult = 21)]
public int Checksum_adds_addends_together(int number)
{
return new Luhn(number).Checksum;
}

[Ignore("Remove to run test")]
[TestCase(738, ExpectedResult = false)]
[TestCase(8739567, ExpectedResult = true)]
public bool Number_is_valid_when_checksum_mod_10_is_zero(int number)
{
return new Luhn(number).Valid;
}

[Ignore("Remove to run test")]
[Test]
public void Luhn_can_create_simple_numbers_with_valid_check_digit()
{
Assert.That(Luhn.Create(123), Is.EqualTo(1230));
}

[Ignore("Remove to run test")]
[Test]
public void Luhn_can_create_larger_numbers_with_valid_check_digit()
{
Assert.That(Luhn.Create(873956), Is.EqualTo(8739567));
}

[Ignore("Remove to run test")]
[Test]
public void Luhn_can_create_huge_numbers_with_valid_check_digit()
{
Assert.That(Luhn.Create(837263756), Is.EqualTo(8372637564));
[TestCase("1", ExpectedResult = false)] // single digit strings can not be valid
[TestCase("0", ExpectedResult = false, Ignore = "Remove to run test")] // a single zero is invalid
[TestCase("046 454 286", ExpectedResult = true, Ignore = "Remove to run test")] // valid Canadian SIN
[TestCase("046 454 287", ExpectedResult = false, Ignore = "Remove to run test")] // invalid Canadian SIN
[TestCase("8273 1232 7352 0569", ExpectedResult = false, Ignore = "Remove to run test")] // invalid credit card
[TestCase("827a 1232 7352 0569", ExpectedResult = false, Ignore = "Remove to run test")] // strings that contain non-digits are not valid
public bool ValidateChecksum(string number)
{
return Luhn.IsValid(number);
}
}

0 comments on commit 6578a96

Please sign in to comment.